-
Notifications
You must be signed in to change notification settings - Fork 0
/
tgo.go
295 lines (237 loc) · 7.19 KB
/
tgo.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
package tgo
/*
#cgo LDFLAGS: -lm
#include "tg.h"
#include <stdlib.h>
#include <stdio.h>
#define MAX_RESPONSE_PER_PIP 8
struct pip_iter_properties_ctx {
struct tg_point pip_point;
char *properties[MAX_RESPONSE_PER_PIP];
uint8_t count;
};
struct pip_iter_one_ctx {
struct tg_point pip_point;
struct tg_geom *geom;
};
bool pip_iter_properties(const struct tg_geom *child, int index, void *udata) {
struct pip_iter_properties_ctx *ctx = udata;
if (tg_geom_intersects_xy(child, ctx->pip_point.x, ctx->pip_point.y)) {
ctx->properties[ctx->count] = (char*)tg_geom_extra_json(child);
//printf("%d %s\n", index, ctx->properties[index]);
ctx->count++;
if (ctx->count >= MAX_RESPONSE_PER_PIP) {
return false;
}
}
return true;
}
bool pip_iter_one(const struct tg_geom *child, int index, void *udata) {
struct pip_iter_one_ctx *ctx = udata;
if (tg_geom_intersects_xy(child, ctx->pip_point.x, ctx->pip_point.y)) {
ctx->geom = (struct tg_geom *)child;
return true;
}
return true;
}
*/
import "C"
import (
"errors"
"fmt"
"runtime"
"unsafe"
)
type Geom struct {
cg *C.struct_tg_geom
}
type GeomType uint8
const (
Point GeomType = iota + 1 // Point.
LineString // LineString.
Polygon // Polygon.
MultiPoint // MultiPoint, collection of points.
MultiLineString // MultiLineString, collection of linestrings.
MultiPolygon // MultiPolygon, collection of polygons.
GeometryCollection // GeometryCollection, collection of geometries.
)
type IndexType uint32
const (
None IndexType = iota + 1 // no indexing available, or disabled
Natural // indexing with natural ring order, for rings/lines
YStripes // indexing using segment striping, rings only
)
// UnmarshalWKT parses geometries from a WKT representation.
// Using the Natural indexation.
func UnmarshalWKT(data string) (*Geom, error) {
return UnmarshalWKTAndIndex(data, Natural)
}
// UnmarshalWKTAndIndex parses geometries from a WKT representation,
// and sets the indexation type.
func UnmarshalWKTAndIndex(data string, idxt IndexType) (*Geom, error) {
cd := C.CString(data)
defer C.free(unsafe.Pointer(cd))
cg := C.tg_parse_wkt_ix(cd, C.enum_tg_index(idxt))
cerr := C.tg_geom_error(cg)
if cerr != nil {
return nil, fmt.Errorf("%s", C.GoString(cerr))
}
g := &Geom{cg}
runtime.SetFinalizer(g, (*Geom).free)
return g, nil
}
// UnmarshalWKB parses geometries from a WKB representation.
// Using the Natural indexation.
func UnmarshalWKB(data []byte) (*Geom, error) {
return UnmarshalWKBAndIndex(data, Natural)
}
// UnmarshalWKBAndIndex parses geometries from a WKB representation,
// and sets the indexation type.
func UnmarshalWKBAndIndex(data []byte, idxt IndexType) (*Geom, error) {
if len(data) == 0 {
return nil, errors.New("empty data")
}
cdata := C.CBytes(data)
defer C.free(cdata)
cg := C.tg_parse_wkb_ix((*C.uchar)(cdata), C.size_t(len(data)), C.enum_tg_index(idxt))
cerr := C.tg_geom_error(cg)
if cerr != nil {
return nil, fmt.Errorf("%s", C.GoString(cerr))
}
g := &Geom{cg}
runtime.SetFinalizer(g, (*Geom).free)
return g, nil
}
// UnmarshalGeoJSON parses geometries from a GeoJSON representation.
// Using the Natural indexation.
func UnmarshalGeoJSON(data []byte) (*Geom, error) {
return UnmarshalGeoJSONAndIndex(data, Natural)
}
// UnmarshalGeoJSONAndIndex parses geometries from a GeoJSON representation,
// and sets the indexation type.
func UnmarshalGeoJSONAndIndex(data []byte, idxt IndexType) (*Geom, error) {
if len(data) == 0 {
return nil, errors.New("empty data")
}
cdata := C.CBytes(data)
defer C.free(cdata)
cg := C.tg_parse_geojson_ix((*C.char)(cdata), C.enum_tg_index(idxt))
cerr := C.tg_geom_error(cg)
if cerr != nil {
return nil, fmt.Errorf("%s", C.GoString(cerr))
}
g := &Geom{cg}
runtime.SetFinalizer(g, (*Geom).free)
return g, nil
}
// Equals returns true if the two geometries are equal
func Equals(g1, g2 *Geom) bool {
return bool(C.tg_geom_equals(g1.cg, g2.cg))
}
func Intersects(g1, g2 *Geom) bool {
return bool(C.tg_geom_intersects(g1.cg, g2.cg))
}
func Disjoint(g1, g2 *Geom) bool {
return bool(C.tg_geom_disjoint(g1.cg, g2.cg))
}
func Contains(g1, g2 *Geom) bool {
return bool(C.tg_geom_contains(g1.cg, g2.cg))
}
func Within(g1, g2 *Geom) bool {
return bool(C.tg_geom_within(g1.cg, g2.cg))
}
func Covers(g1, g2 *Geom) bool {
return bool(C.tg_geom_covers(g1.cg, g2.cg))
}
func CoveredBy(g1, g2 *Geom) bool {
return bool(C.tg_geom_coveredby(g1.cg, g2.cg))
}
func Touches(g1, g2 *Geom) bool {
return bool(C.tg_geom_touches(g1.cg, g2.cg))
}
// MemSize returns the allocation size of the geometry in the C world.
func (g *Geom) MemSize() int {
return int(C.tg_geom_memsize(g.cg))
}
// Properties returns a string that represents any extra JSON from a parsed GeoJSON
// geometry. Such as the "id" or "properties" fields.
func (g *Geom) Properties() string {
return C.GoString(C.tg_geom_extra_json(g.cg))
}
// StabOne performs a Point in Polygon query using the point (x,y)
// and returns the first encountered child geometry
func (g *Geom) StabOne(x, y float64) *Geom {
// creating a point
p := C.struct_tg_point{C.double(x), C.double(y)}
// creating a context for the iterator
ctx := C.struct_pip_iter_one_ctx{pip_point: p}
// calling the C func tg_geom_search
// void tg_geom_search(const struct tg_geom *geom, struct tg_rect rect,
// bool (*iter)(const struct tg_geom *geom, int index, void *udata),
// void *udata);
C.tg_geom_search(g.cg, C.tg_point_rect(p), (*[0]byte)(C.pip_iter_one), (unsafe.Pointer(&ctx)))
if ctx.geom != nil {
return &Geom{cg: ctx.geom}
}
return nil
}
// func (g *Geom) RingSearch() {
// r := C.tg_ring_new(points, len(coords)/2)
// C.tg_ring_ring_search(g.cg,r, bool(*iter)(struct tg_segment aseg, int aidx, struct tg_segment bseg, int bidx, void *udata), void *udata);
// C.tg_ring_free(r)
// }
// AsPoly returns a Poly of the geometry, returns false if not applicable.
func (g *Geom) AsPoly() (*Poly, bool) {
cp := C.tg_geom_poly(g.cg)
if cp == nil {
return nil, false
}
p := &Poly{cp: cp}
return p, true
}
// AsMultiPoly returns a MultiPoly of the geometry, returns false if not applicable.
func (g *Geom) AsMultiPoly() (*MultiPoly, bool) {
if g.Type() != MultiPolygon {
return nil, false
}
count := C.tg_geom_num_polys(g.cg)
if count < 1 {
return nil, false
}
mp := &MultiPoly{cg: g.cg}
return mp, true
}
// AsText returns geometry as WKT
func (g *Geom) AsText() string {
if g.cg == nil {
return ""
}
csz := C.tg_geom_wkt(g.cg, nil, C.size_t(0))
cwkt := C.malloc(csz + 1)
C.tg_geom_wkt(g.cg, (*C.char)(cwkt), csz+1)
defer C.free(cwkt)
return C.GoString((*C.char)(cwkt))
}
// Type returns the geometry type.
func (g *Geom) Type() GeomType {
switch C.tg_geom_typeof(g.cg) {
case C.TG_POINT:
return Point
case C.TG_LINESTRING:
return LineString
case C.TG_POLYGON:
return Polygon
case C.TG_MULTIPOINT:
return MultiPoint
case C.TG_MULTILINESTRING:
return MultiLineString
case C.TG_MULTIPOLYGON:
return MultiPolygon
case C.TG_GEOMETRYCOLLECTION:
return GeometryCollection
}
return 0
}
func (g *Geom) free() {
C.tg_geom_free(g.cg)
}