-
-
Notifications
You must be signed in to change notification settings - Fork 196
/
geometry_math.go
335 lines (316 loc) · 9.23 KB
/
geometry_math.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package basic
import (
"fmt"
"strings"
"errors"
"github.com/go-spatial/tegola"
"github.com/go-spatial/tegola/maths/webmercator"
)
// ApplyToPoints applys the given function to each point in the geometry and any sub geometries, return a new transformed geometry.
func ApplyToPoints(geometry tegola.Geometry, f func(coords ...float64) ([]float64, error)) (G, error) {
switch geo := geometry.(type) {
default:
return G{}, fmt.Errorf("Unknown Geometry: %+v", geometry)
case tegola.Point:
c, err := f(geo.X(), geo.Y())
if err != nil {
return G{}, err
}
if len(c) < 2 {
return G{}, fmt.Errorf("Function did not return minimum number of coordinates got %v expected 2", len(c))
}
return G{Point{c[0], c[1]}}, nil
case tegola.Point3:
c, err := f(geo.X(), geo.Y(), geo.Z())
if err != nil {
return G{}, err
}
if len(c) < 3 {
return G{}, fmt.Errorf("Function did not return minimum number of coordinates got %v expected 3", len(c))
}
return G{Point3{c[0], c[1], c[2]}}, nil
case tegola.MultiPoint:
var pts MultiPoint
for _, pt := range geo.Points() {
c, err := f(pt.X(), pt.Y())
if err != nil {
return G{}, err
}
if len(c) < 2 {
return G{}, fmt.Errorf("Function did not return minimum number of coordinates got %v expected 2", len(c))
}
pts = append(pts, Point{c[0], c[1]})
}
return G{pts}, nil
case tegola.LineString:
var line Line
for _, ptGeo := range geo.Subpoints() {
c, err := f(ptGeo.X(), ptGeo.Y())
if err != nil {
return G{}, err
}
if len(c) < 2 {
return G{}, fmt.Errorf("Function did not return minimum number of coordinates got %v expected 2", len(c))
}
line = append(line, Point{c[0], c[1]})
}
return G{line}, nil
case tegola.MultiLine:
var line MultiLine
for i, lineGeo := range geo.Lines() {
geoLine, err := ApplyToPoints(lineGeo, f)
if err != nil {
return G{}, fmt.Errorf("Got error converting line(%v) of multiline: %v", i, err)
}
if !geoLine.IsLine() {
panic("We did not get the conversion we were expecting")
}
line = append(line, geoLine.AsLine())
}
return G{line}, nil
case tegola.Polygon:
var poly Polygon
for i, line := range geo.Sublines() {
geoLine, err := ApplyToPoints(line, f)
if err != nil {
return G{}, fmt.Errorf("Got error converting line(%v) of polygon: %v", i, err)
}
poly = append(poly, geoLine.AsLine())
}
return G{poly}, nil
case tegola.MultiPolygon:
var mpoly MultiPolygon
for i, poly := range geo.Polygons() {
geoPoly, err := ApplyToPoints(poly, f)
if err != nil {
return G{}, fmt.Errorf("Got error converting poly(%v) of multipolygon: %v", i, err)
}
mpoly = append(mpoly, geoPoly.AsPolygon())
}
return G{mpoly}, nil
}
}
// CloneGeomtry returns a deep clone of the Geometry.
func CloneGeometry(geometry tegola.Geometry) (G, error) {
switch geo := geometry.(type) {
default:
return G{}, fmt.Errorf("Unknown Geometry: %+v", geometry)
case tegola.Point:
return G{Point{geo.X(), geo.Y()}}, nil
case tegola.Point3:
return G{Point3{geo.X(), geo.Y(), geo.Z()}}, nil
case tegola.MultiPoint:
var pts MultiPoint
for _, pt := range geo.Points() {
pts = append(pts, Point{pt.X(), pt.Y()})
}
return G{pts}, nil
case tegola.LineString:
var line Line
for _, ptGeo := range geo.Subpoints() {
line = append(line, Point{ptGeo.X(), ptGeo.Y()})
}
return G{line}, nil
case tegola.MultiLine:
var line MultiLine
for i, lineGeo := range geo.Lines() {
geom, err := CloneGeometry(lineGeo)
if err != nil {
return G{}, fmt.Errorf("Got error converting line(%v) of multiline: %v", i, err)
}
line = append(line, geom.AsLine())
}
return G{line}, nil
case tegola.Polygon:
var poly Polygon
for i, line := range geo.Sublines() {
geom, err := CloneGeometry(line)
if err != nil {
return G{}, fmt.Errorf("Got error converting line(%v) of polygon: %v", i, err)
}
poly = append(poly, geom.AsLine())
}
return G{poly}, nil
case tegola.MultiPolygon:
var mpoly MultiPolygon
for i, poly := range geo.Polygons() {
geom, err := CloneGeometry(poly)
if err != nil {
return G{}, fmt.Errorf("Got error converting poly(%v) of multipolygon: %v", i, err)
}
mpoly = append(mpoly, geom.AsPolygon())
}
return G{mpoly}, nil
}
}
// ToWebMercator takes a SRID and a geometry encode using that srid, and returns a geometry encoded as a WebMercator.
func ToWebMercator(SRID uint64, geometry tegola.Geometry) (G, error) {
switch SRID {
default:
return G{}, fmt.Errorf("Don't know how to convert from %v to %v.", tegola.WebMercator, SRID)
case tegola.WebMercator:
// Instead of just returning the geometry, we are cloning it so that the user of the API can rely
// on the result to alway be a copy. Instead of being a reference in the on instance that it's already
// in the same SRID.
return CloneGeometry(geometry)
case tegola.WGS84:
return ApplyToPoints(geometry, webmercator.PToXY)
}
}
// FromWebMercator takes a geometry encoded with WebMercator, and returns a Geometry encodes to the given srid.
func FromWebMercator(SRID uint64, geometry tegola.Geometry) (G, error) {
switch SRID {
default:
return G{}, fmt.Errorf("Don't know how to convert from %v to %v.", SRID, tegola.WebMercator)
case tegola.WebMercator:
// Instead of just returning the geometry, we are cloning it so that the user of the API can rely
// on the result to alway be a copy. Instead of being a reference in the on instance that it's already
// in the same SRID.
return CloneGeometry(geometry)
case tegola.WGS84:
return ApplyToPoints(geometry, webmercator.PToLonLat)
}
}
func interfaceAsFloatslice(v interface{}) (vals []float64, err error) {
vs, ok := v.([]interface{})
if !ok {
return nil, fmt.Errorf("Incorrect value type looking for float64 slice, not %t.", v)
}
for _, iv := range vs {
vv, ok := iv.(float64)
if !ok {
return nil, fmt.Errorf("Incorrect value type looking for float64 slice, not %t.", v)
}
vals = append(vals, vv)
}
return vals, nil
}
func mapIsOfType(v map[string]interface{}, wants ...string) (string, error) {
typ, ok := v["type"].(string)
if !ok {
return "", fmt.Errorf("Was not able to convert type to string.")
}
for _, want := range wants {
if typ == want {
return typ, nil
}
}
return "", fmt.Errorf("Expected all subtypes to be one of type (%v), not %v", strings.Join(wants, ","), v["type"])
}
func interfaceAsLine(v interface{}) (Line, error) {
vals, err := interfaceAsFloatslice(v)
if err != nil {
return nil, fmt.Errorf("Incorrect values for line type: %v", err)
}
return NewLine(vals...), nil
}
func interfaceAsPoint(v interface{}) (Point, error) {
vals, err := interfaceAsFloatslice(v)
if err != nil {
return Point{}, fmt.Errorf("Incorrect values for point type: %v", err)
}
return Point{vals[0], vals[1]}, nil
}
func interfaceAsPoint3(v interface{}) (Point3, error) {
vals, err := interfaceAsFloatslice(v)
if err != nil {
return Point3{}, fmt.Errorf("Incorrect values for point3 type: %v", err)
}
return Point3{vals[0], vals[1], vals[2]}, nil
}
func forEachMapInSlice(v interface{}, do func(typ string, v interface{}) error, wants ...string) error {
vals, ok := v.([]interface{})
if !ok {
return fmt.Errorf("Expected values to be []interface{}: ")
}
for i, iv := range vals {
v, ok := iv.(map[string]interface{})
if !ok {
return fmt.Errorf("Expected v[%v] to be map[string]interface{}: ", i)
}
typ, err := mapIsOfType(v, wants...)
if err != nil {
return err
}
if err = do(typ, v["value"]); err != nil {
return err
}
}
return nil
}
func interfaceAsPolygon(v interface{}) (Polygon, error) {
var p Polygon
err := forEachMapInSlice(v, func(_ string, v interface{}) error {
l, err := interfaceAsLine(v)
if err != nil {
return err
}
p = append(p, l)
return nil
}, "linestring")
if err != nil {
return nil, err
}
return p, nil
}
func MapAsGeometry(m map[string]interface{}) (geo Geometry, err error) {
typ, err := mapIsOfType(m, "point", "point3", "linestring", "polygon", "multipolygon", "multipoint", "multiline")
if err != nil {
return nil, err
}
switch typ {
case "point":
return interfaceAsPoint(m["value"])
case "point3":
return interfaceAsPoint3(m["value"])
case "linestring":
return interfaceAsLine(m["value"])
case "polygon":
fmt.Println("Working on Polygon:")
return interfaceAsPolygon(m["value"])
case "multipolygon":
fmt.Println("Working on MPolygon:")
var mp MultiPolygon
err := forEachMapInSlice(m["value"], func(_ string, v interface{}) error {
p, err := interfaceAsPolygon(v)
if err != nil {
return err
}
mp = append(mp, p)
return nil
}, "polygon")
if err != nil {
return nil, err
}
return mp, nil
case "multipoint":
var mp MultiPoint
err := forEachMapInSlice(m["value"], func(_ string, v interface{}) error {
p, err := interfaceAsPoint(v)
if err != nil {
return err
}
mp = append(mp, p)
return nil
}, "point")
if err != nil {
return nil, err
}
return mp, nil
case "multiline":
var ml MultiLine
err := forEachMapInSlice(m["value"], func(_ string, v interface{}) error {
l, err := interfaceAsLine(v)
if err != nil {
return err
}
ml = append(ml, l)
return nil
}, "linestring")
if err != nil {
return nil, err
}
return ml, nil
}
return nil, errors.New("Unknown type")
}