1 |
/* Copyright (c) 2001-2008 by Intevation GmbH |
2 |
* Authors: |
3 |
* Bram de Greve <[email protected]> |
4 |
* Bernhard Herzog <[email protected]> |
5 |
* |
6 |
* This program is free software under the LGPL (>=v2) |
7 |
* Read the file COPYING coming with pyshapelib for details. |
8 |
*/ |
9 |
|
10 |
#include "pyshapelib_common.h" |
11 |
|
12 |
/* --- SHPObject ----------------------------------------------------------------------------------------------------- */ |
13 |
|
14 |
typedef struct |
15 |
{ |
16 |
PyObject_HEAD |
17 |
SHPObject* shpObject; |
18 |
} |
19 |
SHPObjectObject; |
20 |
|
21 |
enum { |
22 |
vtXY, |
23 |
vtXYM, |
24 |
vtXYZM, |
25 |
vtInvalid |
26 |
} VertexType; |
27 |
|
28 |
int determine_vertex_type(int shape_type, int* has_z, int* has_m) |
29 |
{ |
30 |
switch (shape_type) |
31 |
{ |
32 |
case SHPT_POINT: |
33 |
case SHPT_ARC: |
34 |
case SHPT_POLYGON: |
35 |
case SHPT_MULTIPOINT: |
36 |
if (has_z) *has_z = 0; |
37 |
if (has_m) *has_m = 0; |
38 |
return vtXY; |
39 |
case SHPT_POINTM: |
40 |
case SHPT_ARCM: |
41 |
case SHPT_POLYGONM: |
42 |
case SHPT_MULTIPOINTM: |
43 |
if (has_z) *has_z = 0; |
44 |
if (has_m) *has_m = 1; |
45 |
case SHPT_POINTZ: |
46 |
case SHPT_ARCZ: |
47 |
case SHPT_POLYGONZ: |
48 |
case SHPT_MULTIPOINTZ: |
49 |
case SHPT_MULTIPATCH: |
50 |
if (has_z) *has_z = 1; |
51 |
if (has_m) *has_m = 1; |
52 |
return vtXYZM; |
53 |
default: |
54 |
if (has_z) *has_z = 0; |
55 |
if (has_m) *has_m = 0; |
56 |
return vtInvalid; |
57 |
} |
58 |
} |
59 |
|
60 |
/* allocator |
61 |
*/ |
62 |
static PyObject* shpobject_new(PyTypeObject* type, PyObject* args, PyObject* kwds) |
63 |
{ |
64 |
SHPObjectObject* self; |
65 |
self = (SHPObjectObject*) type->tp_alloc(type, 0); |
66 |
self->shpObject = NULL; |
67 |
return (PyObject*) self; |
68 |
} |
69 |
|
70 |
/* deallocator |
71 |
*/ |
72 |
static void shpobject_dealloc(SHPObjectObject* self) |
73 |
{ |
74 |
SHPDestroyObject(self->shpObject); |
75 |
self->shpObject = NULL; |
76 |
self->ob_type->tp_free((PyObject*)self); |
77 |
} |
78 |
|
79 |
static int unpack_vertex(PyObject* vertex, int vertex_type, |
80 |
double* xs, double* ys, double* zs, double* ms, int offset); |
81 |
|
82 |
/* The constructor of SHPObject. parts is a list of lists of tuples |
83 |
* describing the parts and their vertices just likethe output of the |
84 |
* vertices() method. part_type_list is the list of part-types and may |
85 |
* be NULL. For the meaning of the part-types and their default value |
86 |
* see the Shaplib documentation. |
87 |
*/ |
88 |
static int shpobject_init(SHPObjectObject* self, PyObject* args, PyObject* kwds) |
89 |
{ |
90 |
int type; |
91 |
int id; |
92 |
PyObject* parts = NULL; |
93 |
PyObject* part_type_list = NULL; |
94 |
|
95 |
int num_parts; |
96 |
int num_vertices; |
97 |
int part_start; |
98 |
|
99 |
double* xs = NULL; |
100 |
double* ys = NULL; |
101 |
double* zs = NULL; |
102 |
double* ms = NULL; |
103 |
int* part_starts = NULL; |
104 |
int* part_types = NULL; |
105 |
|
106 |
PyObject* part; |
107 |
int vertex_type; |
108 |
int has_z; |
109 |
int has_m; |
110 |
|
111 |
int i; |
112 |
int return_code = -1; |
113 |
|
114 |
/* first, unpack parameters */ |
115 |
if (kwds != NULL && PyDict_Size(kwds) > 0) |
116 |
{ |
117 |
PyErr_Format(PyExc_TypeError, "shapelib.SHPObject.__init__ takes no keyword arguments"); |
118 |
return -1; |
119 |
} |
120 |
if (!PyArg_ParseTuple(args, "iiO|O:__init__", &type, &id, &parts, &part_type_list)) return -1; |
121 |
|
122 |
/* check parts */ |
123 |
if (!PySequence_Check(parts)) |
124 |
{ |
125 |
PyErr_SetString(PyExc_TypeError, "parts is not a sequence"); |
126 |
return -1; |
127 |
} |
128 |
num_parts = PySequence_Length(parts); |
129 |
if (num_parts < 0) |
130 |
{ |
131 |
PyErr_SetString(PyExc_TypeError, "cannot determine length of parts"); |
132 |
return -1; |
133 |
} |
134 |
|
135 |
/* parts and part_types have to have the same lengths */ |
136 |
if (part_type_list == Py_None) |
137 |
{ |
138 |
Py_DECREF(part_type_list); |
139 |
part_type_list = NULL; |
140 |
} |
141 |
if (part_type_list) |
142 |
{ |
143 |
if (!PySequence_Check(parts)) |
144 |
{ |
145 |
PyErr_SetString(PyExc_TypeError, "part_type_list is not a sequence"); |
146 |
return -1; |
147 |
} |
148 |
if (PySequence_Length(part_type_list) != num_parts) |
149 |
{ |
150 |
PyErr_SetString(PyExc_TypeError, "parts and part_types have to have the same lengths"); |
151 |
return -1; |
152 |
} |
153 |
} |
154 |
|
155 |
/* determine how many vertices there are altogether */ |
156 |
num_vertices = 0; |
157 |
for (i = 0; i < num_parts; ++i) |
158 |
{ |
159 |
PyObject* part = PySequence_ITEM(parts, i); |
160 |
if (!PySequence_Check(part)) |
161 |
{ |
162 |
PyErr_SetString(PyExc_TypeError, "at least one item in parts is not a sequence"); |
163 |
Py_DECREF(part); |
164 |
return -1; |
165 |
} |
166 |
num_vertices += PySequence_Length(part); |
167 |
Py_DECREF(part); |
168 |
} |
169 |
|
170 |
|
171 |
vertex_type = determine_vertex_type(type, &has_z, &has_m); |
172 |
|
173 |
/* allocate the memory for the various arrays and check for memory errors */ |
174 |
xs = malloc(num_vertices * sizeof(double)); |
175 |
ys = malloc(num_vertices * sizeof(double)); |
176 |
zs = has_z ? malloc(num_vertices * sizeof(double)) : NULL; |
177 |
ms = has_m ? malloc(num_vertices * sizeof(double)) : NULL; |
178 |
part_starts = malloc(num_parts * sizeof(int)); |
179 |
part_types = part_type_list ? malloc(num_parts * sizeof(int)) : 0; |
180 |
|
181 |
if (!xs || !ys || (has_z && !zs) || (has_m && !ms) || !part_starts || (part_type_list && !part_types)) |
182 |
{ |
183 |
PyErr_NoMemory(); |
184 |
goto exit; |
185 |
} |
186 |
|
187 |
/* convert the part types */ |
188 |
if (part_type_list) |
189 |
{ |
190 |
for (i = 0; i < num_parts; i++) |
191 |
{ |
192 |
PyObject* otype = PySequence_ITEM(part_type_list, i); |
193 |
part_types[i] = PyInt_AsLong(otype); |
194 |
Py_DECREF(otype); |
195 |
if (part_types[i] < 0) |
196 |
{ |
197 |
PyErr_SetString(PyExc_TypeError, "at least one item in part_type_list is not an integer or is negative"); |
198 |
goto exit; |
199 |
} |
200 |
} |
201 |
} |
202 |
|
203 |
/* convert the list of parts */ |
204 |
part_start = 0; |
205 |
for (i = 0; i < num_parts; ++i) |
206 |
{ |
207 |
int j, length; |
208 |
|
209 |
part = PySequence_ITEM(parts, i); |
210 |
length = PySequence_Length(part); |
211 |
if (length < 0) goto exit; |
212 |
part_starts[i] = part_start; |
213 |
|
214 |
for (j = 0; j < length; ++j) |
215 |
{ |
216 |
PyObject* vertex = PySequence_ITEM(part, j); |
217 |
if (!unpack_vertex(vertex, vertex_type, xs, ys, zs, ms, part_start + j)) |
218 |
{ |
219 |
Py_DECREF(vertex); |
220 |
PyErr_SetString(PyExc_TypeError, "at least one vertex is of the wrong format"); |
221 |
goto exit; |
222 |
} |
223 |
Py_DECREF(vertex); |
224 |
} |
225 |
Py_DECREF(part); |
226 |
part = NULL; |
227 |
part_start += length; |
228 |
} |
229 |
|
230 |
self->shpObject = SHPCreateObject(type, id, num_parts, part_starts, part_types, num_vertices, xs, ys, zs, ms); |
231 |
return_code = 0; |
232 |
|
233 |
exit: |
234 |
Py_XDECREF(part); |
235 |
free(xs); |
236 |
free(ys); |
237 |
free(zs); |
238 |
free(ms); |
239 |
free(part_starts); |
240 |
free(part_types); |
241 |
return return_code; |
242 |
} |
243 |
|
244 |
/* helper for shpobject_init. Unpacks vertices |
245 |
*/ |
246 |
static int unpack_vertex(PyObject* vertex, int vertex_type, |
247 |
double* xs, double* ys, double* zs, double* ms, int offset) |
248 |
{ |
249 |
int ok; |
250 |
PyObject* m_object; |
251 |
PyObject *err_type, *err_value, *err_traceback; |
252 |
|
253 |
switch (vertex_type) |
254 |
{ |
255 |
case vtXY: |
256 |
return PyArg_ParseTuple(vertex, "dd:__init__", xs + offset, ys + offset); |
257 |
|
258 |
case vtXYM: |
259 |
ms[offset] = PYSHAPELIB_NO_DATA; |
260 |
ok = PyArg_ParseTuple(vertex, "dd|d:__init__", xs + offset, ys + offset, ms + offset); |
261 |
if (!ok) |
262 |
{ |
263 |
/* maybe they specified None as M value */ |
264 |
PyErr_Fetch(&err_type, &err_value, &err_traceback); |
265 |
ok = PyArg_ParseTuple(vertex, "ddO:__init__", xs + offset, ys + offset, &m_object); |
266 |
if (ok && m_object == Py_None) |
267 |
{ |
268 |
Py_XDECREF(err_type); |
269 |
Py_XDECREF(err_value); |
270 |
Py_XDECREF(err_traceback); |
271 |
} |
272 |
else |
273 |
{ |
274 |
PyErr_Restore(err_type, err_value, err_traceback); |
275 |
} |
276 |
} |
277 |
return ok; |
278 |
|
279 |
case vtXYZM: |
280 |
zs[offset] = 0.; |
281 |
ms[offset] = PYSHAPELIB_NO_DATA; |
282 |
ok = PyArg_ParseTuple(vertex, "dd|dd:__init__", xs + offset, ys + offset, |
283 |
zs + offset, ms + offset); |
284 |
if (!ok) |
285 |
{ |
286 |
/* maybe they specified None as M value */ |
287 |
PyErr_Fetch(&err_type, &err_value, &err_traceback); |
288 |
ok = PyArg_ParseTuple(vertex, "dddO:__init__", xs + offset, ys + offset, |
289 |
zs + offset, &m_object); |
290 |
if (ok && m_object == Py_None) |
291 |
{ |
292 |
Py_XDECREF(err_type); |
293 |
Py_XDECREF(err_value); |
294 |
Py_XDECREF(err_traceback); |
295 |
} |
296 |
else |
297 |
{ |
298 |
PyErr_Restore(err_type, err_value, err_traceback); |
299 |
} |
300 |
} |
301 |
return ok; |
302 |
|
303 |
default: |
304 |
PyErr_SetString(PyExc_NotImplementedError, "vertex type not implemented"); |
305 |
return 0; |
306 |
} |
307 |
} |
308 |
|
309 |
/* |
310 |
* The extents() method of SHPObject. |
311 |
* |
312 |
* Return the extents as a tuple of two 4-element lists with the min. |
313 |
* and max. values of x, y, z, m. |
314 |
*/ |
315 |
static PyObject* shpobject_extents(SHPObjectObject* self) |
316 |
{ |
317 |
SHPObject* object = self->shpObject; |
318 |
return Py_BuildValue("(dddd)(dddd)", |
319 |
object->dfXMin, object->dfYMin, object->dfZMin, object->dfMMin, |
320 |
object->dfXMax, object->dfYMax, object->dfZMax, object->dfMMax); |
321 |
} |
322 |
|
323 |
|
324 |
/* |
325 |
* The vertices() method of SHPObject. |
326 |
* |
327 |
* Return the x and y coords of the vertices as a list of lists of |
328 |
* tuples. |
329 |
*/ |
330 |
|
331 |
static PyObject* build_vertex_list(SHPObject *object, int index, int length, int vertex_type); |
332 |
|
333 |
static PyObject* shpobject_vertices(SHPObjectObject* self) |
334 |
{ |
335 |
PyObject *result = NULL; |
336 |
PyObject *part = NULL; |
337 |
int part_idx, vertex_idx; |
338 |
int length = 0; |
339 |
int vertex_type; |
340 |
|
341 |
SHPObject* object = self->shpObject; |
342 |
vertex_type = determine_vertex_type(object->nSHPType, NULL, NULL); |
343 |
|
344 |
if (object->nParts > 0) |
345 |
{ |
346 |
/* A multipart shape. Usual for SHPT_ARC and SHPT_POLYGON */ |
347 |
|
348 |
result = PyList_New(object->nParts); |
349 |
if (!result) |
350 |
return NULL; |
351 |
|
352 |
for (part_idx = 0, vertex_idx = 0; part_idx < object->nParts; part_idx++) |
353 |
{ |
354 |
if (part_idx < object->nParts - 1) |
355 |
length = (object->panPartStart[part_idx + 1] |
356 |
- object->panPartStart[part_idx]); |
357 |
else |
358 |
length = object->nVertices - object->panPartStart[part_idx]; |
359 |
|
360 |
part = build_vertex_list(object, vertex_idx, length, vertex_type); |
361 |
if (!part) goto fail; |
362 |
|
363 |
if (PyList_SetItem(result, part_idx, part) < 0) goto fail; |
364 |
|
365 |
vertex_idx += length; |
366 |
} |
367 |
} |
368 |
else |
369 |
{ |
370 |
/* only one part. usual for SHPT_POINT */ |
371 |
result = build_vertex_list(object, 0, object->nVertices, vertex_type); |
372 |
} |
373 |
|
374 |
return result; |
375 |
|
376 |
fail: |
377 |
Py_XDECREF(part); |
378 |
Py_DECREF(result); |
379 |
return NULL; |
380 |
} |
381 |
|
382 |
|
383 |
/* Return the length coordinates of the shape object starting at vertex |
384 |
* index as a Python-list of tuples. Helper function for |
385 |
* SHPObject_vertices. |
386 |
*/ |
387 |
static PyObject* build_vertex_list(SHPObject *object, int index, int length, int vertex_type) |
388 |
{ |
389 |
int i; |
390 |
PyObject * list; |
391 |
PyObject * vertex = NULL; |
392 |
|
393 |
list = PyList_New(length); |
394 |
if (!list) |
395 |
return NULL; |
396 |
|
397 |
for (i = 0; i < length; i++, index++) |
398 |
{ |
399 |
switch (vertex_type) |
400 |
{ |
401 |
case vtXY: |
402 |
vertex = Py_BuildValue("dd", object->padfX[index], object->padfY[index]); |
403 |
break; |
404 |
case vtXYM: |
405 |
vertex = Py_BuildValue("ddd", object->padfX[index], object->padfY[index], |
406 |
object->padfM[index]); |
407 |
break; |
408 |
case vtXYZM: |
409 |
vertex = Py_BuildValue("dddd", object->padfX[index], object->padfY[index], |
410 |
object->padfZ[index], object->padfM[index]); |
411 |
break; |
412 |
default: |
413 |
goto fail; |
414 |
} |
415 |
|
416 |
if (!vertex || PyList_SetItem(list, i, vertex) < 0) goto fail; |
417 |
} |
418 |
|
419 |
return list; |
420 |
|
421 |
fail: |
422 |
Py_DECREF(list); |
423 |
return NULL; |
424 |
} |
425 |
|
426 |
|
427 |
|
428 |
static PyObject* shpobject_part_types(SHPObjectObject* self) |
429 |
{ |
430 |
int i; |
431 |
PyObject* result = NULL; |
432 |
SHPObject* object = self->shpObject; |
433 |
|
434 |
if (object->nParts == 0 || object->panPartType == 0) |
435 |
{ |
436 |
Py_RETURN_NONE; |
437 |
} |
438 |
|
439 |
result = PyTuple_New(object->nParts); |
440 |
if (!result) return NULL; |
441 |
|
442 |
for (i = 0; i < object->nParts; ++i) |
443 |
{ |
444 |
/* PyTuple_SetItem steals a reference */ |
445 |
PyObject* part_type = PyInt_FromLong((long)object->panPartType[i]); |
446 |
if (!part_type || PyTuple_SetItem(result, i, part_type) < 0) goto fail; |
447 |
} |
448 |
return result; |
449 |
|
450 |
fail: |
451 |
Py_DECREF(result); |
452 |
return NULL; |
453 |
} |
454 |
|
455 |
|
456 |
|
457 |
static PyObject* shpobject_type(SHPObjectObject* self, void* closure) |
458 |
{ |
459 |
return PyInt_FromLong(self->shpObject->nSHPType); |
460 |
} |
461 |
|
462 |
|
463 |
|
464 |
static PyObject* shpobject_id(SHPObjectObject* self, void* closure) |
465 |
{ |
466 |
return PyInt_FromLong(self->shpObject->nShapeId); |
467 |
} |
468 |
|
469 |
|
470 |
|
471 |
/* return a string that can be feeded to eval() to reconstruct the object, |
472 |
* assuming a proper context |
473 |
*/ |
474 |
static PyObject* shpobject_repr(SHPObjectObject* self) |
475 |
{ |
476 |
PyObject* format = NULL; |
477 |
PyObject* args = NULL; |
478 |
PyObject* result = NULL; |
479 |
|
480 |
format = PyString_FromString("shapelib.SHPObject(%i, %i, %s, %s)"); |
481 |
if (!format) return NULL; |
482 |
|
483 |
args = Py_BuildValue("iiNN", |
484 |
self->shpObject->nSHPType, |
485 |
self->shpObject->nShapeId, |
486 |
shpobject_vertices(self), |
487 |
shpobject_part_types(self)); |
488 |
if (!args) |
489 |
{ |
490 |
Py_DECREF(format); |
491 |
return NULL; |
492 |
} |
493 |
|
494 |
result = PyString_Format(format, args); |
495 |
Py_DECREF(args); |
496 |
Py_DECREF(format); |
497 |
return result; |
498 |
} |
499 |
|
500 |
|
501 |
|
502 |
static struct PyMethodDef shpobject_methods[] = |
503 |
{ |
504 |
{"extents", (PyCFunction)shpobject_extents, METH_NOARGS, |
505 |
"extents() -> ((x_min, y_min, z_min, m_min), (x_max, y_max, z_max, m_max))\n\n" |
506 |
"returns the 4D bounding box of the SHPObject"}, |
507 |
{"vertices", (PyCFunction)shpobject_vertices, METH_NOARGS, |
508 |
"vertices() -> [[(x, y, ...), ...], ...]\n\n" |
509 |
"Returns a list of object parts, where each part is again a list of vertices. " |
510 |
"Each vertex is a tuple of two to four doubles, depending on the object type."}, |
511 |
{"part_types", (PyCFunction)shpobject_part_types, METH_NOARGS, |
512 |
"part_types() -> tuple\n\n" |
513 |
"returns a tuple of integers, each integer indicating the type of the " |
514 |
"corresponding part in vertices()"}, |
515 |
{NULL} |
516 |
}; |
517 |
|
518 |
static struct PyGetSetDef shpobject_getsetters[] = |
519 |
{ |
520 |
{"type", (getter)shpobject_type, NULL, "type of the object (read-only)" }, |
521 |
{"id", (getter)shpobject_id, NULL, "id of the object (read-only)" }, |
522 |
{NULL} |
523 |
}; |
524 |
|
525 |
static PyTypeObject SHPObjectType = PYSHAPELIB_DEFINE_TYPE(SHPObjectObject, shpobject, "shapelib.SHPObject", 0); |
526 |
|
527 |
|
528 |
/* --- ShapeFile ----------------------------------------------------------------------------------------------------- */ |
529 |
|
530 |
typedef struct |
531 |
{ |
532 |
PyObject_HEAD |
533 |
SHPHandle handle; |
534 |
} |
535 |
ShapeFileObject; |
536 |
|
537 |
/* allocator |
538 |
*/ |
539 |
static PyObject* shapefile_new(PyTypeObject* type, PyObject* args, PyObject* kwds) |
540 |
{ |
541 |
ShapeFileObject* self; |
542 |
self = (ShapeFileObject*) type->tp_alloc(type, 0); |
543 |
self->handle = NULL; |
544 |
return (PyObject*) self; |
545 |
} |
546 |
|
547 |
/* destructor |
548 |
*/ |
549 |
static void shapefile_dealloc(ShapeFileObject* self) |
550 |
{ |
551 |
SHPClose(self->handle); |
552 |
self->ob_type->tp_free((PyObject*)self); |
553 |
} |
554 |
|
555 |
/* constructor |
556 |
*/ |
557 |
static int shapefile_init(ShapeFileObject* self, PyObject* args, PyObject* kwds) |
558 |
{ |
559 |
SAHooks hooks; |
560 |
char* file = NULL; |
561 |
char* mode = "rb"; |
562 |
static char *kwlist[] = {"name", "mode", NULL}; |
563 |
|
564 |
SHPClose(self->handle); |
565 |
self->handle = NULL; |
566 |
|
567 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|s:ShapeFile", kwlist, |
568 |
PYSHAPELIB_FILENAME_ENCODING, &file, &mode)) return -1; |
569 |
|
570 |
PYSHAPELIB_SETUPHOOKS(&hooks); |
571 |
self->handle = SHPOpenLL(file, mode, &hooks); |
572 |
if (!self->handle) |
573 |
{ |
574 |
PyErr_SetFromErrnoWithFilename(PyExc_IOError, file); |
575 |
PyMem_Free(file); |
576 |
return -1; |
577 |
} |
578 |
PyMem_Free(file); |
579 |
|
580 |
return 0; |
581 |
} |
582 |
|
583 |
|
584 |
|
585 |
static PyObject* shapefile_close(ShapeFileObject* self) |
586 |
{ |
587 |
SHPClose(self->handle); |
588 |
self->handle = NULL; |
589 |
Py_RETURN_NONE; |
590 |
} |
591 |
|
592 |
|
593 |
|
594 |
static PyObject* shapefile_info(ShapeFileObject* self) |
595 |
{ |
596 |
SHPHandle handle = self->handle; |
597 |
return Py_BuildValue("ii(dddd)(dddd)", |
598 |
handle->nRecords, handle->nShapeType, |
599 |
handle->adBoundsMin[0], handle->adBoundsMin[1], handle->adBoundsMin[2], handle->adBoundsMin[3], |
600 |
handle->adBoundsMax[0], handle->adBoundsMax[1], handle->adBoundsMax[2], handle->adBoundsMax[3]); |
601 |
} |
602 |
|
603 |
|
604 |
|
605 |
static PyObject* shapefile_read_object(ShapeFileObject* self, PyObject* args) |
606 |
{ |
607 |
int index; |
608 |
SHPObject* object; |
609 |
SHPObjectObject* result; |
610 |
|
611 |
if (!PyArg_ParseTuple(args, "i:read_object", &index)) return NULL; |
612 |
|
613 |
object = SHPReadObject(self->handle, index); |
614 |
if (!object) |
615 |
{ |
616 |
PyErr_SetString(PyExc_RuntimeError, "failed to read object"); |
617 |
return NULL; |
618 |
} |
619 |
|
620 |
result = PyObject_New(SHPObjectObject, &SHPObjectType); |
621 |
if (!result) |
622 |
{ |
623 |
return PyErr_NoMemory(); |
624 |
} |
625 |
|
626 |
result->shpObject = object; |
627 |
return (PyObject*) result; |
628 |
} |
629 |
|
630 |
|
631 |
|
632 |
static PyObject* shapefile_write_object(ShapeFileObject* self, PyObject* args) |
633 |
{ |
634 |
int index, result; |
635 |
PyObject* object; |
636 |
|
637 |
if (!PyArg_ParseTuple(args, "iO:write_object", &index, &object)) return NULL; |
638 |
|
639 |
if (!PyObject_IsInstance(object, (PyObject*)&SHPObjectType)) |
640 |
{ |
641 |
PyErr_SetString(PyExc_TypeError, "object is not a SHPObject"); |
642 |
return NULL; |
643 |
} |
644 |
|
645 |
result = SHPWriteObject(self->handle, index, ((SHPObjectObject*)object)->shpObject); |
646 |
if (result < 0) |
647 |
{ |
648 |
PyErr_SetString(PyExc_RuntimeError, "failed to write object"); |
649 |
return NULL; |
650 |
} |
651 |
return PyInt_FromLong((long)result); |
652 |
} |
653 |
|
654 |
static PyObject* shapefile_cobject(ShapeFileObject* self) |
655 |
{ |
656 |
return PyCObject_FromVoidPtr(self->handle, NULL); |
657 |
} |
658 |
|
659 |
static PyObject* shapefile_repr(ShapeFileObject* self) |
660 |
{ |
661 |
/* TODO: it would be nice to do something like "shapelib.ShapeFile(filename, mode)" instead */ |
662 |
return PyString_FromFormat("<shapelib.ShapeFile object at %p>", self->handle); |
663 |
} |
664 |
|
665 |
static struct PyMethodDef shapefile_methods[] = |
666 |
{ |
667 |
{"close", (PyCFunction)shapefile_close, METH_NOARGS, |
668 |
"close() -> None\n\n" |
669 |
"close the shape file" }, |
670 |
{"info", (PyCFunction)shapefile_info, METH_NOARGS, |
671 |
"info() -> (num_shapes, type, (x_min, y_min, z_min, m_min), (x_max, y_max, z_max, m_max))\n\n" |
672 |
"returns info about ShapeFile with:\n" |
673 |
"- num_shapes: the number of the objects in the file\n" |
674 |
"- type: the type of the shape file (SHPT_POINT, SHPT_POLYGON, ...)\n" |
675 |
"- (x_min, ...), (x_max, ...): 4D bounding box of the data in the shape file" }, |
676 |
{"read_object", (PyCFunction)shapefile_read_object, METH_VARARGS, |
677 |
"read_object(id) -> SHPObject\n\n" |
678 |
"Returns shape indexed by id" }, |
679 |
{"write_object", (PyCFunction)shapefile_write_object, METH_VARARGS, |
680 |
"write_object(id, object) -> id\n\n" |
681 |
"Write an object at index id, and returns id." |
682 |
"If id == -1, the object is appended at the end of the shape file."}, |
683 |
{"cobject", (PyCFunction)shapefile_cobject, METH_NOARGS, |
684 |
"cobject() -> CObject\n\n" |
685 |
"Return the shapelib SHPHandle as a Python CObject"}, |
686 |
{NULL} |
687 |
}; |
688 |
|
689 |
static struct PyGetSetDef shapefile_getsetters[] = |
690 |
{ |
691 |
{NULL} |
692 |
}; |
693 |
|
694 |
static PyTypeObject ShapeFileType = PYSHAPELIB_DEFINE_TYPE(ShapeFileObject, shapefile, "shapelib.ShapeFile", 0); |
695 |
|
696 |
/* --- shapelib ------------------------------------------------------------------------------------------------------ */ |
697 |
|
698 |
static PyObject* shapelib_open(PyObject* module, PyObject* args) |
699 |
{ |
700 |
return PyObject_CallObject((PyObject*)&ShapeFileType, args); |
701 |
} |
702 |
|
703 |
static PyObject* shapelib_create(PyObject* module, PyObject* args) |
704 |
{ |
705 |
SAHooks hooks; |
706 |
char* file; |
707 |
int type; |
708 |
ShapeFileObject* result; |
709 |
SHPHandle handle = NULL; |
710 |
int wideargument = 0; |
711 |
|
712 |
if (!PyArg_ParseTuple(args, "eti:create", PYSHAPELIB_FILENAME_ENCODING, &file, &type)) return NULL; |
713 |
|
714 |
PYSHAPELIB_SETUPHOOKS(&hooks); |
715 |
handle = SHPCreateLL(file, type, &hooks); |
716 |
if (!handle) |
717 |
{ |
718 |
PyErr_SetFromErrnoWithFilename(PyExc_IOError, file); |
719 |
PyMem_Free(file); |
720 |
return NULL; |
721 |
} |
722 |
PyMem_Free(file); |
723 |
|
724 |
result = PyObject_New(ShapeFileObject, &ShapeFileType); |
725 |
if (!result) |
726 |
{ |
727 |
SHPClose(handle); |
728 |
return PyErr_NoMemory(); |
729 |
} |
730 |
|
731 |
result->handle = handle; |
732 |
return (PyObject*) result; |
733 |
} |
734 |
|
735 |
static PyShapeLibAPI shapelib_the_api = |
736 |
{ |
737 |
SHPReadObject, |
738 |
SHPDestroyObject, |
739 |
SHPCreateTree, |
740 |
SHPDestroyTree, |
741 |
SHPTreeFindLikelyShapes |
742 |
}; |
743 |
|
744 |
static PyObject* shapelib_c_api(PyObject* module) |
745 |
{ |
746 |
return PyCObject_FromVoidPtr(&shapelib_the_api, NULL); |
747 |
} |
748 |
|
749 |
static PyObject* shapelib_type_name(PyObject* module, PyObject* args) |
750 |
{ |
751 |
int type; |
752 |
if (!PyArg_ParseTuple(args, "i:type_name", &type)) return NULL; |
753 |
return PyString_FromString(SHPTypeName(type)); |
754 |
} |
755 |
|
756 |
static PyObject* shapelib_part_type_name(PyObject* module, PyObject* args) |
757 |
{ |
758 |
int type; |
759 |
if (!PyArg_ParseTuple(args, "i:part_type_name", &type)) return NULL; |
760 |
return PyString_FromString(SHPPartTypeName(type)); |
761 |
} |
762 |
|
763 |
static struct PyMethodDef shapelib_methods[] = |
764 |
{ |
765 |
{"open", (PyCFunction)shapelib_open, METH_VARARGS, |
766 |
"open(name [, mode='rb']) -> ShapeFile\n\n" |
767 |
"opens a ShapeFile" }, |
768 |
{"create", (PyCFunction)shapelib_create, METH_VARARGS, |
769 |
"create(name, type) -> ShapeFile\n\n" |
770 |
"creates a ShapeFile of a certain type (one of SHPT_POINT, SHPT_POLYGON)" }, |
771 |
{"c_api", (PyCFunction)shapelib_c_api, METH_NOARGS, |
772 |
"c_api() -> CObject\n\n" |
773 |
"get C API of shapelib as a CObject" }, |
774 |
{"type_name", (PyCFunction)shapelib_type_name, METH_VARARGS, |
775 |
"type_name(type) -> string\n\n" |
776 |
"return type as string" }, |
777 |
{"part_type_name", (PyCFunction)shapelib_part_type_name, METH_VARARGS, |
778 |
"part_type_name(part_type) -> string\n\n" |
779 |
"return part type as string" }, |
780 |
{NULL} |
781 |
}; |
782 |
|
783 |
PyMODINIT_FUNC initshapelib(void) |
784 |
{ |
785 |
PyObject* module = Py_InitModule("shapelib", shapelib_methods); |
786 |
if (!module) return; |
787 |
|
788 |
PYSHAPELIB_ADD_TYPE(SHPObjectType, "SHPObject"); |
789 |
PYSHAPELIB_ADD_TYPE(ShapeFileType, "ShapeFile"); |
790 |
|
791 |
PyModule_AddIntConstant(module, "_have_utf8_hooks", HAVE_UTF8_HOOKS); |
792 |
|
793 |
PYSHAPELIB_ADD_CONSTANT(SHPT_NULL); |
794 |
PYSHAPELIB_ADD_CONSTANT(SHPT_POINT); |
795 |
PYSHAPELIB_ADD_CONSTANT(SHPT_ARC); |
796 |
PYSHAPELIB_ADD_CONSTANT(SHPT_POLYGON); |
797 |
PYSHAPELIB_ADD_CONSTANT(SHPT_MULTIPOINT); |
798 |
PYSHAPELIB_ADD_CONSTANT(SHPT_POINTZ); |
799 |
PYSHAPELIB_ADD_CONSTANT(SHPT_ARCZ); |
800 |
PYSHAPELIB_ADD_CONSTANT(SHPT_POLYGONZ); |
801 |
PYSHAPELIB_ADD_CONSTANT(SHPT_MULTIPOINTZ); |
802 |
PYSHAPELIB_ADD_CONSTANT(SHPT_POINTM); |
803 |
PYSHAPELIB_ADD_CONSTANT(SHPT_ARCM); |
804 |
PYSHAPELIB_ADD_CONSTANT(SHPT_POLYGONM); |
805 |
PYSHAPELIB_ADD_CONSTANT(SHPT_MULTIPOINTM); |
806 |
PYSHAPELIB_ADD_CONSTANT(SHPT_MULTIPATCH); |
807 |
PYSHAPELIB_ADD_CONSTANT(SHPP_TRISTRIP); |
808 |
PYSHAPELIB_ADD_CONSTANT(SHPP_TRIFAN); |
809 |
PYSHAPELIB_ADD_CONSTANT(SHPP_OUTERRING); |
810 |
PYSHAPELIB_ADD_CONSTANT(SHPP_INNERRING); |
811 |
PYSHAPELIB_ADD_CONSTANT(SHPP_FIRSTRING); |
812 |
PYSHAPELIB_ADD_CONSTANT(SHPP_RING); |
813 |
} |
814 |
|