-
Notifications
You must be signed in to change notification settings - Fork 16
/
similar.go
255 lines (239 loc) · 5.92 KB
/
similar.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package geom
import "math"
func similar(a, b, e float64) bool {
return math.Abs(a-b) < e
}
func pointSimilar(p1, p2 Point, e float64) bool {
return similar(p1.X, p2.X, e) && similar(p1.Y, p2.Y, e)
}
func pointsSimilar(p1s, p2s []Point, e float64) bool {
if len(p1s) != len(p2s) {
return false
}
for i, n := 0, len(p1s); i < n; i++ {
if !pointSimilar(p1s[i], p2s[i], e) {
return false
}
}
return true
}
func pointssSimilar(p1ss, p2ss [][]Point, e float64) bool {
if len(p1ss) != len(p2ss) {
return false
}
for i, n := 0, len(p1ss); i < n; i++ {
if !pointsSimilar(p1ss[i], p2ss[i], e) {
return false
}
}
return true
}
// Similar determines whether two geometries are similar within tolerance.
func (p Point) Similar(g Geom, tolerance float64) bool {
switch g.(type) {
case Point:
return pointSimilar(p, g.(Point), tolerance)
default:
return false
}
}
// Similar determines whether two geometries are similar within tolerance.
func (mp MultiPoint) Similar(g Geom, tolerance float64) bool {
switch g.(type) {
case MultiPoint:
return pointsSimilar(mp, g.(MultiPoint), tolerance)
default:
return false
}
}
// Similar determines whether two geometries are similar within tolerance.
// If two lines contain the same points but in different directions it will
// return false.
func (l LineString) Similar(g Geom, tolerance float64) bool {
switch g.(type) {
case LineString:
return pointsSimilar(l, g.(LineString), tolerance)
default:
return false
}
}
// Similar determines whether two geometries are similar within tolerance.
// If ml and g have the similar linestrings but in a different order, it
// will return true.
func (ml MultiLineString) Similar(g Geom, tolerance float64) bool {
switch g.(type) {
case MultiLineString:
ml2 := g.(MultiLineString)
indices := make([]int, len(ml2))
for i := range ml2 {
indices[i] = i
}
for _, l := range ml {
matched := false
for ii, i := range indices {
if l.Similar(ml2[i], tolerance) { // we found a match
matched = true
// remove index i from futher consideration.
if ii == len(indices)-1 {
indices = indices[0:ii]
} else {
indices = append(indices[0:ii], indices[ii+1:len(indices)]...)
}
break
}
}
if !matched {
return false
}
}
return true
default:
return false
}
}
// Similar determines whether two geometries are similar within tolerance.
// If ml and g have the similar polygons but in a different order, it
// will return true.
func (mp MultiPolygon) Similar(g Geom, tolerance float64) bool {
switch g.(type) {
case MultiPolygon:
mp2 := g.(MultiPolygon)
indices := make([]int, len(mp2))
for i := range mp2 {
indices[i] = i
}
for _, l := range mp {
matched := false
for ii, i := range indices {
if l.Similar(mp2[i], tolerance) { // we found a match
matched = true
// remove index i from futher consideration.
if ii == len(indices)-1 {
indices = indices[0:ii]
} else {
indices = append(indices[0:ii], indices[ii+1:len(indices)]...)
}
break
}
}
if !matched {
return false
}
}
return true
default:
return false
}
}
// Similar determines whether two geometries are similar within tolerance.
// If p and g have the same points with the same winding direction, but a
// different starting point, it will return true. If they have the same
// rings but in a different order, it will return true. If the rings have the same
// points but different winding directions, it will return false.
func (p Polygon) Similar(g Geom, tolerance float64) bool {
switch g.(type) {
case Polygon:
p2 := g.(Polygon)
indices := make([]int, len(p2))
for i := range p2 {
indices[i] = i
}
for _, r1 := range p {
matched := false
for ii, i := range indices {
if ringSimilar(r1, p2[i], tolerance) { // we found a match
matched = true
// remove index i from futher consideration.
if ii == len(indices)-1 {
indices = indices[0:ii]
} else {
indices = append(indices[0:ii], indices[ii+1:len(indices)]...)
}
break
}
}
if !matched {
return false
}
}
return true
default:
return false
}
}
// Similar determines whether two bounds are similar within tolerance.
func (b *Bounds) Similar(g Geom, tolerance float64) bool {
switch g.(type) {
case *Bounds:
b2 := g.(*Bounds)
return pointSimilar(b.Min, b2.Min, tolerance) && pointSimilar(b.Max, b2.Max, tolerance)
default:
return false
}
}
// Similar determines whether two geometries collections are similar within tolerance.
// If gc and g have the same geometries
// but in a different order, it will return true.
func (gc GeometryCollection) Similar(g Geom, tolerance float64) bool {
switch g.(type) {
case GeometryCollection:
gc2 := g.(GeometryCollection)
indices := make([]int, len(gc2))
for i := range gc2 {
indices[i] = i
}
for _, gc1 := range gc {
matched := false
for ii, i := range indices {
if gc1.Similar(gc2[i], tolerance) { // we found a match
matched = true
// remove index i from futher consideration.
if ii == len(indices)-1 {
indices = indices[0:ii]
} else {
indices = append(indices[0:ii], indices[ii+1:len(indices)]...)
}
break
}
}
if !matched {
return false
}
}
return true
default:
return false
}
}
func ringSimilar(a, b []Point, e float64) bool {
if len(a) != len(b) {
return false
}
ia := minPt(a)
ib := minPt(b)
for i := 0; i < len(a); i++ {
if !pointSimilar(a[ia], b[ib], e) {
return false
}
ia = nextPt(ia, len(a))
ib = nextPt(ib, len(b))
}
return true
}
// ring iterator function
func nextPt(i, l int) int {
if i == l-2 { // Skip the last point that matches the first point.
return 0
}
return i + 1
}
// find bottom-most of leftmost points, to have fixed anchor
func minPt(c []Point) int {
min := 0
for j, p := range c {
if p.X < c[min].X || p.X == c[min].X && p.Y < c[min].Y {
min = j
}
}
return min
}