-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
geometry.go
128 lines (114 loc) · 2.82 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
// Package tegola describes the basic geometries that can be used to convert to
// and from.
package tegola
import (
"encoding/json"
"fmt"
"io"
)
// Geometry describes a geometry.
type Geometry interface{}
// Point is how a point should look like.
type Point interface {
Geometry
X() float64
Y() float64
}
// Point3 is a point with three dimensions; at current is just converted and treated as a point.
type Point3 interface {
Point
Z() float64
}
// MultiPoint is a Geometry with multiple individual points.
type MultiPoint interface {
Geometry
Points() []Point
}
// LineString is a Geometry of a line.
type LineString interface {
Geometry
Subpoints() []Point
}
// MultiLine is a Geometry with multiple individual lines.
type MultiLine interface {
Geometry
Lines() []LineString
}
// Polygon is a multi-line Geometry where all the lines connect to form an enclose space.
type Polygon interface {
Geometry
Sublines() []LineString
}
// MultiPolygon describes a Geometry multiple intersecting polygons. There should only one
// exterior polygon, and the rest of the polygons should be interior polygons. The interior
// polygons will exclude the area from the exterior polygon.
type MultiPolygon interface {
Geometry
Polygons() []Polygon
}
// Collection is a collections of different geometries.
type Collection interface {
Geometry
Geometries() []Geometry
}
func GeometryAsString(g Geometry) string {
switch geo := g.(type) {
case LineString:
rstring := "["
for _, p := range geo.Subpoints() {
rstring = fmt.Sprintf("%v ( %v %v )", rstring, p.X(), p.Y())
}
rstring += "]"
return rstring
default:
return fmt.Sprintf("%v", g)
}
}
func GeometryAsMap(g Geometry) map[string]interface{} {
js := make(map[string]interface{})
var vals []map[string]interface{}
switch geo := g.(type) {
case Point:
js["type"] = "point"
js["value"] = []float64{geo.X(), geo.Y()}
case Point3:
js["type"] = "point3"
js["value"] = []float64{geo.X(), geo.Y(), geo.Z()}
case MultiPoint:
js["type"] = "multipoint"
for _, p := range geo.Points() {
vals = append(vals, GeometryAsMap(p))
}
js["value"] = vals
case LineString:
js["type"] = "linestring"
var fv []float64
for _, p := range geo.Subpoints() {
fv = append(fv, p.X(), p.Y())
}
js["value"] = fv
case MultiLine:
js["type"] = "multiline"
for _, l := range geo.Lines() {
vals = append(vals, GeometryAsMap(l))
}
js["value"] = vals
case Polygon:
js["type"] = "polygon"
for _, l := range geo.Sublines() {
vals = append(vals, GeometryAsMap(l))
}
js["value"] = vals
case MultiPolygon:
js["type"] = "multipolygon"
for _, p := range geo.Polygons() {
vals = append(vals, GeometryAsMap(p))
}
js["value"] = vals
}
return js
}
func GeometryAsJSON(g Geometry, w io.Writer) error {
enc := json.NewEncoder(w)
return enc.Encode(GeometryAsMap(g))
}