forked from ryankurte/go-mapbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
162 lines (138 loc) · 4.45 KB
/
types.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* go-mapbox Map Matching Module
* Wraps the mapbox Map Matching API for server side use
* See https://www.mapbox.com/api-documentation/#retrieve-a-match for API information
*
* https://github.com/ryankurte/go-mapbox
* Copyright 2017 Ryan Kurte
*/
package mapmatching
import (
"fmt"
)
// MatchingResponse is the response from GetMatching
// https://www.mapbox.com/api-documentation/#match-response-object
type MatchingResponse struct {
Code string
Matchings []Matchings
Tracepoint []TracePoint
}
type Coordinate []float64
type GeojsonGeometry struct {
Coordinates []Coordinate
}
type PolylineGeometry string
// Matchings it a route object with additional confidence field
// https://www.mapbox.com/api-documentation/#match-object
type Matchings struct {
Confidence float64
Distance float64
Duration float64
Geometry interface{} // Issue: must support polyline (string) or geojson (object)
Legs []MatchingLeg
}
func (m *Matchings) GetGeometryGeojson() (*GeojsonGeometry, error) {
geojson, ok := m.Geometry.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("Malformed geojson geometry (expected map[string]interface, received %t)", m.Geometry)
}
t, ok := geojson["type"]
if !ok {
return nil, fmt.Errorf("Malformed geojson geometry (no type defined)")
}
if t != "LineString" {
return nil, fmt.Errorf("Malformed geojson geometry (incorrect type name: %s)", t)
}
v, ok := geojson["coordinates"]
if !ok {
return nil, fmt.Errorf("Malformed geojson geometry (no coordinates defined)")
}
values, ok := v.([]interface{})
//values, ok := v.([][]float64)
if !ok {
return nil, fmt.Errorf("Malformed geojson geometry (coordinates are not an array of float pairs)")
}
geometry := GeojsonGeometry{}
for _, v := range values {
value, ok := v.([]interface{})
if !ok {
return nil, fmt.Errorf("Could not cast value to coordinate slice (type: %t)", v)
}
lat, ok := value[0].(float64)
if !ok {
return nil, fmt.Errorf("Error casting lat (type: %t)", value[0])
}
lng, ok := value[1].(float64)
if !ok {
return nil, fmt.Errorf("Error casting lng (type: %t)", value[1])
}
geometry.Coordinates = append(geometry.Coordinates, []float64{lat, lng})
}
return &geometry, nil
}
func (m *Matchings) GetGeometryPolyline() (string, error) {
g, ok := m.Geometry.(string)
if !ok {
return "", fmt.Errorf("Non polyline geometry (type: %t)", m.Geometry)
}
return g, nil
}
//MatchingLeg legs inside the matching object
type MatchingLeg struct {
Step []float64
Summary string
Duration float64
Distance float64
}
// TracePoint represents the location an input point was matched with
type TracePoint struct {
WaypointIndex int16
Location []float64
Name string
MatchingsIndex int16
}
// OverviewType Type of returned overview geometry
type OverviewType string
const (
//OverviewFull returns a detailed overview geometry
OverviewFull OverviewType = "full"
//OverviewSimplified returns a simplified overview geometry
OverviewSimplified OverviewType = "simplified"
//OverviewFalse returns no overview geometry
OverviewFalse OverviewType = "false"
)
// GeometryType Format of the returned geometry
type GeometryType string
const (
// GeometryGeojson returns a geojson like geometry
GeometryGeojson GeometryType = "geojson"
// GeometryPolyline returns a polyline 5 encoded string like geometry
GeometryPolyline GeometryType = "polyline"
// GeometryPolyline6 returns a polyline 6 encode string like geometry
GeometryPolyline6 GeometryType = "polyline6"
)
// AnnotationType type of metadata to be returned additionally along the route
type AnnotationType string
const (
// AnnotationDuration returns a additional duration metadata
AnnotationDuration AnnotationType = "duration"
// AnnotationDistance returns a additional distance metadata
AnnotationDistance AnnotationType = "distance"
// AnnotationSpeed returns a additional speed metadata
AnnotationSpeed AnnotationType = "speed"
)
// Codes are direction response Codes
// https://www.mapbox.com/api-documentation/#matching-errors
type Codes string
const (
// CodeOK success response
CodeOK Codes = "Ok"
//NoMatchFound No matching route found
NoMatchFound Codes = "NoMatch"
//TooManyCoordinates limited to 100 coordinates per request
TooManyCoordinates Codes = "TooManyCoordinates"
//CodeProfileNotFound invalid routing profile
CodeProfileNotFound Codes = "ProfileNotFound"
// CodeInvalidInput invalid input data to the server
CodeInvalidInput Codes = "InvalidInput"
)