-
Notifications
You must be signed in to change notification settings - Fork 12
/
geojson.go
120 lines (105 loc) · 2.39 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
package tz
import (
"encoding/json"
)
// FeatureCollection ...
type FeatureCollection struct {
featureCollection
}
type featureCollection struct {
Features []*Feature
}
// Feature ...
type Feature struct {
feature
}
type feature struct {
Geometry Geometry
Properties struct {
Tzid string
}
}
// Geometry ...
type Geometry struct {
geometry
}
type geometry struct {
Coordinates [][]Point
Holes [][][]Point
BoundingBoxes [][]Point
}
var jPolyType struct {
Type string
Geometries []*Geometry
}
var jPolygon struct {
Coordinates [][][]float64
}
var jMultiPolygon struct {
Coordinates [][][][]float64
}
// UnmarshalJSON ...
func (g *Geometry) UnmarshalJSON(data []byte) (err error) {
if err := json.Unmarshal(data, &jPolyType); err != nil {
return err
}
if jPolyType.Type == "Polygon" {
if err := json.Unmarshal(data, &jPolygon); err != nil {
return err
}
pol := make([]Point, len(jPolygon.Coordinates[0]))
for i, v := range jPolygon.Coordinates[0] {
pol[i].Lon = v[0]
pol[i].Lat = v[1]
}
b := getBoundingBox(pol)
g.BoundingBoxes = append(g.BoundingBoxes, b)
g.Coordinates = append(g.Coordinates, pol)
var holes [][]Point
if len(jPolygon.Coordinates) > 1 {
holes = make([][]Point, len(jPolygon.Coordinates)-1)
for i := 1; i < len(jPolygon.Coordinates); i++ {
hole := make([]Point, len(jPolygon.Coordinates[i]))
for ii, v := range jPolygon.Coordinates[i] {
hole[ii].Lon = v[0]
hole[ii].Lat = v[1]
}
holes[i-1] = hole
}
}
g.Holes = append(g.Holes, holes)
return nil
}
if jPolyType.Type == "MultiPolygon" {
if err := json.Unmarshal(data, &jMultiPolygon); err != nil {
return err
}
g.BoundingBoxes = make([][]Point, len(jMultiPolygon.Coordinates))
g.Coordinates = make([][]Point, len(jMultiPolygon.Coordinates))
for j, poly := range jMultiPolygon.Coordinates {
pol := make([]Point, len(poly[0]))
for i, v := range poly[0] {
pol[i].Lon = v[0]
pol[i].Lat = v[1]
}
b := getBoundingBox(pol)
g.BoundingBoxes[j] = b
g.Coordinates[j] = pol
var holes [][]Point
if len(poly) > 1 {
holes = make([][]Point, len(poly)-1)
for i := 1; i < len(poly); i++ {
hole := make([]Point, len(poly[i]))
for ii, v := range poly[i] {
hole[ii].Lon = v[0]
hole[ii].Lat = v[1]
}
holes[i-1] = hole
}
}
g.Holes = append(g.Holes, holes)
}
return nil
}
return nil
}