Skip to content

Commit f2ee611

Browse files
committed
feat: added support for all nearest, route, table, match and trip services
1 parent dfd025b commit f2ee611

22 files changed

+1608
-0
lines changed

README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
### GOSRM
2+
---
3+
**GOSRM** is an OSRM client written in Go. It implements all OSRM 5.x installations.
4+
If you want to get the most out of this package I highly recommend to read OSRM [docs](https://github.com/Project-OSRM/osrm-backend/blob/master/docs/http.md).
5+
6+
#### Features
7+
---
8+
- [x] Nearest Service
9+
- [x] Route Service
10+
- [x] Table Service
11+
- [x] Match Service
12+
- [x] Trip Service
13+
- [ ] Tile Service
14+
15+
#### Installation
16+
---
17+
Requires Go >= 1.18: `go get github.com/mojixcoder/gosrm`
18+
19+
#### How to use
20+
---
21+
```go
22+
package main
23+
24+
import (
25+
"context"
26+
"fmt"
27+
28+
"github.com/mojixcoder/gosrm"
29+
)
30+
31+
func main() {
32+
osrm, err := gosrm.New("http://router.project-osrm.org")
33+
checkErr(err)
34+
35+
nearestRes, err := gosrm.Nearest(context.Background(), osrm, gosrm.Request{
36+
Profile: gosrm.ProfileDriving,
37+
Coordinates: []gosrm.Coordinate{{13.388860, 52.517037}},
38+
}, gosrm.WithNumber(3), gosrm.WithBearings([]gosrm.Bearing{{Value: 0, Range: 20}}))
39+
checkErr(err)
40+
41+
fmt.Println("### Nearest Response ###")
42+
fmt.Printf("%#v\n", nearestRes)
43+
fmt.Println("##########")
44+
45+
// String type represents the type of geometries returned by OSRM.
46+
// It can be either string or gosrm.LineString based on geometries option.
47+
// If you don't specify any geometries the default is polyline and you can use string.
48+
routeRes, err := gosrm.Route[string](context.Background(), osrm, gosrm.Request{
49+
Profile: gosrm.ProfileDriving,
50+
Coordinates: []gosrm.Coordinate{{13.388860, 52.517037}, {13.397634, 52.529407}, {13.428555, 52.523219}},
51+
})
52+
checkErr(err)
53+
54+
fmt.Println("\n### Route Response ###")
55+
fmt.Printf("%#v\n", routeRes)
56+
fmt.Println("##########")
57+
58+
tableRes, err := gosrm.Table(context.Background(), osrm, gosrm.Request{
59+
Profile: gosrm.ProfileDriving,
60+
Coordinates: []gosrm.Coordinate{{13.388860, 52.517037}, {13.397634, 52.529407}, {13.428555, 52.523219}},
61+
}, gosrm.WithSources([]uint16{0, 1}), gosrm.WithDestinations([]uint16{2}))
62+
checkErr(err)
63+
64+
fmt.Println("\n### Table Response ###")
65+
fmt.Printf("%#v\n", tableRes)
66+
fmt.Println("##########")
67+
68+
// This time we use geojson geometries so geometry type is gosrm.LineString not string.
69+
matchRes, err := gosrm.Match[gosrm.LineString](context.Background(), osrm, gosrm.Request{
70+
Profile: gosrm.ProfileDriving,
71+
Coordinates: []gosrm.Coordinate{{13.3122, 52.5322}, {13.3065, 52.5283}},
72+
}, gosrm.WithAnnotations(gosrm.AnnotationsTrue), gosrm.WithGeometries(gosrm.GeometryGeoJSON))
73+
checkErr(err)
74+
75+
fmt.Println("\n### Match Response ###")
76+
fmt.Printf("%#v\n", matchRes)
77+
fmt.Println("##########")
78+
79+
tripRes, err := gosrm.Trip[string](context.Background(), osrm, gosrm.Request{
80+
Profile: gosrm.ProfileDriving,
81+
Coordinates: []gosrm.Coordinate{{13.388860, 52.517037}, {13.397634, 52.529407}, {13.428555, 52.523219}, {13.418555, 52.523215}},
82+
}, gosrm.WithSource(gosrm.SourceFirst), gosrm.WithDestination(gosrm.DestinationLast))
83+
checkErr(err)
84+
85+
fmt.Println("\n### Trip Response ###")
86+
fmt.Printf("%#v\n", tripRes)
87+
fmt.Println("##########")
88+
}
89+
90+
func checkErr(err error) {
91+
if err != nil {
92+
panic(err)
93+
}
94+
}
95+
```

consts.go

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
package gosrm
2+
3+
type (
4+
// Geometry is the geometry format.
5+
Geometry string
6+
7+
// Overview is the type for overview option.
8+
Overview string
9+
10+
// ContinueStraight is the type for continue straight option.
11+
ContinueStraight string
12+
13+
// Gaps is the type for gaps option.
14+
Gaps string
15+
16+
// Annotations is the type for annotations option.
17+
Annotations string
18+
19+
// Source is the type for source option.
20+
Source string
21+
22+
// Destination is the type for destination option.
23+
Destination string
24+
25+
// Code is the error code returned by OSRM.
26+
Code string
27+
28+
// Profile is the mode of transportation, is determined statically by the Lua profile that is used to prepare the data using osrm-extract.
29+
// Typically car, bike or foot if using one of the supplied profiles.
30+
Profile string
31+
32+
// Approaches is the type for approaches option.
33+
Approaches string
34+
35+
// Snapping is the type for snapping option.
36+
Snapping string
37+
38+
// FallbackCoordinate is the type for fallback coordinate option.
39+
FallbackCoordinate string
40+
)
41+
42+
const (
43+
// CodeOK is returned when request could be processed as expected.
44+
CodeOK Code = "Ok"
45+
46+
//CodeInvalidUrl is returned when URL string is invalid.
47+
CodeInvalidUrl Code = "InvalidUrl"
48+
49+
// CodeInvalidService is returned when service name is invalid.
50+
CodeInvalidService Code = "InvalidService"
51+
52+
// CodeInvalidVersion is returned when version is not found.
53+
CodeInvalidVersion Code = "InvalidVersion"
54+
55+
// CodeInvalidOptions is returned when options are invalid.
56+
CodeInvalidOptions Code = "InvalidOptions"
57+
58+
// CodeInvalidQuery is returned when the query string is synctactically malformed.
59+
CodeInvalidQuery Code = "InvalidQuery"
60+
61+
// CodeInvalidValue is returned when the successfully parsed query parameters are invalid.
62+
CodeInvalidValue Code = "InvalidValue"
63+
64+
// CodeNoSegment is returned when one of the supplied input coordinates could not snap to street segment.
65+
CodeNoSegment Code = "NoSegment"
66+
67+
// CodeTooBig is returned when the request size violates one of the service specific request size restrictions.
68+
CodeTooBig Code = "TooBig"
69+
70+
// CodeNoRoute is returned when no route was found.
71+
CodeNoRoute Code = "NoRoute"
72+
73+
// CodeNoTable is returned when no route was found.
74+
CodeNoTable Code = "NoTable"
75+
76+
// CodeNoMatch is returned when no match was found.
77+
CodeNoMatch Code = "NoMatch"
78+
79+
// CodeNoTrips is returned when no trips were found because input coordinates are not connected.
80+
CodeNoTrips Code = "NoTrips"
81+
82+
// CodeNotImplemented is returned when this request is not supported.
83+
CodeNotImplemented Code = "NotImplemented"
84+
)
85+
86+
const (
87+
// ProfileDriving is the driving transportation mode.
88+
ProfileDriving Profile = "driving"
89+
90+
// ProfileCar is the car transportation mode.
91+
ProfileCar Profile = "car"
92+
93+
// ProfileBike is the bike transportation mode.
94+
ProfileBike Profile = "bike"
95+
96+
// ProfileFoot is the foot transportation mode.
97+
ProfileFoot Profile = "foot"
98+
)
99+
100+
const (
101+
// GeometryPolyline is the geometry polyline type.
102+
GeometryPolyline Geometry = "polyline"
103+
104+
// GeometryPolyline is the geometry polyline6 type.
105+
GeometryPolyline6 Geometry = "polyline6"
106+
107+
// GeometryPolyline is the geometry geojson type.
108+
GeometryGeoJSON Geometry = "geojson"
109+
)
110+
111+
const (
112+
// OverviewSimplified is the simplified overview.
113+
OverviewSimplified Overview = "simplified"
114+
115+
// OverviewFull is the full overview.
116+
OverviewFull Overview = "full"
117+
118+
// OverviewFalse disables the overview.
119+
OverviewFalse Overview = "false"
120+
)
121+
122+
const (
123+
// ContinueStraightDefault is the default continue straight option value.
124+
ContinueStraightDefault ContinueStraight = "default"
125+
126+
// ContinueStraightTrue enables continue straight.
127+
ContinueStraightTrue ContinueStraight = "true"
128+
129+
// ContinueStraightFalse disables continue straight.
130+
ContinueStraightFalse ContinueStraight = "false"
131+
)
132+
133+
const (
134+
// GapsSplit is the default gaps option value.
135+
GapsSplit Gaps = "split"
136+
137+
// GapsIgnore is the ignore gaps option.
138+
GapsIgnore Gaps = "ignore"
139+
)
140+
141+
const (
142+
// AnnotationsTrue enables annotations.
143+
AnnotationsTrue Annotations = "true"
144+
145+
// AnnotationsFalse disables annotations.
146+
AnnotationsFalse Annotations = "false"
147+
148+
// AnnotationsNodes is node annotations.
149+
AnnotationsNodes Annotations = "nodes"
150+
151+
// AnnotationsSpeed is speed annotations.
152+
AnnotationsSpeed Annotations = "speed"
153+
154+
// AnnotationsWeight is weight annotations.
155+
AnnotationsWeight Annotations = "weight"
156+
157+
// AnnotationsDistance is distance annotations.
158+
AnnotationsDistance Annotations = "distance"
159+
160+
// AnnotationsDuration is duration annotations.
161+
AnnotationsDuration Annotations = "duration"
162+
163+
// AnnotationsDataSources is data sources annotations.
164+
AnnotationsDataSources Annotations = "datasources"
165+
166+
// AnnotationsDurationSpeed is duration and distance annotations.
167+
AnnotationsDurationDistance Annotations = "duration,distance"
168+
)
169+
170+
const (
171+
// SourceAny is the any source option.
172+
SourceAny Source = "any"
173+
174+
// SourceFirst is the first source option.
175+
SourceFirst Source = "first"
176+
)
177+
178+
const (
179+
// DestinationAny is the any destination option.
180+
DestinationAny Destination = "any"
181+
182+
// DestinationLast is the last destination option.
183+
DestinationLast Destination = "last"
184+
)
185+
186+
const (
187+
// ApproachesCurb is the curb approaches.
188+
ApproachesCurb Approaches = "curb"
189+
190+
// ApproachesUnrestricted is the unrestricted approaches.
191+
ApproachesUnrestricted Approaches = "unrestricted"
192+
)
193+
194+
const (
195+
// SnappingDefault is the default snapping.
196+
SnappingDefault Snapping = "default"
197+
198+
// SnappingAny is the any snapping.
199+
SnappingAny Snapping = "any"
200+
)
201+
202+
const (
203+
// FallbackCoordinateInput when using a fallback_speed, use the user-supplied coordinate (input).
204+
FallbackCoordinateInput FallbackCoordinate = "input"
205+
206+
// FallbackCoordinateSnapped when using a fallback_speed, use the snapped location (snapped) for calculating distances.
207+
FallbackCoordinateSnapped FallbackCoordinate = "snapped"
208+
)

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/mojixcoder/gosrm
2+
3+
go 1.20
4+
5+
require github.com/stretchr/testify v1.8.2
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1 // indirect
11+
)

go.sum

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
8+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
9+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
10+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
11+
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
12+
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
13+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
14+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
15+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
16+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
17+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)