forked from weilunwu/go-geofence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geofence_test.go
171 lines (157 loc) · 6.85 KB
/
geofence_test.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
package geofence
import (
"math/rand"
"testing"
"github.com/kellydunn/golang-geo"
"github.com/stretchr/testify/assert"
)
func TestCorrectness(t *testing.T) {
polygon := randomPolygon(2000, 0.1)
geoPoly := geo.NewPolygon(polygon)
holes := []*geo.Point{}
geofence := NewGeofence([][]*geo.Point{polygon, holes}, int64(20))
for i := 0; i < 100000; i++ {
point := randomPoint(200)
assert.Equal(t, geofence.Inside(point), geoPoly.Contains(point))
}
}
func TestHoles(t *testing.T) {
polygon := []*geo.Point{
geo.NewPoint(0, 0),
geo.NewPoint(4, 0),
geo.NewPoint(4, 4),
geo.NewPoint(0, 4),
}
hole := []*geo.Point{
geo.NewPoint(1, 0),
geo.NewPoint(3, 0),
geo.NewPoint(3, 3),
geo.NewPoint(1, 3),
}
geofence := NewGeofence([][]*geo.Point{polygon, hole})
assert.True(t, geofence.Inside(geo.NewPoint(0.5, 0.5)))
assert.False(t, geofence.Inside(geo.NewPoint(2, 2)))
assert.False(t, geofence.Inside(geo.NewPoint(5, 5)))
}
/*===================================================
Benchmark Result 1st version:
BenchmarkGeofence 10000000 109 ns/op
BenchmarkGeoContains 3000000 475 ns/op
====================================================*/
func BenchmarkGeofence(b *testing.B) {
// Chicago geofence
polygon := []*geo.Point{
geo.NewPoint(42.01313565896657, -87.89133314508945),
geo.NewPoint(42.01086525470408, -87.94498134870082),
geo.NewPoint(41.955566495567936, -87.94566393946297),
geo.NewPoint(41.937218295745865, -87.88581848144531),
geo.NewPoint(41.96295962052549, -87.86811594385654),
geo.NewPoint(41.93385557339662, -87.86333084106445),
geo.NewPoint(41.934494079111666, -87.81011581420898),
geo.NewPoint(41.90554916282452, -87.80925750732422),
geo.NewPoint(41.9058827519221, -87.77938842773438),
geo.NewPoint(41.86402837073972, -87.77792931126896),
geo.NewPoint(41.864284053216565, -87.75638580846135),
geo.NewPoint(41.82348977579423, -87.75552751729265),
geo.NewPoint(41.823042045417644, -87.80410768697038),
geo.NewPoint(41.771468158020106, -87.80324938008562),
geo.NewPoint(41.772364335324305, -87.74625778198242),
geo.NewPoint(41.730894639311565, -87.74513235432096),
geo.NewPoint(41.73166805909664, -87.6870346069336),
geo.NewPoint(41.71748939617332, -87.68600471266836),
geo.NewPoint(41.716966221614854, -87.7243280201219),
geo.NewPoint(41.69405798811367, -87.72351264953613),
geo.NewPoint(41.693865716655395, -87.74385454365984),
geo.NewPoint(41.67463566843159, -87.74299623677507),
geo.NewPoint(41.67550471265456, -87.6654052734375),
geo.NewPoint(41.651683859743336, -87.66489028930664),
geo.NewPoint(41.65181212480582, -87.64789581298828),
geo.NewPoint(41.652036588050684, -87.62532234191895),
geo.NewPoint(41.643100214173714, -87.62506484985352),
geo.NewPoint(41.643492184875946, -87.51889228820801),
geo.NewPoint(41.642929165686375, -87.38588330335915),
geo.NewPoint(41.836600482955916, -87.43858338799328),
geo.NewPoint(42.05042567111704, -87.40253437310457),
geo.NewPoint(42.070116505457364, -87.47205723077059),
geo.NewPoint(42.0681413002819, -87.66792302951217),
geo.NewPoint(42.02862488227374, -87.66551960259676),
geo.NewPoint(42.0280511074349, -87.71289814263582),
geo.NewPoint(41.998468275360544, -87.71301263943315),
geo.NewPoint(41.9988509912138, -87.75069231167436),
geo.NewPoint(42.02100207763309, -87.77704238542356),
geo.NewPoint(42.02010937741473, -87.831029893714),
geo.NewPoint(41.98719839843826, -87.83120155116194),
geo.NewPoint(41.9948536336077, -87.86373138340423),
}
geofence := NewGeofence([][]*geo.Point{polygon, []*geo.Point{}})
for i := 0; i < b.N; i++ {
point := randomPointCustom(geofence.minX, geofence.maxX, geofence.minY, geofence.maxY, 100)
geofence.Inside(point)
}
}
func BenchmarkGeoContains(b *testing.B) {
// Chicago geofence
polygon := []*geo.Point{
geo.NewPoint(42.01313565896657, -87.89133314508945),
geo.NewPoint(42.01086525470408, -87.94498134870082),
geo.NewPoint(41.955566495567936, -87.94566393946297),
geo.NewPoint(41.937218295745865, -87.88581848144531),
geo.NewPoint(41.96295962052549, -87.86811594385654),
geo.NewPoint(41.93385557339662, -87.86333084106445),
geo.NewPoint(41.934494079111666, -87.81011581420898),
geo.NewPoint(41.90554916282452, -87.80925750732422),
geo.NewPoint(41.9058827519221, -87.77938842773438),
geo.NewPoint(41.86402837073972, -87.77792931126896),
geo.NewPoint(41.864284053216565, -87.75638580846135),
geo.NewPoint(41.82348977579423, -87.75552751729265),
geo.NewPoint(41.823042045417644, -87.80410768697038),
geo.NewPoint(41.771468158020106, -87.80324938008562),
geo.NewPoint(41.772364335324305, -87.74625778198242),
geo.NewPoint(41.730894639311565, -87.74513235432096),
geo.NewPoint(41.73166805909664, -87.6870346069336),
geo.NewPoint(41.71748939617332, -87.68600471266836),
geo.NewPoint(41.716966221614854, -87.7243280201219),
geo.NewPoint(41.69405798811367, -87.72351264953613),
geo.NewPoint(41.693865716655395, -87.74385454365984),
geo.NewPoint(41.67463566843159, -87.74299623677507),
geo.NewPoint(41.67550471265456, -87.6654052734375),
geo.NewPoint(41.651683859743336, -87.66489028930664),
geo.NewPoint(41.65181212480582, -87.64789581298828),
geo.NewPoint(41.652036588050684, -87.62532234191895),
geo.NewPoint(41.643100214173714, -87.62506484985352),
geo.NewPoint(41.643492184875946, -87.51889228820801),
geo.NewPoint(41.642929165686375, -87.38588330335915),
geo.NewPoint(41.836600482955916, -87.43858338799328),
geo.NewPoint(42.05042567111704, -87.40253437310457),
geo.NewPoint(42.070116505457364, -87.47205723077059),
geo.NewPoint(42.0681413002819, -87.66792302951217),
geo.NewPoint(42.02862488227374, -87.66551960259676),
geo.NewPoint(42.0280511074349, -87.71289814263582),
geo.NewPoint(41.998468275360544, -87.71301263943315),
geo.NewPoint(41.9988509912138, -87.75069231167436),
geo.NewPoint(42.02100207763309, -87.77704238542356),
geo.NewPoint(42.02010937741473, -87.831029893714),
geo.NewPoint(41.98719839843826, -87.83120155116194),
geo.NewPoint(41.9948536336077, -87.86373138340423),
}
golangGeo := geo.NewPolygon(polygon)
for i := 0; i < b.N; i++ {
point := randomPointCustom(41.642929165686375, 42.070116505457364, -87.94566393946297, -87.38588330335915, 100)
golangGeo.Contains(point)
}
}
func randomPoint(length float64) *geo.Point {
return geo.NewPoint(rand.Float64()*length-length/2, rand.Float64()*length-length/2)
}
func randomPolygon(length float64, percentageOfLength float64) []*geo.Point {
polygon := make([]*geo.Point, 1000)
for i := 0; i < 1000; i++ {
polygon[i] = randomPoint(length * percentageOfLength)
}
return polygon
}
func randomPointCustom(minLat float64, maxLat float64, minLng float64, maxLng float64, factor float64) *geo.Point {
latRange := maxLat - minLat
lngRange := maxLng - minLng
return geo.NewPoint((minLat+maxLat)/2-latRange*factor/2+latRange*factor*rand.Float64(), (minLng+maxLng)/2-lngRange*factor/2+lngRange*factor*rand.Float64())
}