From 4a683f4346d59ef6ba26f70ce238a1bb4618861b Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 20 Nov 2020 18:02:18 +0200 Subject: [PATCH 1/4] add support for trip service --- errors.go | 2 + go.mod | 4 +- trip.go | 49 ++++++++++++++++++++++++ trip_test.go | 92 ++++++++++++++++++++++++++++++++++++++++++++++ types.go | 36 ++++++++++++++++++ vendor/modules.txt | 3 ++ 6 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 trip.go create mode 100644 trip_test.go diff --git a/errors.go b/errors.go index b725126..67ff1d3 100644 --- a/errors.go +++ b/errors.go @@ -15,11 +15,13 @@ const ( ErrorCodeNoRoute = "NoRoute" ErrorCodeNoTable = "NoTable" ErrorCodeNoMatch = "NoMatch" + ErrorCodeNoTrips = "NoTrips" errorCodeOK = "Ok" // "Ok" error code never returned to library client, thus not exported ) // Invalid request errors var ( + ErrorNotImplemented = errors.New("osrm5: the request is not implemented") ErrEmptyProfileName = errors.New("osrm5: the request should contain a profile name") ErrNoCoordinates = errors.New("osrm5: the request should contain coordinates") ErrEmptyServiceName = errors.New("osrm5: the request should contain a service name") diff --git a/go.mod b/go.mod index 74470fa..2438826 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,9 @@ module github.com/gojuno/go.osrm +go 1.15 + require ( github.com/paulmach/go.geo v0.0.0-20180829195134-22b514266d33 - github.com/paulmach/go.geojson v1.4.0 // indirect + github.com/paulmach/go.geojson v1.4.0 github.com/stretchr/testify v1.3.0 ) diff --git a/trip.go b/trip.go new file mode 100644 index 0000000..e4e6cb0 --- /dev/null +++ b/trip.go @@ -0,0 +1,49 @@ +package osrm + +type TripRequest struct { + Profile string + Coordinates Geometry + Roundtrip Roundtrip + Source Source + Destination Destination + Steps Steps + Annotations Annotations + Geometries Geometries + Overview Overview +} + +type TripResponse struct { + ResponseStatus + Waypoints []TripWaypoint `json:"waypoints"` + Trips []Route `json:"trips"` +} + +type TripWaypoint struct { + TripsIndex int `json:"trips_index"` + WaypointIndex int `json:"waypoint_index"` + Waypoint +} + +func (r TripRequest) request() *request { + return &request{ + profile: r.Profile, + coords: r.Coordinates, + service: "trip", + options: stepsOptions(r.Steps, r.Annotations, r.Overview, r.Geometries). + setStringer("roundtrip", r.Roundtrip). + setStringer("source", r.Source). + setStringer("destination", r.Destination), + } +} + +func (r TripRequest) IsSupported() bool { + fixedstart := r.Source == SourceFirst || (r.Source == SourceAny && r.Destination == DestinationAny) + fixedend := r.Destination == DestinationLast + roundtrip := r.Roundtrip == RoundtripTrue + if fixedstart && fixedend && !roundtrip { + return true + } else if roundtrip { + return true + } + return false +} diff --git a/trip_test.go b/trip_test.go new file mode 100644 index 0000000..687a6ed --- /dev/null +++ b/trip_test.go @@ -0,0 +1,92 @@ +package osrm + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestEmptyTripRequestOptions(t *testing.T) { + req := TripRequest{} + assert.Equal( + t, + "geometries=polyline6", + req.request().options.encode()) +} + +func TestTripRequestOptionsWithRoundtrip(t *testing.T) { + req := TripRequest{ + Roundtrip: RoundtripFalse, + } + assert.Equal( + t, + "geometries=polyline6&roundtrip=false", + req.request().options.encode()) +} + +func TestTripRequestOptionsWithSource(t *testing.T) { + req := TripRequest{ + Source: SourceFirst, + } + assert.Equal( + t, + "geometries=polyline6&source=first", + req.request().options.encode()) +} + +func TestTripRequestOptionsWithDestination(t *testing.T) { + req := TripRequest{ + Destination: DestinationLast, + } + assert.Equal( + t, + "destination=last&geometries=polyline6", + req.request().options.encode()) +} + +func TestTripRequestOptions(t *testing.T) { + req := TripRequest{ + Roundtrip: RoundtripTrue, + Destination: DestinationLast, + } + assert.Equal( + t, + "destination=last&geometries=polyline6&roundtrip=true", + req.request().options.encode()) +} + +func TestUnsupportedTripRequestOptionsA(t *testing.T) { + req := TripRequest{ + Roundtrip: RoundtripFalse, + Source: SourceFirst, + Destination: DestinationAny, + } + assert.Equal( + t, + false, + req.IsSupported()) +} + +func TestUnsupportedTripRequestOptionsB(t *testing.T) { + req := TripRequest{ + Roundtrip: RoundtripFalse, + Source: SourceAny, + Destination: DestinationLast, + } + assert.Equal( + t, + false, + req.IsSupported()) +} + +func TestUnsupportedTripRequestOptionsC(t *testing.T) { + req := TripRequest{ + Roundtrip: RoundtripFalse, + Source: SourceAny, + Destination: DestinationAny, + } + assert.Equal( + t, + false, + req.IsSupported()) +} diff --git a/types.go b/types.go index 8f5a559..ec776c6 100644 --- a/types.go +++ b/types.go @@ -174,6 +174,42 @@ func (c ContinueStraight) String() string { return string(c) } +type Roundtrip string + +const ( + RoundtripDefault Roundtrip = "true" + RoundtripTrue Roundtrip = "true" + RoundtripFalse Roundtrip = "false" +) + +func (r Roundtrip) String() string { + return string(r) +} + +type Source string + +const ( + SourceDefault Source = "any" + SourceAny Source = "any" + SourceFirst Source = "first" +) + +func (s Source) String() string { + return string(s) +} + +type Destination string + +const ( + DestinationDefault Destination = "any" + DestinationAny Destination = "any" + DestinationLast Destination = "last" +) + +func (d Destination) String() string { + return string(d) +} + // request contains parameters for OSRM query type request struct { profile string diff --git a/vendor/modules.txt b/vendor/modules.txt index 1ccfc62..d4483dd 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,11 +1,14 @@ # github.com/davecgh/go-spew v1.1.0 github.com/davecgh/go-spew/spew # github.com/paulmach/go.geo v0.0.0-20180829195134-22b514266d33 +## explicit github.com/paulmach/go.geo # github.com/paulmach/go.geojson v1.4.0 +## explicit github.com/paulmach/go.geojson # github.com/pmezard/go-difflib v1.0.0 github.com/pmezard/go-difflib/difflib # github.com/stretchr/testify v1.3.0 +## explicit github.com/stretchr/testify/assert github.com/stretchr/testify/require From ad8dc944eab685b860ad24084013b8a3943d6112 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 21 Nov 2020 20:12:50 +0200 Subject: [PATCH 2/4] complete tests --- osrm.go | 13 ++++++++++++ osrm_test.go | 35 ++++++++++++++++++++++++++++++++ testdata/trip_response_full.json | 1 + trip.go | 6 +++--- trip_test.go | 10 ++++----- 5 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 testdata/trip_response_full.json diff --git a/osrm.go b/osrm.go index 8a7ebeb..c50a087 100644 --- a/osrm.go +++ b/osrm.go @@ -133,3 +133,16 @@ func (o OSRM) Nearest(ctx context.Context, r NearestRequest) (*NearestResponse, } return &resp, nil } + +// The trip plugin solves the Traveling Salesman Problem using a greedy heuristic (farthest-insertion algorithm). +// See https://github.com/Project-OSRM/osrm-backend/blob/master/docs/http.md#trip-service for details +func (o OSRM) Trip(ctx context.Context, r TripRequest) (*TripResponse, error) { + var resp TripResponse + if !r.IsSupported() { + return nil, ErrorNotImplemented + } + if err := o.query(ctx, r.request(), &resp); err != nil { + return nil, err + } + return &resp, nil +} diff --git a/osrm_test.go b/osrm_test.go index 70f32a8..2fe43f8 100644 --- a/osrm_test.go +++ b/osrm_test.go @@ -250,3 +250,38 @@ func TestNearestRequest(t *testing.T) { assert.Equal(t, "XhAFgP___38AAAAAWwAAAAAAAAAAAAAAAAAAANvEokIAAAAAAAAAAAAAAABbAAAAAAAAAAAAAACVCQAAevCW-1GSbQLK7pb7P5NtAgAADw3g85BF", r.Waypoints[3].Hint) assert.Equal(t, "-h4FgJQVyYAyAAAA2AAAAAAAAAAAAAAAU4QzQm0XQUMAAAAAAAAAADIAAADYAAAAAAAAAAAAAACVCQAAp_CW-8-VbQLK7pb7P5NtAgAArxLg85BF", r.Waypoints[4].Hint) } + +func TestTripRequest(t *testing.T) { + ts := httptest.NewServer(fixturedHTTPHandler("trip_response_full", func(path, query string) { + assert.Equal(t, `/trip/v1/car/polyline(_al_IowvpA@f@As@PAG\)`, path) + assert.Equal(t, "destination=last&geometries=polyline6&roundtrip=true&source=any", query) + })) + defer ts.Close() + + osrm := NewFromURL("http://router.project-osrm.org") + tgeo := NewGeometryFromPointSet(geo.PointSet{ + + {13.3927165, 52.4956761}, + {13.3925165, 52.4956721}, + {13.3927765, 52.4956821}, + {13.3927925, 52.4955861}, + {13.3926365, 52.4956261}, + }) + r, err := osrm.Trip(context.Background(), TripRequest{ + Profile: "car", + Coordinates: tgeo, + Roundtrip: RoundtripTrue, + Destination: DestinationLast, + }) + + require := require.New(t) + + require.NoError(err) + require.NotNil(r) + assert.Len(t, r.Waypoints, 5) + assert.Equal(t, 13.392608, r.Waypoints[0].Location[0]) + assert.Equal(t, 13.392412, r.Waypoints[1].Location[0]) + assert.Equal(t, 13.392666, r.Waypoints[2].Location[0]) + assert.Equal(t, 13.39265, r.Waypoints[3].Location[0]) + assert.Equal(t, 13.392516, r.Waypoints[4].Location[0]) +} diff --git a/testdata/trip_response_full.json b/testdata/trip_response_full.json new file mode 100644 index 0000000..bacbf59 --- /dev/null +++ b/testdata/trip_response_full.json @@ -0,0 +1 @@ +{"code":"Ok","waypoints":[{"waypoint_index":1,"trips_index":0,"hint":"7e62k1XvtpMdAAAAAAAAAMkFAADvBQAA3XAAQgAAAADJes1E66TlRB0AAAAAAAAAyQUAAO8FAAD5FwEA3B0AAHKLbQJq____PpNtAh4ALxZX0TUP","location":[0.007644,40.733554],"name":""},{"waypoint_index":2,"trips_index":0,"hint":"6REchTsSHIUAAAAALQAAAAAAAAAHBgAAAAAAANKtxUEAAAAA1ttQRAAAAAAtAAAAAAAAAAcGAAD5FwEAONrqAOkNXf76AAAACIIE_gAADwBX0TUP","location":[15.391288,-27.456023],"name":""},{"waypoint_index":3,"trips_index":0,"hint":"6REchTsSHIUAAAAALQAAAAAAAAAHBgAAAAAAANKtxUEAAAAA1ttQRAAAAAAtAAAAAAAAAAcGAAD5FwEAONrqAOkNXf5kAAAAYm4E_gAADwBX0TUP","location":[15.391288,-27.456023],"name":""},{"waypoint_index":4,"trips_index":0,"hint":"6REchTsSHIUAAAAALQAAAAAAAAAHBgAAAAAAANKtxUEAAAAA1ttQRAAAAAAtAAAAAAAAAAcGAAD5FwEAONrqAOkNXf6QEAAATGEE_gAADwBX0TUP","location":[15.391288,-27.456023],"name":""},{"waypoint_index":5,"trips_index":0,"hint":"6REchTsSHIUAAAAALQAAAAAAAAAHBgAAAAAAANKtxUEAAAAA1ttQRAAAAAAtAAAAAAAAAAcGAAD5FwEAONrqAOkNXf4UFAAA9FQE_gAADwBX0TUP","location":[15.391288,-27.456023],"name":""},{"waypoint_index":0,"trips_index":0,"hint":"6REchTsSHIUAAAAALQAAAAAAAAAHBgAAAAAAANKtxUEAAAAA1ttQRAAAAAAtAAAAAAAAAAcGAAD5FwEAONrqAOkNXf7Q_f__Sk8E_gAADwBX0TUP","location":[15.391288,-27.456023],"name":""}],"trips":[{"legs":[{"steps":[],"weight":681257.8,"distance":15308145.4,"summary":"","duration":680919.1},{"steps":[],"weight":691620.3,"distance":15502797.3,"summary":"","duration":691281.6},{"steps":[],"weight":0,"distance":0,"summary":"","duration":0},{"steps":[],"weight":0,"distance":0,"summary":"","duration":0},{"steps":[],"weight":0,"distance":0,"summary":"","duration":0},{"steps":[],"weight":0,"distance":0,"summary":"","duration":0}],"weight_name":"routability","geometry":"l`xjs@oblj\\ufxq@moaDmpmKgxdeD_aejJram}Asqkq@grsi@}mbhByhcuCi`dCgeapJeehyBaoaqCosmtBqznX}pxiAc_zmCopl~EcpjwBatfK_mfpBmecgB{ppeA}wi|Df_jLu}zi@gqus@ucwyJux~yAwq~[{czx@qep|Adv|w@qhfaF_vs|@wu}hDf~ciBugvl@mjiUojxxEzqpcBcwz}BzexDemprAgbamAyzhqDbwwXqjvq@zglkAuuhzK~ymjEkb~Y{euo@bpse@ap`iByywn@spru@_dokJsgy{@{itl@phf`@cx`C~nqiA_vifAdt{NwvzcA~s{cBevgvAnzks@yhjA~gnaDyas}AtturIe{|}@xrqaCglvxA`zzvAyvzhAlnciJpii_@vwj`G`pc|B|oqvE}q~Sji}pCbteh@h`qqA|a_rAxt|Ltu`~@t`jeDyaa~@iekeDqq~qAmz{Lopeh@wzpqA|q~Ssh}pCcb{{BqmwvEgup_@kqe`G|uyhAwwbiJtsuxA}xuvAlo}}@clvaC`hs}Ac`vrIdfjAaimaD|robFgtfhDlk`CmmriAjpqn@mit`@pgqiJt~i|@|ky_BtkiiB}j~Bfozl@_kpsAnjke@b`yZjnxQtxgzKggojEpjvq@{glkAxzhqDcwwXdmprAfbamAbwz}B{exDnjxxE{qpcBtgvl@ljiU|i~hDcl~hBv`eaFxln|@dyp|Ac_}w@vq~[zczx@zuwyJfs~yAnkzi@tvus@|wi|Dg_jLlecgBzppeA`tfK~lfpBnpl~EbpjwB~zxiAfzymCr~ltBnmnX~ohyB~`bqCh`dCfeapJ`vbhBhncuClmkq@pesi@`}djJmzl}AlpmKfxdeDtfxq@loaD????????","weight":1372878.1,"distance":30810942.7,"duration":1372200.7}]} diff --git a/trip.go b/trip.go index e4e6cb0..be8d652 100644 --- a/trip.go +++ b/trip.go @@ -30,9 +30,9 @@ func (r TripRequest) request() *request { coords: r.Coordinates, service: "trip", options: stepsOptions(r.Steps, r.Annotations, r.Overview, r.Geometries). - setStringer("roundtrip", r.Roundtrip). - setStringer("source", r.Source). - setStringer("destination", r.Destination), + setStringer("roundtrip", valueOrDefault(r.Roundtrip, RoundtripDefault)). + setStringer("source", valueOrDefault(r.Source, SourceDefault)). + setStringer("destination", valueOrDefault(r.Destination, DestinationDefault)), } } diff --git a/trip_test.go b/trip_test.go index 687a6ed..b0c968d 100644 --- a/trip_test.go +++ b/trip_test.go @@ -10,7 +10,7 @@ func TestEmptyTripRequestOptions(t *testing.T) { req := TripRequest{} assert.Equal( t, - "geometries=polyline6", + "destination=any&geometries=polyline6&roundtrip=true&source=any", req.request().options.encode()) } @@ -20,7 +20,7 @@ func TestTripRequestOptionsWithRoundtrip(t *testing.T) { } assert.Equal( t, - "geometries=polyline6&roundtrip=false", + "destination=any&geometries=polyline6&roundtrip=false&source=any", req.request().options.encode()) } @@ -30,7 +30,7 @@ func TestTripRequestOptionsWithSource(t *testing.T) { } assert.Equal( t, - "geometries=polyline6&source=first", + "destination=any&geometries=polyline6&roundtrip=true&source=first", req.request().options.encode()) } @@ -40,7 +40,7 @@ func TestTripRequestOptionsWithDestination(t *testing.T) { } assert.Equal( t, - "destination=last&geometries=polyline6", + "destination=last&geometries=polyline6&roundtrip=true&source=any", req.request().options.encode()) } @@ -51,7 +51,7 @@ func TestTripRequestOptions(t *testing.T) { } assert.Equal( t, - "destination=last&geometries=polyline6&roundtrip=true", + "destination=last&geometries=polyline6&roundtrip=true&source=any", req.request().options.encode()) } From 1717533242b74c0490a12bfb963a80d299387eb4 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 21 Nov 2020 20:15:15 +0200 Subject: [PATCH 3/4] add trip testdata --- osrm_test.go | 2 +- testdata/trip_response_full.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/osrm_test.go b/osrm_test.go index 2fe43f8..667b292 100644 --- a/osrm_test.go +++ b/osrm_test.go @@ -258,7 +258,7 @@ func TestTripRequest(t *testing.T) { })) defer ts.Close() - osrm := NewFromURL("http://router.project-osrm.org") + osrm := NewFromURL(ts.URL) tgeo := NewGeometryFromPointSet(geo.PointSet{ {13.3927165, 52.4956761}, diff --git a/testdata/trip_response_full.json b/testdata/trip_response_full.json index bacbf59..a1b6799 100644 --- a/testdata/trip_response_full.json +++ b/testdata/trip_response_full.json @@ -1 +1 @@ -{"code":"Ok","waypoints":[{"waypoint_index":1,"trips_index":0,"hint":"7e62k1XvtpMdAAAAAAAAAMkFAADvBQAA3XAAQgAAAADJes1E66TlRB0AAAAAAAAAyQUAAO8FAAD5FwEA3B0AAHKLbQJq____PpNtAh4ALxZX0TUP","location":[0.007644,40.733554],"name":""},{"waypoint_index":2,"trips_index":0,"hint":"6REchTsSHIUAAAAALQAAAAAAAAAHBgAAAAAAANKtxUEAAAAA1ttQRAAAAAAtAAAAAAAAAAcGAAD5FwEAONrqAOkNXf76AAAACIIE_gAADwBX0TUP","location":[15.391288,-27.456023],"name":""},{"waypoint_index":3,"trips_index":0,"hint":"6REchTsSHIUAAAAALQAAAAAAAAAHBgAAAAAAANKtxUEAAAAA1ttQRAAAAAAtAAAAAAAAAAcGAAD5FwEAONrqAOkNXf5kAAAAYm4E_gAADwBX0TUP","location":[15.391288,-27.456023],"name":""},{"waypoint_index":4,"trips_index":0,"hint":"6REchTsSHIUAAAAALQAAAAAAAAAHBgAAAAAAANKtxUEAAAAA1ttQRAAAAAAtAAAAAAAAAAcGAAD5FwEAONrqAOkNXf6QEAAATGEE_gAADwBX0TUP","location":[15.391288,-27.456023],"name":""},{"waypoint_index":5,"trips_index":0,"hint":"6REchTsSHIUAAAAALQAAAAAAAAAHBgAAAAAAANKtxUEAAAAA1ttQRAAAAAAtAAAAAAAAAAcGAAD5FwEAONrqAOkNXf4UFAAA9FQE_gAADwBX0TUP","location":[15.391288,-27.456023],"name":""},{"waypoint_index":0,"trips_index":0,"hint":"6REchTsSHIUAAAAALQAAAAAAAAAHBgAAAAAAANKtxUEAAAAA1ttQRAAAAAAtAAAAAAAAAAcGAAD5FwEAONrqAOkNXf7Q_f__Sk8E_gAADwBX0TUP","location":[15.391288,-27.456023],"name":""}],"trips":[{"legs":[{"steps":[],"weight":681257.8,"distance":15308145.4,"summary":"","duration":680919.1},{"steps":[],"weight":691620.3,"distance":15502797.3,"summary":"","duration":691281.6},{"steps":[],"weight":0,"distance":0,"summary":"","duration":0},{"steps":[],"weight":0,"distance":0,"summary":"","duration":0},{"steps":[],"weight":0,"distance":0,"summary":"","duration":0},{"steps":[],"weight":0,"distance":0,"summary":"","duration":0}],"weight_name":"routability","geometry":"l`xjs@oblj\\ufxq@moaDmpmKgxdeD_aejJram}Asqkq@grsi@}mbhByhcuCi`dCgeapJeehyBaoaqCosmtBqznX}pxiAc_zmCopl~EcpjwBatfK_mfpBmecgB{ppeA}wi|Df_jLu}zi@gqus@ucwyJux~yAwq~[{czx@qep|Adv|w@qhfaF_vs|@wu}hDf~ciBugvl@mjiUojxxEzqpcBcwz}BzexDemprAgbamAyzhqDbwwXqjvq@zglkAuuhzK~ymjEkb~Y{euo@bpse@ap`iByywn@spru@_dokJsgy{@{itl@phf`@cx`C~nqiA_vifAdt{NwvzcA~s{cBevgvAnzks@yhjA~gnaDyas}AtturIe{|}@xrqaCglvxA`zzvAyvzhAlnciJpii_@vwj`G`pc|B|oqvE}q~Sji}pCbteh@h`qqA|a_rAxt|Ltu`~@t`jeDyaa~@iekeDqq~qAmz{Lopeh@wzpqA|q~Ssh}pCcb{{BqmwvEgup_@kqe`G|uyhAwwbiJtsuxA}xuvAlo}}@clvaC`hs}Ac`vrIdfjAaimaD|robFgtfhDlk`CmmriAjpqn@mit`@pgqiJt~i|@|ky_BtkiiB}j~Bfozl@_kpsAnjke@b`yZjnxQtxgzKggojEpjvq@{glkAxzhqDcwwXdmprAfbamAbwz}B{exDnjxxE{qpcBtgvl@ljiU|i~hDcl~hBv`eaFxln|@dyp|Ac_}w@vq~[zczx@zuwyJfs~yAnkzi@tvus@|wi|Dg_jLlecgBzppeA`tfK~lfpBnpl~EbpjwB~zxiAfzymCr~ltBnmnX~ohyB~`bqCh`dCfeapJ`vbhBhncuClmkq@pesi@`}djJmzl}AlpmKfxdeDtfxq@loaD????????","weight":1372878.1,"distance":30810942.7,"duration":1372200.7}]} +{"code":"Ok","waypoints":[{"waypoint_index":1,"trips_index":0,"hint":"-TCrkv___383AAAAPAAAAD0AAAB2AAAA0t55Qm3amkDfT4hCRY04QzcAAAA8AAAAPQAAAHYAAADrFwEA4FrMAK4GIQNQW8wAQAUhAwIA_wSJ3Slu","location":[13.392608,52.496046],"name":"Blücherstraße"},{"waypoint_index":4,"trips_index":0,"hint":"-TCrkv___38rAAAAPAAAAD0AAAB2AAAASrRDQqQLk0HfT4hCRY04QysAAAA8AAAAPQAAAHYAAADrFwEAHFrMAJgGIQOIWswANgUhAwIA_wSJ3Slu","location":[13.392412,52.496024],"name":"Blücherstraße"},{"waypoint_index":3,"trips_index":0,"hint":"-TCrkv___387AAAAPAAAAD0AAAB2AAAALPiEQjeuUj_fT4hCRY04QzsAAAA8AAAAPQAAAHYAAADrFwEAGlvMALUGIQOMW8wAQAUhAwIA_wSJ3Slu","location":[13.392666,52.496053],"name":"Blücherstraße"},{"waypoint_index":2,"trips_index":0,"hint":"-TCrkv___386AAAAPAAAAD0AAAB2AAAAEsCCQshB9z_fT4hCRY04QzoAAAA8AAAAPQAAAHYAAADrFwEAClvMALMGIQOWW8wA5gQhAwIA_wSJ3Slu","location":[13.39265,52.496051],"name":"Blücherstraße"},{"waypoint_index":0,"trips_index":0,"hint":"-TCrkv___38yAAAAPAAAAD0AAAB2AAAA0XhgQmgFM0HfT4hCRY04QzIAAAA8AAAAPQAAAHYAAADrFwEAhFrMAKQGIQMAW8wADgUhAwIA_wSJ3Slu","location":[13.392516,52.496036],"name":"Blücherstraße"}],"trips":[{"legs":[{"steps":[],"weight":0.5,"distance":6.3,"summary":"","duration":0.5},{"steps":[],"weight":0.3,"distance":2.9,"summary":"","duration":0.3},{"steps":[],"weight":0.1,"distance":1.1,"summary":"","duration":0.1},{"steps":[],"weight":70.6,"distance":541.3,"summary":"","duration":70.6},{"steps":[],"weight":0.7,"distance":7.2,"summary":"","duration":0.7}],"weight_name":"routability","geometry":"gibccBgglpXSwDIsAC_@}E}oAOof@uFSxAn}@zKnuB|Ch~@tEa@kIiiBWoE","weight":72.2,"distance":558.8,"duration":72.2}]} From 5e711f929f5d47f026e5d73204b7efe001ff9781 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 21 Nov 2020 23:56:08 +0200 Subject: [PATCH 4/4] extend table request/response --- go.mod | 4 +++- table.go | 23 +++++++++++++++++++++-- table_test.go | 10 +++++++--- types.go | 12 ++++++++++++ vendor/modules.txt | 3 +++ 5 files changed, 46 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 74470fa..2438826 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,9 @@ module github.com/gojuno/go.osrm +go 1.15 + require ( github.com/paulmach/go.geo v0.0.0-20180829195134-22b514266d33 - github.com/paulmach/go.geojson v1.4.0 // indirect + github.com/paulmach/go.geojson v1.4.0 github.com/stretchr/testify v1.3.0 ) diff --git a/table.go b/table.go index a539131..908ac88 100644 --- a/table.go +++ b/table.go @@ -5,12 +5,20 @@ type TableRequest struct { Profile string Coordinates Geometry Sources, Destinations []int + Annotations Annotations + FallbackSpeed float64 + FallbackCoordinate FallbackCoordinate + ScaleFactor float64 } // TableResponse resresents a response from the table method type TableResponse struct { ResponseStatus - Durations [][]float32 `json:"durations"` + Durations [][]float32 `json:"durations"` + Distances [][]float32 `json:"distances"` + Sources []Waypoint `json:"sources"` + Destinations []Waypoint `json:"destinations"` + FallbackSpeedCells [][]bool `json:"fallback_speed_cells"` } func (r TableRequest) request() *request { @@ -21,7 +29,18 @@ func (r TableRequest) request() *request { if len(r.Destinations) > 0 { opts.addInt("destinations", r.Destinations...) } - + if len(r.Annotations) > 0 { + opts.setStringer("annotations", r.Annotations) + } + if r.FallbackSpeed > 0 { + opts.addFloat("fallback_speed", r.FallbackSpeed) + } + if len(r.FallbackCoordinate) > 0 { + opts.setStringer("fallback_coordinate", r.FallbackCoordinate) + } + if r.ScaleFactor > 0 { + opts.addFloat("scale_factor", r.ScaleFactor) + } return &request{ profile: r.Profile, coords: r.Coordinates, diff --git a/table_test.go b/table_test.go index cf8a55f..d4574bc 100644 --- a/table_test.go +++ b/table_test.go @@ -13,8 +13,12 @@ func TestEmptyTableRequestOptions(t *testing.T) { func TestNotEmptyTableRequestOptions(t *testing.T) { req := TableRequest{ - Sources: []int{0, 1, 2}, - Destinations: []int{1, 3}, + Sources: []int{0, 1, 2}, + Destinations: []int{1, 3}, + Annotations: AnnotationsDuration, + FallbackSpeed: 45, + FallbackCoordinate: FallbackCoordinateSnapped, + ScaleFactor: 1.052, } - assert.Equal(t, "destinations=1;3&sources=0;1;2", req.request().options.encode()) + assert.Equal(t, "annotations=duration&destinations=1;3&fallback_coordinate=snapped&fallback_speed=45&scale_factor=1.052&sources=0;1;2", req.request().options.encode()) } diff --git a/types.go b/types.go index 8f5a559..82eb45c 100644 --- a/types.go +++ b/types.go @@ -159,6 +159,18 @@ func (o Overview) String() string { return string(o) } +type FallbackCoordinate string + +const ( + FallbackCoordinateDefault FallbackCoordinate = "input" + FallbackCoordinateInput FallbackCoordinate = "input" + FallbackCoordinateSnapped FallbackCoordinate = "snapped" +) + +func (f FallbackCoordinate) String() string { + return string(f) +} + // ContinueStraight represents continue_straight OSRM routing parameter type ContinueStraight string diff --git a/vendor/modules.txt b/vendor/modules.txt index 1ccfc62..d4483dd 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,11 +1,14 @@ # github.com/davecgh/go-spew v1.1.0 github.com/davecgh/go-spew/spew # github.com/paulmach/go.geo v0.0.0-20180829195134-22b514266d33 +## explicit github.com/paulmach/go.geo # github.com/paulmach/go.geojson v1.4.0 +## explicit github.com/paulmach/go.geojson # github.com/pmezard/go-difflib v1.0.0 github.com/pmezard/go-difflib/difflib # github.com/stretchr/testify v1.3.0 +## explicit github.com/stretchr/testify/assert github.com/stretchr/testify/require