Skip to content
This repository was archived by the owner on Jan 15, 2026. It is now read-only.

Commit f32e0df

Browse files
Open Source Routing Machine (OSRM)
1 parent f7d6a0a commit f32e0df

5 files changed

Lines changed: 104 additions & 0 deletions

File tree

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ require (
77
github.com/envoyproxy/protoc-gen-validate v0.1.0
88
github.com/friendsofgo/errors v0.9.2
99
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
10+
github.com/gojuno/go.osrm v0.1.0
1011
github.com/google/go-cmp v0.5.8
1112
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
1213
github.com/kat-co/vala v0.0.0-20170210184112-42e1d8b61f12
1314
github.com/lib/pq v1.10.5
15+
github.com/paulmach/go.geo v0.0.0-20180829195134-22b514266d33
1416
github.com/rs/xid v1.4.0
1517
github.com/sethvargo/go-envconfig v0.6.0
1618
github.com/spf13/cobra v1.4.0
@@ -39,6 +41,7 @@ require (
3941
)
4042

4143
require (
44+
github.com/benbjohnson/clock v1.3.0 // indirect
4245
github.com/beorn7/perks v1.0.1 // indirect
4346
github.com/cespare/xxhash/v2 v2.1.2 // indirect
4447
github.com/davecgh/go-spew v1.1.1 // indirect
@@ -55,6 +58,7 @@ require (
5558
github.com/magiconair/properties v1.8.5 // indirect
5659
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
5760
github.com/mitchellh/mapstructure v1.4.2 // indirect
61+
github.com/paulmach/go.geojson v1.4.0 // indirect
5862
github.com/pelletier/go-toml v1.9.4 // indirect
5963
github.com/pmezard/go-difflib v1.0.0 // indirect
6064
github.com/prometheus/client_golang v1.12.1 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRx
151151
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
152152
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
153153
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
154+
github.com/gojuno/go.osrm v0.1.0 h1:M9RH1raBe7D72preWxFOw9WafGftGBQyoGNkG6upAT0=
155+
github.com/gojuno/go.osrm v0.1.0/go.mod h1:XPCHB/Ir2/vHnqhKlfUxIiUGHFtTzgrRxD89JdkJhrs=
154156
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
155157
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
156158
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
@@ -335,6 +337,10 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW
335337
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
336338
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
337339
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
340+
github.com/paulmach/go.geo v0.0.0-20180829195134-22b514266d33 h1:doG/0aLlWE6E4ndyQlkAQrPwaojghwz1IlmH0kjTdyk=
341+
github.com/paulmach/go.geo v0.0.0-20180829195134-22b514266d33/go.mod h1:btFYk/ltlMU7ZKguHS7zQrwHYCtLoXGTaa44OsPbEVw=
342+
github.com/paulmach/go.geojson v1.4.0 h1:5x5moCkCtDo5x8af62P9IOAYGQcYHtxz2QJ3x1DoCgY=
343+
github.com/paulmach/go.geojson v1.4.0/go.mod h1:YaKx1hKpWF+T2oj2lFJPsW/t1Q5e1jQI61eoQSTwpIs=
338344
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
339345
github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM=
340346
github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=

internal/service/osrm/doc.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
Package osrm provides functions over the Open Source Routing Machine project.
3+
4+
We use OSRM's Table service when we want to quickly determine the duration
5+
and/or distance between two points (without traffic awareness).
6+
7+
This could be useful for saving money/requests to Google Maps API every time an
8+
anonymous end user sees an estimate quote for a potential trip.
9+
10+
https://github.com/Project-OSRM/osrm-backend/blob/master/docs/http.md#table-service
11+
*/
12+
package osrm

internal/service/osrm/osrm.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package osrm
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
osrm "github.com/gojuno/go.osrm"
8+
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
9+
"github.com/kevinmichaelchen/api-dispatch/internal/idl/coop/drivers/dispatch/v1beta1"
10+
geo "github.com/paulmach/go.geo"
11+
"go.uber.org/zap"
12+
"time"
13+
)
14+
15+
var (
16+
errFailedRequest = errors.New("failed OSRM request")
17+
)
18+
19+
type CalculateOutput struct {
20+
Duration time.Duration
21+
DistanceMeters int
22+
}
23+
24+
func Calculate(ctx context.Context, a, b *v1beta1.LatLng) (*CalculateOutput, error) {
25+
logger := ctxzap.Extract(ctx)
26+
27+
client := osrm.NewFromURL("https://router.project-osrm.org")
28+
29+
res, err := client.Table(ctx, osrm.TableRequest{
30+
Profile: "car",
31+
Coordinates: osrm.NewGeometryFromPointSet(geo.PointSet{
32+
{a.GetLongitude(), a.GetLatitude()},
33+
{b.GetLongitude(), b.GetLatitude()},
34+
}),
35+
Sources: []int{0},
36+
Destinations: []int{1},
37+
})
38+
39+
if err != nil {
40+
return nil, fmt.Errorf("failed OSRM request: %w", err)
41+
}
42+
43+
if res.Code != "Ok" {
44+
logger.Error("received non-ok OSRM response",
45+
zap.String("error.code", res.Code),
46+
zap.String("error.msg", res.Message),
47+
)
48+
return nil, errFailedRequest
49+
}
50+
51+
return &CalculateOutput{
52+
Duration: time.Duration(res.Durations[0][0]) * time.Second,
53+
DistanceMeters: 0,
54+
}, nil
55+
}

internal/service/osrm/osrm_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package osrm
2+
3+
import (
4+
"context"
5+
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
6+
"github.com/kevinmichaelchen/api-dispatch/internal/idl/coop/drivers/dispatch/v1beta1"
7+
"github.com/stretchr/testify/require"
8+
"go.uber.org/zap/zaptest"
9+
"testing"
10+
"time"
11+
)
12+
13+
func TestCalculate(t *testing.T) {
14+
a := &v1beta1.LatLng{
15+
Latitude: 40.791680675548136,
16+
Longitude: -73.9650115649754,
17+
}
18+
b := &v1beta1.LatLng{
19+
Latitude: 40.76866089218841,
20+
Longitude: -73.98145413365043,
21+
}
22+
23+
ctx := ctxzap.ToContext(context.Background(), zaptest.NewLogger(t))
24+
res, err := Calculate(ctx, a, b)
25+
require.NoError(t, err)
26+
require.Greater(t, res.Duration, time.Duration(0))
27+
}

0 commit comments

Comments
 (0)