forked from akhenakh/regionagogo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geofence.go
200 lines (164 loc) · 4.85 KB
/
geofence.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package regionagogo
import (
"encoding/json"
"errors"
"io"
"log"
"github.com/akhenakh/regionagogo/geostore"
"github.com/golang/geo/s2"
"github.com/kpawlik/geojson"
)
const (
mininumViableLevel = 3 // the minimum cell level we accept
)
var (
defaultCoverer = &s2.RegionCoverer{MinLevel: 1, MaxLevel: 30, MaxCells: 8}
)
// GeoFenceDB is the main interface to store and query your geo database
type GeoFenceDB interface {
// returns a Fence by it's storage id
FenceByID(loopID uint64) *Fence
// returns the fence for the corresponding lat, lng coordinates
StubbingQuery(lat, lng float64, opts ...QueryOptionsFunc) (Fences, error)
// RectQuery perform rectangular query ur upper right bl bottom left
RectQuery(urlat, urlng, bllat, bllng float64, opts ...QueryOptionsFunc) (Fences, error)
// RadiusQuery is performing a radius query
RadiusQuery(lat, lng, radius float64, opts ...QueryOptionsFunc) (Fences, error)
// Store a Fence into the DB
StoreFence(rs *geostore.FenceStorage, cover []uint64) error
// Close the DB
Close() error
}
type QueryOptionsFunc func(*QueryOptions)
// queryOptions used to pass options to DB queries
type QueryOptions struct {
// Returns all fences when multiple fences match
MultipleFences bool
}
// WithMultipleFences enable multi fences in responses
func WithMultipleFences(mf bool) QueryOptionsFunc {
return func(o *QueryOptions) {
o.MultipleFences = mf
}
}
// ImportGeoJSONFile will load a geo json and save the polygons into
// the GeoFence for later lookup
// fields are the properties fields names you want to be associated with each fences
func ImportGeoJSONFile(gs GeoFenceDB, r io.Reader, fields []string) error {
var geo geojson.FeatureCollection
d := json.NewDecoder(r)
if err := d.Decode(&geo); err != nil {
return err
}
var count int
for _, f := range geo.Features {
geom, err := f.GetGeometry()
if err != nil {
return err
}
switch geom.GetType() {
case "Polygon":
mp := geom.(*geojson.Polygon)
for _, p := range mp.Coordinates {
rc, cu := preparePolygon(f, p, fields)
if rc != nil {
if err := gs.StoreFence(rc, cu); err != nil {
return err
}
count++
}
}
case "MultiPolygon":
mp := geom.(*geojson.MultiPolygon)
// multipolygon
for _, m := range mp.Coordinates {
// coordinates polygon
p := m[0]
rc, cu := preparePolygon(f, p, fields)
if rc != nil {
if err := gs.StoreFence(rc, cu); err != nil {
return err
}
count++
}
}
default:
return errors.New("unknown type")
}
}
log.Println(count, "new fences imported")
return nil
}
// preparePolygon transform a geojson polygons into FenceStorage
func preparePolygon(f *geojson.Feature, p geojson.Coordinates, fields []string) (*geostore.FenceStorage, []uint64) {
if isClockwisePolygon(p) {
reversePolygon(p)
}
// polygon
// do not add last point in storage (first point is last point)
points := make([]s2.Point, len(p)-1)
// For type "MultiPolygon", the "coordinates" member must be an array of Polygon coordinate arrays.
// "Polygon", the "coordinates" member must be an array of LinearRing coordinate arrays.
// For Polygons with multiple rings, the first must be the exterior ring and any others must be interior rings or holes.
for i := 0; i < len(p)-1; i++ {
ll := s2.LatLngFromDegrees(float64(p[i][1]), float64(p[i][0]))
points[i] = s2.PointFromLatLng(ll)
}
// TODO: parallelized region cover
l := LoopFenceFromPoints(points)
if l.IsEmpty() || l.IsFull() {
log.Println("invalid loop", f.Properties)
return nil, nil
}
covering := defaultCoverer.Covering(l)
data := make(map[string]string)
for _, field := range fields {
if v, ok := f.Properties[field].(string); !ok {
log.Println("can't find field on", f.Properties)
} else {
data[field] = v
}
}
cu := make([]uint64, len(covering))
var invalidLoop bool
for i, v := range covering {
// added a security there if the level is too high it probably means the polygon is bogus
// this to avoid a large cell to cover everything
if v.Level() < mininumViableLevel {
log.Print("cell level too big", v.Level(), data)
invalidLoop = true
}
cu[i] = uint64(v)
}
// do not insert big loop
if invalidLoop {
return nil, nil
}
var cpoints []*geostore.CPoint
for _, p := range points {
ll := s2.LatLngFromPoint(p)
cpoints = append(cpoints, &geostore.CPoint{Lat: float32(ll.Lat.Degrees()), Lng: float32(ll.Lng.Degrees())})
}
rs := &geostore.FenceStorage{
Points: cpoints,
Data: data,
}
return rs, cu
}
func isClockwisePolygon(p geojson.Coordinates) bool {
sum := 0.0
for i, coord := range p[:len(p)-1] {
next := p[i+1]
sum += float64((next[0] - coord[0]) * (next[1] + coord[1]))
}
if sum == 0 {
return true
}
return sum > 0
}
func reversePolygon(p geojson.Coordinates) {
for i := len(p)/2 - 1; i >= 0; i-- {
opp := len(p) - 1 - i
p[i], p[opp] = p[opp], p[i]
}
}