/
geometry.go
executable file
·628 lines (570 loc) · 17 KB
/
geometry.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/*
Copyright 2016, RadiantBlue Technologies, 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 geojson
import (
"encoding/json"
"fmt"
"strconv"
"strings"
)
// GeoJSON Constants
const (
COORDINATES = "coordinates"
POINT = "Point"
LINESTRING = "LineString"
POLYGON = "Polygon"
MULTIPOINT = "MultiPoint"
MULTILINESTRING = "MultiLineString"
MULTIPOLYGON = "MultiPolygon"
GEOMETRYCOLLECTION = "GeometryCollection"
GEOMETRIES = "geometries"
)
// The Point object contains a single position
type Point struct {
Type string `json:"type"`
Coordinates []float64 `json:"coordinates"`
Bbox BoundingBox `json:"bbox,omitempty"`
}
// PointFromBytes constructs a point from a GeoJSON byte array
// and returns its pointer
func PointFromBytes(bytes []byte) (*Point, error) {
var result Point
err := json.Unmarshal(bytes, &result)
return &result, err
}
// ForceBbox returns a bounding box, creating one by brute force if needed
func (point Point) ForceBbox() BoundingBox {
if len(point.Bbox) > 0 {
return point.Bbox
}
if bbox, err := NewBoundingBox(point.Coordinates); err == nil {
return bbox
}
return []float64{}
}
// String returns the string representation
func (point Point) String() string {
var result string
if bytes, err := json.Marshal(point); err == nil {
result = string(bytes)
} else {
result = err.Error()
}
return result
}
// Map returns a map of the Geometry's members
func (point Point) Map() map[string]interface{} {
result := make(map[string]interface{})
result[COORDINATES] = point.Coordinates
result[TYPE] = POINT
return result
}
// NewPoint is the normal factory method for a Point
func NewPoint(coordinates []float64) *Point {
if len(coordinates) > 1 {
return &Point{Type: POINT, Coordinates: coordinates}
}
return nil
}
// WKT returns the Well Known Text representation of the point
func (point Point) WKT() string {
var result string
switch len(point.Coordinates) {
case 2:
result = fmt.Sprintf("POINT (%v)", array1ToWKTCoordinates(point.Coordinates))
case 3:
result = fmt.Sprintf("POINT Z (%v)", array1ToWKTCoordinates(point.Coordinates))
case 4:
result = fmt.Sprintf("POINT ZM (%v)", array1ToWKTCoordinates(point.Coordinates))
default:
result = "POINT EMPTY"
}
return result
}
func array1ToWKTCoordinates(input []float64) string {
var result string
switch len(input) {
case 2:
result = fmt.Sprintf("%f %f", input[0], input[1])
case 3:
result = fmt.Sprintf("%f %f %f", input[0], input[1], input[2])
case 4:
result = fmt.Sprintf("%f %f %f %f", input[0], input[1], input[2], input[3])
}
return result
}
// The LineString object contains a array of two or more positions
type LineString struct {
Type string `json:"type"`
Coordinates [][]float64 `json:"coordinates"`
Bbox BoundingBox `json:"bbox,omitempty"`
}
// LineStringFromBytes constructs a LineString from a GeoJSON byte array
// and returns its pointer
func LineStringFromBytes(bytes []byte) (*LineString, error) {
var result LineString
err := json.Unmarshal(bytes, &result)
return &result, err
}
// ForceBbox returns a bounding box, creating one by brute force if needed
func (ls LineString) ForceBbox() BoundingBox {
if len(ls.Bbox) > 0 {
return ls.Bbox
}
if bbox, err := NewBoundingBox(ls.Coordinates); err == nil {
return bbox
}
return []float64{}
}
// String returns the string representation
func (ls LineString) String() string {
var result string
if bytes, err := json.Marshal(ls); err == nil {
result = string(bytes)
} else {
result = err.Error()
}
return result
}
// Map returns a map of the Geometry's members
func (ls LineString) Map() map[string]interface{} {
result := make(map[string]interface{})
result[COORDINATES] = ls.Coordinates
result[TYPE] = LINESTRING
return result
}
// NewLineString is the normal factory method for a LineString
func NewLineString(coordinates [][]float64) *LineString {
return &LineString{Type: LINESTRING, Coordinates: coordinates}
}
// The Polygon object contains a array of one or more linear rings
type Polygon struct {
Type string `json:"type"`
Coordinates [][][]float64 `json:"coordinates"`
Bbox BoundingBox `json:"bbox,omitempty"`
}
// PolygonFromBytes constructs a Polygon from a GeoJSON byte array
// and returns its pointer
func PolygonFromBytes(bytes []byte) (*Polygon, error) {
var result Polygon
err := json.Unmarshal(bytes, &result)
return &result, err
}
// ForceBbox returns a bounding box, creating one by brute force if needed
func (polygon Polygon) ForceBbox() BoundingBox {
if len(polygon.Bbox) > 0 {
return polygon.Bbox
}
if bbox, err := NewBoundingBox(polygon.Coordinates); err == nil {
return bbox
}
return []float64{}
}
// String returns the string representation
func (polygon Polygon) String() string {
var result string
if bytes, err := json.Marshal(polygon); err == nil {
result = string(bytes)
} else {
result = err.Error()
}
return result
}
// Map returns a map of the Geometry's members
func (polygon Polygon) Map() map[string]interface{} {
result := make(map[string]interface{})
result[COORDINATES] = polygon.Coordinates
result[TYPE] = POLYGON
return result
}
// NewPolygon is the normal factory method for a Polygon
func NewPolygon(coordinates [][][]float64) *Polygon {
return &Polygon{Type: POLYGON, Coordinates: coordinates}
}
// The MultiPoint object contains a array of one or more points
type MultiPoint struct {
Type string `json:"type"`
Coordinates [][]float64 `json:"coordinates"`
Bbox BoundingBox `json:"bbox,omitempty"`
}
// MultiPointFromBytes constructs a MultiPoint from a GeoJSON byte array
// and returns its pointer
func MultiPointFromBytes(bytes []byte) (*MultiPoint, error) {
var result MultiPoint
err := json.Unmarshal(bytes, &result)
return &result, err
}
// ForceBbox returns a bounding box, creating one by brute force if needed
func (mp MultiPoint) ForceBbox() BoundingBox {
if len(mp.Bbox) > 0 {
return mp.Bbox
}
if bbox, err := NewBoundingBox(mp.Coordinates); err == nil {
return bbox
}
return []float64{}
}
// String returns the string representation
func (mp MultiPoint) String() string {
var result string
if bytes, err := json.Marshal(mp); err == nil {
result = string(bytes)
} else {
result = err.Error()
}
return result
}
// Map returns a map of the Geometry's members
func (mp MultiPoint) Map() map[string]interface{} {
result := make(map[string]interface{})
result[COORDINATES] = mp.Coordinates
result[TYPE] = MULTIPOINT
return result
}
// NewMultiPoint is the normal factory method for a MultiPoint
func NewMultiPoint(coordinates [][]float64) *MultiPoint {
return &MultiPoint{Type: MULTIPOINT, Coordinates: coordinates}
}
// The MultiLineString object contains a array of one or more line strings
type MultiLineString struct {
Type string `json:"type"`
Coordinates [][][]float64 `json:"coordinates"`
Bbox BoundingBox `json:"bbox,omitempty"`
}
// MultiLineStringFromBytes constructs a MultiLineString from a GeoJSON byte array
// and returns its pointer
func MultiLineStringFromBytes(bytes []byte) (*MultiLineString, error) {
var result MultiLineString
err := json.Unmarshal(bytes, &result)
return &result, err
}
// ForceBbox returns a bounding box, creating one by brute force if needed
func (mls MultiLineString) ForceBbox() BoundingBox {
if len(mls.Bbox) > 0 {
return mls.Bbox
}
if bbox, err := NewBoundingBox(mls.Coordinates); err == nil {
return bbox
}
return []float64{}
}
// String returns the string representation
func (mls MultiLineString) String() string {
var result string
if bytes, err := json.Marshal(mls); err == nil {
result = string(bytes)
} else {
result = err.Error()
}
return result
}
// Map returns a map of the Geometry's members
func (mls MultiLineString) Map() map[string]interface{} {
result := make(map[string]interface{})
result[COORDINATES] = mls.Coordinates
result[TYPE] = MULTILINESTRING
return result
}
// NewMultiLineString is the normal factory method for a MultiLineString
func NewMultiLineString(coordinates [][][]float64) *MultiLineString {
return &MultiLineString{Type: MULTILINESTRING, Coordinates: coordinates}
}
// The MultiPolygon object contains a array of one or more polygons
type MultiPolygon struct {
Type string `json:"type"`
Coordinates [][][][]float64 `json:"coordinates"`
Bbox BoundingBox `json:"bbox,omitempty"`
}
// MultiPolygonFromBytes constructs a MultiPolygon from a GeoJSON byte array
// and returns its pointer
func MultiPolygonFromBytes(bytes []byte) (*MultiPolygon, error) {
var result MultiPolygon
err := json.Unmarshal(bytes, &result)
return &result, err
}
// ForceBbox returns a bounding box, creating one by brute force if needed
func (mp MultiPolygon) ForceBbox() BoundingBox {
if len(mp.Bbox) > 0 {
return mp.Bbox
}
if bbox, err := NewBoundingBox(mp.Coordinates); err == nil {
return bbox
}
return []float64{}
}
// String returns the string representation
func (mp MultiPolygon) String() string {
var result string
if bytes, err := json.Marshal(mp); err == nil {
result = string(bytes)
} else {
result = err.Error()
}
return result
}
// Map returns a map of the Geometry's members
func (mp MultiPolygon) Map() map[string]interface{} {
result := make(map[string]interface{})
result[COORDINATES] = mp.Coordinates
result[TYPE] = MULTIPOLYGON
return result
}
// NewMultiPolygon is the normal factory method for a MultiPolygon
func NewMultiPolygon(coordinates [][][][]float64) *MultiPolygon {
return &MultiPolygon{Type: MULTIPOLYGON, Coordinates: coordinates}
}
// The GeometryCollection object contains a array of one or more polygons
type GeometryCollection struct {
Type string `json:"type"`
Geometries []interface{} `json:"geometries"`
Bbox BoundingBox `json:"bbox,omitempty"`
}
// GeometryCollectionFromBytes constructs a GeometryCollection from a GeoJSON byte array
func GeometryCollectionFromBytes(bytes []byte) (*GeometryCollection, error) {
var result GeometryCollection
err := json.Unmarshal(bytes, &result)
var geometries []interface{}
for _, curr := range result.Geometries {
gmap := curr.(map[string]interface{})
geometry := newGeometry(gmap)
geometries = append(geometries, geometry)
}
result.Geometries = geometries
return &result, err
}
// ForceBbox returns a bounding box, creating one by brute force if needed
func (gc GeometryCollection) ForceBbox() BoundingBox {
if len(gc.Bbox) > 0 {
return gc.Bbox
}
var result BoundingBox
for _, geometry := range gc.Geometries {
if bboxIfc, ok := geometry.(BoundingBoxIfc); ok {
result = mergeBboxes(result, bboxIfc.ForceBbox())
}
}
return result
}
// String returns the string representation
func (gc GeometryCollection) String() string {
var result string
if bytes, err := json.Marshal(gc); err == nil {
result = string(bytes)
} else {
result = err.Error()
}
return result
}
// Map returns a map of the Geometry's members
func (gc GeometryCollection) Map() map[string]interface{} {
result := make(map[string]interface{})
geometries := make([]map[string]interface{}, len(gc.Geometries))
for inx, geometry := range gc.Geometries {
switch gt := geometry.(type) {
case map[string]interface{}:
geometries[inx] = gt
case Mapper:
geometries[inx] = gt.Map()
}
}
result[GEOMETRIES] = geometries
result[TYPE] = GEOMETRYCOLLECTION
return result
}
// NewGeometryCollection is the normal factory method for a GeometryCollection
func NewGeometryCollection(geometries []interface{}) *GeometryCollection {
if geometries == nil {
geometries = make([]interface{}, 0)
}
return &GeometryCollection{Type: GEOMETRYCOLLECTION, Geometries: geometries}
}
// This quasi-recursive function determines drills into the
// multidimensional array of interfaces to build a proper
// coordinate array of the right dimension
func interfaceToArray(input interface{}) interface{} {
var (
result interface{}
coords []float64
coords2 [][]float64
coords3 [][][]float64
coords4 [][][][]float64
)
switch it := input.(type) {
case []float64, [][]float64, [][][]float64, [][][][]float64:
result = it
case []interface{}:
for inx := 0; inx < len(it); inx++ {
switch curr1 := it[inx].(type) {
case float64:
coords = append(coords, curr1)
result = coords
case []interface{}:
switch curr2 := interfaceToArray(curr1).(type) {
case []float64:
coords2 = append(coords2, curr2)
result = coords2
case [][]float64:
coords3 = append(coords3, curr2)
result = coords3
case [][][]float64:
coords4 = append(coords4, curr2)
result = coords4
}
}
}
}
return result
}
// newGeometry constructs a Geometry from an interface that represents a
// GeoJSON Geometry Object
func newGeometry(input interface{}) interface{} {
var (
result interface{}
coordinates interface{}
)
switch it := input.(type) {
case map[string]interface{}:
if _, ok := it[COORDINATES]; ok {
coordinates = interfaceToArray(it[COORDINATES])
}
iType := it[TYPE].(string)
switch iType {
case POINT:
result = NewPoint(coordinates.([]float64))
case LINESTRING:
result = NewLineString(coordinates.([][]float64))
case POLYGON:
result = NewPolygon(coordinates.([][][]float64))
case MULTIPOINT:
result = NewMultiPoint(coordinates.([][]float64))
case MULTILINESTRING:
result = NewMultiLineString(coordinates.([][][]float64))
case MULTIPOLYGON:
result = NewMultiPolygon(coordinates.([][][][]float64))
case GEOMETRYCOLLECTION:
geometries := it["geometries"].([]interface{})
for inx, geometry := range geometries {
geometries[inx] = newGeometry(geometry)
}
result = NewGeometryCollection(geometries)
}
case *Point, *LineString, *Polygon, *MultiPoint, *MultiLineString, *MultiPolygon, *GeometryCollection:
result = it
}
return result
}
// ToGeometryArray takes a GeoJSON object and returns an array of
// its constituent geometry objects
func ToGeometryArray(gjObject interface{}) []interface{} {
var result []interface{}
switch typedGJ := gjObject.(type) {
case *FeatureCollection:
// re-enter with dereferenced pointer
result = ToGeometryArray(*typedGJ)
case FeatureCollection:
for _, current := range typedGJ.Features {
result = append(result, current.Geometry)
}
case *Feature:
// re-enter with dereferenced pointer
result = ToGeometryArray(*typedGJ)
case Feature:
result = append(result, typedGJ.Geometry)
case *interface{}:
// re-enter with dereferenced pointer
result = ToGeometryArray(*typedGJ)
default:
// Hopefully this is a Geometry object
result = append(result, typedGJ)
}
return result
}
// WKT returns a GeoJSON object based on the Well-Known Text input
func WKT(input string) interface{} {
parts := strings.SplitN(input, " ", 2)
name := parts[0]
predicate := parseWKTPredicate(parts[1])
switch name {
case "POINT":
if coords, ok := predicate.([]float64); ok {
return NewPoint(coords)
}
return NewPoint(nil)
case "POLYGON":
if coords, ok := predicate.([][][]float64); ok {
return NewPolygon(coords)
}
return NewPolygon(nil)
case "LINESTRING":
if coords, ok := predicate.([][]float64); ok {
return NewLineString(coords)
}
return NewLineString(nil)
case "MULTIPOINT":
switch mpp := predicate.(type) {
case [][]float64:
return NewMultiPoint(mpp)
default:
fmt.Printf("%T\n", mpp)
return NewMultiPoint(nil)
}
}
return nil
}
func parseWKTPredicate(input string) interface{} {
parts := strings.SplitN(input, "(", 2)
switch string(parts[1][0]) {
case "(":
var result2 [][]float64
var result3 [][][]float64
for _, part := range strings.Split(parts[1], ")") {
if strings.Index(part, "(") >= 0 {
currIfc := parseWKTPredicate(part)
switch curr := currIfc.(type) {
case []float64:
result2 = append(result2, curr)
case [][]float64:
result3 = append(result3, curr)
}
// part = strings.Split(part, "(")[1]
// result = append(result, parseWKTCoordinates(strings.Trim(part, " )")))
}
}
if result2 != nil {
return result2
} else if result3 != nil {
return result3
}
default:
switch strings.Index(parts[1], ",") {
case -1:
parts = strings.SplitN(parts[1], ")", 2)
return parseWKTCoordinates(parts[0])
default:
var result [][]float64
for _, part := range strings.Split(parts[1], ",") {
result = append(result, parseWKTCoordinates(strings.Trim(part, " )")))
}
return result
}
}
return nil
}
func parseWKTCoordinates(input string) []float64 {
var coords []float64
for _, part := range strings.Split(input, " ") {
coord, _ := strconv.ParseFloat(part, 10)
coords = append(coords, coord)
}
return coords
}