-
Notifications
You must be signed in to change notification settings - Fork 17
/
route.go
141 lines (123 loc) · 3.84 KB
/
route.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package osrm
import (
"fmt"
"strconv"
geo "github.com/paulmach/go.geo"
)
// RouteRequest represents a request to the route method
type RouteRequest struct {
Profile string
Coordinates Geometry
Bearings []Bearing
Steps Steps
Annotations Annotations
Overview Overview
Geometries Geometries
ContinueStraight ContinueStraight
Waypoints []int
}
// RouteResponse represents a response from the route method
type RouteResponse struct {
ResponseStatus
Routes []Route `json:"routes"`
Waypoints []Waypoint `json:"waypoints"`
}
type Waypoint struct {
Name string `json:"name"`
Location geo.Point `json:"location"`
Distance float32 `json:"distance"`
Hint string `json:"hint"`
}
// Route represents a route through (potentially multiple) points.
type Route struct {
Distance float32 `json:"distance"`
Duration float32 `json:"duration"`
WeightName string `json:"weight_name"`
Wieght float32 `json:"weight"`
Geometry Geometry `json:"geometry"`
Legs []RouteLeg `json:"legs"`
}
// RouteLeg represents a route between two waypoints.
type RouteLeg struct {
Annotation Annotation `json:"annotation"`
Distance float32 `json:"distance"`
Duration float32 `json:"duration"`
Summary string `json:"summary"`
Weight float32 `json:"weight"`
Steps []RouteStep `json:"steps"`
}
// Annotation contains additional metadata for each coordinate along the route geometry
type Annotation struct {
Duration []float32 `json:"duration,omitempty"`
Distance []float32 `json:"distance,omitempty"`
Nodes []uint64 `json:"nodes,omitempty"`
}
// RouteStep represents a route geometry
type RouteStep struct {
Distance float32 `json:"distance"`
Duration float32 `json:"duration"`
Geometry Geometry `json:"geometry"`
Name string `json:"name"`
Mode string `json:"mode"`
DrivingSide string `json:"driving_side"`
Weight float32 `json:"weight"`
Maneuver StepManeuver `json:"maneuver"`
Intersections []Intersection `json:"intersections,omitempty"`
}
type Intersection struct {
Location geo.Point `json:"location"`
Bearings []uint16 `json:"bearings"`
Entry []bool `json:"entry"`
In *uint32 `json:"in,omitempty"`
Out *uint32 `json:"out,omitempty"`
Lanes []Lane `json:"lanes,omitempty"`
}
type Lane struct {
Indications []string `json:"indications"`
Valid bool `json:"valid"`
}
// StepManeuver contains information about maneuver in step
type StepManeuver struct {
Location geo.Point `json:"location"`
BearingBefore float32 `json:"bearing_before"`
BearingAfter float32 `json:"bearing_after"`
Type string `json:"type"`
Modifier string `json:"modifier,omitempty"`
Exit *uint32 `json:"exit,omitempty"`
}
func (r RouteRequest) request() *request {
opts := stepsOptions(r.Steps, r.Annotations, r.Overview, r.Geometries).
setStringer("continue_straight", r.ContinueStraight)
if len(r.Waypoints) > 0 {
waypoints := ""
for i, w := range r.Waypoints {
if i > 0 {
waypoints += ";"
}
waypoints += strconv.Itoa(w)
}
opts.set("waypoints", waypoints)
}
if len(r.Bearings) > 0 {
opts.set("bearings", bearings(r.Bearings))
}
return &request{
profile: r.Profile,
coords: r.Coordinates,
service: "route",
options: opts,
}
}
func stepsOptions(steps Steps, annotations Annotations, overview Overview, geometries Geometries) options {
return options{}.
setStringer("steps", steps).
setStringer("annotations", annotations).
setStringer("geometries", valueOrDefault(geometries, GeometriesPolyline6)).
setStringer("overview", overview)
}
func valueOrDefault(value, def fmt.Stringer) fmt.Stringer {
if value.String() == "" {
return def
}
return value
}