forked from twpayne/go-geos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
216 lines (199 loc) · 6.19 KB
/
Copy pathcontext.go
File metadata and controls
216 lines (199 loc) · 6.19 KB
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
package geos
// #include <stdlib.h>
// #include "go-geos.h"
import "C"
import (
"runtime"
"runtime/cgo"
"sync"
"sync/atomic"
"unsafe"
)
// A Context is a context.
type Context struct {
mutex sync.Mutex
cHandle C.GEOSContextHandle_t
refCount *atomic.Int64
ewkbWithSRIDWriter func() *WKBWriter
geoJSONReader func() *GeoJSONReader
geoJSONWriter func() *GeoJSONWriter
wkbWriter func() *WKBWriter
wkbReader func() *WKBReader
wktReader func() *WKTReader
wktWriter func() *WKTWriter
err error
errPHandle cgo.Handle
}
// NewContext returns a new Context.
func NewContext() *Context {
cHandle := C.GEOS_init_r()
var refCount atomic.Int64
c := &Context{
cHandle: cHandle,
refCount: &refCount,
}
c.ref()
runtime.AddCleanup(c, func(cHandle C.GEOSContextHandle_t) {
// Inline unref here so that the cleanup function does not hold a
// reference to c.
if refCount.Add(-1) == 0 {
C.finishGEOS_r(cHandle)
}
}, cHandle)
c.ewkbWithSRIDWriter = sync.OnceValue(func() *WKBWriter {
return c.NewWKBWriter(
WithWKBWriterFlavor(WKBFlavorExtended),
WithWKBWriterIncludeSRID(true),
)
})
c.geoJSONReader = sync.OnceValue(func() *GeoJSONReader {
return c.NewGeoJSONReader()
})
c.geoJSONWriter = sync.OnceValue(func() *GeoJSONWriter {
return c.NewGeoJSONWriter()
})
c.wkbReader = sync.OnceValue(func() *WKBReader {
return c.NewWKBReader()
})
c.wkbWriter = sync.OnceValue(func() *WKBWriter {
return c.NewWKBWriter()
})
c.wktReader = sync.OnceValue(func() *WKTReader {
return c.NewWKTReader()
})
c.wktWriter = sync.OnceValue(func() *WKTWriter {
return c.NewWKTWriter()
})
c.errPHandle = cgo.NewHandle(&c.err)
runtime.AddCleanup(c, cgo.Handle.Delete, c.errPHandle)
// FIXME golangci-lint complains about the following line saying: Error:
// dupSubExpr: suspicious identical LHS and RHS for `==` operator (gocritic)
// As the line does not contain an `==` operator, disable gocritic on this
// line.
//nolint:gocritic
C.GEOSContext_setErrorMessageHandler_r(c.cHandle, C.GEOSMessageHandler_r(C.c_errorMessageHandler), unsafe.Pointer(&c.errPHandle))
return c
}
// Clone clones g into c.
func (c *Context) Clone(g *Geom) *Geom {
if g.context == c {
return g.Clone()
}
// FIXME use a more intelligent method than a WKB roundtrip (although a WKB
// roundtrip might actually be quite fast if the cgo overhead is
// significant)
clone, err := c.NewGeomFromWKB(g.ToEWKBWithSRID())
if err != nil {
panic(err)
}
return clone
}
// NewGeomFromGeoJSON returns a new geometry in JSON format from json.
func (c *Context) NewGeomFromGeoJSON(geoJSON string) (*Geom, error) {
return c.geoJSONReader().ReadGeometry(geoJSON)
}
// NewGeomFromWKB parses a geometry in WKB format from wkb.
func (c *Context) NewGeomFromWKB(wkb []byte) (*Geom, error) {
return c.wkbReader().Read(wkb)
}
// NewGeomFromWKT parses a geometry in WKT format from wkt.
func (c *Context) NewGeomFromWKT(wkt string) (*Geom, error) {
return c.wktReader().Read(wkt)
}
// OrientationIndex returns the orientation index from A to B and then to P.
func (c *Context) OrientationIndex(ax, ay, bx, by, px, py float64) int {
c.mutex.Lock()
defer c.mutex.Unlock()
return int(C.GEOSOrientationIndex_r(c.cHandle, C.double(ax), C.double(ay), C.double(bx), C.double(by), C.double(px), C.double(py)))
}
// Polygonize returns a set of geometries which contains linework that
// represents the edges of a planar graph.
func (c *Context) Polygonize(geoms []*Geom) *Geom {
c.mutex.Lock()
defer c.mutex.Unlock()
cGeoms, unlockFunc := c.cGeomsLocked(geoms)
defer unlockFunc()
return c.newNonNilGeom(C.GEOSPolygonize_r(c.cHandle, cGeoms, C.uint(len(geoms))), nil)
}
// PolygonizeValid returns a set of polygons which contains linework that
// represents the edges of a planar graph.
func (c *Context) PolygonizeValid(geoms []*Geom) *Geom {
c.mutex.Lock()
defer c.mutex.Unlock()
cGeoms, unlockFunc := c.cGeomsLocked(geoms)
defer unlockFunc()
return c.newNonNilGeom(C.GEOSPolygonize_valid_r(c.cHandle, cGeoms, C.uint(len(geoms))), nil)
}
// RelatePatternMatch returns if two DE9IM patterns are consistent.
func (c *Context) RelatePatternMatch(mat, pat string) bool {
matCStr := C.CString(mat)
defer C.free(unsafe.Pointer(matCStr))
patCStr := C.CString(pat)
defer C.free(unsafe.Pointer(patCStr))
c.mutex.Lock()
defer c.mutex.Unlock()
switch C.GEOSRelatePatternMatch_r(c.cHandle, matCStr, patCStr) {
case 0:
return false
case 1:
return true
default:
panic(c.err)
}
}
// SegmentIntersection returns the coordinate where two lines intersect.
func (c *Context) SegmentIntersection(ax0, ay0, ax1, ay1, bx0, by0, bx1, by1 float64) (x, y float64, intersection bool) {
c.mutex.Lock()
defer c.mutex.Unlock()
var cx, cy float64
switch C.GEOSSegmentIntersection_r(c.cHandle,
C.double(ax0), C.double(ay0), C.double(ax1), C.double(ay1),
C.double(bx0), C.double(by0), C.double(bx1), C.double(by1),
(*C.double)(&cx), (*C.double)(&cy)) {
case 1:
return cx, cy, true
case -1:
return 0, 0, false
default:
panic(c.err)
}
}
func (c *Context) cGeomsLocked(geoms []*Geom) (**C.struct_GEOSGeom_t, func()) {
if len(geoms) == 0 {
return nil, func() {}
}
uniqueContexts := map[*Context]struct{}{c: {}}
var extraContexts []*Context
cGeoms := make([]*C.struct_GEOSGeom_t, len(geoms))
for i := range cGeoms {
geom := geoms[i]
if _, ok := uniqueContexts[geom.context]; !ok {
geom.context.mutex.Lock()
uniqueContexts[geom.context] = struct{}{}
extraContexts = append(extraContexts, geom.context)
}
cGeoms[i] = geom.cGeom
}
return &cGeoms[0], func() {
for i := len(extraContexts) - 1; i >= 0; i-- {
extraContexts[i].mutex.Unlock()
}
}
}
// ref increases c's reference count by 1.
func (c *Context) ref() {
c.refCount.Add(1)
}
// unref decreases c's reference count by 1 and finishes c if its reference
// count becomes zero.
func (c *Context) unref() {
if c.refCount.Add(-1) == 0 {
C.finishGEOS_r(c.cHandle)
}
}
//export go_errorMessageHandler
func go_errorMessageHandler(message *C.char, userdata unsafe.Pointer) {
errPHandle := (*cgo.Handle)(userdata)
errP := errPHandle.Value().(*error) //nolint:forcetypeassert,revive
*errP = Error(C.GoString(message))
}