-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeojson.go
313 lines (249 loc) · 7.21 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
package gegography
import (
"encoding/json"
)
type geoJSONGeometry struct {
Type string `json:"type"`
Coordinates json.RawMessage `json:"coordinates"`
}
type geoJSONFeature struct {
Type string `json:"type"`
Properties map[string]interface{} `json:"properties,omitempty"`
Geometry geoJSONGeometry `json:"geometry"`
}
type geoJSON struct {
Type string `json:"type"`
Name string `json:"name,omitempty"`
CoordinateReferenceSystem *CRS `json:"crs,omitempty"`
Features []geoJSONFeature `json:"features"`
}
type gjPoint []float64
type gjMultiPoint []gjPoint
type gjPolygon []gjMultiPoint
type gjMultiPolygon []gjPolygon
func (g gjPoint) toPoint() Point {
return Point{X: g[0], Y: g[1]}
}
func (g gjMultiPoint) toMultiPoint() MultiPoint {
mp := make(MultiPoint, 0)
for x := range g {
_g := g[x]
mp = append(mp, _g.toPoint())
}
return mp
}
func (g gjPolygon) toPolygon() Polygon {
p := make(Polygon, 0)
for x := range g {
_g := g[x]
p = append(p, _g.toMultiPoint())
}
return p
}
func (g gjMultiPolygon) toMultiPolygon() MultiPolygon {
mp := make(MultiPolygon, 0)
for x := range g {
_g := g[x]
mp = append(mp, _g.toPolygon())
}
return mp
}
func (f *Feature) toGeoJSONFeature() (geoJSONFeature, error) {
var coordinates interface{}
switch f.Type {
case "Point":
coordinates = f.Coordinates.(Point).toGeoJSON()
case "MultiPoint":
coordinates = f.Coordinates.(MultiPoint).toGeoJSON()
case "LineString":
coordinates = f.Coordinates.(MultiPoint).toGeoJSON()
case "Polygon":
coordinates = f.Coordinates.(Polygon).toGeoJSON()
case "MultiLineString":
coordinates = f.Coordinates.(Polygon).toGeoJSON()
case "MultiPolygon":
coordinates = f.Coordinates.(MultiPolygon).toGeoJSON()
default:
return geoJSONFeature{}, GeoTypeError{Type: f.Type}
}
jc, err := json.Marshal(coordinates)
if err != nil {
return geoJSONFeature{}, err
}
gjf := geoJSONFeature{
Type: "Feature",
Properties: f.Properties,
}
gjf.Geometry.Type = f.Type
gjf.Geometry.Coordinates = jc
return gjf, nil
}
// ToGeoJSON exports a Feature to a byte array containing JSON conforming to the GeoJSON format
func (f *Feature) ToGeoJSON() ([]byte, error) {
gjf, err := f.toGeoJSONFeature()
if err != nil {
return nil, err
}
out, err := json.Marshal(gjf)
if err != nil {
return nil, err
}
return out, nil
}
func (p Point) toGeoJSON() gjPoint {
return gjPoint{p.X, p.Y}
}
func (mp MultiPoint) toGeoJSON() gjMultiPoint {
gjmp := make(gjMultiPoint, 0)
for x := range mp {
p := mp[x]
gjmp = append(gjmp, p.toGeoJSON())
}
return gjmp
}
func (p Polygon) toGeoJSON() gjPolygon {
gjp := make(gjPolygon, 0)
for x := range p {
mp := p[x]
gjp = append(gjp, mp.toGeoJSON())
}
return gjp
}
func (mp MultiPolygon) toGeoJSON() gjMultiPolygon {
gjmp := make(gjMultiPolygon, 0)
for x := range mp {
p := mp[x]
gjmp = append(gjmp, p.toGeoJSON())
}
return gjmp
}
func (fc *FeatureCollection) toGeoJSONStruct() (geoJSON, error) {
gj := geoJSON{Name: fc.Name, CoordinateReferenceSystem: fc.CoordinateReferenceSystem, Type: "FeatureCollection"}
for x := range fc.Features {
f := fc.Features[x]
gjf, err := f.toGeoJSONFeature()
if err != nil {
return geoJSON{}, err
}
gj.Features = append(gj.Features, gjf)
}
return gj, nil
}
// ToGeoJSON exports a FeatureCollection to a byte array containing JSON conforming to the GeoJSON format
func (fc *FeatureCollection) ToGeoJSON() ([]byte, error) {
gj, err := fc.toGeoJSONStruct()
if err != nil {
return nil, err
}
return json.Marshal(gj)
}
// ToGeoJSONFeatureArray exports a FeatureCollection to a byte array conforming to the GeoJSON format but discards everything except the feature array
func (fc *FeatureCollection) ToGeoJSONFeatureArray() ([]byte, error) {
gj, err := fc.toGeoJSONStruct()
if err != nil {
return nil, err
}
return json.Marshal(gj.Features)
}
// ToPrettyGeoJSON exports a FeatureCollection to a byte array containing indented JSON conforming to the GeoJSON format
func (fc *FeatureCollection) ToPrettyGeoJSON() ([]byte, error) {
gj, err := fc.toGeoJSONStruct()
if err != nil {
return nil, err
}
return json.MarshalIndent(gj, "", "\t")
}
// LoadGeoJSONFeature parses an array of bytes conforming to a GeoJSON feature to a Feature
func LoadGeoJSONFeature(input []byte) (Feature, error) {
var feature geoJSONFeature
if err := json.Unmarshal(input, &feature); err != nil {
return Feature{}, err
}
var coordinates interface{}
var err error
switch feature.Geometry.Type {
case "Point":
var g gjPoint
err = json.Unmarshal(feature.Geometry.Coordinates, &g)
coordinates = g.toPoint()
case "MultiPoint":
var g gjMultiPoint
err = json.Unmarshal(feature.Geometry.Coordinates, &g)
coordinates = g.toMultiPoint()
case "LineString":
var g gjMultiPoint
err = json.Unmarshal(feature.Geometry.Coordinates, &g)
coordinates = g.toMultiPoint()
case "MultiLineString":
var g gjPolygon
err = json.Unmarshal(feature.Geometry.Coordinates, &g)
coordinates = g.toPolygon()
case "Polygon":
var g gjPolygon
err = json.Unmarshal(feature.Geometry.Coordinates, &g)
coordinates = g.toPolygon()
case "MultiPolygon":
var g gjMultiPolygon
err = json.Unmarshal(feature.Geometry.Coordinates, &g)
coordinates = g.toMultiPolygon()
default:
return Feature{}, GeoTypeError{Type: feature.Type}
}
if err != nil {
return Feature{}, GeoFormatError{Msg: "GeoJSON is malformed"}
}
return Feature{Type: feature.Geometry.Type, Properties: feature.Properties, Coordinates: coordinates}, nil
}
// LoadGeoJSON parses an array of bytes conforming to the GeoJSON format to a FeatureCollection
func LoadGeoJSON(input []byte) (FeatureCollection, error) {
var gj geoJSON
if err := json.Unmarshal(input, &gj); err != nil {
return FeatureCollection{}, err
}
var fc FeatureCollection
fc.Features = make([]Feature, 0)
fc.Name = gj.Name
var coordinates interface{}
var err error
for f := range gj.Features {
skip := false
feature := gj.Features[f]
switch feature.Geometry.Type {
case "Point":
var g gjPoint
err = json.Unmarshal(feature.Geometry.Coordinates, &g)
coordinates = g.toPoint()
case "MultiPoint":
var g gjMultiPoint
err = json.Unmarshal(feature.Geometry.Coordinates, &g)
coordinates = g.toMultiPoint()
case "LineString":
var g gjMultiPoint
err = json.Unmarshal(feature.Geometry.Coordinates, &g)
coordinates = g.toMultiPoint()
case "MultiLineString":
var g gjPolygon
err = json.Unmarshal(feature.Geometry.Coordinates, &g)
coordinates = g.toPolygon()
case "Polygon":
var g gjPolygon
err = json.Unmarshal(feature.Geometry.Coordinates, &g)
coordinates = g.toPolygon()
case "MultiPolygon":
var g gjMultiPolygon
err = json.Unmarshal(feature.Geometry.Coordinates, &g)
coordinates = g.toMultiPolygon()
case "":
skip = true
default:
return FeatureCollection{}, GeoTypeError{Type: feature.Type}
}
if !skip {
if err != nil {
return FeatureCollection{}, GeoFormatError{Msg: "GeoJSON is malformed"}
}
fc.Features = append(fc.Features, Feature{Type: feature.Geometry.Type, Properties: feature.Properties, Coordinates: coordinates})
}
}
return fc, nil
}