This repository has been archived by the owner on Jun 10, 2019. It is now read-only.
/
geojson.go
709 lines (476 loc) · 12.7 KB
/
geojson.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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
package geojson
import (
"errors"
"fmt"
rtreego "github.com/dhconnelly/rtreego"
gabs "github.com/jeffail/gabs"
geo "github.com/kellydunn/golang-geo"
ioutil "io/ioutil"
"log"
"strconv"
"sync"
)
/*
- gabs is what handles marshaling a random bag of GeoJSON
- rtreego is imported to convert a WOFFeature in to a handy rtreego.Spatial object for indexing by go-whosonfirst-pip
- geo is imported to convert a WOFFeature geometry into a list of geo.Polygon objects for doing containment checks in go-whosonfirst-pip
(only Polygons and MultiPolygons are supported at the moment)
*/
// See also
// https://github.com/dhconnelly/rtreego#storing-updating-and-deleting-objects
// sudo make me an interface
// (201251207/thisisaaronland)
type WOFSpatial struct {
bounds *rtreego.Rect
Id int
Name string
Placetype string
Offset int // used when calling EnSpatializeGeom in order to know which polygon we care about
Deprecated bool
Superseded bool
}
// sudo make me an interface
// (201251207/thisisaaronland)
type WOFPolygon struct {
OuterRing geo.Polygon
InteriorRings []geo.Polygon
}
func (p *WOFPolygon) CountPoints() int {
count := len(p.OuterRing.Points())
for _, r := range p.InteriorRings {
count += len(r.Points())
}
return count
}
func (p *WOFPolygon) Contains(latitude float64, longitude float64) bool {
pt := geo.NewPoint(latitude, longitude)
contains := false
if p.OuterRing.Contains(pt) {
contains = true
}
if contains && len(p.InteriorRings) > 0 {
wg := new(sync.WaitGroup)
for _, r := range p.InteriorRings {
wg.Add(1)
go func(poly geo.Polygon, point *geo.Point) {
defer wg.Done()
/*
File under yak-shaving: Some way to send an intercept to poly.Contains
to stop the raycasting if any one of these goroutines gets the answer
it needs independent the results of the others. Like I said... yaks.
(20151028/thisisaaronland)
*/
if poly.Contains(point) {
contains = false
}
}(r, pt)
}
wg.Wait()
}
return contains
}
func (sp WOFSpatial) Bounds() *rtreego.Rect {
return sp.bounds
}
// sudo make me an interface
// (201251207/thisisaaronland)
type WOFFeature struct {
Parsed *gabs.Container
}
func (wof WOFFeature) Body() *gabs.Container {
return wof.Parsed
}
func (wof WOFFeature) Dumps() string {
return wof.Parsed.String()
}
func (wof WOFFeature) Id() int {
id, ok := wof.IntProperty("wof:id")
if ok {
return id
}
id, ok = wof.IntProperty("id")
if ok {
return id
}
id, ok = wof.IntValue("id") // see this - we're looking for an ID on the feature not the properties hash
if ok {
return id
}
return -1
}
func (wof WOFFeature) Name() string {
name, ok := wof.name("properties.wof:name")
if ok {
return name
}
name, ok = wof.name("properties.name")
if ok {
return name
}
return "a place with no name"
}
func (wof WOFFeature) name(path string) (string, bool) {
return wof.StringValue(path)
}
func (wof WOFFeature) Placetype() string {
pt, ok := wof.placetype("properties.wof:placetype")
if ok {
return pt
}
pt, ok = wof.placetype("properties.placetype")
if ok {
return pt
}
return "here be dragons"
}
func (wof WOFFeature) Deprecated() bool {
path := "edtf:deprecated"
d, ok := wof.StringProperty(path)
if ok && d != "" && d != "u" && d != "uuuu" {
return true
}
return false
}
func (wof WOFFeature) Superseded() bool {
path := "edtf:superseded"
d, ok := wof.StringProperty(path)
if ok && d != "" && d != "u" && d != "uuuu" {
return true
}
body := wof.Body()
pointers := body.Path("properties.wof:superseded_by").Data()
if len(pointers.([]interface{})) != 0 {
return true
}
return false
}
func (wof WOFFeature) Hierarchy() []map[string]int {
hierarchies := make([]map[string]int, 0)
body := wof.Body()
str_hierarchies := body.Path("properties.wof:hierarchy")
children, _ := str_hierarchies.Children()
for _, _hierarchy := range children {
hier := make(map[string]int)
_hier, _ := _hierarchy.S().ChildrenMap()
for k, v := range _hier {
// good times... this needs to be put in a function somewhere
// (20161113/thisisaaronland)
var id int
var ok bool
id_float, ok := v.Data().(float64)
if ok {
id = int(id_float)
}
if !ok {
id_int, ok := v.Data().(int)
if ok {
id = id_int
}
}
if !ok {
// See this. It's important. We are pairing it with `ok` below
// and if we don't pre-declare the variable then we end up having
// to do `str_id, ok := v.Data().(string)` which have the awesome
// side-effect of creating a locally-scope instance (of the `ok`
// variable. At which point it will appear to the outside world
// that the type-casting (to a string) will have failed even though
// it hasn't. Computers... amirite? (20161128/thisisaaronland)
var str_id string
str_id, ok = v.Data().(string)
if ok {
strconv_id, err := strconv.Atoi(str_id)
if err == nil {
id = strconv_id
} else {
ok = false
}
}
}
if !ok {
log.Println("Failed to type cast (%s = %s) in to a WOF ID", k, v)
id = -1
}
hier[k] = id
}
hierarchies = append(hierarchies, hier)
}
return hierarchies
}
func (wof WOFFeature) placetype(path string) (string, bool) {
return wof.StringValue(path)
}
func (wof WOFFeature) StringProperty(prop string) (string, bool) {
path := fmt.Sprintf("properties.%s", prop)
return wof.StringValue(path)
}
func (wof WOFFeature) StringValue(path string) (string, bool) {
body := wof.Body()
var value string
var ok bool
value, ok = body.Path(path).Data().(string)
return value, ok
}
func (wof WOFFeature) IntProperty(prop string) (int, bool) {
path := fmt.Sprintf("properties.%s", prop)
return wof.IntValue(path)
}
func (wof WOFFeature) IntValue(path string) (int, bool) {
body := wof.Body()
var id_float float64
var id_str string
var id int
var ok bool
// what follows shouldn't be necessary but appears to be
// for... uh, reasons (20151013/thisisaaronland)
id_float, ok = body.Path(path).Data().(float64)
if ok {
id = int(id_float)
} else {
id, ok = body.Path(path).Data().(int)
}
// But wait... there's more (20151028/thisisaaronland)
if !ok {
id_str, ok = body.Path(path).Data().(string)
if ok {
id_int, err := strconv.Atoi(id_str)
if err != nil {
ok = false
} else {
id = id_int
}
}
}
if !ok {
id = -1
}
return id, ok
}
// sudo make me a package function and accept an interface
// (20151207/thisisaaronland)
func (wof WOFFeature) EnSpatialize() (*WOFSpatial, error) {
id := wof.Id()
name := wof.Name()
placetype := wof.Placetype()
deprecated := wof.Deprecated()
superseded := wof.Superseded()
body := wof.Body()
var swlon float64
var swlat float64
var nelon float64
var nelat float64
children, _ := body.S("bbox").Children()
if len(children) != 4 {
return nil, errors.New("weird and freaky bounding box")
}
swlon = children[0].Data().(float64)
swlat = children[1].Data().(float64)
nelon = children[2].Data().(float64)
nelat = children[3].Data().(float64)
llat := nelat - swlat
llon := nelon - swlon
/*
fmt.Printf("%f - %f = %f\n", nelat, swlat, llat)
fmt.Printf("%f - %f = %f\n", nelon, swlon, llon)
*/
pt := rtreego.Point{swlon, swlat}
rect, err := rtreego.NewRect(pt, []float64{llon, llat})
if err != nil {
return nil, err
}
return &WOFSpatial{rect, id, name, placetype, -1, deprecated, superseded}, nil
}
// sudo make me a package function and accept an interface
// (20151207/thisisaaronland)
func (wof WOFFeature) EnSpatializeGeom() ([]*WOFSpatial, error) {
id := wof.Id()
name := wof.Name()
placetype := wof.Placetype()
deprecated := wof.Deprecated()
superseded := wof.Superseded()
spatial := make([]*WOFSpatial, 0)
polygons := wof.GeomToPolygons()
for offset, poly := range polygons {
swlat := 0.0
swlon := 0.0
nelat := 0.0
nelon := 0.0
for _, pt := range poly.OuterRing.Points() {
lat := pt.Lat()
lon := pt.Lng()
if swlat == 0.0 || swlat > lat {
swlat = lat
}
if swlon == 0.0 || swlon > lon {
swlon = lon
}
if nelat == 0.0 || nelat < lat {
nelat = lat
}
if nelon == 0.0 || nelon < lon {
nelon = lon
}
// fmt.Println(lat, lon, swlat, swlon, nelat, nelon)
}
llat := nelat - swlat
llon := nelon - swlon
pt := rtreego.Point{swlon, swlat}
rect, err := rtreego.NewRect(pt, []float64{llon, llat})
if err != nil {
return nil, err
}
sp := WOFSpatial{rect, id, name, placetype, offset, deprecated, superseded}
spatial = append(spatial, &sp)
}
return spatial, nil
}
func (wof WOFFeature) Contains(latitude float64, longitude float64) bool {
polygons := wof.GeomToPolygons()
contains := false
wg := new(sync.WaitGroup)
for _, p := range polygons {
wg.Add(1)
go func(poly *WOFPolygon, lat float64, lon float64) {
defer wg.Done()
if poly.Contains(lat, lon) {
contains = true
}
}(p, latitude, longitude)
}
wg.Wait()
return contains
}
// sudo make me a package function and accept an interface... maybe?
// (20151207/thisisaaronland)}
func (wof WOFFeature) GeomToPolygons() []*WOFPolygon {
body := wof.Body()
var geom_type string
geom_type, _ = body.Path("geometry.type").Data().(string)
children, _ := body.S("geometry").ChildrenMap()
polygons := make([]*WOFPolygon, 0)
for key, child := range children {
if key != "coordinates" {
continue
}
var coordinates []interface{}
coordinates, _ = child.Data().([]interface{})
if geom_type == "Polygon" {
polygons = append(polygons, wof.DumpPolygon(coordinates))
} else if geom_type == "MultiPolygon" {
polygons = wof.DumpMultiPolygon(coordinates)
} else {
// pass
}
}
return polygons
}
// sudo these don't need to be public methods
// (20151207/thisisaaronland)
func (wof WOFFeature) DumpMultiPolygon(coordinates []interface{}) []*WOFPolygon {
polygons := make([]*WOFPolygon, 0)
for _, ipolys := range coordinates {
polys := ipolys.([]interface{})
polygon := wof.DumpPolygon(polys)
polygons = append(polygons, polygon)
}
return polygons
}
// sudo these don't need to be public methods
// (20151207/thisisaaronland)
func (wof WOFFeature) DumpPolygon(coordinates []interface{}) *WOFPolygon {
polygons := make([]geo.Polygon, 0)
for _, ipoly := range coordinates {
poly := ipoly.([]interface{})
polygon := wof.DumpCoords(poly)
polygons = append(polygons, polygon)
}
return &WOFPolygon{
OuterRing: polygons[0],
InteriorRings: polygons[1:],
}
}
// sudo these don't need to be public methods
// (20151207/thisisaaronland)
func (wof WOFFeature) DumpCoords(poly []interface{}) geo.Polygon {
polygon := geo.Polygon{}
for _, icoords := range poly {
coords := icoords.([]interface{})
lon := coords[0].(float64)
lat := coords[1].(float64)
pt := geo.NewPoint(lat, lon)
polygon.Add(pt)
}
return polygon
}
// see below (20151207/thisisaaronland)
func UnmarshalFile(path string) (*WOFFeature, error) {
body, read_err := ioutil.ReadFile(path)
if read_err != nil {
return nil, read_err
}
return UnmarshalFeature(body)
}
// this is disabled for now even though it (or something like it) is
// probably the new new; note the way we end up parsing the JSON body
// twice... we should not do that (20151207/thisisaaronland)
/*
func UnmarshalFileMulti(path string) ([]*WOFFeature, error) {
body, read_err := ioutil.ReadFile(path)
if read_err != nil {
return nil, read_err
}
parsed, parse_err := gabs.ParseJSON(body)
if parse_err != nil {
return nil, parse_err
}
isa, ok := parsed.Path("type").Data().(string)
if !ok {
return nil, errors.New("failed to determine type")
}
features := make([]*WOFFeature, 0)
if isa == "Feature" {
f, err := UnmarshalFeature(body)
if err != nil {
return nil, err
}
features = append(features, f)
} else if isa == "FeatureCollection" {
collection, err := UnmarshalFeatureCollection(body)
if err != nil {
return nil, err
}
features = collection
} else {
return nil, errors.New("unknown type")
}
return features, nil
}
*/
// see above inre passing bytes or an already parsed thing-y
// (20151207/thisisaaronland)
func UnmarshalFeatureCollection(raw []byte) ([]*WOFFeature, error) {
parsed, parse_err := gabs.ParseJSON(raw)
if parse_err != nil {
return nil, parse_err
}
children, _ := parsed.S("features").Children()
collection := make([]*WOFFeature, 0)
for _, child := range children {
f := WOFFeature{
Parsed: child,
}
collection = append(collection, &f)
}
return collection, nil
}
// see above inre passing bytes or an already parsed thing-y
// (20151207/thisisaaronland)
func UnmarshalFeature(raw []byte) (*WOFFeature, error) {
parsed, parse_err := gabs.ParseJSON(raw)
if parse_err != nil {
return nil, parse_err
}
rsp := WOFFeature{
Parsed: parsed,
}
return &rsp, nil
}