/
geojson.go
73 lines (63 loc) · 1.68 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
package main
import (
"github.com/paulsmith/gogeos/geos"
)
type gjFeatureCollection struct {
Type string `json:"type"`
Features []gjFeature `json:"features"`
}
type gjFeature struct {
Type string `json:"type"`
Geometry gjGeometry `json:"geometry"`
Properties GeoData `json:"properties"`
}
// Note: gjGeometry is limited to the MultiPolygon primitive
type gjGeometry struct {
Type string `json:"type"`
Coords [][][][]float64 `json:"coordinates"`
}
func ToGjFeature(g *geos.Geometry) (gjFeature, error) {
var gjf gjFeature
gjf.Type = "Feature"
gjf.Geometry.Type = "MultiPolygon"
ngeoms, _ := g.NGeometry()
gjf.Geometry.Coords = make([][][][]float64, ngeoms)
for i := 0; i < ngeoms; i++ {
subg, err := g.Geometry(i)
if err != nil {
return gjf, err
}
holes, err := subg.Holes()
if err != nil {
return gjf, err
}
gjf.Geometry.Coords[i] = make([][][]float64, 1+len(holes))
shell, err := subg.Shell()
if err != nil {
return gjf, err
}
coords, err := shell.Coords()
if err != nil {
return gjf, err
}
gjf.Geometry.Coords[i][0] = make([][]float64, len(coords))
for j, coord := range coords {
gjf.Geometry.Coords[i][0][j] = make([]float64, 2)
gjf.Geometry.Coords[i][0][j][0] = coord.X
gjf.Geometry.Coords[i][0][j][1] = coord.Y
}
for k, hole := range holes {
coords, err := hole.Coords()
if err != nil {
return gjf, err
}
gjf.Geometry.Coords[i][1+k] = make([][]float64, len(coords))
for j, coord := range coords {
gjf.Geometry.Coords[i][1+k][j] = make([]float64, 2)
gjf.Geometry.Coords[i][1+k][j][0] = coord.X
gjf.Geometry.Coords[i][1+k][j][1] = coord.Y
}
}
}
return gjf, nil
}