-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeometry.go
433 lines (370 loc) · 9.42 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package geom
import (
"fmt"
"runtime"
"sync"
"github.com/vistarmedia/geom/geos-go"
"github.com/vistarmedia/geom/geos-go/handle"
)
type GeometryType int
const (
POINT GeometryType = iota
LINESTRING
LINEARRING
POLYGON
MULTIPOINT
MULTILINESTRING
MULTIPOLYGON
GEOMETRYCOLLECTION
)
// See OGC Simple Feature Specification for geometry operation details:
// http://portal.opengeospatial.org/files/?artifact_id=25355
type Geometry struct {
hp handle.GeosHandleProvider
g *geos.Geometry
}
type toGeos interface {
UnsafeToGeos() *geos.Geometry
}
type unaryOp func(*geos.Handle) (*geos.Geometry, error)
type unaryPredicate func(*geos.Handle) (bool, error)
type binaryOp func(*geos.Handle, *geos.Geometry) (*geos.Geometry, error)
type binaryPredicate func(*geos.Handle, *geos.Geometry) (bool, error)
func newGeometry(
hp handle.GeosHandleProvider, g *geos.Geometry) *Geometry {
geom := &Geometry{
hp: hp,
g: g,
}
runtime.SetFinalizer(geom, func(geom1 *Geometry) {
h := geom1.hp.Get()
geom1.g.Destroy(h)
geom1.hp.Put(h)
})
return geom
}
func newGeometryOrError(
hp handle.GeosHandleProvider,
g *geos.Geometry,
err error) (*Geometry, error) {
if err != nil {
return nil, err
} else {
return newGeometry(hp, g), nil
}
}
func (g *Geometry) unaryOperation(op unaryOp) (*Geometry, error) {
h := g.hp.Get()
geom, err := op(h)
g.hp.Put(h)
return newGeometryOrError(g.hp, geom, err)
}
func (g *Geometry) unaryPredicate(op unaryPredicate) (bool, error) {
h := g.hp.Get()
defer g.hp.Put(h)
return op(h)
}
func (g *Geometry) binaryOperation(op binaryOp, o toGeos) (*Geometry, error) {
h := g.hp.Get()
geom, err := op(h, o.UnsafeToGeos())
runtime.KeepAlive(o)
g.hp.Put(h)
return newGeometryOrError(g.hp, geom, err)
}
func (g *Geometry) binaryPredicate(op binaryPredicate, o toGeos) (bool, error) {
h := g.hp.Get()
val, err := op(h, o.UnsafeToGeos())
runtime.KeepAlive(o)
g.hp.Put(h)
return val, err
}
func (g *Geometry) Prepared() *PreparedGeometry {
h := g.hp.Get()
p := g.g.Prepared(h)
g.hp.Put(h)
prep := &PreparedGeometry{
hp: g.hp,
p: p,
parent: g,
}
runtime.SetFinalizer(prep, func(prep1 *PreparedGeometry) {
h := prep1.hp.Get()
prep1.p.Destroy(h)
prep1.hp.Put(h)
})
return prep
}
// Unsafe access to the geos geometry. This geometry is still subject to GC.
// For internal use only.
func (g *Geometry) UnsafeToGeos() *geos.Geometry {
return g.g
}
func (g *Geometry) Type() GeometryType {
h := g.hp.Get()
id := g.g.TypeId(h)
g.hp.Put(h)
switch id {
case geos.POINT:
return POINT
case geos.LINESTRING:
return LINESTRING
case geos.LINEARRING:
return LINEARRING
case geos.POLYGON:
return POLYGON
case geos.MULTIPOINT:
return MULTIPOINT
case geos.MULTILINESTRING:
return MULTILINESTRING
case geos.MULTIPOLYGON:
return MULTIPOLYGON
case geos.GEOMETRYCOLLECTION:
return GEOMETRYCOLLECTION
default:
panic(fmt.Sprintf("Unknown GEOS geometry id: %d", id))
}
}
func (g *Geometry) Area() float64 {
h := g.hp.Get()
defer g.hp.Put(h)
return g.g.Area(h)
}
func (g *Geometry) ClipByRect(
xmin, ymin, xmax, ymax float64) (*Geometry, error) {
h := g.hp.Get()
geom, err := g.g.ClipByRect(h, xmin, ymin, xmax, ymax)
g.hp.Put(h)
return newGeometryOrError(g.hp, geom, err)
}
func (g *Geometry) Buffer(width float64, quadsegs int) (*Geometry, error) {
h := g.hp.Get()
geom, err := g.g.Buffer(h, width, quadsegs)
g.hp.Put(h)
return newGeometryOrError(g.hp, geom, err)
}
func (g *Geometry) Intersection(o toGeos) (*Geometry, error) {
return g.binaryOperation(g.g.Intersection, o)
}
func (g *Geometry) Union(o toGeos) (*Geometry, error) {
return g.binaryOperation(g.g.Union, o)
}
func (g *Geometry) Envelope() (*Geometry, error) {
return g.unaryOperation(g.g.Envelope)
}
func (g *Geometry) Intersects(o toGeos) (bool, error) {
return g.binaryPredicate(g.g.Intersects, o)
}
func (g *Geometry) Contains(o toGeos) (bool, error) {
return g.binaryPredicate(g.g.Contains, o)
}
func (g *Geometry) Disjoint(o toGeos) (bool, error) {
return g.binaryPredicate(g.g.Disjoint, o)
}
func (g *Geometry) Touches(o toGeos) (bool, error) {
return g.binaryPredicate(g.g.Touches, o)
}
func (g *Geometry) Overlaps(o toGeos) (bool, error) {
return g.binaryPredicate(g.g.Overlaps, o)
}
func (g *Geometry) Within(o toGeos) (bool, error) {
return g.binaryPredicate(g.g.Within, o)
}
func (g *Geometry) IsEmpty() (bool, error) {
return g.unaryPredicate(g.g.IsEmpty)
}
// Minimum and maximum X and Y bounds for a geometry. This will cast the
// envelope to a Polygon, get the envelope, then inspect the shell directly.
// We can inspect exact array positions because, according to the geos docs,
// "When Envelope objects are created or initialized, the supplies extent values
// are automatically sorted into the correct order."
func (g *Geometry) Bounds() (c0 Coord, c1 Coord, err error) {
var (
env *Geometry
shell []Coord
)
// TODO: envelope, polygon, and shell are all going to get and return a handle
// to the pool. We should just re-use the same handle.
if env, err = g.Envelope(); err != nil {
return
}
if shell, err = env.Polygon().Shell(); err != nil {
return
}
return shell[0], shell[2], nil
}
// Coerces to Point. Panics if the underlying type doesnt match.
func (g *Geometry) Point() Point {
if id := g.Type(); id != POINT {
panic(fmt.Sprintf("Cannot cast geom with type %d to POINT (%d)", id, POINT))
}
return newPoint(g)
}
// Coerces to LinearRing. Panics if the underlying type doesnt match.
func (g *Geometry) LinearRing() LinearRing {
if id := g.Type(); id != LINEARRING {
panic(fmt.Sprintf(
"Cannot cast geom with type %d to LINEARRING (%d)", id, LINEARRING))
}
return newLinearRing(g)
}
// Coerces to Polygon. Panics if the underlying type doesnt match.
func (g *Geometry) Polygon() Polygon {
if id := g.Type(); id != POLYGON {
panic(fmt.Sprintf(
"Cannot cast geom with type %d to POLYGON (%d)", id, POLYGON))
}
return newPolygon(g)
}
// Number of geometries in this geometry. Non-collection types will always
// return 1.
func (g *Geometry) NumGeometries() (int, error) {
h := g.hp.Get()
defer g.hp.Put(h)
n, err := g.g.NumGeometries(h)
if err != nil {
return 0, err
}
return n, nil
}
// Gets the nth geometry in this presumed collection. Non-collect geometries
// will only accept 0. Collection retains ownership of the underlying geometry.
// A GC-managed clone is returned.
func (g *Geometry) GeometryN(n int) (*Geometry, error) {
h := g.hp.Get()
defer g.hp.Put(h)
owned, err := g.g.GeometryN(h, n)
if err != nil {
return nil, err
}
cloned := owned.Clone(h)
return newGeometry(g.hp, cloned), nil
}
// Slice of all geometries of this geometry. If this is not a geometry
// collection, it will have one element
func (g *Geometry) Geometries() ([]*Geometry, error) {
n, err := g.NumGeometries()
if err != nil {
return nil, err
}
geoms := make([]*Geometry, n)
for i := 0; i < n; i++ {
if geom, err := g.GeometryN(i); err != nil {
return nil, err
} else {
geoms[i] = geom
}
}
return geoms, nil
}
// Expensive to create, but faster predicate operations.
type PreparedGeometry struct {
hp handle.GeosHandleProvider
p *geos.PreparedGeometry
// Hold on to the parent geom so it doesnt get GCed
parent *Geometry
// PreparedGeoms are not thread safe. Lock operations.
sync.Mutex
}
func (pg *PreparedGeometry) Covers(o toGeos) (bool, error) {
h := pg.hp.Get()
defer pg.hp.Put(h)
pg.Lock()
defer pg.Unlock()
val, err := pg.p.Covers(h, o.UnsafeToGeos())
runtime.KeepAlive(o)
runtime.KeepAlive(pg.parent)
return val, err
}
// Point
type Point struct {
*Geometry
}
func newPoint(g *Geometry) Point {
return Point{g}
}
func (p Point) Coord() (Coord, error) {
h := p.hp.Get()
defer p.hp.Put(h)
cs, err := p.g.CoordSeq(h)
if err != nil {
return Coord{}, err
}
return Coord{cs.X(h, 0), cs.Y(h, 0)}, nil
}
// LinearRing
type LinearRing struct {
*Geometry
}
func newLinearRing(g *Geometry) LinearRing {
return LinearRing{g}
}
func (lr LinearRing) Coords() (coords []Coord, err error) {
h := lr.hp.Get()
defer lr.hp.Put(h)
cs, err := lr.g.CoordSeq(h)
if err != nil {
return
}
for i := uint(0); i < cs.Size(h); i++ {
coords = append(coords, Coord{cs.X(h, i), cs.Y(h, i)})
}
return
}
// Polygon
type Polygon struct {
*Geometry
}
func newPolygon(g *Geometry) Polygon {
return Polygon{g}
}
func (p Polygon) Shell() (coords []Coord, err error) {
h := p.hp.Get()
defer p.hp.Put(h)
geosShell, err := p.g.ExteriorRing(h)
if err != nil {
return
}
cs, err := geosShell.CoordSeq(h)
if err != nil {
return
}
// It would be nice if this could just return a LinearRing, but the polygon
// owns the shell so we would have to clone it to be safe.
for i := uint(0); i < cs.Size(h); i++ {
coords = append(coords, Coord{cs.X(h, i), cs.Y(h, i)})
}
return
}
func (p Polygon) Holes() (coords [][]Coord, err error) {
h := p.hp.Get()
defer p.hp.Put(h)
numRings, err := p.g.NumInteriorRings(h)
if err != nil {
return
}
for ringIdx := 0; ringIdx < numRings; ringIdx++ {
var geosRing *geos.Geometry
geosRing, err = p.g.InteriorRingN(h, ringIdx)
if err != nil {
return
}
var cs *geos.CoordSeq
cs, err = geosRing.CoordSeq(h)
if err != nil {
return
}
var ringCoords []Coord
for i := uint(0); i < cs.Size(h); i++ {
ringCoords = append(ringCoords, Coord{cs.X(h, i), cs.Y(h, i)})
}
coords = append(coords, ringCoords)
}
return
}
// Multipolygon
type Multipolygon struct {
*Geometry
}
func newMultiPolygon(g *Geometry) Multipolygon {
return Multipolygon{g}
}