-
Notifications
You must be signed in to change notification settings - Fork 560
/
pygeom.c
469 lines (410 loc) · 11.7 KB
/
pygeom.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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#define PY_SSIZE_T_CLEAN
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include "pygeom.h"
#include <Python.h>
#include <structmember.h>
#include "geos.h"
/* This initializes a global geometry type registry */
PyObject* geom_registry[1] = {NULL};
/* Initializes a new geometry object */
PyObject* GeometryObject_FromGEOS(GEOSGeometry* ptr, GEOSContextHandle_t ctx) {
if (ptr == NULL) {
Py_INCREF(Py_None);
return Py_None;
}
int type_id = GEOSGeomTypeId_r(ctx, ptr);
if (type_id == -1) {
return NULL;
}
PyObject* type_obj = PyList_GET_ITEM(geom_registry[0], type_id);
if (type_obj == NULL) {
return NULL;
}
if (!PyType_Check(type_obj)) {
PyErr_Format(PyExc_RuntimeError, "Invalid registry value");
return NULL;
}
PyTypeObject* type = (PyTypeObject*)type_obj;
GeometryObject* self = (GeometryObject*)type->tp_alloc(type, 0);
if (self == NULL) {
return NULL;
} else {
self->ptr = ptr;
self->ptr_prepared = NULL;
self->weakreflist = (PyObject *)NULL;
return (PyObject*)self;
}
}
static void GeometryObject_dealloc(GeometryObject* self) {
if (self->weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject *)self);
}
if (self->ptr != NULL) {
// not using GEOS_INIT, but using global context instead
GEOSContextHandle_t ctx = geos_context[0];
GEOSGeom_destroy_r(ctx, self->ptr);
if (self->ptr_prepared != NULL) {
GEOSPreparedGeom_destroy_r(ctx, self->ptr_prepared);
}
}
Py_TYPE(self)->tp_free((PyObject*)self);
}
static PyMemberDef GeometryObject_members[] = {
{"_geom", T_PYSSIZET, offsetof(GeometryObject, ptr), READONLY,
"pointer to GEOSGeometry"},
{"_geom_prepared", T_PYSSIZET, offsetof(GeometryObject, ptr_prepared), READONLY,
"pointer to PreparedGEOSGeometry"},
{NULL} /* Sentinel */
};
static PyObject* GeometryObject_ToWKT(GeometryObject* obj) {
char* wkt;
PyObject* result;
GEOSGeometry* geom = obj->ptr;
if (geom == NULL) {
Py_INCREF(Py_None);
return Py_None;
}
GEOS_INIT;
#if GEOS_SINCE_3_9_0
errstate = wkt_empty_3d_geometry(ctx, geom, &wkt);
if (errstate != PGERR_SUCCESS) {
goto finish;
}
if (wkt != NULL) {
result = PyUnicode_FromString(wkt);
goto finish;
}
#else
// Before GEOS 3.9.0, there was as segfault on e.g. MULTIPOINT (1 1, EMPTY)
errstate = check_to_wkt_compatible(ctx, geom);
if (errstate != PGERR_SUCCESS) {
goto finish;
}
#endif
GEOSWKTWriter* writer = GEOSWKTWriter_create_r(ctx);
if (writer == NULL) {
errstate = PGERR_GEOS_EXCEPTION;
goto finish;
}
char trim = 1;
int precision = 3;
int dimension = 3;
int use_old_3d = 0;
GEOSWKTWriter_setRoundingPrecision_r(ctx, writer, precision);
GEOSWKTWriter_setTrim_r(ctx, writer, trim);
GEOSWKTWriter_setOutputDimension_r(ctx, writer, dimension);
GEOSWKTWriter_setOld3D_r(ctx, writer, use_old_3d);
// Check if the above functions caused a GEOS exception
if (last_error[0] != 0) {
errstate = PGERR_GEOS_EXCEPTION;
goto finish;
}
wkt = GEOSWKTWriter_write_r(ctx, writer, geom);
result = PyUnicode_FromString(wkt);
GEOSFree_r(ctx, wkt);
GEOSWKTWriter_destroy_r(ctx, writer);
finish:
GEOS_FINISH;
if (errstate == PGERR_SUCCESS) {
return result;
} else {
return NULL;
}
}
static PyObject* GeometryObject_ToWKB(GeometryObject* obj) {
unsigned char* wkb = NULL;
char has_empty = 0;
size_t size;
PyObject* result = NULL;
GEOSGeometry* geom = NULL;
GEOSWKBWriter* writer = NULL;
if (obj->ptr == NULL) {
Py_INCREF(Py_None);
return Py_None;
}
GEOS_INIT;
#if !GEOS_SINCE_3_9_0
// WKB Does not allow empty points in GEOS < 3.9.
// We check for that and patch the POINT EMPTY if necessary
has_empty = has_point_empty(ctx, obj->ptr);
if (has_empty == 2) {
errstate = PGERR_GEOS_EXCEPTION;
goto finish;
}
if (has_empty == 1) {
geom = point_empty_to_nan_all_geoms(ctx, obj->ptr);
} else {
geom = obj->ptr;
}
#else
geom = obj->ptr;
#endif // !GEOS_SINCE_3_9_0
/* Create the WKB writer */
writer = GEOSWKBWriter_create_r(ctx);
if (writer == NULL) {
errstate = PGERR_GEOS_EXCEPTION;
goto finish;
}
// Allow 3D output and include SRID
GEOSWKBWriter_setOutputDimension_r(ctx, writer, 3);
GEOSWKBWriter_setIncludeSRID_r(ctx, writer, 1);
// Check if the above functions caused a GEOS exception
if (last_error[0] != 0) {
errstate = PGERR_GEOS_EXCEPTION;
goto finish;
}
wkb = GEOSWKBWriter_write_r(ctx, writer, geom, &size);
if (wkb == NULL) {
errstate = PGERR_GEOS_EXCEPTION;
goto finish;
}
result = PyBytes_FromStringAndSize((char*)wkb, size);
finish:
// Destroy the geom if it was patched (POINT EMPTY patch)
if (has_empty && (geom != NULL)) {
GEOSGeom_destroy_r(ctx, geom);
}
if (writer != NULL) {
GEOSWKBWriter_destroy_r(ctx, writer);
}
if (wkb != NULL) {
GEOSFree_r(ctx, wkb);
}
GEOS_FINISH;
return result;
}
static PyObject* GeometryObject_repr(GeometryObject* self) {
PyObject *result, *wkt, *truncated;
wkt = GeometryObject_ToWKT(self);
// we never want a repr() to fail; that can be very confusing
if (wkt == NULL) {
PyErr_Clear();
return PyUnicode_FromString("<shapely.Geometry Exception in WKT writer>");
}
// the total length is limited to 80 characters
if (PyUnicode_GET_LENGTH(wkt) > 62) {
truncated = PyUnicode_Substring(wkt, 0, 59);
result = PyUnicode_FromFormat("<shapely.Geometry %U...>", truncated);
Py_XDECREF(truncated);
} else {
result = PyUnicode_FromFormat("<shapely.Geometry %U>", wkt);
}
Py_XDECREF(wkt);
return result;
}
static PyObject* GeometryObject_str(GeometryObject* self) {
return GeometryObject_ToWKT(self);
}
/* For lookups in sets / dicts.
* Python should be told how to generate a hash from the Geometry object. */
static Py_hash_t GeometryObject_hash(GeometryObject* self) {
PyObject* wkb = NULL;
Py_hash_t x;
if (self->ptr == NULL) {
return -1;
}
// Transform to a WKB (PyBytes object)
wkb = GeometryObject_ToWKB(self);
if (wkb == NULL) {
return -1;
}
// Use the python built-in method to hash the PyBytes object
x = wkb->ob_type->tp_hash(wkb);
if (x == -1) {
x = -2;
} else {
x ^= 374761393UL; // to make the result distinct from the actual WKB hash //
}
Py_DECREF(wkb);
return x;
}
static PyObject* GeometryObject_richcompare(GeometryObject* self, PyObject* other,
int op) {
PyObject* result = NULL;
GEOS_INIT;
if (Py_TYPE(self)->tp_richcompare != Py_TYPE(other)->tp_richcompare) {
result = Py_NotImplemented;
} else {
GeometryObject* other_geom = (GeometryObject*)other;
switch (op) {
case Py_LT:
result = Py_NotImplemented;
break;
case Py_LE:
result = Py_NotImplemented;
break;
case Py_EQ:
result =
GEOSEqualsExact_r(ctx, self->ptr, other_geom->ptr, 0) ? Py_True : Py_False;
break;
case Py_NE:
result =
GEOSEqualsExact_r(ctx, self->ptr, other_geom->ptr, 0) ? Py_False : Py_True;
break;
case Py_GT:
result = Py_NotImplemented;
break;
case Py_GE:
result = Py_NotImplemented;
break;
}
}
GEOS_FINISH;
Py_XINCREF(result);
return result;
}
static PyObject* GeometryObject_FromWKT(PyObject* value) {
PyObject* result = NULL;
const char* wkt;
GEOSGeometry* geom;
GEOSWKTReader* reader;
/* Cast the PyObject str to char* */
if (PyUnicode_Check(value)) {
wkt = PyUnicode_AsUTF8(value);
if (wkt == NULL) {
return NULL;
}
} else {
PyErr_Format(PyExc_TypeError, "Expected bytes, found %s", value->ob_type->tp_name);
return NULL;
}
GEOS_INIT;
reader = GEOSWKTReader_create_r(ctx);
if (reader == NULL) {
errstate = PGERR_GEOS_EXCEPTION;
goto finish;
}
geom = GEOSWKTReader_read_r(ctx, reader, wkt);
GEOSWKTReader_destroy_r(ctx, reader);
if (geom == NULL) {
errstate = PGERR_GEOS_EXCEPTION;
goto finish;
}
result = GeometryObject_FromGEOS(geom, ctx);
if (result == NULL) {
GEOSGeom_destroy_r(ctx, geom);
PyErr_Format(PyExc_RuntimeError, "Could not instantiate a new Geometry object");
}
finish:
GEOS_FINISH;
if (errstate == PGERR_SUCCESS) {
return result;
} else {
return NULL;
}
}
static PyObject* GeometryObject_FromWKB(PyObject* value) {
PyObject* result = NULL;
unsigned char* wkb = NULL;
Py_ssize_t size;
GEOSGeometry* geom = NULL;
GEOSWKBReader* reader = NULL;
/* Cast the PyObject bytes to char* */
if (!PyBytes_Check(value)) {
PyErr_Format(PyExc_TypeError, "Expected bytes, found %s", value->ob_type->tp_name);
return NULL;
}
size = PyBytes_Size(value);
wkb = (unsigned char*)PyBytes_AsString(value);
if (wkb == NULL) {
return NULL;
}
GEOS_INIT;
reader = GEOSWKBReader_create_r(ctx);
if (reader == NULL) {
errstate = PGERR_GEOS_EXCEPTION;
goto finish;
}
geom = GEOSWKBReader_read_r(ctx, reader, wkb, size);
if (geom == NULL) {
errstate = PGERR_GEOS_EXCEPTION;
goto finish;
}
result = GeometryObject_FromGEOS(geom, ctx);
if (result == NULL) {
GEOSGeom_destroy_r(ctx, geom);
PyErr_Format(PyExc_RuntimeError, "Could not instantiate a new Geometry object");
}
finish:
if (reader != NULL) {
GEOSWKBReader_destroy_r(ctx, reader);
}
GEOS_FINISH;
return result;
}
static PyMethodDef GeometryObject_methods[] = {
{NULL} /* Sentinel */
};
PyTypeObject GeometryType = {
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "shapely.lib.Geometry",
.tp_doc = "Geometry type",
.tp_basicsize = sizeof(GeometryObject),
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_dealloc = (destructor)GeometryObject_dealloc,
.tp_members = GeometryObject_members,
.tp_methods = GeometryObject_methods,
.tp_repr = (reprfunc)GeometryObject_repr,
.tp_hash = (hashfunc)GeometryObject_hash,
.tp_richcompare = (richcmpfunc)GeometryObject_richcompare,
.tp_weaklistoffset = offsetof(GeometryObject, weakreflist),
.tp_str = (reprfunc)GeometryObject_str,
};
/* Check if type `a` is a subclass of type `b`
(copied from cython generated code) */
int __Pyx_InBases(PyTypeObject* a, PyTypeObject* b) {
while (a) {
a = a->tp_base;
if (a == b) return 1;
}
return b == &PyBaseObject_Type;
}
/* Get a GEOSGeometry pointer from a GeometryObject, or NULL if the input is
Py_None. Returns 0 on error, 1 on success. */
char get_geom(GeometryObject* obj, GEOSGeometry** out) {
// Numpy treats NULL the same as Py_None
if ((obj == NULL) || ((PyObject*)obj == Py_None)) {
*out = NULL;
return 1;
}
PyTypeObject* type = ((PyObject*)obj)->ob_type;
if ((type != &GeometryType) && !(__Pyx_InBases(type, &GeometryType))) {
return 0;
} else {
*out = obj->ptr;
return 1;
}
}
/* Get a GEOSGeometry AND GEOSPreparedGeometry pointer from a GeometryObject,
or NULL if the input is Py_None. Returns 0 on error, 1 on success. */
char get_geom_with_prepared(GeometryObject* obj, GEOSGeometry** out,
GEOSPreparedGeometry** prep) {
if (!get_geom(obj, out)) {
// It is not a GeometryObject / None: Error
return 0;
}
if (*out != NULL) {
// Only if it is not None, fill the prepared geometry
*prep = obj->ptr_prepared;
} else {
*prep = NULL;
}
return 1;
}
int init_geom_type(PyObject* m) {
Py_ssize_t i;
PyObject* type;
if (PyType_Ready(&GeometryType) < 0) {
return -1;
}
type = (PyObject*)&GeometryType;
Py_INCREF(type);
PyModule_AddObject(m, "Geometry", type);
geom_registry[0] = PyList_New(8);
for (i = 0; i < 8; i++) {
Py_INCREF(type);
PyList_SET_ITEM(geom_registry[0], i, type);
}
PyModule_AddObject(m, "registry", geom_registry[0]);
return 0;
}