-
-
Notifications
You must be signed in to change notification settings - Fork 364
/
pyextend.i
315 lines (253 loc) · 9.08 KB
/
pyextend.i
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/******************************************************************************
* $Id$
*
* Project: MapServer
* Purpose: Python-specific extensions to MapScript objects
* Author: Sean Gillies, sgillies@frii.com
*
******************************************************************************
*
* Python-specific mapscript code has been moved into this
* SWIG interface file to improve the readibility of the main
* interface file. The main mapscript.i file includes this
* file when SWIGPYTHON is defined (via 'swig -python ...').
*
*****************************************************************************/
/* fromstring: Factory for mapfile objects */
%pythoncode {
def fromstring(data, mappath=None):
"""Creates map objects from mapfile strings.
Parameters
==========
data : string
Mapfile in a string.
mappath : string
Optional root map path, enabling relative paths in mapfile.
Example
=======
>>> mo = fromstring("MAP\nNAME 'test'\nEND")
>>> mo.name
'test'
"""
import re
if re.search(r"^\s*MAP", data, re.I):
return msLoadMapFromString(data, mappath)
elif re.search(r"^\s*LAYER", data, re.I):
ob = layerObj()
ob.updateFromString(data)
return ob
elif re.search(r"^\s*CLASS", data, re.I):
ob = classObj()
ob.updateFromString(data)
return ob
elif re.search(r"^\s*STYLE", data, re.I):
ob = styleObj()
ob.updateFromString(data)
return ob
else:
raise ValueError("No map, layer, class, or style found. Can not load from provided string")
}
/* ===========================================================================
Python rectObj extensions
======================================================================== */
%extend pointObj {
%pythoncode {
def __str__(self):
return self.toString()
}
}
/* ===========================================================================
Python rectObj extensions
======================================================================== */
%extend rectObj {
%pythoncode {
def __str__(self):
return self.toString()
def __contains__(self, item):
item_type = item.__class__.__name__
if item_type == "pointObj":
if item.x >= self.minx and item.x <= self.maxx \
and item.y >= self.miny and item.y <= self.maxy:
return True
else:
return False
else:
raise TypeError('__contains__ does not yet handle %s' % (item_type))
}
}
/******************************************************************************
* Extensions to mapObj
*****************************************************************************/
%extend mapObj {
/* getLayerOrder() extension returns the map layerorder as a native
sequence */
PyObject *getLayerOrder() {
int i;
PyObject *order;
order = PyTuple_New(self->numlayers);
for (i = 0; i < self->numlayers; i++) {
PyTuple_SetItem(order,i,PyInt_FromLong((long)self->layerorder[i]));
}
return order;
}
int setLayerOrder(PyObject *order) {
int i, size;
size = PyTuple_Size(order);
for (i = 0; i < size; i++) {
self->layerorder[i] = (int)PyInt_AsLong(PyTuple_GetItem(order, i));
}
return MS_SUCCESS;
}
PyObject* getSize()
{
PyObject* output ;
output = PyTuple_New(2);
PyTuple_SetItem(output,0,PyInt_FromLong((long)self->width));
PyTuple_SetItem(output,1,PyInt_FromLong((long)self->height));
return output;
}
%pythoncode {
def get_height(self):
return self.getSize()[1] # <-- second member is the height
def get_width(self):
return self.getSize()[0] # <-- first member is the width
def set_height(self, value):
return self.setSize(self.getSize()[0], value)
def set_width(self, value):
return self.setSize(value, self.getSize()[1])
width = property(get_width, set_width)
height = property(get_height, set_height)
}
}
/******************************************************************************
* Extensions to imageObj
*****************************************************************************/
%extend imageObj {
/* ======================================================================
write()
Write image data to an open Python file or file-like object.
Overrides extension method in mapscript/swiginc/image.i.
Intended to replace saveToString.
====================================================================== */
int write( PyObject *file=Py_None )
{
unsigned char *imgbuffer=NULL;
int imgsize;
PyObject *noerr;
int retval=MS_FAILURE;
rendererVTableObj *renderer = NULL;
/* Return immediately if image driver is not GD */
if ( !MS_RENDERER_PLUGIN(self->format) )
{
msSetError(MS_IMGERR, "Writing of %s format not implemented",
"imageObj::write", self->format->driver);
return MS_FAILURE;
}
if (file == Py_None) /* write to stdout */
retval = msSaveImage(NULL, self, NULL);
else /* presume a Python file-like object */
{
imgbuffer = msSaveImageBuffer(self, &imgsize,
self->format);
if (imgsize == 0)
{
msSetError(MS_IMGERR, "failed to get image buffer", "write()");
return MS_FAILURE;
}
%#if PY_MAJOR_VERSION >= 3
// https://docs.python.org/3/c-api/arg.html
noerr = PyObject_CallMethod(file, "write", "y#", imgbuffer, imgsize);
%#else
// https://docs.python.org/2/c-api/arg.html
noerr = PyObject_CallMethod(file, "write", "s#", imgbuffer, imgsize);
%#endif
free(imgbuffer);
if (noerr == NULL)
return MS_FAILURE;
else
Py_DECREF(noerr);
retval = MS_SUCCESS;
}
return retval;
}
/* Deprecated */
PyObject *saveToString() {
int size=0;
unsigned char *imgbytes;
PyObject *imgstring;
imgbytes = msSaveImageBuffer(self, &size, self->format);
if (size == 0)
{
msSetError(MS_IMGERR, "failed to get image buffer", "saveToString()");
return NULL;
}
imgstring = PyBytes_FromStringAndSize((const char*) imgbytes, size);
free(imgbytes);
return imgstring;
}
}
/******************************************************************************
* Extensions to styleObj
*****************************************************************************/
%extend styleObj {
void pattern_set(int nListSize, double* pListValues)
{
if( nListSize < 2 )
{
msSetError(MS_SYMERR, "Not enough pattern elements. A minimum of 2 are required", "pattern_set()");
return;
}
if( nListSize > MS_MAXPATTERNLENGTH )
{
msSetError(MS_MISCERR, "Too many elements", "pattern_set()");
return;
}
memcpy( self->pattern, pListValues, sizeof(double) * nListSize);
self->patternlength = nListSize;
}
void pattern_get(double** argout, int* pnListSize)
{
*pnListSize = self->patternlength;
*argout = (double*) malloc(sizeof(double) * *pnListSize);
memcpy( *argout, self->pattern, sizeof(double) * *pnListSize);
}
void patternlength_set2(int patternlength)
{
msSetError(MS_MISCERR, "pattern is read-only", "patternlength_set()");
}
%pythoncode {
__swig_setmethods__["patternlength"] = _mapscript.styleObj_patternlength_set2
__swig_getmethods__["patternlength"] = _mapscript.styleObj_patternlength_get
if _newclass:patternlength = _swig_property(_mapscript.styleObj_patternlength_get, _mapscript.styleObj_patternlength_set2)
__swig_setmethods__["pattern"] = _mapscript.styleObj_pattern_set
__swig_getmethods__["pattern"] = _mapscript.styleObj_pattern_get
if _newclass:pattern = _swig_property(_mapscript.styleObj_pattern_get, _mapscript.styleObj_pattern_set)
}
}
/******************************************************************************
* Extensions to hashTableObj - add dict methods
*****************************************************************************/
%extend hashTableObj{
%pythoncode %{
def __getitem__(self, key):
return self.get(key)
def __setitem__(self, key, value):
return self.set(key, value)
def __delitem__(self, key) :
return self.remove(key)
def __contains__(self, key):
return key.lower() in [k.lower() for k in self.keys()]
def __len__(self):
return self.numitems
def keys(self):
keys = []
k = None
while True :
k = self.nextKey(k)
if k :
keys.append(k)
else :
break
return keys
%}
};