-
Notifications
You must be signed in to change notification settings - Fork 6
/
road_position.go
100 lines (92 loc) · 2.96 KB
/
road_position.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
package horizon
import (
"fmt"
"github.com/golang/geo/s2"
)
// RoadPositions Set of states
type RoadPositions []*RoadPosition
// RoadPosition Representation of state (in terms of Hidden Markov Model)
/*
ID - unique identifier of state
GraphEdge - pointer to closest edge in graph
GraphVertex - indentifier of closest vertex
Projected - point (Observation) project onto edge, pointer to GeoPoint
beforeProjection - distance from starting point to projected one
afterProjection - distance from projected point to last one
next - index of the next vertex in s2.Polyline after the projected point
*/
type RoadPosition struct {
Projected *GeoPoint
GraphEdge *Edge
beforeProjection float64
afterProjection float64
GraphVertex int64
RoadPositionID int
next int
}
// NewRoadPositionFromLonLat Returns pointer to created State
/*
stateID - unique identifier for state
graphVertex - indentifier of vertex which is closest to Observation
e - pointer to Edge
lon - longitude (X for SRID = 0)
lat - latitude (Y for SRID = 0)
srid - SRID (see https://en.wikipedia.org/wiki/Spatial_reference_system)
*/
func NewRoadPositionFromLonLat(stateID int, graphVertex int64, e *Edge, lon, lat float64, srid ...int) *RoadPosition {
state := RoadPosition{
RoadPositionID: stateID,
GraphEdge: e,
GraphVertex: graphVertex,
}
if len(srid) != 0 {
switch srid[0] {
case 0:
state.Projected = NewEuclideanPoint(lon, lat)
case 4326:
state.Projected = NewWGS84Point(lon, lat)
default:
state.Projected = NewWGS84Point(lon, lat)
}
}
return &state
}
// NewRoadPositionFromS2LatLng Returns pointer to created State
/*
stateID - unique identifier for state
graphVertex - indentifier of vertex which is closest to Observation
e - pointer to Edge
lon - longitude (X for SRID = 0)
lat - latitude (Y for SRID = 0)
srid - SRID (see https://en.wikipedia.org/wiki/Spatial_reference_system)
*/
func NewRoadPositionFromS2LatLng(stateID int, graphVertex int64, e *Edge, latLng *s2.LatLng, srid ...int) *RoadPosition {
state := RoadPosition{
RoadPositionID: stateID,
GraphEdge: e,
GraphVertex: graphVertex,
}
if len(srid) != 0 {
switch srid[0] {
case 0:
state.Projected = NewEuclideanPoint(latLng.Lng.Degrees(), latLng.Lat.Degrees())
case 4326:
state.Projected = NewWGS84Point(latLng.Lng.Degrees(), latLng.Lat.Degrees())
default:
state.Projected = NewWGS84Point(latLng.Lng.Degrees(), latLng.Lat.Degrees())
}
}
return &state
}
// ID Method to fit interface State (see https://github.com/LdDl/viterbi/blob/master/viterbi.go#L9)
func (state RoadPosition) ID() int {
return state.RoadPositionID
}
// String Pretty format for State
func (state RoadPosition) String() string {
latlng := s2.LatLngFromPoint(state.Projected.Point)
return fmt.Sprintf(
"State is:\n\tSourceVertexID => %v\n\tTargetVertexID => %v\n\tSRID: %d\n\tCoords => %v",
state.GraphEdge.Source, state.GraphEdge.Target, state.Projected.srid, latlng.String(),
)
}