-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgeometry.go
415 lines (366 loc) · 10.9 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package geojson
import (
"errors"
"fmt"
"reflect"
)
const (
INIT_GEOM_CAP = 10
)
type CoordType float64
// Function which will try convert interface{} to CoordType object.
// If conversion is not possible then raise Panic.
func Coord(obj interface{}) (ct CoordType) {
switch num := obj.(type) {
case float64:
ct = CoordType(num)
break
case int:
ct = CoordType(num)
break
case float32:
ct = CoordType(num)
break
case int64:
ct = CoordType(num)
break
default:
panic(fmt.Sprintf("Error: Cannot parse object: '%v' type: '%v' to CoordType!", obj, reflect.TypeOf(obj)))
}
return
}
// Coordinate is a type to represent one coordinate (x,y). This coordinate can have any number of dimensions per the spec.
// To simplify create new instance.
// c := Coordinate{x, y}
// or
// c := Coordinate{x, y, z}
type Coordinate []CoordType
// Coordinates is a lice of coordinates.
// Simply creation:
// c := Coordinates{{1, 2}, {2,2}}
// or
// c := Coordinates{Coordinate{1, 2}, Coordinate{2,2}}
type Coordinates []Coordinate
// MultiLine represents of set of lines
type MultiLine []Coordinates
// Geometry interface for types which represent geometries
type Geometry interface {
GetType() string
AddGeometry(interface{}) error
GetCoordinates() interface{}
}
//Point coordinates are in x, y order
//(easting, northing for projected coordinates,
//longitude, latitude for geographic coordinates)
//Out example:
// { "type": "Point", "coordinates": [100.0, 0.0] }
type Point struct {
Type string `json:"type" bson:"type"`
Coordinates Coordinate `json:"coordinates" bson:"coordinates"`
Crs *CRS `json:"crs,omitempty" bson:"crs,omitempty"`
}
// AddGeometry adds geometry to coordinates.
// New value will replace existing
func (t *Point) AddGeometry(g interface{}) error {
if c, ok := g.(Coordinate); ok {
t.Coordinates = c
} else {
return fmt.Errorf("AssertionError: %v to %v", g, "Coordinate")
}
return nil
}
// GetType return type of geometry
func (t Point) GetType() string {
return t.Type
}
//GetCoordinates return coordinates of geometry
func (t Point) GetCoordinates() interface{} {
return t.Coordinates
}
//Factory function to create new object
func NewPoint(c Coordinate) *Point {
return &Point{Type: "Point", Coordinates: c}
}
//Coordinates of a MultiPoint are an array of positions:
// Out example:
// { "type": "MultiPoint",
// "coordinates": [ [100.0, 0.0], [101.0, 1.0] ]
// }
type MultiPoint struct {
Type string `json:"type" bson:"type"`
Coordinates Coordinates `json:"coordinates" bson:"coordinates"`
Crs *CRS `json:"crs,omitempty" bson:"crs,omitempty"`
}
// Add geometry to MultiPoint, if paremeter will be append to coordinates
func (t *MultiPoint) AddGeometry(g interface{}) error {
switch c := g.(type) {
case Coordinate:
t.AddCoordinates(c)
break
case Coordinates:
t.AddCoordinates(c...)
break
default:
return errors.New(fmt.Sprintf("AssertionError %v", g))
}
return nil
}
// Add new point to MultiPoint object
func (t *MultiPoint) AddCoordinates(p ...Coordinate) {
t.Coordinates = append(t.Coordinates, p...)
}
func (t MultiPoint) GetType() string {
return t.Type
}
//GetCoordinates return coordinates of geometry
func (t MultiPoint) GetCoordinates() interface{} {
return t.Coordinates
}
//Factory function to create new object
func NewMultiPoint(coordinates Coordinates) *MultiPoint {
if coordinates == nil {
coordinates = make(Coordinates, 0, INIT_GEOM_CAP)
}
return &MultiPoint{Type: "MultiPoint", Coordinates: coordinates}
}
//Coordinates of LineString are an array of positions
// Out example:
// { "type": "LineString",
// "coordinates": [ [100.0, 0.0], [101.0, 1.0] ]
// }
type LineString struct {
Type string `json:"type" bson:"type"`
Coordinates Coordinates `json:"coordinates" bson:"coordinates"`
Crs *CRS `json:"crs,omitempty" bson:"crs,omitempty"`
}
// Add new position to LineString
func (t *LineString) AddCoordinates(c ...Coordinate) {
t.Coordinates = append(t.Coordinates, c...)
}
// Add new position to LineString
func (t *LineString) AddGeometry(g interface{}) error {
switch c := g.(type) {
case Coordinate:
t.AddCoordinates(c)
break
case Coordinates:
t.AddCoordinates(c...)
break
default:
return errors.New(fmt.Sprintf("AssertionError %v", g))
}
return nil
}
func (t LineString) GetType() string {
return t.Type
}
//GetCoordinates return coordinates of geometry
func (t LineString) GetCoordinates() interface{} {
return t.Coordinates
}
//Factory function to create new object with points
func NewLineString(coordinates Coordinates) *LineString {
if coordinates == nil {
coordinates = make(Coordinates, 0, INIT_GEOM_CAP)
}
return &LineString{Type: "LineString", Coordinates: coordinates}
}
// For type "MultiLineString", the "coordinates" member must be an array
// of LineString coordinate arrays.
// Out example:
// { "type": "MultiLineString",
// "coordinates": [
// [ [100.0, 0.0], [101.0, 1.0] ],
// [ [102.0, 2.0], [103.0, 3.0] ]
// ]
// }
type MultiLineString struct {
Type string `json:"type" bson:"type"`
Coordinates MultiLine `json:"coordinates" bson:"coordinates"`
Crs *CRS `json:"crs,omitempty" bson:"crs,omitempty"`
}
// Add new line or line to MultiLineString
func (t *MultiLineString) AddGeometry(g interface{}) error {
switch c := g.(type) {
case MultiLine:
t.AddCoordinates(c...)
break
case Coordinates:
t.AddCoordinates(c)
break
default:
return errors.New(fmt.Sprintf("AssertionError %v", g))
}
return nil
}
// Add collection of coordinates to MultiLineString
// new data are append
func (t *MultiLineString) AddCoordinates(coordinates ...Coordinates) {
t.Coordinates = append(t.Coordinates, coordinates...)
}
func (t MultiLineString) GetType() string {
return t.Type
}
//GetCoordinates return coordinates of geometry
func (t MultiLineString) GetCoordinates() interface{} {
return t.Coordinates
}
// Factory function for type MultiLineString
func NewMultiLineString(coordinates MultiLine) *MultiLineString {
if coordinates == nil {
coordinates = make(MultiLine, 0, INIT_GEOM_CAP)
}
return &MultiLineString{Type: "MultiLineString", Coordinates: coordinates}
}
// For type "Polygon", the "coordinates" member must be an array of LinearRing
// coordinate arrays. For Polygons with multiple rings, the first must be
// the exterior ring and any others must be interior rings or holes.
// Out example:
//{ "type": "Polygon",
// "coordinates": [
// [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
// [100.0, 1.0], [100.0, 0.0] ]
// ]
// }
type Polygon struct {
Type string `json:"type" bson:"type"`
Coordinates MultiLine `json:"coordinates,float" bson:"coordinates"`
Crs *CRS `json:"crs,omitempty" bson:"crs,omitempty"`
}
// Add new polygon or hole to Polygon
func (t *Polygon) AddGeometry(g interface{}) error {
switch c := g.(type) {
case MultiLine:
t.AddCoordinates(c...)
break
case Coordinates:
t.AddCoordinates(c)
break
default:
return errors.New(fmt.Sprintf("AssertionError %v", g))
}
return nil
}
// add new polygon or hole.
// new values are append
func (t *Polygon) AddCoordinates(coordinates ...Coordinates) {
t.Coordinates = append(t.Coordinates, coordinates...)
}
// GetType return type of geometry
func (t Polygon) GetType() string {
return t.Type
}
//GetCoordinates return coordinates of geometry
func (t Polygon) GetCoordinates() interface{} {
return t.Coordinates
}
// factory function
func NewPolygon(coordinates MultiLine) *Polygon {
if coordinates == nil {
coordinates = make(MultiLine, 0, INIT_GEOM_CAP)
}
return &Polygon{Type: "Polygon", Coordinates: coordinates}
}
// For type "MultiPolygon", the "coordinates" member must
// be an array of Polygon coordinate arrays.
// Out example
//{ "type": "MultiPolygon",
// "coordinates": [
// [[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]],
// [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]],
// [[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]
// ]
// }
type MultiPolygon struct {
Type string `json:"type" bson:"type"`
Coordinates []MultiLine `json:"coordinates" bson:"coordinates"`
Crs *CRS `json:"crs,omitempty" bson:"crs,omitempty"`
}
// add new polygon or hole.
// new values are append
func (t *MultiPolygon) AddCoordinates(lines ...MultiLine) {
t.Coordinates = append(t.Coordinates, lines...)
}
// AddGeometry add new polygon or hole to Polygon
func (t *MultiPolygon) AddGeometry(g interface{}) error {
switch c := g.(type) {
case []MultiLine:
t.AddCoordinates(c...)
break
case MultiLine:
t.AddCoordinates(c)
break
default:
return errors.New(fmt.Sprintf("AssertionError %v", g))
}
return nil
}
// GetType return type of geometry
func (t MultiPolygon) GetType() string {
return t.Type
}
//GetCoordinates return coordinates of geometry return object geometry
func (t MultiPolygon) GetCoordinates() interface{} {
return t.Coordinates
}
// factory function
func NewMultiPolygon(coordinates []MultiLine) *MultiPolygon {
if coordinates == nil {
coordinates = make([]MultiLine, 0, INIT_GEOM_CAP)
}
return &MultiPolygon{Type: "MultiPolygon", Coordinates: coordinates}
}
// GeometryCollection is a GeoJSON object with type "GeometryCollection" is a geometry object
// which represents a collection of geometry objects.
// A geometry collection must have a member with the name
// "geometries". The value corresponding to "geometries" is an array.
// Each element in this array is a GeoJSON geometry object.
// Out example:
//{ "type": "GeometryCollection",
// "geometries": [
// { "type": "Point",
// "coordinates": [100.0, 0.0]
// },
// { "type": "LineString",
// "coordinates": [ [101.0, 0.0], [102.0, 1.0] ]
// }
// ]
//}
type GeometryCollection struct {
Type string `json:"type" bson:"type"`
Geometries []interface{} `json:"geometries" bson:"geometries"`
Crs *CRS `json:"crs,omitempty" bson:"crs,omitempty"`
}
// AddGeometries appends new values to Geometries
func (t *GeometryCollection) AddGeometries(g ...interface{}) {
t.Geometries = append(t.Geometries, g...)
}
// AddGeometry add new geometry or hole to GeometryCollection
func (t *GeometryCollection) AddGeometry(g interface{}) error {
switch c := g.(type) {
case []interface{}:
t.AddGeometries(c...)
break
case interface{}:
t.AddGeometries(c)
break
default:
return errors.New(fmt.Sprintf("AssertionError %v", g))
}
return nil
}
// GetType return type of geometry
func (t GeometryCollection) GetType() string {
return t.Type
}
//GetCoordinates return coordinates of geometry
func (t GeometryCollection) GetCoordinates() interface{} {
return t.Geometries
}
// factory function
func NewGeometryCollection(g []interface{}) *GeometryCollection {
if g == nil {
g = make([]interface{}, 0, 10)
}
return &GeometryCollection{Type: "GeometryCollection", Geometries: g}
}