1 |
/* SWIG (www.swig.org) interface file for shapelib |
#include "shapefil.h" |
2 |
* |
#include "pyshapelib_common.h" |
3 |
* At the moment (Dec 2000) this file is only useful to generate Python |
#include "pyshapelib_api.h" |
|
* bindings. Invoke swig as follows: |
|
|
* |
|
|
* swig -python -shadow shapelib.i |
|
|
* |
|
|
* to generate shapelib_wrap.c and shapelib.py. shapelib_wrap.c |
|
|
* defines a bunch of Python-functions that wrap the appripriate |
|
|
* shapelib functions and shapelib.py contains an object oriented |
|
|
* wrapper around shapelib_wrap.c. |
|
|
* |
|
|
* Shapelib, and hence this module too, defines two types of objects, |
|
|
* shapes and shapefiles. |
|
|
*/ |
|
4 |
|
|
5 |
%module shapelib |
/* --- SHPObject ----------------------------------------------------------------------------------------------------- */ |
6 |
|
|
7 |
/* |
typedef struct |
8 |
* First, a %{,%}-Block. These blocks are copied verbatim to the |
{ |
9 |
* shapelib_wrap.c file and are not parsed by SWIG. This is the place to |
PyObject_HEAD |
10 |
* import headerfiles and define helper-functions that are needed by the |
SHPObject* shpObject; |
11 |
* automatically generated wrappers. |
} |
12 |
*/ |
PySHPObject; |
13 |
|
|
14 |
%{ |
/* allocator |
15 |
|
*/ |
16 |
|
static PyObject* PySHPObject_new(PyTypeObject* type, PyObject* args, PyObject* kwds) |
17 |
|
{ |
18 |
|
PySHPObject* self; |
19 |
|
self = (PySHPObject*) type->tp_alloc(type, 0); |
20 |
|
self->shpObject = NULL; |
21 |
|
return (PyObject*) self; |
22 |
|
} |
23 |
|
|
24 |
/* import the shapelib headefile. */ |
/* deallocator |
|
#include "shapefil.h" |
|
|
#include "pyshapelib_api.h" |
|
|
|
|
|
/* |
|
|
* Rename a few shapelib functions that are effectively methods with |
|
|
* preprocessor macros so that they have the names that swig expects |
|
|
* (e.g. the destructor of SHPObject has to be called delete_SHPObject) |
|
25 |
*/ |
*/ |
26 |
|
static void PySHPObject_dealloc(PySHPObject* self) |
27 |
|
{ |
28 |
|
SHPDestroyObject(self->shpObject); |
29 |
|
self->shpObject = NULL; |
30 |
|
self->ob_type->tp_free((PyObject*)self); |
31 |
|
} |
32 |
|
|
33 |
|
/* The constructor of SHPObject. parts is a list of lists of tuples |
34 |
|
* describing the parts and their vertices just likethe output of the |
35 |
|
* vertices() method. part_type_list is the list of part-types and may |
36 |
|
* be NULL. For the meaning of the part-types and their default value |
37 |
|
* see the Shaplib documentation. |
38 |
|
*/ |
39 |
|
static int PySHPObject_init(PySHPObject* self, PyObject* args, PyObject* kwds) |
40 |
|
{ |
41 |
|
int type; |
42 |
|
int id; |
43 |
|
PyObject* parts = NULL; |
44 |
|
PyObject* part_type_list = NULL; |
45 |
|
|
46 |
|
int num_parts; |
47 |
|
int num_vertices; |
48 |
|
int part_start; |
49 |
|
|
50 |
|
double* xs = NULL; |
51 |
|
double* ys = NULL; |
52 |
|
int* part_starts = NULL; |
53 |
|
int* part_types = NULL; |
54 |
|
|
55 |
|
int i; |
56 |
|
int return_code = -1; |
57 |
|
|
58 |
|
/* first, unpack parameters */ |
59 |
|
if (kwds != NULL && PyDict_Size(kwds) > 0) |
60 |
|
{ |
61 |
|
PyErr_Format(PyExc_TypeError, "shapelib.SHPObject.__init__ takes no keyword arguments"); |
62 |
|
return -1; |
63 |
|
} |
64 |
|
if (!PyArg_ParseTuple(args, "iiO|O", &type, &id, &parts, &part_type_list)) return -1; |
65 |
|
|
66 |
|
if (!PySequence_Check(parts)) |
67 |
|
{ |
68 |
|
PyErr_SetString(PyExc_TypeError, "parts is not a sequence"); |
69 |
|
return -1; |
70 |
|
} |
71 |
|
num_parts = PySequence_Length(parts); |
72 |
|
if (num_parts < 0) |
73 |
|
{ |
74 |
|
PyErr_SetString(PyExc_TypeError, "cannot determine length of parts"); |
75 |
|
return -1; |
76 |
|
} |
77 |
|
|
78 |
|
/* parts and part_types have to have the same lengths */ |
79 |
|
if (part_type_list == Py_None) |
80 |
|
{ |
81 |
|
Py_DECREF(part_type_list); |
82 |
|
part_type_list = NULL; |
83 |
|
} |
84 |
|
if (part_type_list) |
85 |
|
{ |
86 |
|
if (!PySequence_Check(parts)) |
87 |
|
{ |
88 |
|
PyErr_SetString(PyExc_TypeError, "part_type_list is not a sequence"); |
89 |
|
return -1; |
90 |
|
} |
91 |
|
if (PySequence_Length(part_type_list) != num_parts) |
92 |
|
{ |
93 |
|
PyErr_SetString(PyExc_TypeError, "parts and part_types have to have the same lengths"); |
94 |
|
return -1; |
95 |
|
} |
96 |
|
} |
97 |
|
|
98 |
|
/* determine how many vertices there are altogether */ |
99 |
|
num_vertices = 0; |
100 |
|
for (i = 0; i < num_parts; ++i) |
101 |
|
{ |
102 |
|
PyObject* part = PySequence_ITEM(parts, i); |
103 |
|
if (!PySequence_Check(part)) |
104 |
|
{ |
105 |
|
PyErr_SetString(PyExc_TypeError, "at least one item in parts is not a sequence"); |
106 |
|
Py_DECREF(part); |
107 |
|
return -1; |
108 |
|
} |
109 |
|
num_vertices += PySequence_Length(part); |
110 |
|
Py_DECREF(part); |
111 |
|
} |
112 |
|
|
113 |
|
/* allocate the memory for the various arrays and check for memory errors */ |
114 |
|
xs = malloc(num_vertices * sizeof(double)); |
115 |
|
ys = malloc(num_vertices * sizeof(double)); |
116 |
|
part_starts = malloc(num_parts * sizeof(int)); |
117 |
|
part_types = part_type_list ? malloc(num_parts * sizeof(int)) : 0; |
118 |
|
|
119 |
|
if (!xs || !ys || !part_starts || (part_type_list && !part_types)) |
120 |
|
{ |
121 |
|
PyErr_NoMemory(); |
122 |
|
goto exit; |
123 |
|
} |
124 |
|
|
125 |
|
/* convert the part types */ |
126 |
|
if (part_type_list) |
127 |
|
{ |
128 |
|
for (i = 0; i < num_parts; i++) |
129 |
|
{ |
130 |
|
PyObject* otype = PySequence_ITEM(part_type_list, i); |
131 |
|
part_types[i] = PyInt_AsLong(otype); |
132 |
|
Py_DECREF(otype); |
133 |
|
if (part_types[i] < 0) |
134 |
|
{ |
135 |
|
PyErr_SetString(PyExc_TypeError, "at least one item in part_type_list is not an integer or is negative"); |
136 |
|
goto exit; |
137 |
|
} |
138 |
|
} |
139 |
|
} |
140 |
|
|
141 |
|
/* convert the list of parts */ |
142 |
|
part_start = 0; |
143 |
|
for (i = 0; i < num_parts; ++i) |
144 |
|
{ |
145 |
|
int j, length; |
146 |
|
|
147 |
|
PyObject* part = PySequence_ITEM(parts, i); |
148 |
|
length = PySequence_Length(part); |
149 |
|
part_starts[i] = part_start; |
150 |
|
|
151 |
|
for (j = 0; j < length; ++j) |
152 |
|
{ |
153 |
|
PyObject* vertex = PySequence_ITEM(part, j); |
154 |
|
if (!PyArg_ParseTuple(vertex, "dd", xs + part_start + j, ys + part_start + j)) |
155 |
|
{ |
156 |
|
PyErr_SetString(PyExc_TypeError, "at least one part contains an vertex that's not a tuple of two doubles"); |
157 |
|
Py_DECREF(vertex); |
158 |
|
Py_DECREF(part); |
159 |
|
goto exit; |
160 |
|
} |
161 |
|
Py_DECREF(vertex); |
162 |
|
} |
163 |
|
Py_DECREF(part); |
164 |
|
part_start += length; |
165 |
|
} |
166 |
|
|
167 |
|
self->shpObject = SHPCreateObject(type, id, num_parts, part_starts, part_types, num_vertices, xs, ys, NULL, NULL); |
168 |
|
return_code = 0; |
169 |
|
|
170 |
|
exit: |
171 |
|
free(xs); |
172 |
|
free(ys); |
173 |
|
free(part_starts); |
174 |
|
free(part_types); |
175 |
|
return return_code; |
176 |
|
} |
177 |
|
|
|
#define delete_SHPObject SHPDestroyObject |
|
|
|
|
178 |
/* |
/* |
179 |
* The extents() method of SHPObject. |
* The extents() method of SHPObject. |
180 |
* |
* |
181 |
* 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. |
182 |
* and max. values of x, y, z, m. |
* and max. values of x, y, z, m. |
183 |
*/ |
*/ |
184 |
static PyObject * |
static PyObject* PySHPObject_extents(PySHPObject* self) |
|
SHPObject_extents(SHPObject *object) |
|
185 |
{ |
{ |
186 |
return Py_BuildValue("[dddd][dddd]", |
SHPObject* object = self->shpObject; |
187 |
object->dfXMin, object->dfYMin, object->dfZMin, |
return Py_BuildValue("(dddd)(dddd)", |
188 |
object->dfMMin, |
object->dfXMin, object->dfYMin, object->dfZMin, object->dfMMin, |
189 |
object->dfXMax, object->dfYMax, object->dfZMax, |
object->dfXMax, object->dfYMax, object->dfZMax, object->dfMMax); |
|
object->dfMMax); |
|
190 |
} |
} |
191 |
|
|
192 |
|
|
193 |
/* |
/* |
194 |
* The vertices() method of SHPObject. |
* The vertices() method of SHPObject. |
195 |
* |
* |
196 |
* Return the x and y coords of the vertices as a list of lists of |
* Return the x and y coords of the vertices as a list of lists of |
197 |
* tuples. |
* tuples. |
198 |
*/ |
*/ |
199 |
|
|
200 |
static PyObject* build_vertex_list(SHPObject *object, int index, int length); |
static PyObject* build_vertex_list(SHPObject *object, int index, int length); |
201 |
|
|
202 |
static PyObject* |
static PyObject* PySHPObject_vertices(PySHPObject* self) |
|
SHPObject_vertices(SHPObject *object) |
|
203 |
{ |
{ |
204 |
PyObject *result = NULL; |
PyObject *result = NULL; |
205 |
PyObject *part = NULL; |
PyObject *part = NULL; |
206 |
int part_idx, vertex_idx; |
int part_idx, vertex_idx; |
207 |
int length = 0; |
int length = 0; |
208 |
|
SHPObject* object = self->shpObject; |
209 |
|
|
210 |
if (object->nParts > 0) |
if (object->nParts > 0) |
211 |
{ |
{ |
212 |
/* A multipart shape. Usual for SHPT_ARC and SHPT_POLYGON */ |
/* A multipart shape. Usual for SHPT_ARC and SHPT_POLYGON */ |
213 |
|
|
214 |
result = PyList_New(object->nParts); |
result = PyList_New(object->nParts); |
215 |
if (!result) |
if (!result) |
216 |
return NULL; |
return NULL; |
217 |
|
|
218 |
|
for (part_idx = 0, vertex_idx = 0; part_idx < object->nParts; part_idx++) |
219 |
|
{ |
220 |
|
if (part_idx < object->nParts - 1) |
221 |
|
length = (object->panPartStart[part_idx + 1] |
222 |
|
- object->panPartStart[part_idx]); |
223 |
|
else |
224 |
|
length = object->nVertices - object->panPartStart[part_idx]; |
225 |
|
|
226 |
|
part = build_vertex_list(object, vertex_idx, length); |
227 |
|
if (!part) |
228 |
|
goto fail; |
229 |
|
|
230 |
|
if (PyList_SetItem(result, part_idx, part) < 0) |
231 |
|
goto fail; |
232 |
|
|
233 |
for (part_idx = 0, vertex_idx = 0; part_idx < object->nParts; |
vertex_idx += length; |
234 |
part_idx++) |
} |
235 |
|
} |
236 |
|
else |
237 |
{ |
{ |
238 |
if (part_idx < object->nParts - 1) |
/* only one part. usual for SHPT_POINT */ |
239 |
length = (object->panPartStart[part_idx + 1] |
result = build_vertex_list(object, 0, object->nVertices); |
240 |
- object->panPartStart[part_idx]); |
} |
241 |
else |
|
242 |
length = object->nVertices - object->panPartStart[part_idx]; |
return result; |
243 |
|
|
244 |
part = build_vertex_list(object, vertex_idx, length); |
fail: |
245 |
if (!part) |
Py_XDECREF(part); |
246 |
goto fail; |
Py_DECREF(result); |
247 |
|
return NULL; |
|
if (PyList_SetItem(result, part_idx, part) < 0) |
|
|
goto fail; |
|
|
|
|
|
vertex_idx += length; |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
/* only one part. usual for SHPT_POINT */ |
|
|
result = build_vertex_list(object, 0, object->nVertices); |
|
|
} |
|
|
|
|
|
return result; |
|
|
|
|
|
fail: |
|
|
Py_XDECREF(part); |
|
|
Py_DECREF(result); |
|
|
return NULL; |
|
248 |
} |
} |
249 |
|
|
250 |
|
|
251 |
/* Return the length coordinates of the shape object starting at vertex |
/* Return the length coordinates of the shape object starting at vertex |
252 |
* index as a Python-list of tuples. Helper function for |
* index as a Python-list of tuples. Helper function for |
253 |
* SHPObject_vertices. |
* SHPObject_vertices. |
254 |
*/ |
*/ |
255 |
static PyObject* |
static PyObject* build_vertex_list(SHPObject *object, int index, int length) |
|
build_vertex_list(SHPObject *object, int index, int length) |
|
256 |
{ |
{ |
257 |
int i; |
int i; |
258 |
PyObject * list; |
PyObject * list; |
259 |
PyObject * vertex = NULL; |
PyObject * vertex = NULL; |
260 |
|
|
261 |
list = PyList_New(length); |
list = PyList_New(length); |
262 |
if (!list) |
if (!list) |
263 |
return NULL; |
return NULL; |
264 |
|
|
265 |
for (i = 0; i < length; i++, index++) |
for (i = 0; i < length; i++, index++) |
266 |
{ |
{ |
267 |
vertex = Py_BuildValue("dd", object->padfX[index], |
vertex = Py_BuildValue("dd", object->padfX[index], |
268 |
object->padfY[index]); |
object->padfY[index]); |
269 |
if (!vertex) |
if (!vertex) |
270 |
goto fail; |
goto fail; |
271 |
if (PyList_SetItem(list, i, vertex) < 0) |
if (PyList_SetItem(list, i, vertex) < 0) |
272 |
goto fail; |
goto fail; |
273 |
} |
} |
|
|
|
|
return list; |
|
|
|
|
|
fail: |
|
|
Py_XDECREF(vertex); |
|
|
Py_DECREF(list); |
|
|
return NULL; |
|
|
} |
|
274 |
|
|
275 |
|
return list; |
276 |
|
|
277 |
|
fail: |
278 |
|
Py_XDECREF(vertex); |
279 |
|
Py_DECREF(list); |
280 |
|
return NULL; |
281 |
|
} |
282 |
|
|
283 |
|
|
284 |
|
|
285 |
/* The constructor of SHPObject. parts is a list of lists of tuples |
static PyObject* PySHPObject_part_types(PySHPObject* self) |
|
* describing the parts and their vertices just likethe output of the |
|
|
* vertices() method. part_type_list is the list of part-types and may |
|
|
* be NULL. For the meaning of the part-types and their default value |
|
|
* see the Shaplib documentation. |
|
|
*/ |
|
|
SHPObject * new_SHPObject(int type, int id, PyObject * parts, |
|
|
PyObject * part_type_list) |
|
286 |
{ |
{ |
287 |
/* arrays to hold thex and y coordinates of the vertices */ |
int i; |
288 |
double *xs = NULL, *ys = NULL; |
PyObject* result = NULL; |
289 |
/* number of all vertices of all parts */ |
SHPObject* object = self->shpObject; |
290 |
int num_vertices; |
|
291 |
/* number of parts in the list parts */ |
if (object->nParts == 0 || object->panPartType == 0) |
|
int num_parts; |
|
|
/* start index of in xs and ys of the part currently worked on */ |
|
|
int part_start; |
|
|
/* array of start indices in xs and ys as expected by shapelib */ |
|
|
int *part_starts = NULL; |
|
|
|
|
|
/* generic counter */ |
|
|
int i; |
|
|
|
|
|
/* array of part types. holds the converted content of |
|
|
* part_type_list. Stays NULL of part_type_list is NULL |
|
|
*/ |
|
|
int *part_types = NULL; |
|
|
|
|
|
/* temporary python objects referring to the the list items being |
|
|
* worked on. |
|
|
*/ |
|
|
PyObject * part = NULL, *tuple = NULL; |
|
|
|
|
|
/* The result object */ |
|
|
SHPObject *result; |
|
|
|
|
|
num_parts = PySequence_Length(parts); |
|
|
num_vertices = 0; |
|
|
|
|
|
/* parts and part_types have to have the same lengths */ |
|
|
if (part_type_list |
|
|
&& PySequence_Length(parts) != PySequence_Length(part_type_list)) |
|
|
{ |
|
|
PyErr_SetString(PyExc_TypeError, |
|
|
"parts and part_types have to have the same lengths"); |
|
|
return NULL; |
|
|
} |
|
|
|
|
|
/* determine how many vertices there are altogether */ |
|
|
for (i = 0; i < num_parts; i++) |
|
|
{ |
|
|
PyObject * part = PySequence_GetItem(parts, i); |
|
|
if (!part) |
|
|
return NULL; |
|
|
num_vertices += PySequence_Length(part); |
|
|
Py_DECREF(part); |
|
|
} |
|
|
|
|
|
/* allocate the memory for the various arrays and check for memory |
|
|
errors */ |
|
|
xs = malloc(num_vertices * sizeof(double)); |
|
|
ys = malloc(num_vertices * sizeof(double)); |
|
|
part_starts = malloc(num_parts * sizeof(int)); |
|
|
if (part_type_list) |
|
|
part_types = malloc(num_parts * sizeof(int)); |
|
|
|
|
|
if (!xs || !ys || !part_starts || (part_type_list && !part_types)) |
|
|
{ |
|
|
PyErr_NoMemory(); |
|
|
goto fail; |
|
|
} |
|
|
|
|
|
/* convert the part types */ |
|
|
if (part_type_list) |
|
|
{ |
|
|
for (i = 0; i < num_parts; i++) |
|
292 |
{ |
{ |
293 |
PyObject * otype = PySequence_GetItem(part_type_list, i); |
Py_RETURN_NONE; |
|
if (!otype) |
|
|
return NULL; |
|
|
part_types[i] = PyInt_AsLong(otype); |
|
|
Py_DECREF(otype); |
|
294 |
} |
} |
295 |
} |
|
296 |
|
result = PyTuple_New(object->nParts); |
297 |
/* convert the list of parts */ |
if (!result) return NULL; |
298 |
part_start = 0; |
|
299 |
for (i = 0; i < num_parts; i++) |
for (i = 0; i < object->nParts; ++i) |
300 |
{ |
{ |
301 |
int j, length; |
/* PyTuple_SetItem steals a reference */ |
302 |
|
PyObject* part_type = PyInt_FromLong((long)object->panPartType[i]); |
303 |
part = PySequence_GetItem(parts, i); |
if (!part_type || PyTuple_SetItem(result, i, part_type) < 0) goto fail; |
304 |
length = PySequence_Length(part); |
} |
305 |
part_starts[i] = part_start; |
return result; |
306 |
|
|
307 |
for (j = 0; j < length; j++) |
fail: |
308 |
{ |
Py_DECREF(result); |
309 |
tuple = PySequence_GetItem(part, j); |
return NULL; |
|
if (!tuple) |
|
|
goto fail; |
|
|
|
|
|
if (!PyArg_ParseTuple(tuple, "dd", xs + part_start + j, |
|
|
ys + part_start + j)) |
|
|
{ |
|
|
goto fail; |
|
|
} |
|
|
Py_DECREF(tuple); |
|
|
tuple = NULL; |
|
|
} |
|
|
Py_DECREF(part); |
|
|
part = NULL; |
|
|
part_start += length; |
|
|
} |
|
|
|
|
|
result = SHPCreateObject(type, id, num_parts, part_starts, part_types, |
|
|
num_vertices, xs, ys, NULL, NULL); |
|
|
free(xs); |
|
|
free(ys); |
|
|
free(part_starts); |
|
|
free(part_types); |
|
|
return result; |
|
|
|
|
|
fail: |
|
|
free(xs); |
|
|
free(ys); |
|
|
free(part_starts); |
|
|
free(part_types); |
|
|
Py_XDECREF(part); |
|
|
Py_XDECREF(tuple); |
|
|
return NULL; |
|
310 |
} |
} |
311 |
|
|
|
%} |
|
312 |
|
|
313 |
|
|
314 |
|
static PyObject* PySHPObject_type(PySHPObject* self, void* closure) |
315 |
|
{ |
316 |
|
return PyInt_FromLong(self->shpObject->nSHPType); |
317 |
|
} |
318 |
|
|
319 |
|
|
|
/* |
|
|
* The SWIG Interface definition. |
|
|
*/ |
|
320 |
|
|
321 |
/* include some common SWIG type definitions and standard exception |
static PyObject* PySHPObject_id(PySHPObject* self, void* closure) |
322 |
handling code */ |
{ |
323 |
%include typemaps.i |
return PyInt_FromLong(self->shpObject->nShapeId); |
324 |
%include exception.i |
} |
325 |
|
|
326 |
|
|
327 |
/* |
|
328 |
* SHPObject -- Represents one shape |
/* return a string that can be feeded to eval() to reconstruct the object, |
329 |
|
* assuming a proper context |
330 |
*/ |
*/ |
331 |
|
static PyObject* PySHPObject_repr(PySHPObject* self) |
332 |
|
{ |
333 |
|
PyObject* format = NULL; |
334 |
|
PyObject* args = NULL; |
335 |
|
PyObject* result = NULL; |
336 |
|
|
337 |
|
format = PyString_FromString("shapelib.SHPObject(%i, %i, %s, %s)"); |
338 |
|
if (!format) return NULL; |
339 |
|
|
340 |
/* Exception typemap for the SHPObject constructor. The constructor the |
args = Py_BuildValue("iiNN", |
341 |
the wrapper function defined above which returns NULL in case of |
self->shpObject->nSHPType, |
342 |
error. */ |
self->shpObject->nShapeId, |
343 |
|
PySHPObject_vertices(self), |
344 |
%typemap(python,except) SHPObject*new_SHPObject { |
PySHPObject_part_types(self)); |
345 |
$function; |
if (!args) |
346 |
if (PyErr_Occurred()) |
{ |
347 |
return NULL; |
Py_DECREF(format); |
348 |
|
return NULL; |
349 |
|
} |
350 |
|
|
351 |
|
result = PyString_Format(format, args); |
352 |
|
Py_DECREF(args); |
353 |
|
Py_DECREF(format); |
354 |
|
return result; |
355 |
} |
} |
356 |
|
|
|
/* Define the SHPObject struct for SWIG. This has to have the same name |
|
|
* as the underlying C-struct in shapfil.h, but we don't have to repeat |
|
|
* all the fields here, only those we want to access directly, and we |
|
|
* can define methods for the object oriented interface. |
|
|
*/ |
|
357 |
|
|
|
typedef struct { |
|
358 |
|
|
359 |
/* The shape object has two read-only attributes: */ |
static PyMethodDef PySHPObject_methods[] = |
360 |
|
{ |
361 |
|
{"extents", (PyCFunction)PySHPObject_extents, METH_NOARGS, NULL}, |
362 |
|
{"vertices", (PyCFunction)PySHPObject_vertices, METH_NOARGS, NULL}, |
363 |
|
{"part_types", (PyCFunction)PySHPObject_part_types, METH_NOARGS, NULL}, |
364 |
|
{NULL} |
365 |
|
}; |
366 |
|
|
367 |
/* The type of the shape. In the c-struct defined the field is |
static PyGetSetDef PySHPObject_getsetters[] = |
368 |
* called 'nSHPType' but for the python bindings 'type' is more |
{ |
369 |
* appropriate. |
{"type", (getter)PySHPObject_type, NULL, NULL }, |
370 |
*/ |
{"id", (getter)PySHPObject_id, NULL, NULL }, |
371 |
%readonly %name(type) int nSHPType; |
{NULL} |
372 |
|
}; |
|
/* The id of the shape. Here 'id' is a better name than 'nShapeId'. */ |
|
|
%readonly %name(id) int nShapeId; |
|
|
|
|
|
/* The methods */ |
|
|
%addmethods { |
|
|
|
|
|
/* the constructor */ |
|
|
SHPObject(int type, int id, PyObject * parts, |
|
|
PyObject * part_types = NULL); |
|
|
|
|
|
/* The destructor */ |
|
|
~SHPObject(); |
|
|
|
|
|
/* extents and vertices correspond to the SHPObject_extents and |
|
|
* SHPObject_vertices defined above |
|
|
*/ |
|
|
PyObject *extents(); |
|
|
PyObject *vertices(); |
|
|
} |
|
|
} SHPObject; |
|
373 |
|
|
374 |
|
static PyTypeObject PySHPObjectType = PYSHAPELIB_DEFINE_TYPE(PySHPObject, "shapelib.SHPObject", 0); |
375 |
|
|
|
/* |
|
|
* ShapeFile -- Represents the shape file |
|
|
*/ |
|
376 |
|
|
377 |
/* Here we do things a little different. We define a new C-struct that |
/* --- ShapeFile ----------------------------------------------------------------------------------------------------- */ |
|
* holds the SHPHandle. This is mainly done so we can separate the |
|
|
* close() method from the destructor but it also helps with exception |
|
|
* handling. |
|
|
* |
|
|
* After the ShapeFile has been opened or created the handle is not |
|
|
* NULL. The close() method closes the file and sets handle to NULL as |
|
|
* an indicator that the file has been closed. |
|
|
*/ |
|
378 |
|
|
379 |
/* First, define the C-struct */ |
typedef struct |
380 |
%{ |
{ |
381 |
typedef struct { |
PyObject_HEAD |
382 |
SHPHandle handle; |
SHPHandle handle; |
383 |
} ShapeFile; |
} |
384 |
%} |
PyShapeFile; |
385 |
|
|
386 |
/* define and use some typemaps for the info() method whose |
/* allocator |
|
* C-implementation has four output parameters that are returned through |
|
|
* pointers passed into the function. SWIG already has definitions for |
|
|
* common types such as int* and we can use those for the first two |
|
|
* parameters: |
|
387 |
*/ |
*/ |
388 |
|
static PyObject* PyShapeFile_new(PyTypeObject* type, PyObject* args, PyObject* kwds) |
389 |
%apply int * OUTPUT { int * output_entities } |
{ |
390 |
%apply int * OUTPUT { int * output_type } |
PyShapeFile* self; |
391 |
|
self = (PyShapeFile*) type->tp_alloc(type, 0); |
392 |
|
self->handle = NULL; |
393 |
|
return (PyObject*) self; |
394 |
|
} |
395 |
|
|
396 |
/* for the last two, the 4-element arrays of min- and max-values, we |
/* constructor |
|
* have to define our own typemaps: |
|
397 |
*/ |
*/ |
398 |
%typemap (python,ignore) double * extents(double temp[4]) { |
static int PyShapeFile_init(PyShapeFile* self, PyObject* args, PyObject* kwds) |
399 |
$target = temp; |
{ |
400 |
|
char* file; |
401 |
|
char* mode = "rb"; |
402 |
|
if (kwds != NULL && PyDict_Size(kwds) > 0) |
403 |
|
{ |
404 |
|
PyErr_Format(PyExc_TypeError, "shapelib.ShapeFile.__init__ takes no keyword arguments"); |
405 |
|
return -1; |
406 |
|
} |
407 |
|
if (!PyArg_ParseTuple(args, "s|s", &file, &mode)) return -1; |
408 |
|
|
409 |
|
self->handle = SHPOpen(file, mode); |
410 |
|
return self->handle ? 0 : -1; |
411 |
} |
} |
412 |
|
|
413 |
%typemap (python,argout) double * extents { |
static PyObject* PyShapeFile_close(PyShapeFile* self) |
414 |
PyObject * list = Py_BuildValue("[dddd]", |
{ |
415 |
$source[0], $source[1], |
SHPClose(self->handle); |
416 |
$source[2], $source[3]); |
self->handle = NULL; |
417 |
$target = t_output_helper($target,list); |
Py_RETURN_NONE; |
418 |
} |
} |
419 |
|
|
420 |
%apply double * extents { double * output_min_bounds } |
/* destructor |
|
%apply double * extents { double * output_max_bounds } |
|
|
|
|
|
/* The first argument to the ShapeFile methods is a ShapeFile pointer. |
|
|
* We have to check whether handle is not NULL in most methods but not |
|
|
* all. In the destructor and the close method, it's OK for handle to be |
|
|
* NULL. We achieve this by checking whether the preprocessor macro |
|
|
* NOCHECK_$name is defined. SWIG replaces $name with the name of the |
|
|
* function for which the code is inserted. In the %{,%}-block below we |
|
|
* define the macros for the destructor and the close() method. |
|
421 |
*/ |
*/ |
422 |
|
static void PyShapeFile_dealloc(PyShapeFile* self) |
423 |
|
{ |
424 |
|
PyShapeFile_close(self); |
425 |
|
self->ob_type->tp_free((PyObject*)self); |
426 |
|
} |
427 |
|
|
428 |
|
static PyObject* PyShapeFile_info(PyShapeFile* self) |
429 |
|
{ |
430 |
|
SHPHandle handle = self->handle; |
431 |
|
return Py_BuildValue("ii(dddd)(dddd)", |
432 |
|
handle->nRecords, handle->nShapeType, |
433 |
|
handle->adBoundsMin[0], handle->adBoundsMin[1], handle->adBoundsMin[2], handle->adBoundsMin[3], |
434 |
|
handle->adBoundsMax[0], handle->adBoundsMax[1], handle->adBoundsMax[2], handle->adBoundsMax[3]); |
435 |
|
} |
436 |
|
|
437 |
%typemap(python,check) ShapeFile *{ |
static PyObject* PyShapeFile_read_object(PyShapeFile* self, PyObject* args) |
438 |
%#ifndef NOCHECK_$name |
{ |
439 |
if (!$target || !$target->handle) |
int index; |
440 |
SWIG_exception(SWIG_TypeError, "shapefile already closed"); |
SHPObject* object; |
441 |
%#endif |
PySHPObject* result; |
442 |
} |
|
443 |
|
if (!PyArg_ParseTuple(args, "i", &index)) return NULL; |
444 |
%{ |
|
445 |
#define NOCHECK_delete_ShapeFile |
object = SHPReadObject(self->handle, index); |
446 |
#define NOCHECK_ShapeFile_close |
if (!object) |
447 |
%} |
{ |
448 |
|
PyErr_SetString(PyExc_RuntimeError, "failed to read object"); |
449 |
/* An exception handle for the constructor and the module level open() |
return NULL; |
450 |
* and create() functions. |
} |
451 |
* |
|
452 |
* Annoyingly, we *have* to put braces around the SWIG_exception() |
result = PyObject_New(PySHPObject, &PySHPObjectType); |
453 |
* calls, at least in the python case, because of the way the macro is |
if (!result) |
454 |
* written. Of course, always putting braces around the branches of an |
{ |
455 |
* if-statement is often considered good practice. |
return PyErr_NoMemory(); |
456 |
*/ |
} |
457 |
%typemap(python,except) ShapeFile * { |
|
458 |
$function; |
result->shpObject = object; |
459 |
if (!$source) |
return (PyObject*) result; |
|
{ |
|
|
SWIG_exception(SWIG_MemoryError, "no memory"); |
|
|
} |
|
|
else if (!$source->handle) |
|
|
{ |
|
|
SWIG_exception(SWIG_IOError, "$name failed"); |
|
|
} |
|
460 |
} |
} |
461 |
|
|
462 |
|
static PyObject* PyShapeFile_write_object(PyShapeFile* self, PyObject* args) |
463 |
|
{ |
464 |
|
int index, result; |
465 |
|
PyObject* object; |
466 |
|
|
467 |
|
if (!PyArg_ParseTuple(args, "iO", &index, &object)) return NULL; |
468 |
|
|
469 |
|
if (!PyObject_IsInstance(object, (PyObject*)&PySHPObjectType)) |
470 |
|
{ |
471 |
|
PyErr_SetString(PyExc_TypeError, "object is not a SHPObject"); |
472 |
|
return NULL; |
473 |
|
} |
474 |
|
|
475 |
|
result = SHPWriteObject(self->handle, index, ((PySHPObject*)object)->shpObject); |
476 |
|
if (result < 0) |
477 |
|
{ |
478 |
|
PyErr_SetString(PyExc_RuntimeError, "failed to write object"); |
479 |
|
return NULL; |
480 |
|
} |
481 |
|
return PyInt_FromLong((long)result); |
482 |
|
} |
483 |
|
|
484 |
/* |
static PyObject* PyShapeFile_cobject(PyShapeFile* self) |
485 |
* The SWIG-version of the ShapeFile struct. |
{ |
486 |
*/ |
return PyCObject_FromVoidPtr(self->handle, NULL); |
487 |
|
} |
488 |
|
|
489 |
typedef struct |
static PyObject* PyShapeFile_repr(PyShapeFile* self) |
490 |
{ |
{ |
491 |
/* Only methods and no attributes here: */ |
/* TODO: it would be nice to do something like "shapelib.ShapeFile(filename, mode)" instead */ |
492 |
%addmethods { |
return PyString_FromFormat("<shapelib.ShapeFile object at %p>", self->handle); |
493 |
|
} |
494 |
|
|
495 |
/* The constructor. Takes two arguments, the filename and the |
static PyMethodDef PyShapeFile_methods[] = |
496 |
* optinal mode which are passed through to SHPOpen (due to the |
{ |
497 |
* renaming trick) |
{"close", (PyCFunction)PyShapeFile_close, METH_NOARGS, "close the shape file" }, |
498 |
*/ |
{"info", (PyCFunction)PyShapeFile_info, METH_NOARGS, |
499 |
ShapeFile(char *file, char * mode = "rb") { |
"Return a tuple (NUM_SHAPES, TYPE, MIN, MAX) where NUM_SHAPES is the number of shapes in the file, TYPE is the " |
500 |
ShapeFile * self = malloc(sizeof(ShapeFile)); |
"shape type and MIN and MAX are 4-element tuples with the min. and max. values of the data." }, |
501 |
if (self) |
{"read_object", (PyCFunction)PyShapeFile_read_object, METH_VARARGS, "Return object number i" }, |
502 |
self->handle = SHPOpen(file, mode); |
{"write_object", (PyCFunction)PyShapeFile_write_object, METH_VARARGS, "Write an object"}, |
503 |
return self; |
{"cobject", (PyCFunction)PyShapeFile_cobject, METH_NOARGS, "Return the shapelib SHPHandle as a Python CObject"}, |
504 |
} |
{NULL} |
505 |
|
}; |
|
/* The destructor. Equivalent to SHPClose */ |
|
|
~ShapeFile() { |
|
|
if (self->handle) |
|
|
SHPClose(self->handle); |
|
|
free(self); |
|
|
} |
|
|
|
|
|
/* close the shape file and set handle to NULL */ |
|
|
void close() { |
|
|
if (self->handle) |
|
|
{ |
|
|
SHPClose(self->handle); |
|
|
self->handle = NULL; |
|
|
} |
|
|
} |
|
|
|
|
|
/* info() -- Return a tuple (NUM_SHAPES, TYPE, MIN, MAX) where |
|
|
* NUM_SHAPES is the number of shapes in the file, TYPE is the |
|
|
* shape type and MIN and MAX are 4-element lists with the min. |
|
|
* and max. values of the data. |
|
|
* |
|
|
* The arguments of the underlying shapelib function SHPGetInfo |
|
|
* are all output parameters. To tell SWIG this, we have defined |
|
|
* some typemaps above |
|
|
*/ |
|
|
void info(int * output_entities, int * output_type, |
|
|
double * output_min_bounds, double *output_max_bounds) { |
|
|
SHPGetInfo(self->handle, output_entities, output_type, |
|
|
output_min_bounds, output_max_bounds); |
|
|
} |
|
|
|
|
|
/* Return object number i */ |
|
|
%new SHPObject * read_object(int i) { |
|
|
return SHPReadObject(self->handle, i); |
|
|
} |
|
|
|
|
|
/* Write an object */ |
|
|
int write_object(int iShape, SHPObject * psObject) { |
|
|
return SHPWriteObject(self->handle, iShape, psObject); |
|
|
} |
|
|
|
|
|
/* Return the shapelib SHPHandle as a Python CObject */ |
|
|
PyObject * cobject() { |
|
|
return PyCObject_FromVoidPtr(self->handle, NULL); |
|
|
} |
|
|
} |
|
506 |
|
|
507 |
} ShapeFile; |
static PyGetSetDef PyShapeFile_getsetters[] = |
508 |
|
{ |
509 |
|
{NULL} |
510 |
|
}; |
511 |
|
|
512 |
|
static PyTypeObject PyShapeFileType = PYSHAPELIB_DEFINE_TYPE(PyShapeFile, "shapelib.ShapeFile", 0); |
513 |
|
|
514 |
/* |
/* --- shapelib ------------------------------------------------------------------------------------------------------ */ |
|
* Two module level functions, open() and create() that correspond to |
|
|
* SHPOpen and SHPCreate respectively. open() is equivalent to the |
|
|
* ShapeFile constructor. |
|
|
*/ |
|
515 |
|
|
516 |
%{ |
static PyObject* shapelib_open(PyObject* module, PyObject* args) |
517 |
ShapeFile * open_ShapeFile(const char *filename, const char * mode) { |
{ |
518 |
ShapeFile * self = malloc(sizeof(ShapeFile)); |
return PyObject_CallObject((PyObject*)&PyShapeFileType, args); |
519 |
if (self) |
} |
|
self->handle = SHPOpen(filename, mode); |
|
|
return self; |
|
|
} |
|
|
%} |
|
|
|
|
|
%name(open) %new ShapeFile *open_ShapeFile(const char *filename, |
|
|
const char * mode = "rb"); |
|
|
|
|
|
|
|
|
%{ |
|
|
ShapeFile * create_ShapeFile(const char *filename, int type) { |
|
|
ShapeFile * self = malloc(sizeof(ShapeFile)); |
|
|
if (self) |
|
|
self->handle = SHPCreate(filename, type); |
|
|
return self; |
|
|
} |
|
|
%} |
|
|
|
|
|
%name(create) %new ShapeFile * create_ShapeFile(const char *filename, |
|
|
int type); |
|
|
|
|
|
|
|
|
/* Module level function to expose some of the shapelib functions linked |
|
|
* with the shapefile C-module to other Python extension modules. This |
|
|
* is a kludge to make a Thuban extension work that reads shapes from |
|
|
* shapefiles opened by the shapefile module. |
|
|
*/ |
|
520 |
|
|
521 |
%{ |
static PyObject* shapelib_create(PyObject* module, PyObject* args) |
522 |
static PyShapeLibAPI the_api = { |
{ |
523 |
|
char* file; |
524 |
|
int type; |
525 |
|
PyShapeFile* result; |
526 |
|
|
527 |
|
if (!PyArg_ParseTuple(args, "si", &file, &type)) return NULL; |
528 |
|
|
529 |
|
result = PyObject_New(PyShapeFile, &PyShapeFileType); |
530 |
|
if (!result) |
531 |
|
{ |
532 |
|
return PyErr_NoMemory(); |
533 |
|
} |
534 |
|
|
535 |
|
result->handle = SHPCreate(file, type); |
536 |
|
if (!result->handle) |
537 |
|
{ |
538 |
|
PyObject_Del((PyObject*)result); |
539 |
|
PyErr_SetString(PyExc_RuntimeError, "Failed to create ShapeFile"); |
540 |
|
return NULL; |
541 |
|
} |
542 |
|
|
543 |
|
return (PyObject*) result; |
544 |
|
} |
545 |
|
|
546 |
|
static PyShapeLibAPI shapelib_the_api = |
547 |
|
{ |
548 |
SHPReadObject, |
SHPReadObject, |
549 |
SHPDestroyObject, |
SHPDestroyObject, |
550 |
SHPCreateTree, |
SHPCreateTree, |
551 |
SHPDestroyTree, |
SHPDestroyTree, |
552 |
SHPTreeFindLikelyShapes |
SHPTreeFindLikelyShapes |
553 |
}; |
}; |
|
|
|
|
PyObject * c_api() { |
|
|
return PyCObject_FromVoidPtr(&the_api, NULL); |
|
|
} |
|
|
%} |
|
|
|
|
|
PyObject * c_api(); |
|
554 |
|
|
555 |
|
static PyObject* shapelib_c_api(PyObject* module) |
556 |
|
{ |
557 |
|
return PyCObject_FromVoidPtr(&shapelib_the_api, NULL); |
558 |
|
} |
559 |
|
|
560 |
/* |
static PyObject* shapelib_type_name(PyObject* module, PyObject* args) |
561 |
* Module Level functions |
{ |
562 |
*/ |
int type; |
563 |
|
if (!PyArg_ParseTuple(args, "i", &type)) return NULL; |
564 |
/* convert shapefile types to names */ |
return PyString_FromString(SHPTypeName(type)); |
565 |
%name(type_name) const char *SHPTypeName(int nSHPType); |
} |
|
%name(part_type_name) const char *SHPPartTypeName(int nPartType); |
|
|
|
|
566 |
|
|
567 |
/* |
static PyObject* shapelib_part_type_name(PyObject* module, PyObject* args) |
568 |
* Finally, constants copied from shapefil.h |
{ |
569 |
*/ |
int type; |
570 |
|
if (!PyArg_ParseTuple(args, "i", &type)) return NULL; |
571 |
|
return PyString_FromString(SHPPartTypeName(type)); |
572 |
|
} |
573 |
|
|
574 |
/* -------------------------------------------------------------------- */ |
static PyMethodDef shapelib_methods[] = |
575 |
/* Shape types (nSHPType) */ |
{ |
576 |
/* -------------------------------------------------------------------- */ |
{"open", (PyCFunction)shapelib_open, METH_VARARGS, "open a ShapeFile" }, |
577 |
#define SHPT_NULL 0 |
{"create", (PyCFunction)shapelib_create, METH_VARARGS, "create a ShapeFile" }, |
578 |
#define SHPT_POINT 1 |
{"c_api", (PyCFunction)shapelib_c_api, METH_NOARGS, "get C API of shapelib" }, |
579 |
#define SHPT_ARC 3 |
{"type_name", (PyCFunction)shapelib_type_name, METH_VARARGS, "return type as string" }, |
580 |
#define SHPT_POLYGON 5 |
{"part_type_name", (PyCFunction)shapelib_part_type_name, METH_VARARGS, "return part type as string" }, |
581 |
#define SHPT_MULTIPOINT 8 |
{NULL} |
582 |
#define SHPT_POINTZ 11 |
}; |
|
#define SHPT_ARCZ 13 |
|
|
#define SHPT_POLYGONZ 15 |
|
|
#define SHPT_MULTIPOINTZ 18 |
|
|
#define SHPT_POINTM 21 |
|
|
#define SHPT_ARCM 23 |
|
|
#define SHPT_POLYGONM 25 |
|
|
#define SHPT_MULTIPOINTM 28 |
|
|
#define SHPT_MULTIPATCH 31 |
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */ |
|
|
/* Part types - everything but SHPT_MULTIPATCH just uses */ |
|
|
/* SHPP_RING. */ |
|
|
/* -------------------------------------------------------------------- */ |
|
|
|
|
|
#define SHPP_TRISTRIP 0 |
|
|
#define SHPP_TRIFAN 1 |
|
|
#define SHPP_OUTERRING 2 |
|
|
#define SHPP_INNERRING 3 |
|
|
#define SHPP_FIRSTRING 4 |
|
|
#define SHPP_RING 5 |
|
583 |
|
|
584 |
|
PyMODINIT_FUNC initshapelib(void) |
585 |
|
{ |
586 |
|
PyObject* module = Py_InitModule("shapelib", shapelib_methods); |
587 |
|
if (!module) return; |
588 |
|
|
589 |
|
PYSHAPELIB_ADD_TYPE(PySHPObjectType, "SHPObject"); |
590 |
|
PYSHAPELIB_ADD_TYPE(PyShapeFileType, "ShapeFile"); |
591 |
|
|
592 |
|
PYSHAPELIB_ADD_CONSTANT(SHPT_NULL); |
593 |
|
PYSHAPELIB_ADD_CONSTANT(SHPT_POINT); |
594 |
|
PYSHAPELIB_ADD_CONSTANT(SHPT_ARC); |
595 |
|
PYSHAPELIB_ADD_CONSTANT(SHPT_POLYGON); |
596 |
|
PYSHAPELIB_ADD_CONSTANT(SHPT_MULTIPOINT); |
597 |
|
PYSHAPELIB_ADD_CONSTANT(SHPT_POINTZ); |
598 |
|
PYSHAPELIB_ADD_CONSTANT(SHPT_ARCZ); |
599 |
|
PYSHAPELIB_ADD_CONSTANT(SHPT_POLYGONZ); |
600 |
|
PYSHAPELIB_ADD_CONSTANT(SHPT_MULTIPOINTZ); |
601 |
|
PYSHAPELIB_ADD_CONSTANT(SHPT_POINTM); |
602 |
|
PYSHAPELIB_ADD_CONSTANT(SHPT_ARCM); |
603 |
|
PYSHAPELIB_ADD_CONSTANT(SHPT_POLYGONM); |
604 |
|
PYSHAPELIB_ADD_CONSTANT(SHPT_MULTIPOINTM); |
605 |
|
PYSHAPELIB_ADD_CONSTANT(SHPT_MULTIPATCH); |
606 |
|
PYSHAPELIB_ADD_CONSTANT(SHPP_TRISTRIP); |
607 |
|
PYSHAPELIB_ADD_CONSTANT(SHPP_TRIFAN); |
608 |
|
PYSHAPELIB_ADD_CONSTANT(SHPP_OUTERRING); |
609 |
|
PYSHAPELIB_ADD_CONSTANT(SHPP_INNERRING); |
610 |
|
PYSHAPELIB_ADD_CONSTANT(SHPP_FIRSTRING); |
611 |
|
PYSHAPELIB_ADD_CONSTANT(SHPP_RING); |
612 |
|
} |
613 |
|
|