Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
)
13 changes: 13 additions & 0 deletions osrm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
35 changes: 35 additions & 0 deletions osrm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(ts.URL)
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])
}
23 changes: 21 additions & 2 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
10 changes: 7 additions & 3 deletions table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
1 change: 1 addition & 0 deletions testdata/trip_response_full.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"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}]}
49 changes: 49 additions & 0 deletions trip.go
Original file line number Diff line number Diff line change
@@ -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", valueOrDefault(r.Roundtrip, RoundtripDefault)).
setStringer("source", valueOrDefault(r.Source, SourceDefault)).
setStringer("destination", valueOrDefault(r.Destination, DestinationDefault)),
}
}

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
}
92 changes: 92 additions & 0 deletions trip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package osrm

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestEmptyTripRequestOptions(t *testing.T) {
req := TripRequest{}
assert.Equal(
t,
"destination=any&geometries=polyline6&roundtrip=true&source=any",
req.request().options.encode())
}

func TestTripRequestOptionsWithRoundtrip(t *testing.T) {
req := TripRequest{
Roundtrip: RoundtripFalse,
}
assert.Equal(
t,
"destination=any&geometries=polyline6&roundtrip=false&source=any",
req.request().options.encode())
}

func TestTripRequestOptionsWithSource(t *testing.T) {
req := TripRequest{
Source: SourceFirst,
}
assert.Equal(
t,
"destination=any&geometries=polyline6&roundtrip=true&source=first",
req.request().options.encode())
}

func TestTripRequestOptionsWithDestination(t *testing.T) {
req := TripRequest{
Destination: DestinationLast,
}
assert.Equal(
t,
"destination=last&geometries=polyline6&roundtrip=true&source=any",
req.request().options.encode())
}

func TestTripRequestOptions(t *testing.T) {
req := TripRequest{
Roundtrip: RoundtripTrue,
Destination: DestinationLast,
}
assert.Equal(
t,
"destination=last&geometries=polyline6&roundtrip=true&source=any",
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())
}
48 changes: 48 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -174,6 +186,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
Expand Down
3 changes: 3 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
@@ -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