|
#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) |
enum { |
13 |
|
vtXY, |
14 |
|
vtXYM, |
15 |
|
vtXYZM, |
16 |
|
vtInvalid |
17 |
|
} VertexType; |
18 |
|
|
19 |
|
int determine_vertex_type(int shape_type, int* has_z, int* has_m) |
20 |
|
{ |
21 |
|
switch (shape_type) |
22 |
|
{ |
23 |
|
case SHPT_POINT: |
24 |
|
case SHPT_ARC: |
25 |
|
case SHPT_POLYGON: |
26 |
|
case SHPT_MULTIPOINT: |
27 |
|
if (has_z) *has_z = 0; |
28 |
|
if (has_m) *has_m = 0; |
29 |
|
return vtXY; |
30 |
|
case SHPT_POINTM: |
31 |
|
case SHPT_ARCM: |
32 |
|
case SHPT_POLYGONM: |
33 |
|
case SHPT_MULTIPOINTM: |
34 |
|
if (has_z) *has_z = 0; |
35 |
|
if (has_m) *has_m = 1; |
36 |
|
case SHPT_POINTZ: |
37 |
|
case SHPT_ARCZ: |
38 |
|
case SHPT_POLYGONZ: |
39 |
|
case SHPT_MULTIPOINTZ: |
40 |
|
case SHPT_MULTIPATCH: |
41 |
|
if (has_z) *has_z = 1; |
42 |
|
if (has_m) *has_m = 1; |
43 |
|
return vtXYZM; |
44 |
|
default: |
45 |
|
if (has_z) *has_z = 0; |
46 |
|
if (has_m) *has_m = 0; |
47 |
|
return vtInvalid; |
48 |
|
} |
49 |
|
} |
50 |
|
|
51 |
|
/* allocator |
52 |
|
*/ |
53 |
|
static PyObject* shpobject_new(PyTypeObject* type, PyObject* args, PyObject* kwds) |
54 |
{ |
{ |
55 |
PySHPObject* self; |
SHPObjectObject* self; |
56 |
self = (PySHPObject*) type->tp_alloc(type, 0); |
self = (SHPObjectObject*) type->tp_alloc(type, 0); |
57 |
self->shpObject = NULL; |
self->shpObject = NULL; |
58 |
return (PyObject*) self; |
return (PyObject*) self; |
59 |
} |
} |
60 |
|
|
61 |
static void PySHPObject_dealloc(PySHPObject* self) |
/* deallocator |
62 |
|
*/ |
63 |
|
static void shpobject_dealloc(SHPObjectObject* self) |
64 |
{ |
{ |
65 |
SHPDestroyObject(self->shpObject); |
SHPDestroyObject(self->shpObject); |
66 |
self->shpObject = NULL; |
self->shpObject = NULL; |
73 |
* 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 |
74 |
* see the Shaplib documentation. |
* see the Shaplib documentation. |
75 |
*/ |
*/ |
76 |
static int PySHPObject_init(PySHPObject* self, PyObject* args, PyObject* kwds) |
static int shpobject_init(SHPObjectObject* self, PyObject* args, PyObject* kwds) |
77 |
{ |
{ |
78 |
int type; |
int type; |
79 |
int id; |
int id; |
86 |
|
|
87 |
double* xs = NULL; |
double* xs = NULL; |
88 |
double* ys = NULL; |
double* ys = NULL; |
89 |
|
double* zs = NULL; |
90 |
|
double* ms = NULL; |
91 |
int* part_starts = NULL; |
int* part_starts = NULL; |
92 |
int* part_types = NULL; |
int* part_types = NULL; |
93 |
|
|
94 |
|
PyObject* part; |
95 |
|
int vertex_type; |
96 |
|
int has_z; |
97 |
|
int has_m; |
98 |
|
|
99 |
int i; |
int i; |
100 |
int return_code = -1; |
int ok, return_code = -1; |
101 |
|
|
102 |
/* first, unpack parameters */ |
/* first, unpack parameters */ |
103 |
if (kwds != NULL && PyDict_Size(kwds) > 0) |
if (kwds != NULL && PyDict_Size(kwds) > 0) |
107 |
} |
} |
108 |
if (!PyArg_ParseTuple(args, "iiO|O", &type, &id, &parts, &part_type_list)) return -1; |
if (!PyArg_ParseTuple(args, "iiO|O", &type, &id, &parts, &part_type_list)) return -1; |
109 |
|
|
110 |
|
/* check parts */ |
111 |
if (!PySequence_Check(parts)) |
if (!PySequence_Check(parts)) |
112 |
{ |
{ |
113 |
PyErr_SetString(PyExc_TypeError, "parts is not a sequence"); |
PyErr_SetString(PyExc_TypeError, "parts is not a sequence"); |
121 |
} |
} |
122 |
|
|
123 |
/* parts and part_types have to have the same lengths */ |
/* parts and part_types have to have the same lengths */ |
124 |
|
if (part_type_list == Py_None) |
125 |
|
{ |
126 |
|
Py_DECREF(part_type_list); |
127 |
|
part_type_list = NULL; |
128 |
|
} |
129 |
if (part_type_list) |
if (part_type_list) |
130 |
{ |
{ |
131 |
if (!PySequence_Check(parts)) |
if (!PySequence_Check(parts)) |
154 |
num_vertices += PySequence_Length(part); |
num_vertices += PySequence_Length(part); |
155 |
Py_DECREF(part); |
Py_DECREF(part); |
156 |
} |
} |
157 |
|
|
158 |
|
|
159 |
|
vertex_type = determine_vertex_type(type, &has_z, &has_m); |
160 |
|
|
161 |
/* allocate the memory for the various arrays and check for memory errors */ |
/* allocate the memory for the various arrays and check for memory errors */ |
162 |
xs = malloc(num_vertices * sizeof(double)); |
xs = malloc(num_vertices * sizeof(double)); |
163 |
ys = malloc(num_vertices * sizeof(double)); |
ys = malloc(num_vertices * sizeof(double)); |
164 |
|
zs = has_z ? malloc(num_vertices * sizeof(double)) : NULL; |
165 |
|
ms = has_m ? malloc(num_vertices * sizeof(double)) : NULL; |
166 |
part_starts = malloc(num_parts * sizeof(int)); |
part_starts = malloc(num_parts * sizeof(int)); |
167 |
part_types = part_type_list ? malloc(num_parts * sizeof(int)) : 0; |
part_types = part_type_list ? malloc(num_parts * sizeof(int)) : 0; |
168 |
|
|
169 |
if (!xs || !ys || !part_starts || (part_type_list && !part_types)) |
if (!xs || !ys || (has_z && !zs) || (has_m && !ms) || !part_starts || (part_type_list && !part_types)) |
170 |
{ |
{ |
171 |
PyErr_NoMemory(); |
PyErr_NoMemory(); |
172 |
goto exit; |
goto exit; |
194 |
{ |
{ |
195 |
int j, length; |
int j, length; |
196 |
|
|
197 |
PyObject* part = PySequence_ITEM(parts, i); |
part = PySequence_ITEM(parts, i); |
198 |
length = PySequence_Length(part); |
length = PySequence_Length(part); |
199 |
|
if (length < 0) goto exit; |
200 |
part_starts[i] = part_start; |
part_starts[i] = part_start; |
201 |
|
|
202 |
for (j = 0; j < length; ++j) |
for (j = 0; j < length; ++j) |
203 |
{ |
{ |
204 |
PyObject* vertex = PySequence_ITEM(part, j); |
PyObject* vertex = PySequence_ITEM(part, j); |
205 |
if (!PyArg_ParseTuple(vertex, "dd", xs + part_start + j, ys + part_start + j)) |
switch (vertex_type) |
206 |
{ |
{ |
207 |
PyErr_SetString(PyExc_TypeError, "at least one part contains an vertex that's not a tuple of two doubles"); |
case vtXY: |
208 |
Py_DECREF(vertex); |
ok = PyArg_ParseTuple(vertex, "dd", xs + part_start + j, ys + part_start + j); |
209 |
Py_DECREF(part); |
break; |
210 |
goto exit; |
case vtXYM: |
211 |
|
ok = PyArg_ParseTuple(vertex, "ddd", xs + part_start + j, ys + part_start + j, ms + part_start + j); |
212 |
|
break; |
213 |
|
case vtXYZM: |
214 |
|
ms[part_start + j] = 0.; |
215 |
|
ok = PyArg_ParseTuple(vertex, "ddd|d", xs + part_start + j, ys + part_start + j, zs + part_start + j, |
216 |
|
ms + part_start + j); |
217 |
|
break; |
218 |
} |
} |
219 |
Py_DECREF(vertex); |
Py_DECREF(vertex); |
220 |
|
if (!ok) |
221 |
|
{ |
222 |
|
PyErr_SetString(PyExc_TypeError, "at least one vertex is of the wrong format"); |
223 |
|
goto exit; |
224 |
|
} |
225 |
} |
} |
226 |
Py_DECREF(part); |
Py_DECREF(part); |
227 |
|
part = NULL; |
228 |
part_start += length; |
part_start += length; |
229 |
} |
} |
230 |
|
|
231 |
self->shpObject = SHPCreateObject(type, id, num_parts, part_starts, part_types, num_vertices, xs, ys, NULL, NULL); |
self->shpObject = SHPCreateObject(type, id, num_parts, part_starts, part_types, num_vertices, xs, ys, zs, ms); |
232 |
return_code = 0; |
return_code = 0; |
233 |
|
|
234 |
exit: |
exit: |
235 |
|
Py_XDECREF(part); |
236 |
free(xs); |
free(xs); |
237 |
free(ys); |
free(ys); |
238 |
|
free(zs); |
239 |
|
free(ms); |
240 |
free(part_starts); |
free(part_starts); |
241 |
free(part_types); |
free(part_types); |
242 |
return return_code; |
return return_code; |
248 |
* 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. |
249 |
* and max. values of x, y, z, m. |
* and max. values of x, y, z, m. |
250 |
*/ |
*/ |
251 |
static PyObject* PySHPObject_extents(PySHPObject* self) |
static PyObject* shpobject_extents(SHPObjectObject* self) |
252 |
{ |
{ |
253 |
SHPObject* object = self->shpObject; |
SHPObject* object = self->shpObject; |
254 |
return Py_BuildValue("(dddd)(dddd)", |
return Py_BuildValue("(dddd)(dddd)", |
264 |
* tuples. |
* tuples. |
265 |
*/ |
*/ |
266 |
|
|
267 |
static PyObject* build_vertex_list(SHPObject *object, int index, int length); |
static PyObject* build_vertex_list(SHPObject *object, int index, int length, int vertex_type); |
268 |
|
|
269 |
static PyObject* PySHPObject_vertices(PySHPObject* self) |
static PyObject* shpobject_vertices(SHPObjectObject* self) |
270 |
{ |
{ |
271 |
PyObject *result = NULL; |
PyObject *result = NULL; |
272 |
PyObject *part = NULL; |
PyObject *part = NULL; |
273 |
int part_idx, vertex_idx; |
int part_idx, vertex_idx; |
274 |
int length = 0; |
int length = 0; |
275 |
|
int vertex_type; |
276 |
|
|
277 |
SHPObject* object = self->shpObject; |
SHPObject* object = self->shpObject; |
278 |
|
vertex_type = determine_vertex_type(object->nSHPType, NULL, NULL); |
279 |
|
|
280 |
if (object->nParts > 0) |
if (object->nParts > 0) |
281 |
{ |
{ |
282 |
/* A multipart shape. Usual for SHPT_ARC and SHPT_POLYGON */ |
/* A multipart shape. Usual for SHPT_ARC and SHPT_POLYGON */ |
283 |
|
|
284 |
result = PyList_New(object->nParts); |
result = PyList_New(object->nParts); |
285 |
if (!result) |
if (!result) |
286 |
return NULL; |
return NULL; |
287 |
|
|
288 |
for (part_idx = 0, vertex_idx = 0; part_idx < object->nParts; |
for (part_idx = 0, vertex_idx = 0; part_idx < object->nParts; part_idx++) |
289 |
part_idx++) |
{ |
290 |
{ |
if (part_idx < object->nParts - 1) |
291 |
if (part_idx < object->nParts - 1) |
length = (object->panPartStart[part_idx + 1] |
292 |
length = (object->panPartStart[part_idx + 1] |
- object->panPartStart[part_idx]); |
293 |
- object->panPartStart[part_idx]); |
else |
294 |
else |
length = object->nVertices - object->panPartStart[part_idx]; |
295 |
length = object->nVertices - object->panPartStart[part_idx]; |
|
296 |
|
part = build_vertex_list(object, vertex_idx, length, vertex_type); |
297 |
part = build_vertex_list(object, vertex_idx, length); |
if (!part) goto fail; |
|
if (!part) |
|
|
goto fail; |
|
298 |
|
|
299 |
if (PyList_SetItem(result, part_idx, part) < 0) |
if (PyList_SetItem(result, part_idx, part) < 0) goto fail; |
|
goto fail; |
|
300 |
|
|
301 |
vertex_idx += length; |
vertex_idx += length; |
302 |
} |
} |
303 |
} |
} |
304 |
else |
else |
305 |
{ |
{ |
306 |
/* only one part. usual for SHPT_POINT */ |
/* only one part. usual for SHPT_POINT */ |
307 |
result = build_vertex_list(object, 0, object->nVertices); |
result = build_vertex_list(object, 0, object->nVertices, vertex_type); |
308 |
} |
} |
309 |
|
|
310 |
return result; |
return result; |
320 |
* index as a Python-list of tuples. Helper function for |
* index as a Python-list of tuples. Helper function for |
321 |
* SHPObject_vertices. |
* SHPObject_vertices. |
322 |
*/ |
*/ |
323 |
static PyObject* build_vertex_list(SHPObject *object, int index, int length) |
static PyObject* build_vertex_list(SHPObject *object, int index, int length, int vertex_type) |
324 |
{ |
{ |
325 |
int i; |
int i; |
326 |
PyObject * list; |
PyObject * list; |
328 |
|
|
329 |
list = PyList_New(length); |
list = PyList_New(length); |
330 |
if (!list) |
if (!list) |
331 |
return NULL; |
return NULL; |
332 |
|
|
333 |
for (i = 0; i < length; i++, index++) |
for (i = 0; i < length; i++, index++) |
334 |
{ |
{ |
335 |
vertex = Py_BuildValue("dd", object->padfX[index], |
switch (vertex_type) |
336 |
object->padfY[index]); |
{ |
337 |
if (!vertex) |
case vtXY: |
338 |
goto fail; |
vertex = Py_BuildValue("dd", object->padfX[index], object->padfY[index]); |
339 |
if (PyList_SetItem(list, i, vertex) < 0) |
break; |
340 |
goto fail; |
case vtXYM: |
341 |
} |
vertex = Py_BuildValue("ddd", object->padfX[index], object->padfY[index], |
342 |
|
object->padfM[index]); |
343 |
|
case vtXYZM: |
344 |
|
vertex = Py_BuildValue("dddd", object->padfX[index], object->padfY[index], |
345 |
|
object->padfZ[index], object->padfM[index]); |
346 |
|
break; |
347 |
|
default: |
348 |
|
goto fail; |
349 |
|
} |
350 |
|
|
351 |
|
if (!vertex || PyList_SetItem(list, i, vertex) < 0) goto fail; |
352 |
|
} |
353 |
|
|
354 |
return list; |
return list; |
355 |
|
|
356 |
fail: |
fail: |
|
Py_XDECREF(vertex); |
|
357 |
Py_DECREF(list); |
Py_DECREF(list); |
358 |
return NULL; |
return NULL; |
359 |
} |
} |
360 |
|
|
361 |
static PyObject* PySHPObject_type(PySHPObject* self, void* closure) |
|
362 |
|
|
363 |
|
static PyObject* shpobject_part_types(SHPObjectObject* self) |
364 |
|
{ |
365 |
|
int i; |
366 |
|
PyObject* result = NULL; |
367 |
|
SHPObject* object = self->shpObject; |
368 |
|
|
369 |
|
if (object->nParts == 0 || object->panPartType == 0) |
370 |
|
{ |
371 |
|
Py_RETURN_NONE; |
372 |
|
} |
373 |
|
|
374 |
|
result = PyTuple_New(object->nParts); |
375 |
|
if (!result) return NULL; |
376 |
|
|
377 |
|
for (i = 0; i < object->nParts; ++i) |
378 |
|
{ |
379 |
|
/* PyTuple_SetItem steals a reference */ |
380 |
|
PyObject* part_type = PyInt_FromLong((long)object->panPartType[i]); |
381 |
|
if (!part_type || PyTuple_SetItem(result, i, part_type) < 0) goto fail; |
382 |
|
} |
383 |
|
return result; |
384 |
|
|
385 |
|
fail: |
386 |
|
Py_DECREF(result); |
387 |
|
return NULL; |
388 |
|
} |
389 |
|
|
390 |
|
|
391 |
|
|
392 |
|
static PyObject* shpobject_type(SHPObjectObject* self, void* closure) |
393 |
{ |
{ |
394 |
return PyInt_FromLong(self->shpObject->nSHPType); |
return PyInt_FromLong(self->shpObject->nSHPType); |
395 |
} |
} |
396 |
|
|
397 |
static PyObject* PySHPObject_id(PySHPObject* self, void* closure) |
|
398 |
|
|
399 |
|
static PyObject* shpobject_id(SHPObjectObject* self, void* closure) |
400 |
{ |
{ |
401 |
return PyInt_FromLong(self->shpObject->nShapeId); |
return PyInt_FromLong(self->shpObject->nShapeId); |
402 |
} |
} |
403 |
|
|
404 |
static PyMethodDef PySHPObject_methods[] = |
|
405 |
|
|
406 |
|
/* return a string that can be feeded to eval() to reconstruct the object, |
407 |
|
* assuming a proper context |
408 |
|
*/ |
409 |
|
static PyObject* shpobject_repr(SHPObjectObject* self) |
410 |
|
{ |
411 |
|
PyObject* format = NULL; |
412 |
|
PyObject* args = NULL; |
413 |
|
PyObject* result = NULL; |
414 |
|
|
415 |
|
format = PyString_FromString("shapelib.SHPObject(%i, %i, %s, %s)"); |
416 |
|
if (!format) return NULL; |
417 |
|
|
418 |
|
args = Py_BuildValue("iiNN", |
419 |
|
self->shpObject->nSHPType, |
420 |
|
self->shpObject->nShapeId, |
421 |
|
shpobject_vertices(self), |
422 |
|
shpobject_part_types(self)); |
423 |
|
if (!args) |
424 |
|
{ |
425 |
|
Py_DECREF(format); |
426 |
|
return NULL; |
427 |
|
} |
428 |
|
|
429 |
|
result = PyString_Format(format, args); |
430 |
|
Py_DECREF(args); |
431 |
|
Py_DECREF(format); |
432 |
|
return result; |
433 |
|
} |
434 |
|
|
435 |
|
|
436 |
|
|
437 |
|
static struct PyMethodDef shpobject_methods[] = |
438 |
{ |
{ |
439 |
{"extents", (PyCFunction)PySHPObject_extents, METH_NOARGS, NULL}, |
{"extents", (PyCFunction)shpobject_extents, METH_NOARGS, NULL}, |
440 |
{"vertices", (PyCFunction)PySHPObject_vertices, METH_NOARGS, NULL}, |
{"vertices", (PyCFunction)shpobject_vertices, METH_NOARGS, NULL}, |
441 |
|
{"part_types", (PyCFunction)shpobject_part_types, METH_NOARGS, NULL}, |
442 |
{NULL} |
{NULL} |
443 |
}; |
}; |
444 |
|
|
445 |
static PyGetSetDef PySHPObject_getsetters[] = |
static struct PyGetSetDef shpobject_getsetters[] = |
446 |
{ |
{ |
447 |
{"type", (getter)PySHPObject_type, NULL, NULL }, |
{"type", (getter)shpobject_type, NULL, NULL }, |
448 |
{"id", (getter)PySHPObject_id, NULL, NULL }, |
{"id", (getter)shpobject_id, NULL, NULL }, |
449 |
{NULL} |
{NULL} |
450 |
}; |
}; |
451 |
|
|
452 |
static PyTypeObject PySHPObjectType = PYSHAPELIB_DEFINE_TYPE(PySHPObject, "shapelib.SHPObject", 0); |
static PyTypeObject SHPObjectType = PYSHAPELIB_DEFINE_TYPE(SHPObjectObject, shpobject, "shapelib.SHPObject", 0); |
453 |
|
|
454 |
|
|
455 |
/* --- ShapeFile ----------------------------------------------------------------------------------------------------- */ |
/* --- ShapeFile ----------------------------------------------------------------------------------------------------- */ |
459 |
PyObject_HEAD |
PyObject_HEAD |
460 |
SHPHandle handle; |
SHPHandle handle; |
461 |
} |
} |
462 |
PyShapeFile; |
ShapeFileObject; |
463 |
|
|
464 |
static PyObject* PyShapeFile_new(PyTypeObject* type, PyObject* args, PyObject* kwds) |
/* allocator |
465 |
|
*/ |
466 |
|
static PyObject* shapefile_new(PyTypeObject* type, PyObject* args, PyObject* kwds) |
467 |
{ |
{ |
468 |
PyShapeFile* self; |
ShapeFileObject* self; |
469 |
self = (PyShapeFile*) type->tp_alloc(type, 0); |
self = (ShapeFileObject*) type->tp_alloc(type, 0); |
470 |
self->handle = NULL; |
self->handle = NULL; |
471 |
return (PyObject*) self; |
return (PyObject*) self; |
472 |
} |
} |
473 |
|
|
474 |
static int PyShapeFile_init(PyShapeFile* self, PyObject* args, PyObject* kwds) |
/* destructor |
475 |
|
*/ |
476 |
|
static void shapefile_dealloc(ShapeFileObject* self) |
477 |
|
{ |
478 |
|
SHPClose(self->handle); |
479 |
|
self->ob_type->tp_free((PyObject*)self); |
480 |
|
} |
481 |
|
|
482 |
|
/* constructor |
483 |
|
*/ |
484 |
|
static int shapefile_init(ShapeFileObject* self, PyObject* args, PyObject* kwds) |
485 |
{ |
{ |
486 |
char* file; |
char* file; |
487 |
char* mode = "rb"; |
char* mode = "rb"; |
496 |
return self->handle ? 0 : -1; |
return self->handle ? 0 : -1; |
497 |
} |
} |
498 |
|
|
499 |
static PyObject* PyShapeFile_close(PyShapeFile* self) |
static PyObject* shapefile_close(ShapeFileObject* self) |
500 |
{ |
{ |
501 |
SHPClose(self->handle); |
SHPClose(self->handle); |
502 |
self->handle = NULL; |
self->handle = NULL; |
503 |
Py_RETURN_NONE; |
Py_RETURN_NONE; |
504 |
} |
} |
505 |
|
|
506 |
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) |
|
507 |
{ |
{ |
508 |
SHPHandle handle = self->handle; |
SHPHandle handle = self->handle; |
509 |
return Py_BuildValue("ii(dddd)(dddd)", |
return Py_BuildValue("ii(dddd)(dddd)", |
512 |
handle->adBoundsMax[0], handle->adBoundsMax[1], handle->adBoundsMax[2], handle->adBoundsMax[3]); |
handle->adBoundsMax[0], handle->adBoundsMax[1], handle->adBoundsMax[2], handle->adBoundsMax[3]); |
513 |
} |
} |
514 |
|
|
515 |
static PyObject* PyShapeFile_read_object(PyShapeFile* self, PyObject* args) |
static PyObject* shapefile_read_object(ShapeFileObject* self, PyObject* args) |
516 |
{ |
{ |
517 |
int index; |
int index; |
518 |
SHPObject* object; |
SHPObject* object; |
519 |
PySHPObject* result; |
SHPObjectObject* result; |
520 |
|
|
521 |
if (!PyArg_ParseTuple(args, "i", &index)) return NULL; |
if (!PyArg_ParseTuple(args, "i", &index)) return NULL; |
522 |
|
|
527 |
return NULL; |
return NULL; |
528 |
} |
} |
529 |
|
|
530 |
result = PyObject_New(PySHPObject, &PySHPObjectType); |
result = PyObject_New(SHPObjectObject, &SHPObjectType); |
531 |
if (!result) |
if (!result) |
532 |
{ |
{ |
533 |
return PyErr_NoMemory(); |
return PyErr_NoMemory(); |
537 |
return (PyObject*) result; |
return (PyObject*) result; |
538 |
} |
} |
539 |
|
|
540 |
static PyObject* PyShapeFile_write_object(PyShapeFile* self, PyObject* args) |
static PyObject* shapefile_write_object(ShapeFileObject* self, PyObject* args) |
541 |
{ |
{ |
542 |
int index, result; |
int index, result; |
543 |
PyObject* object; |
PyObject* object; |
544 |
|
|
545 |
if (!PyArg_ParseTuple(args, "iO", &index, &object)) return NULL; |
if (!PyArg_ParseTuple(args, "iO", &index, &object)) return NULL; |
546 |
|
|
547 |
if (!PyObject_IsInstance(object, (PyObject*)&PySHPObjectType)) |
if (!PyObject_IsInstance(object, (PyObject*)&SHPObjectType)) |
548 |
{ |
{ |
549 |
PyErr_SetString(PyExc_TypeError, "object is not a SHPObject"); |
PyErr_SetString(PyExc_TypeError, "object is not a SHPObject"); |
550 |
return NULL; |
return NULL; |
551 |
} |
} |
552 |
|
|
553 |
result = SHPWriteObject(self->handle, index, ((PySHPObject*)object)->shpObject); |
result = SHPWriteObject(self->handle, index, ((SHPObjectObject*)object)->shpObject); |
554 |
if (result < 0) |
if (result < 0) |
555 |
{ |
{ |
556 |
PyErr_SetString(PyExc_RuntimeError, "failed to write object"); |
PyErr_SetString(PyExc_RuntimeError, "failed to write object"); |
559 |
return PyInt_FromLong((long)result); |
return PyInt_FromLong((long)result); |
560 |
} |
} |
561 |
|
|
562 |
static PyObject* PyShapeFile_cobject(PyShapeFile* self) |
static PyObject* shapefile_cobject(ShapeFileObject* self) |
563 |
{ |
{ |
564 |
return PyCObject_FromVoidPtr(self->handle, NULL); |
return PyCObject_FromVoidPtr(self->handle, NULL); |
565 |
} |
} |
566 |
|
|
567 |
static PyMethodDef PyShapeFile_methods[] = |
static PyObject* shapefile_repr(ShapeFileObject* self) |
568 |
|
{ |
569 |
|
/* TODO: it would be nice to do something like "shapelib.ShapeFile(filename, mode)" instead */ |
570 |
|
return PyString_FromFormat("<shapelib.ShapeFile object at %p>", self->handle); |
571 |
|
} |
572 |
|
|
573 |
|
static struct PyMethodDef shapefile_methods[] = |
574 |
{ |
{ |
575 |
{"close", (PyCFunction)PyShapeFile_close, METH_NOARGS, "close the shape file" }, |
{"close", (PyCFunction)shapefile_close, METH_NOARGS, "close the shape file" }, |
576 |
{"info", (PyCFunction)PyShapeFile_info, METH_NOARGS, |
{"info", (PyCFunction)shapefile_info, METH_NOARGS, |
577 |
"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 " |
578 |
"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." }, |
579 |
{"read_object", (PyCFunction)PyShapeFile_read_object, METH_VARARGS, "Return object number i" }, |
{"read_object", (PyCFunction)shapefile_read_object, METH_VARARGS, "Return object number i" }, |
580 |
{"write_object", (PyCFunction)PyShapeFile_write_object, METH_VARARGS, "Write an object"}, |
{"write_object", (PyCFunction)shapefile_write_object, METH_VARARGS, "Write an object"}, |
581 |
{"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"}, |
582 |
{NULL} |
{NULL} |
583 |
}; |
}; |
584 |
|
|
585 |
static PyGetSetDef PyShapeFile_getsetters[] = |
static struct PyGetSetDef shapefile_getsetters[] = |
586 |
{ |
{ |
587 |
{NULL} |
{NULL} |
588 |
}; |
}; |
589 |
|
|
590 |
static PyTypeObject PyShapeFileType = PYSHAPELIB_DEFINE_TYPE(PyShapeFile, "shapelib.ShapeFile", 0); |
static PyTypeObject ShapeFileType = PYSHAPELIB_DEFINE_TYPE(ShapeFileObject, shapefile, "shapelib.ShapeFile", 0); |
591 |
|
|
592 |
/* --- shapelib ------------------------------------------------------------------------------------------------------ */ |
/* --- shapelib ------------------------------------------------------------------------------------------------------ */ |
593 |
|
|
594 |
static PyObject* shapelib_open(PyObject* module, PyObject* args) |
static PyObject* shapelib_open(PyObject* module, PyObject* args) |
595 |
{ |
{ |
596 |
return PyObject_CallObject((PyObject*)&PyShapeFileType, args); |
return PyObject_CallObject((PyObject*)&ShapeFileType, args); |
597 |
} |
} |
598 |
|
|
599 |
static PyObject* shapelib_create(PyObject* module, PyObject* args) |
static PyObject* shapelib_create(PyObject* module, PyObject* args) |
600 |
{ |
{ |
601 |
char* file; |
char* file; |
602 |
int type; |
int type; |
603 |
PyShapeFile* result; |
ShapeFileObject* result; |
604 |
|
|
605 |
if (!PyArg_ParseTuple(args, "si", &file, &type)) return NULL; |
if (!PyArg_ParseTuple(args, "si", &file, &type)) return NULL; |
606 |
|
|
607 |
result = PyObject_New(PyShapeFile, &PyShapeFileType); |
result = PyObject_New(ShapeFileObject, &ShapeFileType); |
608 |
if (!result) |
if (!result) |
609 |
{ |
{ |
610 |
return PyErr_NoMemory(); |
return PyErr_NoMemory(); |
649 |
return PyString_FromString(SHPPartTypeName(type)); |
return PyString_FromString(SHPPartTypeName(type)); |
650 |
} |
} |
651 |
|
|
652 |
static PyMethodDef shapelib_methods[] = |
static struct PyMethodDef shapelib_methods[] = |
653 |
{ |
{ |
654 |
{"open", (PyCFunction)shapelib_open, METH_VARARGS, "open a ShapeFile" }, |
{"open", (PyCFunction)shapelib_open, METH_VARARGS, "open a ShapeFile" }, |
655 |
{"create", (PyCFunction)shapelib_create, METH_VARARGS, "create a ShapeFile" }, |
{"create", (PyCFunction)shapelib_create, METH_VARARGS, "create a ShapeFile" }, |
664 |
PyObject* module = Py_InitModule("shapelib", shapelib_methods); |
PyObject* module = Py_InitModule("shapelib", shapelib_methods); |
665 |
if (!module) return; |
if (!module) return; |
666 |
|
|
667 |
PYSHAPELIB_ADD_TYPE(PySHPObjectType, "SHPObject"); |
PYSHAPELIB_ADD_TYPE(SHPObjectType, "SHPObject"); |
668 |
PYSHAPELIB_ADD_TYPE(PyShapeFileType, "ShapeFile"); |
PYSHAPELIB_ADD_TYPE(ShapeFileType, "ShapeFile"); |
669 |
|
|
670 |
PYSHAPELIB_ADD_CONSTANT(SHPT_NULL); |
PYSHAPELIB_ADD_CONSTANT(SHPT_NULL); |
671 |
PYSHAPELIB_ADD_CONSTANT(SHPT_POINT); |
PYSHAPELIB_ADD_CONSTANT(SHPT_POINT); |