|
#include "shapefil.h" |
|
1 |
#include "pyshapelib_common.h" |
#include "pyshapelib_common.h" |
|
#include "pyshapelib_api.h" |
|
2 |
|
|
3 |
/* --- SHPObject ----------------------------------------------------------------------------------------------------- */ |
/* --- SHPObject ----------------------------------------------------------------------------------------------------- */ |
4 |
|
|
7 |
PyObject_HEAD |
PyObject_HEAD |
8 |
SHPObject* shpObject; |
SHPObject* shpObject; |
9 |
} |
} |
10 |
PySHPObject; |
SHPObjectObject; |
11 |
|
|
12 |
static PyObject* PySHPObject_new(PyTypeObject* type, PyObject* args, PyObject* kwds) |
/* allocator |
13 |
|
*/ |
14 |
|
static PyObject* shpobject_new(PyTypeObject* type, PyObject* args, PyObject* kwds) |
15 |
{ |
{ |
16 |
PySHPObject* self; |
SHPObjectObject* self; |
17 |
self = (PySHPObject*) type->tp_alloc(type, 0); |
self = (SHPObjectObject*) type->tp_alloc(type, 0); |
18 |
self->shpObject = NULL; |
self->shpObject = NULL; |
19 |
return (PyObject*) self; |
return (PyObject*) self; |
20 |
} |
} |
21 |
|
|
22 |
static void PySHPObject_dealloc(PySHPObject* self) |
/* deallocator |
23 |
|
*/ |
24 |
|
static void shpobject_dealloc(SHPObjectObject* self) |
25 |
{ |
{ |
26 |
SHPDestroyObject(self->shpObject); |
SHPDestroyObject(self->shpObject); |
27 |
self->shpObject = NULL; |
self->shpObject = NULL; |
34 |
* be NULL. For the meaning of the part-types and their default value |
* be NULL. For the meaning of the part-types and their default value |
35 |
* see the Shaplib documentation. |
* see the Shaplib documentation. |
36 |
*/ |
*/ |
37 |
static int PySHPObject_init(PySHPObject* self, PyObject* args, PyObject* kwds) |
static int shpobject_init(SHPObjectObject* self, PyObject* args, PyObject* kwds) |
38 |
{ |
{ |
39 |
int type; |
int type; |
40 |
int id; |
int id; |
74 |
} |
} |
75 |
|
|
76 |
/* parts and part_types have to have the same lengths */ |
/* parts and part_types have to have the same lengths */ |
77 |
|
if (part_type_list == Py_None) |
78 |
|
{ |
79 |
|
Py_DECREF(part_type_list); |
80 |
|
part_type_list = NULL; |
81 |
|
} |
82 |
if (part_type_list) |
if (part_type_list) |
83 |
{ |
{ |
84 |
if (!PySequence_Check(parts)) |
if (!PySequence_Check(parts)) |
179 |
* Return the extents as a tuple of two 4-element lists with the min. |
* Return the extents as a tuple of two 4-element lists with the min. |
180 |
* and max. values of x, y, z, m. |
* and max. values of x, y, z, m. |
181 |
*/ |
*/ |
182 |
static PyObject* PySHPObject_extents(PySHPObject* self) |
static PyObject* shpobject_extents(SHPObjectObject* self) |
183 |
{ |
{ |
184 |
SHPObject* object = self->shpObject; |
SHPObject* object = self->shpObject; |
185 |
return Py_BuildValue("(dddd)(dddd)", |
return Py_BuildValue("(dddd)(dddd)", |
197 |
|
|
198 |
static PyObject* build_vertex_list(SHPObject *object, int index, int length); |
static PyObject* build_vertex_list(SHPObject *object, int index, int length); |
199 |
|
|
200 |
static PyObject* PySHPObject_vertices(PySHPObject* self) |
static PyObject* shpobject_vertices(SHPObjectObject* self) |
201 |
{ |
{ |
202 |
PyObject *result = NULL; |
PyObject *result = NULL; |
203 |
PyObject *part = NULL; |
PyObject *part = NULL; |
210 |
/* A multipart shape. Usual for SHPT_ARC and SHPT_POLYGON */ |
/* A multipart shape. Usual for SHPT_ARC and SHPT_POLYGON */ |
211 |
|
|
212 |
result = PyList_New(object->nParts); |
result = PyList_New(object->nParts); |
213 |
if (!result) |
if (!result) |
214 |
return NULL; |
return NULL; |
215 |
|
|
216 |
for (part_idx = 0, vertex_idx = 0; part_idx < object->nParts; |
for (part_idx = 0, vertex_idx = 0; part_idx < object->nParts; part_idx++) |
217 |
part_idx++) |
{ |
218 |
{ |
if (part_idx < object->nParts - 1) |
219 |
if (part_idx < object->nParts - 1) |
length = (object->panPartStart[part_idx + 1] |
220 |
length = (object->panPartStart[part_idx + 1] |
- object->panPartStart[part_idx]); |
221 |
- object->panPartStart[part_idx]); |
else |
222 |
else |
length = object->nVertices - object->panPartStart[part_idx]; |
223 |
length = object->nVertices - object->panPartStart[part_idx]; |
|
224 |
|
part = build_vertex_list(object, vertex_idx, length); |
225 |
part = build_vertex_list(object, vertex_idx, length); |
if (!part) |
226 |
if (!part) |
goto fail; |
|
goto fail; |
|
227 |
|
|
228 |
if (PyList_SetItem(result, part_idx, part) < 0) |
if (PyList_SetItem(result, part_idx, part) < 0) |
229 |
goto fail; |
goto fail; |
230 |
|
|
231 |
vertex_idx += length; |
vertex_idx += length; |
232 |
} |
} |
233 |
} |
} |
234 |
else |
else |
235 |
{ |
{ |
236 |
/* only one part. usual for SHPT_POINT */ |
/* only one part. usual for SHPT_POINT */ |
237 |
result = build_vertex_list(object, 0, object->nVertices); |
result = build_vertex_list(object, 0, object->nVertices); |
238 |
} |
} |
239 |
|
|
240 |
return result; |
return result; |
258 |
|
|
259 |
list = PyList_New(length); |
list = PyList_New(length); |
260 |
if (!list) |
if (!list) |
261 |
return NULL; |
return NULL; |
262 |
|
|
263 |
for (i = 0; i < length; i++, index++) |
for (i = 0; i < length; i++, index++) |
264 |
{ |
{ |
265 |
vertex = Py_BuildValue("dd", object->padfX[index], |
vertex = Py_BuildValue("dd", object->padfX[index], |
266 |
object->padfY[index]); |
object->padfY[index]); |
267 |
if (!vertex) |
if (!vertex) |
268 |
goto fail; |
goto fail; |
269 |
if (PyList_SetItem(list, i, vertex) < 0) |
if (PyList_SetItem(list, i, vertex) < 0) |
270 |
goto fail; |
goto fail; |
271 |
} |
} |
272 |
|
|
273 |
return list; |
return list; |
278 |
return NULL; |
return NULL; |
279 |
} |
} |
280 |
|
|
281 |
static PyObject* PySHPObject_type(PySHPObject* self, void* closure) |
|
282 |
|
|
283 |
|
static PyObject* shpobject_part_types(SHPObjectObject* self) |
284 |
|
{ |
285 |
|
int i; |
286 |
|
PyObject* result = NULL; |
287 |
|
SHPObject* object = self->shpObject; |
288 |
|
|
289 |
|
if (object->nParts == 0 || object->panPartType == 0) |
290 |
|
{ |
291 |
|
Py_RETURN_NONE; |
292 |
|
} |
293 |
|
|
294 |
|
result = PyTuple_New(object->nParts); |
295 |
|
if (!result) return NULL; |
296 |
|
|
297 |
|
for (i = 0; i < object->nParts; ++i) |
298 |
|
{ |
299 |
|
/* PyTuple_SetItem steals a reference */ |
300 |
|
PyObject* part_type = PyInt_FromLong((long)object->panPartType[i]); |
301 |
|
if (!part_type || PyTuple_SetItem(result, i, part_type) < 0) goto fail; |
302 |
|
} |
303 |
|
return result; |
304 |
|
|
305 |
|
fail: |
306 |
|
Py_DECREF(result); |
307 |
|
return NULL; |
308 |
|
} |
309 |
|
|
310 |
|
|
311 |
|
|
312 |
|
static PyObject* shpobject_type(SHPObjectObject* self, void* closure) |
313 |
{ |
{ |
314 |
return PyInt_FromLong(self->shpObject->nSHPType); |
return PyInt_FromLong(self->shpObject->nSHPType); |
315 |
} |
} |
316 |
|
|
317 |
static PyObject* PySHPObject_id(PySHPObject* self, void* closure) |
|
318 |
|
|
319 |
|
static PyObject* shpobject_id(SHPObjectObject* self, void* closure) |
320 |
{ |
{ |
321 |
return PyInt_FromLong(self->shpObject->nShapeId); |
return PyInt_FromLong(self->shpObject->nShapeId); |
322 |
} |
} |
323 |
|
|
324 |
static PyMethodDef PySHPObject_methods[] = |
|
325 |
|
|
326 |
|
/* return a string that can be feeded to eval() to reconstruct the object, |
327 |
|
* assuming a proper context |
328 |
|
*/ |
329 |
|
static PyObject* shpobject_repr(SHPObjectObject* self) |
330 |
|
{ |
331 |
|
PyObject* format = NULL; |
332 |
|
PyObject* args = NULL; |
333 |
|
PyObject* result = NULL; |
334 |
|
|
335 |
|
format = PyString_FromString("shapelib.SHPObject(%i, %i, %s, %s)"); |
336 |
|
if (!format) return NULL; |
337 |
|
|
338 |
|
args = Py_BuildValue("iiNN", |
339 |
|
self->shpObject->nSHPType, |
340 |
|
self->shpObject->nShapeId, |
341 |
|
shpobject_vertices(self), |
342 |
|
shpobject_part_types(self)); |
343 |
|
if (!args) |
344 |
|
{ |
345 |
|
Py_DECREF(format); |
346 |
|
return NULL; |
347 |
|
} |
348 |
|
|
349 |
|
result = PyString_Format(format, args); |
350 |
|
Py_DECREF(args); |
351 |
|
Py_DECREF(format); |
352 |
|
return result; |
353 |
|
} |
354 |
|
|
355 |
|
|
356 |
|
|
357 |
|
static struct PyMethodDef shpobject_methods[] = |
358 |
{ |
{ |
359 |
{"extents", (PyCFunction)PySHPObject_extents, METH_NOARGS, NULL}, |
{"extents", (PyCFunction)shpobject_extents, METH_NOARGS, NULL}, |
360 |
{"vertices", (PyCFunction)PySHPObject_vertices, METH_NOARGS, NULL}, |
{"vertices", (PyCFunction)shpobject_vertices, METH_NOARGS, NULL}, |
361 |
|
{"part_types", (PyCFunction)shpobject_part_types, METH_NOARGS, NULL}, |
362 |
{NULL} |
{NULL} |
363 |
}; |
}; |
364 |
|
|
365 |
static PyGetSetDef PySHPObject_getsetters[] = |
static struct PyGetSetDef shpobject_getsetters[] = |
366 |
{ |
{ |
367 |
{"type", (getter)PySHPObject_type, NULL, NULL }, |
{"type", (getter)shpobject_type, NULL, NULL }, |
368 |
{"id", (getter)PySHPObject_id, NULL, NULL }, |
{"id", (getter)shpobject_id, NULL, NULL }, |
369 |
{NULL} |
{NULL} |
370 |
}; |
}; |
371 |
|
|
372 |
static PyTypeObject PySHPObjectType = PYSHAPELIB_DEFINE_TYPE(PySHPObject, "shapelib.SHPObject", 0); |
static PyTypeObject SHPObjectType = PYSHAPELIB_DEFINE_TYPE(SHPObjectObject, shpobject, "shapelib.SHPObject", 0); |
373 |
|
|
374 |
|
|
375 |
/* --- ShapeFile ----------------------------------------------------------------------------------------------------- */ |
/* --- ShapeFile ----------------------------------------------------------------------------------------------------- */ |
379 |
PyObject_HEAD |
PyObject_HEAD |
380 |
SHPHandle handle; |
SHPHandle handle; |
381 |
} |
} |
382 |
PyShapeFile; |
ShapeFileObject; |
383 |
|
|
384 |
static PyObject* PyShapeFile_new(PyTypeObject* type, PyObject* args, PyObject* kwds) |
/* allocator |
385 |
|
*/ |
386 |
|
static PyObject* shapefile_new(PyTypeObject* type, PyObject* args, PyObject* kwds) |
387 |
{ |
{ |
388 |
PyShapeFile* self; |
ShapeFileObject* self; |
389 |
self = (PyShapeFile*) type->tp_alloc(type, 0); |
self = (ShapeFileObject*) type->tp_alloc(type, 0); |
390 |
self->handle = NULL; |
self->handle = NULL; |
391 |
return (PyObject*) self; |
return (PyObject*) self; |
392 |
} |
} |
393 |
|
|
394 |
static int PyShapeFile_init(PyShapeFile* self, PyObject* args, PyObject* kwds) |
/* destructor |
395 |
|
*/ |
396 |
|
static void shapefile_dealloc(ShapeFileObject* self) |
397 |
|
{ |
398 |
|
SHPClose(self->handle); |
399 |
|
self->ob_type->tp_free((PyObject*)self); |
400 |
|
} |
401 |
|
|
402 |
|
/* constructor |
403 |
|
*/ |
404 |
|
static int shapefile_init(ShapeFileObject* self, PyObject* args, PyObject* kwds) |
405 |
{ |
{ |
406 |
char* file; |
char* file; |
407 |
char* mode = "rb"; |
char* mode = "rb"; |
416 |
return self->handle ? 0 : -1; |
return self->handle ? 0 : -1; |
417 |
} |
} |
418 |
|
|
419 |
static PyObject* PyShapeFile_close(PyShapeFile* self) |
static PyObject* shapefile_close(ShapeFileObject* self) |
420 |
{ |
{ |
421 |
SHPClose(self->handle); |
SHPClose(self->handle); |
422 |
self->handle = NULL; |
self->handle = NULL; |
423 |
Py_RETURN_NONE; |
Py_RETURN_NONE; |
424 |
} |
} |
425 |
|
|
426 |
static void PyShapeFile_dealloc(PyShapeFile* self) |
static PyObject* shapefile_info(ShapeFileObject* self) |
|
{ |
|
|
PyShapeFile_close(self); |
|
|
self->ob_type->tp_free((PyObject*)self); |
|
|
} |
|
|
|
|
|
static PyObject* PyShapeFile_info(PyShapeFile* self) |
|
427 |
{ |
{ |
428 |
SHPHandle handle = self->handle; |
SHPHandle handle = self->handle; |
429 |
return Py_BuildValue("ii(dddd)(dddd)", |
return Py_BuildValue("ii(dddd)(dddd)", |
432 |
handle->adBoundsMax[0], handle->adBoundsMax[1], handle->adBoundsMax[2], handle->adBoundsMax[3]); |
handle->adBoundsMax[0], handle->adBoundsMax[1], handle->adBoundsMax[2], handle->adBoundsMax[3]); |
433 |
} |
} |
434 |
|
|
435 |
static PyObject* PyShapeFile_read_object(PyShapeFile* self, PyObject* args) |
static PyObject* shapefile_read_object(ShapeFileObject* self, PyObject* args) |
436 |
{ |
{ |
437 |
int index; |
int index; |
438 |
SHPObject* object; |
SHPObject* object; |
439 |
PySHPObject* result; |
SHPObjectObject* result; |
440 |
|
|
441 |
if (!PyArg_ParseTuple(args, "i", &index)) return NULL; |
if (!PyArg_ParseTuple(args, "i", &index)) return NULL; |
442 |
|
|
447 |
return NULL; |
return NULL; |
448 |
} |
} |
449 |
|
|
450 |
result = PyObject_New(PySHPObject, &PySHPObjectType); |
result = PyObject_New(SHPObjectObject, &SHPObjectType); |
451 |
if (!result) |
if (!result) |
452 |
{ |
{ |
453 |
return PyErr_NoMemory(); |
return PyErr_NoMemory(); |
457 |
return (PyObject*) result; |
return (PyObject*) result; |
458 |
} |
} |
459 |
|
|
460 |
static PyObject* PyShapeFile_write_object(PyShapeFile* self, PyObject* args) |
static PyObject* shapefile_write_object(ShapeFileObject* self, PyObject* args) |
461 |
{ |
{ |
462 |
int index, result; |
int index, result; |
463 |
PyObject* object; |
PyObject* object; |
464 |
|
|
465 |
if (!PyArg_ParseTuple(args, "iO", &index, &object)) return NULL; |
if (!PyArg_ParseTuple(args, "iO", &index, &object)) return NULL; |
466 |
|
|
467 |
if (!PyObject_IsInstance(object, (PyObject*)&PySHPObjectType)) |
if (!PyObject_IsInstance(object, (PyObject*)&SHPObjectType)) |
468 |
{ |
{ |
469 |
PyErr_SetString(PyExc_TypeError, "object is not a SHPObject"); |
PyErr_SetString(PyExc_TypeError, "object is not a SHPObject"); |
470 |
return NULL; |
return NULL; |
471 |
} |
} |
472 |
|
|
473 |
result = SHPWriteObject(self->handle, index, ((PySHPObject*)object)->shpObject); |
result = SHPWriteObject(self->handle, index, ((SHPObjectObject*)object)->shpObject); |
474 |
if (result < 0) |
if (result < 0) |
475 |
{ |
{ |
476 |
PyErr_SetString(PyExc_RuntimeError, "failed to write object"); |
PyErr_SetString(PyExc_RuntimeError, "failed to write object"); |
479 |
return PyInt_FromLong((long)result); |
return PyInt_FromLong((long)result); |
480 |
} |
} |
481 |
|
|
482 |
static PyObject* PyShapeFile_cobject(PyShapeFile* self) |
static PyObject* shapefile_cobject(ShapeFileObject* self) |
483 |
{ |
{ |
484 |
return PyCObject_FromVoidPtr(self->handle, NULL); |
return PyCObject_FromVoidPtr(self->handle, NULL); |
485 |
} |
} |
486 |
|
|
487 |
static PyMethodDef PyShapeFile_methods[] = |
static PyObject* shapefile_repr(ShapeFileObject* self) |
488 |
|
{ |
489 |
|
/* TODO: it would be nice to do something like "shapelib.ShapeFile(filename, mode)" instead */ |
490 |
|
return PyString_FromFormat("<shapelib.ShapeFile object at %p>", self->handle); |
491 |
|
} |
492 |
|
|
493 |
|
static struct PyMethodDef shapefile_methods[] = |
494 |
{ |
{ |
495 |
{"close", (PyCFunction)PyShapeFile_close, METH_NOARGS, "close the shape file" }, |
{"close", (PyCFunction)shapefile_close, METH_NOARGS, "close the shape file" }, |
496 |
{"info", (PyCFunction)PyShapeFile_info, METH_NOARGS, |
{"info", (PyCFunction)shapefile_info, METH_NOARGS, |
497 |
"Return a tuple (NUM_SHAPES, TYPE, MIN, MAX) where NUM_SHAPES is the number of shapes in the file, TYPE is the " |
"Return a tuple (NUM_SHAPES, TYPE, MIN, MAX) where NUM_SHAPES is the number of shapes in the file, TYPE is the " |
498 |
"shape type and MIN and MAX are 4-element tuples with the min. and max. values of the data." }, |
"shape type and MIN and MAX are 4-element tuples with the min. and max. values of the data." }, |
499 |
{"read_object", (PyCFunction)PyShapeFile_read_object, METH_VARARGS, "Return object number i" }, |
{"read_object", (PyCFunction)shapefile_read_object, METH_VARARGS, "Return object number i" }, |
500 |
{"write_object", (PyCFunction)PyShapeFile_write_object, METH_VARARGS, "Write an object"}, |
{"write_object", (PyCFunction)shapefile_write_object, METH_VARARGS, "Write an object"}, |
501 |
{"cobject", (PyCFunction)PyShapeFile_cobject, METH_NOARGS, "Return the shapelib SHPHandle as a Python CObject"}, |
{"cobject", (PyCFunction)shapefile_cobject, METH_NOARGS, "Return the shapelib SHPHandle as a Python CObject"}, |
502 |
{NULL} |
{NULL} |
503 |
}; |
}; |
504 |
|
|
505 |
static PyGetSetDef PyShapeFile_getsetters[] = |
static struct PyGetSetDef shapefile_getsetters[] = |
506 |
{ |
{ |
507 |
{NULL} |
{NULL} |
508 |
}; |
}; |
509 |
|
|
510 |
static PyTypeObject PyShapeFileType = PYSHAPELIB_DEFINE_TYPE(PyShapeFile, "shapelib.ShapeFile", 0); |
static PyTypeObject ShapeFileType = PYSHAPELIB_DEFINE_TYPE(ShapeFileObject, shapefile, "shapelib.ShapeFile", 0); |
511 |
|
|
512 |
/* --- shapelib ------------------------------------------------------------------------------------------------------ */ |
/* --- shapelib ------------------------------------------------------------------------------------------------------ */ |
513 |
|
|
514 |
static PyObject* shapelib_open(PyObject* module, PyObject* args) |
static PyObject* shapelib_open(PyObject* module, PyObject* args) |
515 |
{ |
{ |
516 |
return PyObject_CallObject((PyObject*)&PyShapeFileType, args); |
return PyObject_CallObject((PyObject*)&ShapeFileType, args); |
517 |
} |
} |
518 |
|
|
519 |
static PyObject* shapelib_create(PyObject* module, PyObject* args) |
static PyObject* shapelib_create(PyObject* module, PyObject* args) |
520 |
{ |
{ |
521 |
char* file; |
char* file; |
522 |
int type; |
int type; |
523 |
PyShapeFile* result; |
ShapeFileObject* result; |
524 |
|
|
525 |
if (!PyArg_ParseTuple(args, "si", &file, &type)) return NULL; |
if (!PyArg_ParseTuple(args, "si", &file, &type)) return NULL; |
526 |
|
|
527 |
result = PyObject_New(PyShapeFile, &PyShapeFileType); |
result = PyObject_New(ShapeFileObject, &ShapeFileType); |
528 |
if (!result) |
if (!result) |
529 |
{ |
{ |
530 |
return PyErr_NoMemory(); |
return PyErr_NoMemory(); |
569 |
return PyString_FromString(SHPPartTypeName(type)); |
return PyString_FromString(SHPPartTypeName(type)); |
570 |
} |
} |
571 |
|
|
572 |
static PyMethodDef shapelib_methods[] = |
static struct PyMethodDef shapelib_methods[] = |
573 |
{ |
{ |
574 |
{"open", (PyCFunction)shapelib_open, METH_VARARGS, "open a ShapeFile" }, |
{"open", (PyCFunction)shapelib_open, METH_VARARGS, "open a ShapeFile" }, |
575 |
{"create", (PyCFunction)shapelib_create, METH_VARARGS, "create a ShapeFile" }, |
{"create", (PyCFunction)shapelib_create, METH_VARARGS, "create a ShapeFile" }, |
584 |
PyObject* module = Py_InitModule("shapelib", shapelib_methods); |
PyObject* module = Py_InitModule("shapelib", shapelib_methods); |
585 |
if (!module) return; |
if (!module) return; |
586 |
|
|
587 |
PYSHAPELIB_ADD_TYPE(PySHPObjectType, "SHPObject"); |
PYSHAPELIB_ADD_TYPE(SHPObjectType, "SHPObject"); |
588 |
PYSHAPELIB_ADD_TYPE(PyShapeFileType, "ShapeFile"); |
PYSHAPELIB_ADD_TYPE(ShapeFileType, "ShapeFile"); |
589 |
|
|
590 |
PYSHAPELIB_ADD_CONSTANT(SHPT_NULL); |
PYSHAPELIB_ADD_CONSTANT(SHPT_NULL); |
591 |
PYSHAPELIB_ADD_CONSTANT(SHPT_POINT); |
PYSHAPELIB_ADD_CONSTANT(SHPT_POINT); |