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 |
2735 |
/* The constructor of SHPObject. parts is a list of lists of tuples |
71 |
|
|
* describing the parts and their vertices just likethe output of the |
72 |
|
|
* vertices() method. part_type_list is the list of part-types and may |
73 |
|
|
* be NULL. For the meaning of the part-types and their default value |
74 |
|
|
* see the Shaplib documentation. |
75 |
|
|
*/ |
76 |
bramz |
2742 |
static int shpobject_init(SHPObjectObject* self, PyObject* args, PyObject* kwds) |
77 |
bramz |
2735 |
{ |
78 |
|
|
int type; |
79 |
|
|
int id; |
80 |
|
|
PyObject* parts = NULL; |
81 |
|
|
PyObject* part_type_list = NULL; |
82 |
|
|
|
83 |
|
|
int num_parts; |
84 |
|
|
int num_vertices; |
85 |
|
|
int part_start; |
86 |
|
|
|
87 |
|
|
double* xs = NULL; |
88 |
|
|
double* ys = NULL; |
89 |
bramz |
2743 |
double* zs = NULL; |
90 |
|
|
double* ms = NULL; |
91 |
bramz |
2735 |
int* part_starts = NULL; |
92 |
|
|
int* part_types = NULL; |
93 |
|
|
|
94 |
bramz |
2743 |
PyObject* part; |
95 |
|
|
int vertex_type; |
96 |
|
|
int has_z; |
97 |
|
|
int has_m; |
98 |
|
|
|
99 |
bramz |
2735 |
int i; |
100 |
bramz |
2743 |
int ok, return_code = -1; |
101 |
bramz |
2735 |
|
102 |
|
|
/* first, unpack parameters */ |
103 |
|
|
if (kwds != NULL && PyDict_Size(kwds) > 0) |
104 |
|
|
{ |
105 |
|
|
PyErr_Format(PyExc_TypeError, "shapelib.SHPObject.__init__ takes no keyword arguments"); |
106 |
|
|
return -1; |
107 |
|
|
} |
108 |
|
|
if (!PyArg_ParseTuple(args, "iiO|O", &type, &id, &parts, &part_type_list)) return -1; |
109 |
|
|
|
110 |
bramz |
2743 |
/* check parts */ |
111 |
bramz |
2735 |
if (!PySequence_Check(parts)) |
112 |
|
|
{ |
113 |
|
|
PyErr_SetString(PyExc_TypeError, "parts is not a sequence"); |
114 |
|
|
return -1; |
115 |
|
|
} |
116 |
|
|
num_parts = PySequence_Length(parts); |
117 |
|
|
if (num_parts < 0) |
118 |
|
|
{ |
119 |
|
|
PyErr_SetString(PyExc_TypeError, "cannot determine length of parts"); |
120 |
|
|
return -1; |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
/* parts and part_types have to have the same lengths */ |
124 |
bramz |
2741 |
if (part_type_list == Py_None) |
125 |
|
|
{ |
126 |
|
|
Py_DECREF(part_type_list); |
127 |
|
|
part_type_list = NULL; |
128 |
|
|
} |
129 |
bramz |
2735 |
if (part_type_list) |
130 |
|
|
{ |
131 |
|
|
if (!PySequence_Check(parts)) |
132 |
|
|
{ |
133 |
|
|
PyErr_SetString(PyExc_TypeError, "part_type_list is not a sequence"); |
134 |
|
|
return -1; |
135 |
|
|
} |
136 |
|
|
if (PySequence_Length(part_type_list) != num_parts) |
137 |
|
|
{ |
138 |
|
|
PyErr_SetString(PyExc_TypeError, "parts and part_types have to have the same lengths"); |
139 |
|
|
return -1; |
140 |
|
|
} |
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
/* determine how many vertices there are altogether */ |
144 |
|
|
num_vertices = 0; |
145 |
|
|
for (i = 0; i < num_parts; ++i) |
146 |
|
|
{ |
147 |
|
|
PyObject* part = PySequence_ITEM(parts, i); |
148 |
|
|
if (!PySequence_Check(part)) |
149 |
|
|
{ |
150 |
|
|
PyErr_SetString(PyExc_TypeError, "at least one item in parts is not a sequence"); |
151 |
|
|
Py_DECREF(part); |
152 |
|
|
return -1; |
153 |
|
|
} |
154 |
|
|
num_vertices += PySequence_Length(part); |
155 |
|
|
Py_DECREF(part); |
156 |
|
|
} |
157 |
bramz |
2743 |
|
158 |
|
|
|
159 |
|
|
vertex_type = determine_vertex_type(type, &has_z, &has_m); |
160 |
bramz |
2735 |
|
161 |
|
|
/* allocate the memory for the various arrays and check for memory errors */ |
162 |
|
|
xs = malloc(num_vertices * sizeof(double)); |
163 |
|
|
ys = malloc(num_vertices * sizeof(double)); |
164 |
bramz |
2743 |
zs = has_z ? malloc(num_vertices * sizeof(double)) : NULL; |
165 |
|
|
ms = has_m ? malloc(num_vertices * sizeof(double)) : NULL; |
166 |
bramz |
2735 |
part_starts = malloc(num_parts * sizeof(int)); |
167 |
|
|
part_types = part_type_list ? malloc(num_parts * sizeof(int)) : 0; |
168 |
|
|
|
169 |
bramz |
2743 |
if (!xs || !ys || (has_z && !zs) || (has_m && !ms) || !part_starts || (part_type_list && !part_types)) |
170 |
bramz |
2735 |
{ |
171 |
|
|
PyErr_NoMemory(); |
172 |
|
|
goto exit; |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
/* convert the part types */ |
176 |
|
|
if (part_type_list) |
177 |
|
|
{ |
178 |
|
|
for (i = 0; i < num_parts; i++) |
179 |
|
|
{ |
180 |
|
|
PyObject* otype = PySequence_ITEM(part_type_list, i); |
181 |
|
|
part_types[i] = PyInt_AsLong(otype); |
182 |
|
|
Py_DECREF(otype); |
183 |
|
|
if (part_types[i] < 0) |
184 |
|
|
{ |
185 |
|
|
PyErr_SetString(PyExc_TypeError, "at least one item in part_type_list is not an integer or is negative"); |
186 |
|
|
goto exit; |
187 |
|
|
} |
188 |
|
|
} |
189 |
|
|
} |
190 |
|
|
|
191 |
|
|
/* convert the list of parts */ |
192 |
|
|
part_start = 0; |
193 |
|
|
for (i = 0; i < num_parts; ++i) |
194 |
|
|
{ |
195 |
|
|
int j, length; |
196 |
|
|
|
197 |
bramz |
2743 |
part = PySequence_ITEM(parts, i); |
198 |
bramz |
2735 |
length = PySequence_Length(part); |
199 |
bramz |
2743 |
if (length < 0) goto exit; |
200 |
bramz |
2735 |
part_starts[i] = part_start; |
201 |
|
|
|
202 |
|
|
for (j = 0; j < length; ++j) |
203 |
|
|
{ |
204 |
|
|
PyObject* vertex = PySequence_ITEM(part, j); |
205 |
bramz |
2743 |
switch (vertex_type) |
206 |
bramz |
2735 |
{ |
207 |
bramz |
2743 |
case vtXY: |
208 |
|
|
ok = PyArg_ParseTuple(vertex, "dd", xs + part_start + j, ys + part_start + j); |
209 |
|
|
break; |
210 |
|
|
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); |
220 |
|
|
if (!ok) |
221 |
|
|
{ |
222 |
|
|
PyErr_SetString(PyExc_TypeError, "at least one vertex is of the wrong format"); |
223 |
bramz |
2735 |
goto exit; |
224 |
|
|
} |
225 |
|
|
} |
226 |
|
|
Py_DECREF(part); |
227 |
bramz |
2743 |
part = NULL; |
228 |
bramz |
2735 |
part_start += length; |
229 |
|
|
} |
230 |
|
|
|
231 |
bramz |
2743 |
self->shpObject = SHPCreateObject(type, id, num_parts, part_starts, part_types, num_vertices, xs, ys, zs, ms); |
232 |
bramz |
2735 |
return_code = 0; |
233 |
|
|
|
234 |
|
|
exit: |
235 |
bramz |
2743 |
Py_XDECREF(part); |
236 |
bramz |
2735 |
free(xs); |
237 |
|
|
free(ys); |
238 |
bramz |
2743 |
free(zs); |
239 |
|
|
free(ms); |
240 |
bramz |
2735 |
free(part_starts); |
241 |
|
|
free(part_types); |
242 |
|
|
return return_code; |
243 |
|
|
} |
244 |
|
|
|
245 |
jan |
1611 |
/* |
246 |
bramz |
2735 |
* The extents() method of SHPObject. |
247 |
|
|
* |
248 |
|
|
* Return the extents as a tuple of two 4-element lists with the min. |
249 |
|
|
* and max. values of x, y, z, m. |
250 |
|
|
*/ |
251 |
bramz |
2742 |
static PyObject* shpobject_extents(SHPObjectObject* self) |
252 |
jan |
1611 |
{ |
253 |
bramz |
2735 |
SHPObject* object = self->shpObject; |
254 |
|
|
return Py_BuildValue("(dddd)(dddd)", |
255 |
|
|
object->dfXMin, object->dfYMin, object->dfZMin, object->dfMMin, |
256 |
|
|
object->dfXMax, object->dfYMax, object->dfZMax, object->dfMMax); |
257 |
jan |
1611 |
} |
258 |
|
|
|
259 |
|
|
|
260 |
|
|
/* |
261 |
bramz |
2735 |
* The vertices() method of SHPObject. |
262 |
|
|
* |
263 |
|
|
* Return the x and y coords of the vertices as a list of lists of |
264 |
|
|
* tuples. |
265 |
|
|
*/ |
266 |
jan |
1611 |
|
267 |
bramz |
2743 |
static PyObject* build_vertex_list(SHPObject *object, int index, int length, int vertex_type); |
268 |
jan |
1611 |
|
269 |
bramz |
2742 |
static PyObject* shpobject_vertices(SHPObjectObject* self) |
270 |
jan |
1611 |
{ |
271 |
bramz |
2735 |
PyObject *result = NULL; |
272 |
|
|
PyObject *part = NULL; |
273 |
|
|
int part_idx, vertex_idx; |
274 |
|
|
int length = 0; |
275 |
bramz |
2743 |
int vertex_type; |
276 |
|
|
|
277 |
bramz |
2735 |
SHPObject* object = self->shpObject; |
278 |
bramz |
2743 |
vertex_type = determine_vertex_type(object->nSHPType, NULL, NULL); |
279 |
jan |
1611 |
|
280 |
bramz |
2735 |
if (object->nParts > 0) |
281 |
|
|
{ |
282 |
|
|
/* A multipart shape. Usual for SHPT_ARC and SHPT_POLYGON */ |
283 |
jan |
1611 |
|
284 |
bramz |
2735 |
result = PyList_New(object->nParts); |
285 |
bramz |
2741 |
if (!result) |
286 |
|
|
return NULL; |
287 |
jan |
1611 |
|
288 |
bramz |
2741 |
for (part_idx = 0, vertex_idx = 0; part_idx < object->nParts; part_idx++) |
289 |
|
|
{ |
290 |
|
|
if (part_idx < object->nParts - 1) |
291 |
bramz |
2743 |
length = (object->panPartStart[part_idx + 1] |
292 |
|
|
- object->panPartStart[part_idx]); |
293 |
bramz |
2741 |
else |
294 |
bramz |
2743 |
length = object->nVertices - object->panPartStart[part_idx]; |
295 |
bramz |
2741 |
|
296 |
bramz |
2743 |
part = build_vertex_list(object, vertex_idx, length, vertex_type); |
297 |
|
|
if (!part) goto fail; |
298 |
jan |
1611 |
|
299 |
bramz |
2743 |
if (PyList_SetItem(result, part_idx, part) < 0) goto fail; |
300 |
jan |
1611 |
|
301 |
bramz |
2741 |
vertex_idx += length; |
302 |
|
|
} |
303 |
jan |
1611 |
} |
304 |
bramz |
2735 |
else |
305 |
|
|
{ |
306 |
bramz |
2741 |
/* only one part. usual for SHPT_POINT */ |
307 |
bramz |
2743 |
result = build_vertex_list(object, 0, object->nVertices, vertex_type); |
308 |
bramz |
2735 |
} |
309 |
jan |
1611 |
|
310 |
bramz |
2735 |
return result; |
311 |
jan |
1611 |
|
312 |
bramz |
2735 |
fail: |
313 |
|
|
Py_XDECREF(part); |
314 |
|
|
Py_DECREF(result); |
315 |
|
|
return NULL; |
316 |
jan |
1611 |
} |
317 |
|
|
|
318 |
|
|
|
319 |
|
|
/* Return the length coordinates of the shape object starting at vertex |
320 |
bramz |
2735 |
* index as a Python-list of tuples. Helper function for |
321 |
|
|
* SHPObject_vertices. |
322 |
|
|
*/ |
323 |
bramz |
2743 |
static PyObject* build_vertex_list(SHPObject *object, int index, int length, int vertex_type) |
324 |
jan |
1611 |
{ |
325 |
bramz |
2735 |
int i; |
326 |
|
|
PyObject * list; |
327 |
|
|
PyObject * vertex = NULL; |
328 |
jan |
1611 |
|
329 |
bramz |
2735 |
list = PyList_New(length); |
330 |
|
|
if (!list) |
331 |
bramz |
2741 |
return NULL; |
332 |
jan |
1611 |
|
333 |
bramz |
2735 |
for (i = 0; i < length; i++, index++) |
334 |
|
|
{ |
335 |
bramz |
2743 |
switch (vertex_type) |
336 |
|
|
{ |
337 |
|
|
case vtXY: |
338 |
|
|
vertex = Py_BuildValue("dd", object->padfX[index], object->padfY[index]); |
339 |
|
|
break; |
340 |
|
|
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 |
bramz |
2741 |
goto fail; |
349 |
bramz |
2743 |
} |
350 |
|
|
|
351 |
|
|
if (!vertex || PyList_SetItem(list, i, vertex) < 0) goto fail; |
352 |
bramz |
2735 |
} |
353 |
bramz |
2743 |
|
354 |
bramz |
2735 |
return list; |
355 |
jan |
1611 |
|
356 |
bramz |
2735 |
fail: |
357 |
|
|
Py_DECREF(list); |
358 |
|
|
return NULL; |
359 |
jan |
1611 |
} |
360 |
|
|
|
361 |
bramz |
2741 |
|
362 |
|
|
|
363 |
bramz |
2742 |
static PyObject* shpobject_part_types(SHPObjectObject* self) |
364 |
bramz |
2741 |
{ |
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 |
bramz |
2742 |
static PyObject* shpobject_type(SHPObjectObject* self, void* closure) |
393 |
bramz |
2735 |
{ |
394 |
|
|
return PyInt_FromLong(self->shpObject->nSHPType); |
395 |
|
|
} |
396 |
jan |
1611 |
|
397 |
bramz |
2741 |
|
398 |
|
|
|
399 |
bramz |
2742 |
static PyObject* shpobject_id(SHPObjectObject* self, void* closure) |
400 |
bramz |
2735 |
{ |
401 |
|
|
return PyInt_FromLong(self->shpObject->nShapeId); |
402 |
|
|
} |
403 |
jan |
1611 |
|
404 |
bramz |
2741 |
|
405 |
|
|
|
406 |
|
|
/* return a string that can be feeded to eval() to reconstruct the object, |
407 |
|
|
* assuming a proper context |
408 |
|
|
*/ |
409 |
bramz |
2742 |
static PyObject* shpobject_repr(SHPObjectObject* self) |
410 |
bramz |
2741 |
{ |
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 |
bramz |
2742 |
shpobject_vertices(self), |
422 |
|
|
shpobject_part_types(self)); |
423 |
bramz |
2741 |
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 |
bramz |
2742 |
static struct PyMethodDef shpobject_methods[] = |
438 |
bramz |
2735 |
{ |
439 |
bramz |
2742 |
{"extents", (PyCFunction)shpobject_extents, METH_NOARGS, NULL}, |
440 |
|
|
{"vertices", (PyCFunction)shpobject_vertices, METH_NOARGS, NULL}, |
441 |
|
|
{"part_types", (PyCFunction)shpobject_part_types, METH_NOARGS, NULL}, |
442 |
bramz |
2735 |
{NULL} |
443 |
|
|
}; |
444 |
jan |
1611 |
|
445 |
bramz |
2742 |
static struct PyGetSetDef shpobject_getsetters[] = |
446 |
jan |
1611 |
{ |
447 |
bramz |
2742 |
{"type", (getter)shpobject_type, NULL, NULL }, |
448 |
|
|
{"id", (getter)shpobject_id, NULL, NULL }, |
449 |
bramz |
2735 |
{NULL} |
450 |
|
|
}; |
451 |
jan |
1611 |
|
452 |
bramz |
2742 |
static PyTypeObject SHPObjectType = PYSHAPELIB_DEFINE_TYPE(SHPObjectObject, shpobject, "shapelib.SHPObject", 0); |
453 |
jan |
1611 |
|
454 |
|
|
|
455 |
bramz |
2735 |
/* --- ShapeFile ----------------------------------------------------------------------------------------------------- */ |
456 |
jan |
1611 |
|
457 |
bramz |
2735 |
typedef struct |
458 |
|
|
{ |
459 |
|
|
PyObject_HEAD |
460 |
|
|
SHPHandle handle; |
461 |
|
|
} |
462 |
bramz |
2742 |
ShapeFileObject; |
463 |
jan |
1611 |
|
464 |
bramz |
2741 |
/* allocator |
465 |
|
|
*/ |
466 |
bramz |
2742 |
static PyObject* shapefile_new(PyTypeObject* type, PyObject* args, PyObject* kwds) |
467 |
bramz |
2735 |
{ |
468 |
bramz |
2742 |
ShapeFileObject* self; |
469 |
|
|
self = (ShapeFileObject*) type->tp_alloc(type, 0); |
470 |
bramz |
2735 |
self->handle = NULL; |
471 |
|
|
return (PyObject*) self; |
472 |
|
|
} |
473 |
jan |
1611 |
|
474 |
bramz |
2742 |
/* 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 |
bramz |
2741 |
/* constructor |
483 |
|
|
*/ |
484 |
bramz |
2742 |
static int shapefile_init(ShapeFileObject* self, PyObject* args, PyObject* kwds) |
485 |
bramz |
2735 |
{ |
486 |
|
|
char* file; |
487 |
|
|
char* mode = "rb"; |
488 |
|
|
if (kwds != NULL && PyDict_Size(kwds) > 0) |
489 |
|
|
{ |
490 |
|
|
PyErr_Format(PyExc_TypeError, "shapelib.ShapeFile.__init__ takes no keyword arguments"); |
491 |
|
|
return -1; |
492 |
|
|
} |
493 |
|
|
if (!PyArg_ParseTuple(args, "s|s", &file, &mode)) return -1; |
494 |
|
|
|
495 |
|
|
self->handle = SHPOpen(file, mode); |
496 |
|
|
return self->handle ? 0 : -1; |
497 |
|
|
} |
498 |
jan |
1611 |
|
499 |
bramz |
2742 |
static PyObject* shapefile_close(ShapeFileObject* self) |
500 |
bramz |
2735 |
{ |
501 |
|
|
SHPClose(self->handle); |
502 |
|
|
self->handle = NULL; |
503 |
|
|
Py_RETURN_NONE; |
504 |
|
|
} |
505 |
jan |
1611 |
|
506 |
bramz |
2742 |
static PyObject* shapefile_info(ShapeFileObject* self) |
507 |
bramz |
2735 |
{ |
508 |
|
|
SHPHandle handle = self->handle; |
509 |
|
|
return Py_BuildValue("ii(dddd)(dddd)", |
510 |
|
|
handle->nRecords, handle->nShapeType, |
511 |
|
|
handle->adBoundsMin[0], handle->adBoundsMin[1], handle->adBoundsMin[2], handle->adBoundsMin[3], |
512 |
|
|
handle->adBoundsMax[0], handle->adBoundsMax[1], handle->adBoundsMax[2], handle->adBoundsMax[3]); |
513 |
|
|
} |
514 |
jan |
1611 |
|
515 |
bramz |
2742 |
static PyObject* shapefile_read_object(ShapeFileObject* self, PyObject* args) |
516 |
bramz |
2735 |
{ |
517 |
|
|
int index; |
518 |
|
|
SHPObject* object; |
519 |
bramz |
2742 |
SHPObjectObject* result; |
520 |
bramz |
2735 |
|
521 |
|
|
if (!PyArg_ParseTuple(args, "i", &index)) return NULL; |
522 |
|
|
|
523 |
|
|
object = SHPReadObject(self->handle, index); |
524 |
|
|
if (!object) |
525 |
jan |
1611 |
{ |
526 |
bramz |
2735 |
PyErr_SetString(PyExc_RuntimeError, "failed to read object"); |
527 |
jan |
1611 |
return NULL; |
528 |
|
|
} |
529 |
bramz |
2735 |
|
530 |
bramz |
2742 |
result = PyObject_New(SHPObjectObject, &SHPObjectType); |
531 |
bramz |
2735 |
if (!result) |
532 |
jan |
1611 |
{ |
533 |
bramz |
2735 |
return PyErr_NoMemory(); |
534 |
jan |
1611 |
} |
535 |
bramz |
2735 |
|
536 |
|
|
result->shpObject = object; |
537 |
|
|
return (PyObject*) result; |
538 |
jan |
1611 |
} |
539 |
|
|
|
540 |
bramz |
2742 |
static PyObject* shapefile_write_object(ShapeFileObject* self, PyObject* args) |
541 |
bramz |
2735 |
{ |
542 |
|
|
int index, result; |
543 |
|
|
PyObject* object; |
544 |
|
|
|
545 |
|
|
if (!PyArg_ParseTuple(args, "iO", &index, &object)) return NULL; |
546 |
|
|
|
547 |
bramz |
2742 |
if (!PyObject_IsInstance(object, (PyObject*)&SHPObjectType)) |
548 |
bramz |
2735 |
{ |
549 |
|
|
PyErr_SetString(PyExc_TypeError, "object is not a SHPObject"); |
550 |
|
|
return NULL; |
551 |
|
|
} |
552 |
|
|
|
553 |
bramz |
2742 |
result = SHPWriteObject(self->handle, index, ((SHPObjectObject*)object)->shpObject); |
554 |
bramz |
2735 |
if (result < 0) |
555 |
|
|
{ |
556 |
|
|
PyErr_SetString(PyExc_RuntimeError, "failed to write object"); |
557 |
|
|
return NULL; |
558 |
|
|
} |
559 |
|
|
return PyInt_FromLong((long)result); |
560 |
jan |
1611 |
} |
561 |
|
|
|
562 |
bramz |
2742 |
static PyObject* shapefile_cobject(ShapeFileObject* self) |
563 |
bramz |
2735 |
{ |
564 |
|
|
return PyCObject_FromVoidPtr(self->handle, NULL); |
565 |
jan |
1611 |
} |
566 |
|
|
|
567 |
bramz |
2742 |
static PyObject* shapefile_repr(ShapeFileObject* self) |
568 |
bramz |
2741 |
{ |
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 |
bramz |
2742 |
static struct PyMethodDef shapefile_methods[] = |
574 |
bramz |
2735 |
{ |
575 |
bramz |
2742 |
{"close", (PyCFunction)shapefile_close, METH_NOARGS, "close the shape file" }, |
576 |
|
|
{"info", (PyCFunction)shapefile_info, METH_NOARGS, |
577 |
bramz |
2735 |
"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." }, |
579 |
bramz |
2742 |
{"read_object", (PyCFunction)shapefile_read_object, METH_VARARGS, "Return object number i" }, |
580 |
|
|
{"write_object", (PyCFunction)shapefile_write_object, METH_VARARGS, "Write an object"}, |
581 |
|
|
{"cobject", (PyCFunction)shapefile_cobject, METH_NOARGS, "Return the shapelib SHPHandle as a Python CObject"}, |
582 |
bramz |
2735 |
{NULL} |
583 |
|
|
}; |
584 |
jan |
1611 |
|
585 |
bramz |
2742 |
static struct PyGetSetDef shapefile_getsetters[] = |
586 |
bramz |
2735 |
{ |
587 |
|
|
{NULL} |
588 |
|
|
}; |
589 |
jan |
1611 |
|
590 |
bramz |
2742 |
static PyTypeObject ShapeFileType = PYSHAPELIB_DEFINE_TYPE(ShapeFileObject, shapefile, "shapelib.ShapeFile", 0); |
591 |
jan |
1611 |
|
592 |
bramz |
2735 |
/* --- shapelib ------------------------------------------------------------------------------------------------------ */ |
593 |
jan |
1611 |
|
594 |
bramz |
2735 |
static PyObject* shapelib_open(PyObject* module, PyObject* args) |
595 |
|
|
{ |
596 |
bramz |
2742 |
return PyObject_CallObject((PyObject*)&ShapeFileType, args); |
597 |
jan |
1611 |
} |
598 |
|
|
|
599 |
bramz |
2735 |
static PyObject* shapelib_create(PyObject* module, PyObject* args) |
600 |
jan |
1611 |
{ |
601 |
bramz |
2735 |
char* file; |
602 |
|
|
int type; |
603 |
bramz |
2742 |
ShapeFileObject* result; |
604 |
bramz |
2735 |
|
605 |
|
|
if (!PyArg_ParseTuple(args, "si", &file, &type)) return NULL; |
606 |
|
|
|
607 |
bramz |
2742 |
result = PyObject_New(ShapeFileObject, &ShapeFileType); |
608 |
bramz |
2735 |
if (!result) |
609 |
|
|
{ |
610 |
|
|
return PyErr_NoMemory(); |
611 |
jan |
1611 |
} |
612 |
bramz |
2735 |
|
613 |
|
|
result->handle = SHPCreate(file, type); |
614 |
|
|
if (!result->handle) |
615 |
|
|
{ |
616 |
|
|
PyObject_Del((PyObject*)result); |
617 |
|
|
PyErr_SetString(PyExc_RuntimeError, "Failed to create ShapeFile"); |
618 |
|
|
return NULL; |
619 |
jan |
1611 |
} |
620 |
bramz |
2735 |
|
621 |
|
|
return (PyObject*) result; |
622 |
|
|
} |
623 |
|
|
|
624 |
|
|
static PyShapeLibAPI shapelib_the_api = |
625 |
|
|
{ |
626 |
jan |
1611 |
SHPReadObject, |
627 |
|
|
SHPDestroyObject, |
628 |
|
|
SHPCreateTree, |
629 |
|
|
SHPDestroyTree, |
630 |
|
|
SHPTreeFindLikelyShapes |
631 |
bramz |
2735 |
}; |
632 |
jan |
1611 |
|
633 |
bramz |
2735 |
static PyObject* shapelib_c_api(PyObject* module) |
634 |
|
|
{ |
635 |
|
|
return PyCObject_FromVoidPtr(&shapelib_the_api, NULL); |
636 |
|
|
} |
637 |
jan |
1611 |
|
638 |
bramz |
2735 |
static PyObject* shapelib_type_name(PyObject* module, PyObject* args) |
639 |
|
|
{ |
640 |
|
|
int type; |
641 |
|
|
if (!PyArg_ParseTuple(args, "i", &type)) return NULL; |
642 |
|
|
return PyString_FromString(SHPTypeName(type)); |
643 |
|
|
} |
644 |
jan |
1611 |
|
645 |
bramz |
2735 |
static PyObject* shapelib_part_type_name(PyObject* module, PyObject* args) |
646 |
|
|
{ |
647 |
|
|
int type; |
648 |
|
|
if (!PyArg_ParseTuple(args, "i", &type)) return NULL; |
649 |
|
|
return PyString_FromString(SHPPartTypeName(type)); |
650 |
|
|
} |
651 |
jan |
1611 |
|
652 |
bramz |
2742 |
static struct PyMethodDef shapelib_methods[] = |
653 |
bramz |
2735 |
{ |
654 |
|
|
{"open", (PyCFunction)shapelib_open, METH_VARARGS, "open a ShapeFile" }, |
655 |
|
|
{"create", (PyCFunction)shapelib_create, METH_VARARGS, "create a ShapeFile" }, |
656 |
|
|
{"c_api", (PyCFunction)shapelib_c_api, METH_NOARGS, "get C API of shapelib" }, |
657 |
|
|
{"type_name", (PyCFunction)shapelib_type_name, METH_VARARGS, "return type as string" }, |
658 |
|
|
{"part_type_name", (PyCFunction)shapelib_part_type_name, METH_VARARGS, "return part type as string" }, |
659 |
|
|
{NULL} |
660 |
|
|
}; |
661 |
jan |
1611 |
|
662 |
bramz |
2735 |
PyMODINIT_FUNC initshapelib(void) |
663 |
|
|
{ |
664 |
|
|
PyObject* module = Py_InitModule("shapelib", shapelib_methods); |
665 |
|
|
if (!module) return; |
666 |
|
|
|
667 |
bramz |
2742 |
PYSHAPELIB_ADD_TYPE(SHPObjectType, "SHPObject"); |
668 |
|
|
PYSHAPELIB_ADD_TYPE(ShapeFileType, "ShapeFile"); |
669 |
bramz |
2735 |
|
670 |
|
|
PYSHAPELIB_ADD_CONSTANT(SHPT_NULL); |
671 |
|
|
PYSHAPELIB_ADD_CONSTANT(SHPT_POINT); |
672 |
|
|
PYSHAPELIB_ADD_CONSTANT(SHPT_ARC); |
673 |
|
|
PYSHAPELIB_ADD_CONSTANT(SHPT_POLYGON); |
674 |
|
|
PYSHAPELIB_ADD_CONSTANT(SHPT_MULTIPOINT); |
675 |
|
|
PYSHAPELIB_ADD_CONSTANT(SHPT_POINTZ); |
676 |
|
|
PYSHAPELIB_ADD_CONSTANT(SHPT_ARCZ); |
677 |
|
|
PYSHAPELIB_ADD_CONSTANT(SHPT_POLYGONZ); |
678 |
|
|
PYSHAPELIB_ADD_CONSTANT(SHPT_MULTIPOINTZ); |
679 |
|
|
PYSHAPELIB_ADD_CONSTANT(SHPT_POINTM); |
680 |
|
|
PYSHAPELIB_ADD_CONSTANT(SHPT_ARCM); |
681 |
|
|
PYSHAPELIB_ADD_CONSTANT(SHPT_POLYGONM); |
682 |
|
|
PYSHAPELIB_ADD_CONSTANT(SHPT_MULTIPOINTM); |
683 |
|
|
PYSHAPELIB_ADD_CONSTANT(SHPT_MULTIPATCH); |
684 |
|
|
PYSHAPELIB_ADD_CONSTANT(SHPP_TRISTRIP); |
685 |
|
|
PYSHAPELIB_ADD_CONSTANT(SHPP_TRIFAN); |
686 |
|
|
PYSHAPELIB_ADD_CONSTANT(SHPP_OUTERRING); |
687 |
|
|
PYSHAPELIB_ADD_CONSTANT(SHPP_INNERRING); |
688 |
|
|
PYSHAPELIB_ADD_CONSTANT(SHPP_FIRSTRING); |
689 |
|
|
PYSHAPELIB_ADD_CONSTANT(SHPP_RING); |
690 |
|
|
} |
691 |
jan |
1611 |
|