forked from vladoatanasov/valhalla-go-bindings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute.go
113 lines (101 loc) · 3.65 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
package valhalla
import (
"bytes"
easyjson "github.com/mailru/easyjson"
)
type RouteRequest struct {
Locations []Location `json:"locations,omitempty"`
Costing string `json:"costing,omitempty"`
DirectionsOptions DirectionsOptions `json:"directions_options,omitempty"`
ID string `json:"id,omitempty"`
CostingOptions CostingOptions `json:"costing_options,omitempty"`
AvoidLocations []Location `json:"avoid_locations,omitempty"`
Verbose bool `json:"verbose,omitempty"`
// isochrone request
Contours []Contour `json:"contours,omitempty"`
Polygons bool `json:"polygons,omitempty"`
Denoise float32 `json:"denoise,omitempty"`
Generalize float32 `json:"generalize,omitempty"`
}
type Contour struct {
Time int `json:"time,omitempty"`
Color string `json:"color,omitempty"`
}
type RouteResponse struct {
ID string `json:"id"`
Trip struct {
Language string `json:"language"`
Status int `json:"status"`
Units string `json:"units"`
StatusMessage string `json:"status_message"`
Legs []struct {
Shape string `json:"shape"`
Summary struct {
MaxLon float64 `json:"max_lon"`
MaxLat float64 `json:"max_lat"`
Time float64 `json:"time"`
Length float64 `json:"length"`
MinLat float64 `json:"min_lat"`
MinLon float64 `json:"min_lon"`
} `json:"summary"`
Maneuvers []Maneuver `json:"maneuvers"`
} `json:"legs"`
Summary struct {
MaxLon float64 `json:"max_lon"`
MaxLat float64 `json:"max_lat"`
Time float64 `json:"time"`
Length float64 `json:"length"`
MinLat float64 `json:"min_lat"`
MinLon float64 `json:"min_lon"`
} `json:"summary"`
Locations []struct {
OriginalIndex int `json:"original_index"`
Type string `json:"type"`
Lon float64 `json:"lon"`
Lat float64 `json:"lat"`
SideOfStreet string `json:"side_of_street"`
} `json:"locations"`
} `json:"trip"`
Alternates []RouteResponse `json:"alternates"`
}
type Maneuver struct {
TravelMode string `json:"travel_mode"`
BeginShapeIndex int `json:"begin_shape_index"`
Length float64 `json:"length"`
Time float64 `json:"time"`
Type int `json:"type"`
EndShapeIndex int `json:"end_shape_index"`
Instruction string `json:"instruction"`
VerbalPreTransitionInstruction string `json:"verbal_pre_transition_instruction"`
TravelType string `json:"travel_type"`
StreetNames []string `json:"street_names,omitempty"`
VerbalTransitionAlertInstruction string `json:"verbal_transition_alert_instruction,omitempty"`
VerbalPostTransitionInstruction string `json:"verbal_post_transition_instruction,omitempty"`
Sign struct {
ExitBranchElements []struct {
Text string `json:"text"`
} `json:"exit_branch_elements"`
} `json:"sign,omitempty"`
}
type ValhallaError struct {
ErrorCode int `json:"error_code"`
Error string `json:"error"`
StatusCode int `json:"status_code"`
Status string `json:"status"`
}
func (c *Client) Route(request RouteRequest) (RouteResponse, error) {
r, err := easyjson.Marshal(request)
if err != nil {
return RouteResponse{}, err
}
response, err := c.request("GET", "route", bytes.NewReader(r))
if err != nil {
return RouteResponse{}, err
}
result := RouteResponse{}
err = easyjson.Unmarshal(response, &result)
if err != nil {
return RouteResponse{}, err
}
return result, nil
}