-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgo-geos.c
247 lines (222 loc) · 7.4 KB
/
go-geos.c
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
#include "go-geos.h"
// Using cgo to call C functions from Go has a high overhead. The functions in
// this file batch multiple calls to GEOS in C (rather than Go) to increase
// performance.
enum { bounds_MinX, bounds_MinY, bounds_MaxX, bounds_MaxY };
uintptr_t c_GEOSGeom_getUserData_r(GEOSContextHandle_t handle,
const GEOSGeometry *g) {
void *userdata = GEOSGeom_getUserData_r(handle, g);
return (uintptr_t)userdata;
}
void c_GEOSGeom_setUserData_r(GEOSContextHandle_t handle, GEOSGeometry *g,
uintptr_t userdata) {
GEOSGeom_setUserData_r(handle, g, (void *)userdata);
}
// c_GEOSGeomBounds_r extends bounds to include g.
void c_GEOSGeomBounds_r(GEOSContextHandle_t handle, const GEOSGeometry *g,
double *minX, double *minY, double *maxX,
double *maxY) {
if (GEOSisEmpty_r(handle, g)) {
return;
}
switch (GEOSGeomTypeId_r(handle, g)) {
case GEOS_POINT: {
double x;
GEOSGeomGetX_r(handle, g, &x);
if (x < *minX) {
*minX = x;
}
if (x > *maxX) {
*maxX = x;
}
double y;
GEOSGeomGetY_r(handle, g, &y);
if (y < *minY) {
*minY = y;
}
if (y > *maxY) {
*maxY = y;
}
} break;
case GEOS_LINESTRING:
// fallthrough
case GEOS_LINEARRING: {
const GEOSCoordSequence *s = GEOSGeom_getCoordSeq_r(handle, g);
unsigned int size;
GEOSCoordSeq_getSize_r(handle, s, &size);
for (int i = 0; i < size; ++i) {
double x;
GEOSCoordSeq_getX_r(handle, s, i, &x);
if (x < *minX) {
*minX = x;
}
if (x > *maxX) {
*maxX = x;
}
double y;
GEOSCoordSeq_getY_r(handle, s, i, &y);
if (y < *minY) {
*minY = y;
}
if (y > *maxY) {
*maxY = y;
}
}
} break;
case GEOS_POLYGON:
c_GEOSGeomBounds_r(handle, GEOSGetExteriorRing_r(handle, g), minX, minY,
maxX, maxY);
for (int i = 0, n = GEOSGetNumInteriorRings_r(handle, g); i < n; ++i) {
c_GEOSGeomBounds_r(handle, GEOSGetInteriorRingN_r(handle, g, i), minX,
minY, maxX, maxY);
}
break;
case GEOS_MULTIPOINT:
// fallthrough
case GEOS_MULTILINESTRING:
// fallthrough
case GEOS_MULTIPOLYGON:
// fallthrough
case GEOS_GEOMETRYCOLLECTION:
for (int i = 0, n = GEOSGetNumGeometries_r(handle, g); i < n; ++i) {
c_GEOSGeomBounds_r(handle, GEOSGetGeometryN_r(handle, g, i), minX, minY,
maxX, maxY);
}
break;
}
}
// c_GEOSGeomGetInfo_r returns information about g. It returns 0 on any
// exception, 1 otherwise.
int c_GEOSGeomGetInfo_r(GEOSContextHandle_t handle, const GEOSGeometry *g,
int *typeID, int *numGeometries, int *numPoints,
int *numInteriorRings) {
*typeID = GEOSGeomTypeId_r(handle, g);
if (*typeID == -1) {
return 0;
}
*numGeometries = GEOSGetNumGeometries_r(handle, g);
if (*numGeometries == -1) {
return 0;
}
switch (*typeID) {
case GEOS_LINESTRING:
// fallthrough
case GEOS_LINEARRING:
*numPoints = GEOSGeomGetNumPoints_r(handle, g);
if (*numPoints == -1) {
return 0;
}
break;
case GEOS_POLYGON:
*numInteriorRings = GEOSGetNumInteriorRings_r(handle, g);
if (*numInteriorRings == -1) {
return 0;
}
break;
}
return 1;
}
void c_errorMessageHandler(const char *message, void *userdata) {
void go_errorMessageHandler(const char *, void *);
go_errorMessageHandler(message, userdata);
}
// c_newGEOSGeomFromBounds_r returns a new GEOSGeom representing bounds. It
// returns NULL on any exception.
GEOSGeometry *c_newGEOSGeomFromBounds_r(GEOSContextHandle_t handle, int *typeID,
double minX, double minY, double maxX,
double maxY) {
if (minX > maxX || minY > maxY) {
*typeID = GEOS_POINT;
return GEOSGeom_createEmptyPoint_r(handle);
}
if (minX == maxX && minY == maxY) {
GEOSCoordSequence *s = GEOSCoordSeq_create_r(handle, 1, 2);
if (s == NULL) {
return NULL;
}
if (GEOSCoordSeq_setX_r(handle, s, 0, minX) == 0 ||
GEOSCoordSeq_setY_r(handle, s, 0, minY) == 0) {
GEOSCoordSeq_destroy_r(handle, s);
return NULL;
}
GEOSGeometry *g = GEOSGeom_createPoint_r(handle, s);
if (g == NULL) {
GEOSCoordSeq_destroy_r(handle, s);
return NULL;
}
*typeID = GEOS_POINT;
return g;
}
const double flatCoords[10] = {minX, minY, maxX, minY, maxX,
maxY, minX, maxY, minX, minY};
GEOSCoordSequence *s =
GEOSCoordSeq_copyFromBuffer_r(handle, flatCoords, 5, 0, 0);
if (s == NULL) {
return NULL;
}
GEOSGeometry *shell = GEOSGeom_createLinearRing_r(handle, s);
if (shell == NULL) {
GEOSCoordSeq_destroy_r(handle, s);
return NULL;
}
GEOSGeometry *polygon = GEOSGeom_createPolygon_r(handle, shell, NULL, 0);
if (polygon == NULL) {
GEOSGeom_destroy_r(handle, shell);
return NULL;
}
*typeID = GEOS_POLYGON;
return polygon;
}
void c_GEOSSTRtree_query_callback(void *elem, void *userdata) {
void go_GEOSSTRtree_query_callback(void *, void *);
go_GEOSSTRtree_query_callback(elem, userdata);
}
int c_GEOSSTRtree_distance_callback(const void *item1, const void *item2,
double *distance, void *userdata) {
int go_GEOSSTRtree_distance_callback(const void *, const void *, double *,
void *);
return go_GEOSSTRtree_distance_callback(item1, item2, distance, userdata);
}
GEOSGeometry *c_GEOSMakeValidWithParams_r(GEOSContextHandle_t handle,
const GEOSGeometry *g,
enum GEOSMakeValidMethods method,
int keepCollapsed) {
GEOSGeometry *res;
GEOSMakeValidParams *par;
par = GEOSMakeValidParams_create_r(handle);
GEOSMakeValidParams_setKeepCollapsed_r(handle, par, keepCollapsed);
GEOSMakeValidParams_setMethod_r(handle, par, method);
res = GEOSMakeValidWithParams_r(handle, g, par);
GEOSMakeValidParams_destroy_r(handle, par);
return res;
}
#if GEOS_VERSION_MAJOR < 3 || \
(GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR < 11)
GEOSGeometry *GEOSConcaveHull_r(GEOSContextHandle_t handle,
const GEOSGeometry *g, double ratio,
unsigned int allowHoles) {
return NULL;
}
#endif
#if GEOS_VERSION_MAJOR < 3 || \
(GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR < 12)
GEOSGeometry *GEOSConcaveHullByLength_r(GEOSContextHandle_t handle,
const GEOSGeometry *g, double ratio,
unsigned int allowHoles) {
return NULL;
}
char GEOSPreparedContainsXY_r(GEOSContextHandle_t handle,
const GEOSPreparedGeometry *pg1, double x,
double y) {
return 0;
}
char GEOSPreparedIntersectsXY_r(GEOSContextHandle_t handle,
const GEOSPreparedGeometry *pg1, double x,
double y) {
return 0;
}
GEOSGeometry *GEOSDisjointSubsetUnion_r(GEOSContextHandle_t handle,
const GEOSGeometry *g) {
return NULL;
}
#endif