forked from blevesearch/bleve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeo_test.go
183 lines (169 loc) · 4.52 KB
/
geo_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
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright (c) 2017 Couchbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package geo
import (
"math"
"testing"
)
func TestMortonHashMortonUnhash(t *testing.T) {
tests := []struct {
lon float64
lat float64
}{
{-180.0, -90.0},
{-5, 27.3},
{0, 0},
{1.0, 1.0},
{24.7, -80.4},
{180.0, 90.0},
}
for _, test := range tests {
hash := MortonHash(test.lon, test.lat)
lon := MortonUnhashLon(hash)
lat := MortonUnhashLat(hash)
if compareGeo(test.lon, lon) != 0 {
t.Errorf("expected lon %f, got %f, hash %x", test.lon, lon, hash)
}
if compareGeo(test.lat, lat) != 0 {
t.Errorf("expected lat %f, got %f, hash %x", test.lat, lat, hash)
}
}
}
func TestScaleLonUnscaleLon(t *testing.T) {
tests := []struct {
lon float64
}{
{-180.0},
{0.0},
{1.0},
{180.0},
}
for _, test := range tests {
s := scaleLon(test.lon)
lon := unscaleLon(s)
if compareGeo(test.lon, lon) != 0 {
t.Errorf("expected %f, got %f, scaled was %d", test.lon, lon, s)
}
}
}
func TestScaleLatUnscaleLat(t *testing.T) {
tests := []struct {
lat float64
}{
{-90.0},
{0.0},
{1.0},
{90.0},
}
for _, test := range tests {
s := scaleLat(test.lat)
lat := unscaleLat(s)
if compareGeo(test.lat, lat) != 0 {
t.Errorf("expected %.16f, got %.16f, scaled was %d", test.lat, lat, s)
}
}
}
func TestRectFromPointDistance(t *testing.T) {
// at the equator 1 degree of latitude is about 110567 meters
_, upperLeftLat, _, lowerRightLat, err := RectFromPointDistance(0, 0, 110567)
if err != nil {
t.Fatal(err)
}
if math.Abs(upperLeftLat-1) > 1E-2 {
t.Errorf("expected bounding box upper left lat to be almost 1, got %f", upperLeftLat)
}
if math.Abs(lowerRightLat+1) > 1E-2 {
t.Errorf("expected bounding box lower right lat to be almost -1, got %f", lowerRightLat)
}
}
func TestRectIntersects(t *testing.T) {
tests := []struct {
aMinX float64
aMinY float64
aMaxX float64
aMaxY float64
bMinX float64
bMinY float64
bMaxX float64
bMaxY float64
want bool
}{
// clearly overlap
{0, 0, 2, 2, 1, 1, 3, 3, true},
// clearly do not overalp
{0, 0, 1, 1, 2, 2, 3, 3, false},
// share common point
{0, 0, 1, 1, 1, 1, 2, 2, true},
}
for _, test := range tests {
got := RectIntersects(test.aMinX, test.aMinY, test.aMaxX, test.aMaxY, test.bMinX, test.bMinY, test.bMaxX, test.bMaxY)
if test.want != got {
t.Errorf("expected intersects %t, got %t for %f %f %f %f %f %f %f %f", test.want, got, test.aMinX, test.aMinY, test.aMaxX, test.aMaxY, test.bMinX, test.bMinY, test.bMaxX, test.bMaxY)
}
}
}
func TestRectWithin(t *testing.T) {
tests := []struct {
aMinX float64
aMinY float64
aMaxX float64
aMaxY float64
bMinX float64
bMinY float64
bMaxX float64
bMaxY float64
want bool
}{
// clearly within
{1, 1, 2, 2, 0, 0, 3, 3, true},
// clearly not within
{0, 0, 1, 1, 2, 2, 3, 3, false},
// overlapping
{0, 0, 2, 2, 1, 1, 3, 3, false},
// share common point
{0, 0, 1, 1, 1, 1, 2, 2, false},
// within, but boxes reversed (b is within a, but not a within b)
{0, 0, 3, 3, 1, 1, 2, 2, false},
}
for _, test := range tests {
got := RectWithin(test.aMinX, test.aMinY, test.aMaxX, test.aMaxY, test.bMinX, test.bMinY, test.bMaxX, test.bMaxY)
if test.want != got {
t.Errorf("expected within %t, got %t for %f %f %f %f %f %f %f %f", test.want, got, test.aMinX, test.aMinY, test.aMaxX, test.aMaxY, test.bMinX, test.bMinY, test.bMaxX, test.bMaxY)
}
}
}
func TestBoundingBoxContains(t *testing.T) {
tests := []struct {
lon float64
lat float64
minX float64
minY float64
maxX float64
maxY float64
want bool
}{
// clearly contains
{1, 1, 0, 0, 2, 2, true},
// clearly does not contain
{0, 0, 1, 1, 2, 2, false},
// on corner
{0, 0, 0, 0, 2, 2, true},
}
for _, test := range tests {
got := BoundingBoxContains(test.lon, test.lat, test.minX, test.minY, test.maxX, test.maxY)
if test.want != got {
t.Errorf("expected box contains %t, got %t for %f,%f in %f %f %f %f ", test.want, got, test.lon, test.lat, test.minX, test.minY, test.maxX, test.maxY)
}
}
}