|
#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 |
/* allocator |
/* allocator |
13 |
*/ |
*/ |
14 |
static PyObject* PySHPObject_new(PyTypeObject* type, PyObject* args, PyObject* kwds) |
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 |
/* deallocator |
/* deallocator |
23 |
*/ |
*/ |
24 |
static void PySHPObject_dealloc(PySHPObject* self) |
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; |
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; |
280 |
|
|
281 |
|
|
282 |
|
|
283 |
static PyObject* PySHPObject_part_types(PySHPObject* self) |
static PyObject* shpobject_part_types(SHPObjectObject* self) |
284 |
{ |
{ |
285 |
int i; |
int i; |
286 |
PyObject* result = NULL; |
PyObject* result = NULL; |
309 |
|
|
310 |
|
|
311 |
|
|
312 |
static PyObject* PySHPObject_type(PySHPObject* self, void* closure) |
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 |
|
|
318 |
|
|
319 |
static PyObject* PySHPObject_id(PySHPObject* self, void* closure) |
static PyObject* shpobject_id(SHPObjectObject* self, void* closure) |
320 |
{ |
{ |
321 |
return PyInt_FromLong(self->shpObject->nShapeId); |
return PyInt_FromLong(self->shpObject->nShapeId); |
322 |
} |
} |
326 |
/* return a string that can be feeded to eval() to reconstruct the object, |
/* return a string that can be feeded to eval() to reconstruct the object, |
327 |
* assuming a proper context |
* assuming a proper context |
328 |
*/ |
*/ |
329 |
static PyObject* PySHPObject_repr(PySHPObject* self) |
static PyObject* shpobject_repr(SHPObjectObject* self) |
330 |
{ |
{ |
331 |
PyObject* format = NULL; |
PyObject* format = NULL; |
332 |
PyObject* args = NULL; |
PyObject* args = NULL; |
338 |
args = Py_BuildValue("iiNN", |
args = Py_BuildValue("iiNN", |
339 |
self->shpObject->nSHPType, |
self->shpObject->nSHPType, |
340 |
self->shpObject->nShapeId, |
self->shpObject->nShapeId, |
341 |
PySHPObject_vertices(self), |
shpobject_vertices(self), |
342 |
PySHPObject_part_types(self)); |
shpobject_part_types(self)); |
343 |
if (!args) |
if (!args) |
344 |
{ |
{ |
345 |
Py_DECREF(format); |
Py_DECREF(format); |
354 |
|
|
355 |
|
|
356 |
|
|
357 |
static PyMethodDef PySHPObject_methods[] = |
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)PySHPObject_part_types, METH_NOARGS, NULL}, |
{"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 |
/* allocator |
/* allocator |
385 |
*/ |
*/ |
386 |
static PyObject* PyShapeFile_new(PyTypeObject* type, PyObject* args, PyObject* kwds) |
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 |
|
/* 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 |
/* constructor |
403 |
*/ |
*/ |
404 |
static int PyShapeFile_init(PyShapeFile* self, PyObject* args, PyObject* kwds) |
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 |
/* destructor |
static PyObject* shapefile_info(ShapeFileObject* self) |
|
*/ |
|
|
static void PyShapeFile_dealloc(PyShapeFile* 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 PyObject* PyShapeFile_repr(PyShapeFile* self) |
static PyObject* shapefile_repr(ShapeFileObject* self) |
488 |
{ |
{ |
489 |
/* TODO: it would be nice to do something like "shapelib.ShapeFile(filename, mode)" instead */ |
/* 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); |
return PyString_FromFormat("<shapelib.ShapeFile object at %p>", self->handle); |
491 |
} |
} |
492 |
|
|
493 |
static PyMethodDef PyShapeFile_methods[] = |
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); |