forked from omniscale/imposm3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geos.go
364 lines (318 loc) · 7.09 KB
/
geos.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
package geos
/*
#cgo LDFLAGS: -lgeos_c
#include "geos_c.h"
#include <stdlib.h>
extern void goLogString(char *msg);
extern void debug_wrap(const char *fmt, ...);
extern GEOSContextHandle_t initGEOS_r_debug();
extern void initGEOS_debug();
*/
import "C"
import (
"errors"
"runtime"
"unsafe"
"github.com/omniscale/imposm3/logging"
)
var log = logging.NewLogger("GEOS")
//export goLogString
func goLogString(msg *C.char) {
log.Printf(C.GoString(msg))
}
type Geos struct {
v C.GEOSContextHandle_t
srid int
wkbwriter *C.GEOSWKBWriter
}
type Geom struct {
v *C.GEOSGeometry
}
type CreateError string
type Error string
func (e Error) Error() string {
return string(e)
}
func (e CreateError) Error() string {
return string(e)
}
func NewGeos() *Geos {
geos := &Geos{}
geos.v = C.initGEOS_r_debug()
return geos
}
func (this *Geos) Finish() {
if this.v != nil {
C.finishGEOS_r(this.v)
this.v = nil
}
}
func init() {
/*
Init global GEOS handle for non _r calls.
In theory we need to always call the _r functions
with a thread/goroutine-local GEOS instance to get thread
safe behaviour. Some functions don't need a GEOS instance though
and we can make use of that e.g. to call GEOSGeom_destroy in
finalizer.
*/
C.initGEOS_debug()
}
func (this *Geos) Destroy(geom *Geom) {
runtime.SetFinalizer(geom, nil)
if geom.v != nil {
C.GEOSGeom_destroy_r(this.v, geom.v)
geom.v = nil
} else {
log.Printf("double free?")
}
}
func destroyGeom(geom *Geom) {
C.GEOSGeom_destroy(geom.v)
}
func (this *Geos) DestroyLater(geom *Geom) {
runtime.SetFinalizer(geom, destroyGeom)
}
func (this *Geos) Clone(geom *Geom) *Geom {
if geom == nil || geom.v == nil {
return nil
}
result := C.GEOSGeom_clone_r(this.v, geom.v)
if result == nil {
return nil
}
return &Geom{result}
}
func (this *Geos) SetHandleSrid(srid int) {
this.srid = srid
}
func (this *Geos) NumGeoms(geom *Geom) int32 {
count := int32(C.GEOSGetNumGeometries_r(this.v, geom.v))
return count
}
func (this *Geos) NumCoordinates(geom *Geom) int32 {
count := int32(C.GEOSGetNumCoordinates_r(this.v, geom.v))
return count
}
func (this *Geos) Geoms(geom *Geom) []*Geom {
count := this.NumGeoms(geom)
var result []*Geom
for i := 0; int32(i) < count; i++ {
part := C.GEOSGetGeometryN_r(this.v, geom.v, C.int(i))
if part == nil {
return nil
}
result = append(result, &Geom{part})
}
return result
}
func (this *Geos) ExteriorRing(geom *Geom) *Geom {
ring := C.GEOSGetExteriorRing_r(this.v, geom.v)
if ring == nil {
return nil
}
return &Geom{ring}
}
func (this *Geos) BoundsPolygon(bounds Bounds) *Geom {
coordSeq, err := this.CreateCoordSeq(5, 2)
if err != nil {
return nil
}
// coordSeq inherited by LineString, no destroy
if err := coordSeq.SetXY(this, 0, bounds.MinX, bounds.MinY); err != nil {
return nil
}
if err := coordSeq.SetXY(this, 1, bounds.MaxX, bounds.MinY); err != nil {
return nil
}
if err := coordSeq.SetXY(this, 2, bounds.MaxX, bounds.MaxY); err != nil {
return nil
}
if err := coordSeq.SetXY(this, 3, bounds.MinX, bounds.MaxY); err != nil {
return nil
}
if err := coordSeq.SetXY(this, 4, bounds.MinX, bounds.MinY); err != nil {
return nil
}
geom, err := coordSeq.AsLinearRing(this)
if err != nil {
return nil
}
// geom inherited by Polygon, no destroy
geom = this.Polygon(geom, nil)
return geom
}
func (this *Geos) Point(x, y float64) *Geom {
coordSeq, err := this.CreateCoordSeq(1, 2)
if err != nil {
return nil
}
// coordSeq inherited by LineString
coordSeq.SetXY(this, 0, x, y)
geom, err := coordSeq.AsPoint(this)
if err != nil {
return nil
}
return geom
}
func (this *Geos) Polygon(exterior *Geom, interiors []*Geom) *Geom {
if len(interiors) == 0 {
geom := C.GEOSGeom_createPolygon_r(this.v, exterior.v, nil, C.uint(0))
if geom == nil {
return nil
}
err := C.GEOSNormalize_r(this.v, geom)
if err != 0 {
C.GEOSGeom_destroy(geom)
return nil
}
return &Geom{geom}
}
interiorPtr := make([]*C.GEOSGeometry, len(interiors))
for i, geom := range interiors {
interiorPtr[i] = geom.v
}
geom := C.GEOSGeom_createPolygon_r(this.v, exterior.v, &interiorPtr[0], C.uint(len(interiors)))
if geom == nil {
return nil
}
err := C.GEOSNormalize_r(this.v, geom)
if err != 0 {
C.GEOSGeom_destroy(geom)
return nil
}
return &Geom{geom}
}
func (this *Geos) MultiPolygon(polygons []*Geom) *Geom {
if len(polygons) == 0 {
return nil
}
polygonPtr := make([]*C.GEOSGeometry, len(polygons))
for i, geom := range polygons {
polygonPtr[i] = geom.v
}
geom := C.GEOSGeom_createCollection_r(this.v, C.GEOS_MULTIPOLYGON, &polygonPtr[0], C.uint(len(polygons)))
if geom == nil {
return nil
}
return &Geom{geom}
}
func (this *Geos) MultiLineString(lines []*Geom) *Geom {
if len(lines) == 0 {
return nil
}
linePtr := make([]*C.GEOSGeometry, len(lines))
for i, geom := range lines {
linePtr[i] = geom.v
}
geom := C.GEOSGeom_createCollection_r(this.v, C.GEOS_MULTILINESTRING, &linePtr[0], C.uint(len(lines)))
if geom == nil {
return nil
}
return &Geom{geom}
}
func (this *Geos) IsValid(geom *Geom) bool {
if C.GEOSisValid_r(this.v, geom.v) == 1 {
return true
}
return false
}
func (this *Geos) IsSimple(geom *Geom) bool {
if C.GEOSisSimple_r(this.v, geom.v) == 1 {
return true
}
return false
}
func (this *Geos) IsEmpty(geom *Geom) bool {
if C.GEOSisEmpty_r(this.v, geom.v) == 1 {
return true
}
return false
}
func (this *Geos) Type(geom *Geom) string {
geomType := C.GEOSGeomType_r(this.v, geom.v)
if geomType == nil {
return "Unknown"
}
defer C.free(unsafe.Pointer(geomType))
return C.GoString(geomType)
}
func (this *Geos) Equals(a, b *Geom) bool {
result := C.GEOSEquals_r(this.v, a.v, b.v)
if result == 1 {
return true
}
return false
}
func (g *Geos) MakeValid(geom *Geom) (*Geom, error) {
if g.IsValid(geom) {
return geom, nil
}
fixed := g.Buffer(geom, 0)
if fixed == nil {
return nil, errors.New("Error while fixing geom with buffer(0)")
}
g.Destroy(geom)
return fixed, nil
}
func (this *Geom) Area() float64 {
var area C.double
if ret := C.GEOSArea(this.v, &area); ret == 1 {
return float64(area)
} else {
return 0
}
}
func (this *Geom) Length() float64 {
var length C.double
if ret := C.GEOSLength(this.v, &length); ret == 1 {
return float64(length)
} else {
return 0
}
}
type Bounds struct {
MinX float64
MinY float64
MaxX float64
MaxY float64
}
var NilBounds = Bounds{1e20, 1e20, -1e20, -1e20}
func (this *Geom) Bounds() Bounds {
geom := C.GEOSEnvelope(this.v)
if geom == nil {
return NilBounds
}
defer C.GEOSGeom_destroy(geom)
extRing := C.GEOSGetExteriorRing(geom)
if extRing == nil {
return NilBounds
}
cs := C.GEOSGeom_getCoordSeq(extRing)
var csLen C.uint
C.GEOSCoordSeq_getSize(cs, &csLen)
minx := 1.e+20
maxx := -1e+20
miny := 1.e+20
maxy := -1e+20
var temp C.double
for i := 0; i < int(csLen); i++ {
C.GEOSCoordSeq_getX(cs, C.uint(i), &temp)
x := float64(temp)
if x < minx {
minx = x
}
if x > maxx {
maxx = x
}
C.GEOSCoordSeq_getY(cs, C.uint(i), &temp)
y := float64(temp)
if y < miny {
miny = y
}
if y > maxy {
maxy = y
}
}
return Bounds{minx, miny, maxx, maxy}
}