Skip to content

Commit

Permalink
Open Source Routing Machine (OSRM)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmichaelchen committed May 28, 2022
1 parent f7d6a0a commit f32e0df
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ require (
github.com/envoyproxy/protoc-gen-validate v0.1.0
github.com/friendsofgo/errors v0.9.2
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
github.com/gojuno/go.osrm v0.1.0
github.com/google/go-cmp v0.5.8
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/kat-co/vala v0.0.0-20170210184112-42e1d8b61f12
github.com/lib/pq v1.10.5
github.com/paulmach/go.geo v0.0.0-20180829195134-22b514266d33
github.com/rs/xid v1.4.0
github.com/sethvargo/go-envconfig v0.6.0
github.com/spf13/cobra v1.4.0
Expand Down Expand Up @@ -39,6 +41,7 @@ require (
)

require (
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand All @@ -55,6 +58,7 @@ require (
github.com/magiconair/properties v1.8.5 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.4.2 // indirect
github.com/paulmach/go.geojson v1.4.0 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRx
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/gojuno/go.osrm v0.1.0 h1:M9RH1raBe7D72preWxFOw9WafGftGBQyoGNkG6upAT0=
github.com/gojuno/go.osrm v0.1.0/go.mod h1:XPCHB/Ir2/vHnqhKlfUxIiUGHFtTzgrRxD89JdkJhrs=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down Expand Up @@ -335,6 +337,10 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/paulmach/go.geo v0.0.0-20180829195134-22b514266d33 h1:doG/0aLlWE6E4ndyQlkAQrPwaojghwz1IlmH0kjTdyk=
github.com/paulmach/go.geo v0.0.0-20180829195134-22b514266d33/go.mod h1:btFYk/ltlMU7ZKguHS7zQrwHYCtLoXGTaa44OsPbEVw=
github.com/paulmach/go.geojson v1.4.0 h1:5x5moCkCtDo5x8af62P9IOAYGQcYHtxz2QJ3x1DoCgY=
github.com/paulmach/go.geojson v1.4.0/go.mod h1:YaKx1hKpWF+T2oj2lFJPsW/t1Q5e1jQI61eoQSTwpIs=
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM=
github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
Expand Down
12 changes: 12 additions & 0 deletions internal/service/osrm/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Package osrm provides functions over the Open Source Routing Machine project.
We use OSRM's Table service when we want to quickly determine the duration
and/or distance between two points (without traffic awareness).
This could be useful for saving money/requests to Google Maps API every time an
anonymous end user sees an estimate quote for a potential trip.
https://github.com/Project-OSRM/osrm-backend/blob/master/docs/http.md#table-service
*/
package osrm
55 changes: 55 additions & 0 deletions internal/service/osrm/osrm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package osrm

import (
"context"
"errors"
"fmt"
osrm "github.com/gojuno/go.osrm"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"github.com/kevinmichaelchen/api-dispatch/internal/idl/coop/drivers/dispatch/v1beta1"
geo "github.com/paulmach/go.geo"
"go.uber.org/zap"
"time"
)

var (
errFailedRequest = errors.New("failed OSRM request")
)

type CalculateOutput struct {
Duration time.Duration
DistanceMeters int
}

func Calculate(ctx context.Context, a, b *v1beta1.LatLng) (*CalculateOutput, error) {
logger := ctxzap.Extract(ctx)

client := osrm.NewFromURL("https://router.project-osrm.org")

res, err := client.Table(ctx, osrm.TableRequest{
Profile: "car",
Coordinates: osrm.NewGeometryFromPointSet(geo.PointSet{
{a.GetLongitude(), a.GetLatitude()},
{b.GetLongitude(), b.GetLatitude()},
}),
Sources: []int{0},
Destinations: []int{1},
})

if err != nil {
return nil, fmt.Errorf("failed OSRM request: %w", err)
}

if res.Code != "Ok" {
logger.Error("received non-ok OSRM response",
zap.String("error.code", res.Code),
zap.String("error.msg", res.Message),
)
return nil, errFailedRequest
}

return &CalculateOutput{
Duration: time.Duration(res.Durations[0][0]) * time.Second,
DistanceMeters: 0,
}, nil
}
27 changes: 27 additions & 0 deletions internal/service/osrm/osrm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package osrm

import (
"context"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"github.com/kevinmichaelchen/api-dispatch/internal/idl/coop/drivers/dispatch/v1beta1"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
"testing"
"time"
)

func TestCalculate(t *testing.T) {
a := &v1beta1.LatLng{
Latitude: 40.791680675548136,
Longitude: -73.9650115649754,
}
b := &v1beta1.LatLng{
Latitude: 40.76866089218841,
Longitude: -73.98145413365043,
}

ctx := ctxzap.ToContext(context.Background(), zaptest.NewLogger(t))
res, err := Calculate(ctx, a, b)
require.NoError(t, err)
require.Greater(t, res.Duration, time.Duration(0))
}

0 comments on commit f32e0df

Please sign in to comment.