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