-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeometrycollection.go
145 lines (121 loc) · 4.02 KB
/
geometrycollection.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
package joejson
import (
"encoding/json"
"fmt"
)
// GeometryTypeGeometryCollection is the value for a GeometryCollection's 'type' member.
const GeometryTypeGeometryCollection = "GeometryCollection"
// GeometryCollection is a slice of Geometries.
type GeometryCollection []GeometryCollectionMember
// AppendPoint appends a Point to the collection.
func (g GeometryCollection) AppendPoint(m Point) GeometryCollection {
return append(g, GeometryCollectionMember{m})
}
// AppendMultiPoint appends a MultiPoint to the collection.
func (g GeometryCollection) AppendMultiPoint(m MultiPoint) GeometryCollection {
return append(g, GeometryCollectionMember{m})
}
// AppendLineString appends a LineString to the collection.
func (g GeometryCollection) AppendLineString(m LineString) GeometryCollection {
return append(g, GeometryCollectionMember{m})
}
// AppendMuliLineString appends a MultiLineString to the collection.
func (g GeometryCollection) AppendMuliLineString(m MultiLineString) GeometryCollection {
return append(g, GeometryCollectionMember{m})
}
// AppendPolygon appends a Polygon to the collection.
func (g GeometryCollection) AppendPolygon(m Polygon) GeometryCollection {
return append(g, GeometryCollectionMember{m})
}
// AppendMultiPolygon appends a MultiPolygon to the collection.
func (g GeometryCollection) AppendMultiPolygon(m MultiPolygon) GeometryCollection {
return append(g, GeometryCollectionMember{m})
}
// MarshalJSON is a custom JSON marshaller.
func (g GeometryCollection) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Geometries []GeometryCollectionMember `json:"geometries"`
Type string `json:"type"`
}{
g,
GeometryTypeGeometryCollection,
})
}
// UnmarshalJSON is a custom JSON unmarshaller.
func (g *GeometryCollection) UnmarshalJSON(b []byte) error {
var tmp struct {
Geometries []json.RawMessage `json:"geometries"`
Type string `json:"type"`
}
if err := json.Unmarshal(b, &tmp); err != nil {
return err
}
if tmp.Type != GeometryTypeGeometryCollection {
return fmt.Errorf("invalid type %q, expected %q", tmp.Type, GeometryTypeGeometryCollection)
}
for _, geom := range tmp.Geometries {
geom, err := unmarshalGeometry(geom)
if err != nil {
return err
}
*g = append(*g, GeometryCollectionMember{geom})
}
return nil
}
// GeometryCollectionMember is a Geometry belonging to a GeometryCollection.
type GeometryCollectionMember struct {
geometry any
}
// AsPoint casts the Geometry to a Point.
func (g GeometryCollectionMember) AsPoint() (Point, bool) {
p, ok := g.geometry.(Point)
return p, ok
}
// AsMultiPoint casts the Geometry to a MultiPoint.
func (g GeometryCollectionMember) AsMultiPoint() (MultiPoint, bool) {
p, ok := g.geometry.(MultiPoint)
return p, ok
}
// AsLineString casts the Geometry to a LineString.
func (g GeometryCollectionMember) AsLineString() (LineString, bool) {
p, ok := g.geometry.(LineString)
return p, ok
}
// AsMultiLineString casts the Geometry to a MultiLineString.
func (g GeometryCollectionMember) AsMultiLineString() (MultiLineString, bool) {
p, ok := g.geometry.(MultiLineString)
return p, ok
}
// AsPolygon casts the Geometry to a Polygon.
func (g *GeometryCollectionMember) AsPolygon() (Polygon, bool) {
p, ok := g.geometry.(Polygon)
return p, ok
}
// AsMultiPolygon casts the Geometry to a MultiPolygon.
func (g *GeometryCollectionMember) AsMultiPolygon() (MultiPolygon, bool) {
p, ok := g.geometry.(MultiPolygon)
return p, ok
}
// MarshalJSON is a custom JSON marshaller.
func (g GeometryCollectionMember) MarshalJSON() ([]byte, error) {
return json.Marshal(g.geometry)
}
// Type is the type of the Geometry.
func (g GeometryCollectionMember) Type() string {
switch g.geometry.(type) {
case Point:
return GeometryTypePoint
case MultiPoint:
return GeometryTypeMultiPoint
case LineString:
return GeometryTypeLineString
case MultiLineString:
return GeometryTypeMultiLineString
case Polygon:
return GeometryTypePolygon
case MultiPolygon:
return GeometryTypeMultiPolygon
default:
return ""
}
}