~ubuntu-branches/debian/sid/gdal/sid

« back to all changes in this revision

Viewing changes to swig/include/python/gdal_python.i

  • Committer: Package Import Robot
  • Author(s): Francesco Paolo Lovergine
  • Date: 2012-05-07 15:04:42 UTC
  • mfrom: (5.5.16 experimental)
  • Revision ID: package-import@ubuntu.com-20120507150442-2eks97loeh6rq005
Tags: 1.9.0-1
* Ready for sid, starting transition.
* All symfiles updated to latest builds.
* Added dh_numpy call in debian/rules to depend on numpy ABI.
* Policy bumped to 3.9.3, no changes required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * $Id: gdal_python.i 19824 2010-06-09 18:44:06Z rouault $
 
2
 * $Id: gdal_python.i 22938 2011-08-15 18:12:28Z rouault $
3
3
 *
4
4
 * python specific code for gdal bindings.
5
5
 */
86
86
%include "python_exceptions.i"
87
87
%include "python_strings.i"
88
88
 
 
89
%import typemaps_python.i
 
90
 
 
91
/* -------------------------------------------------------------------- */
 
92
/*      VSIFReadL()                                                     */
 
93
/* -------------------------------------------------------------------- */
 
94
 
 
95
%rename (VSIFReadL) wrapper_VSIFReadL;
 
96
 
 
97
%apply ( void **outPythonObject ) { (void **buf ) };
 
98
%inline %{
 
99
int wrapper_VSIFReadL( void **buf, int nMembSize, int nMembCount, VSILFILE *fp)
 
100
{
 
101
    GIntBig buf_size = nMembSize * nMembCount;
 
102
 
 
103
    if (buf_size == 0)
 
104
    {
 
105
        *buf = NULL;
 
106
        return 0;
 
107
    }
 
108
#if PY_VERSION_HEX >= 0x03000000 
 
109
    *buf = (void *)PyBytes_FromStringAndSize( NULL, buf_size ); 
 
110
    if (*buf == NULL)
 
111
    {
 
112
        *buf = Py_None;
 
113
        CPLError(CE_Failure, CPLE_OutOfMemory, "Cannot allocate result buffer");
 
114
        return 0;
 
115
    }
 
116
    PyObject* o = (PyObject*) *buf;
 
117
    char *data = PyBytes_AsString(o); 
 
118
    GIntBig nRet = (GIntBig)VSIFReadL( data, nMembSize, nMembCount, fp );
 
119
    if (nRet * nMembSize < buf_size)
 
120
    {
 
121
        _PyBytes_Resize(&o, nRet * nMembSize);
 
122
        *buf = o;
 
123
    }
 
124
    return nRet;
 
125
#else 
 
126
    *buf = (void *)PyString_FromStringAndSize( NULL, buf_size ); 
 
127
    if (*buf == NULL)
 
128
    {
 
129
        CPLError(CE_Failure, CPLE_OutOfMemory, "Cannot allocate result buffer");
 
130
        return 0;
 
131
    }
 
132
    PyObject* o = (PyObject*) *buf;
 
133
    char *data = PyString_AsString(o); 
 
134
    GIntBig nRet = (GIntBig)VSIFReadL( data, nMembSize, nMembCount, fp );
 
135
    if (nRet * nMembSize < buf_size)
 
136
    {
 
137
        _PyString_Resize(&o, nRet * nMembSize);
 
138
        *buf = o;
 
139
    }
 
140
    return nRet;
 
141
#endif
 
142
}
 
143
%}
 
144
%clear (void **buf );
 
145
 
 
146
/* -------------------------------------------------------------------- */
 
147
/*      GDAL_GCP                                                        */
 
148
/* -------------------------------------------------------------------- */
 
149
 
89
150
%extend GDAL_GCP {
90
151
%pythoncode {
91
152
  def __str__(self):
113
174
}
114
175
 
115
176
%extend GDALRasterBandShadow {
 
177
%apply ( void **outPythonObject ) { (void **buf ) };
 
178
%apply ( int *optional_int ) {(int*)};
 
179
%feature( "kwargs" ) ReadRaster1;
 
180
  CPLErr ReadRaster1( int xoff, int yoff, int xsize, int ysize,
 
181
                     void **buf,
 
182
                     int *buf_xsize = 0,
 
183
                     int *buf_ysize = 0,
 
184
                     int *buf_type = 0,
 
185
                     int *buf_pixel_space = 0,
 
186
                     int *buf_line_space = 0) {
 
187
    int nxsize = (buf_xsize==0) ? xsize : *buf_xsize;
 
188
    int nysize = (buf_ysize==0) ? ysize : *buf_ysize;
 
189
    GDALDataType ntype  = (buf_type==0) ? GDALGetRasterDataType(self)
 
190
                                        : (GDALDataType)*buf_type;
 
191
    int pixel_space = (buf_pixel_space == 0) ? 0 : *buf_pixel_space;
 
192
    int line_space = (buf_line_space == 0) ? 0 : *buf_line_space;
 
193
 
 
194
    GIntBig buf_size = ComputeBandRasterIOSize( nxsize, nysize, GDALGetDataTypeSize( ntype ) / 8,
 
195
                                            pixel_space, line_space, FALSE ); 
 
196
    if (buf_size == 0)
 
197
    {
 
198
        *buf = NULL;
 
199
        return CE_Failure;
 
200
    }
 
201
%#if PY_VERSION_HEX >= 0x03000000 
 
202
    *buf = (void *)PyBytes_FromStringAndSize( NULL, buf_size ); 
 
203
    if (*buf == NULL)
 
204
    {
 
205
        *buf = Py_None;
 
206
        CPLError(CE_Failure, CPLE_OutOfMemory, "Cannot allocate result buffer");
 
207
        return CE_Failure;
 
208
    }
 
209
    char *data = PyBytes_AsString( (PyObject *)*buf ); 
 
210
%#else 
 
211
    *buf = (void *)PyString_FromStringAndSize( NULL, buf_size ); 
 
212
    if (*buf == NULL)
 
213
    {
 
214
        CPLError(CE_Failure, CPLE_OutOfMemory, "Cannot allocate result buffer");
 
215
        return CE_Failure;
 
216
    }
 
217
    char *data = PyString_AsString( (PyObject *)*buf ); 
 
218
%#endif
 
219
    CPLErr eErr = GDALRasterIO( self, GF_Read, xoff, yoff, xsize, ysize, 
 
220
                         (void *) data, nxsize, nysize, ntype, 
 
221
                         pixel_space, line_space ); 
 
222
    if (eErr == CE_Failure)
 
223
    {
 
224
        Py_DECREF((PyObject*)*buf);
 
225
        *buf = NULL;
 
226
    }
 
227
    return eErr;
 
228
  }
 
229
%clear (void **buf );
 
230
%clear (int*);
 
231
 
116
232
%pythoncode {
 
233
 
 
234
  def ReadRaster(self, xoff, yoff, xsize, ysize,
 
235
                   buf_xsize = None, buf_ysize = None, buf_type = None,
 
236
                   buf_pixel_space = None, buf_line_space = None ):
 
237
 
 
238
      return _gdal.Band_ReadRaster1(self, xoff, yoff, xsize, ysize,
 
239
                                    buf_xsize, buf_ysize, buf_type,
 
240
                                    buf_pixel_space, buf_line_space)
 
241
 
117
242
  def ReadAsArray(self, xoff=0, yoff=0, win_xsize=None, win_ysize=None,
118
243
                  buf_xsize=None, buf_ysize=None, buf_obj=None):
119
244
      import gdalnumeric
134
259
}
135
260
 
136
261
%extend GDALDatasetShadow {
 
262
%feature("kwargs") ReadRaster1;
 
263
%apply (int *optional_int) { (GDALDataType *buf_type) };
 
264
%apply (int nList, int *pList ) { (int band_list, int *pband_list ) };
 
265
%apply ( void **outPythonObject ) { (void **buf ) };
 
266
%apply ( int *optional_int ) {(int*)};
 
267
CPLErr ReadRaster1(  int xoff, int yoff, int xsize, int ysize,
 
268
                    void **buf,
 
269
                    int *buf_xsize = 0, int *buf_ysize = 0,
 
270
                    GDALDataType *buf_type = 0,
 
271
                    int band_list = 0, int *pband_list = 0,
 
272
                    int* buf_pixel_space = 0, int* buf_line_space = 0, int* buf_band_space = 0 )
 
273
{
 
274
    int nxsize = (buf_xsize==0) ? xsize : *buf_xsize;
 
275
    int nysize = (buf_ysize==0) ? ysize : *buf_ysize;
 
276
    GDALDataType ntype;
 
277
    if ( buf_type != 0 ) {
 
278
      ntype = (GDALDataType) *buf_type;
 
279
    } else {
 
280
      int lastband = GDALGetRasterCount( self ) - 1;
 
281
      if (lastband < 0)
 
282
      {
 
283
          *buf = NULL;
 
284
          return CE_Failure;
 
285
      }
 
286
      ntype = GDALGetRasterDataType( GDALGetRasterBand( self, lastband ) );
 
287
    }
 
288
 
 
289
    int pixel_space = (buf_pixel_space == 0) ? 0 : *buf_pixel_space;
 
290
    int line_space = (buf_line_space == 0) ? 0 : *buf_line_space;
 
291
    int band_space = (buf_band_space == 0) ? 0 : *buf_band_space;
 
292
 
 
293
    GIntBig buf_size = ComputeDatasetRasterIOSize (nxsize, nysize, GDALGetDataTypeSize( ntype ) / 8,
 
294
                                               band_list ? band_list : GDALGetRasterCount(self), pband_list, band_list,
 
295
                                               pixel_space, line_space, band_space, FALSE);
 
296
    if (buf_size == 0)
 
297
    {
 
298
        *buf = NULL;
 
299
        return CE_Failure;
 
300
    }
 
301
 
 
302
%#if PY_VERSION_HEX >= 0x03000000 
 
303
    *buf = (void *)PyBytes_FromStringAndSize( NULL, buf_size ); 
 
304
    if (*buf == NULL)
 
305
    {
 
306
        CPLError(CE_Failure, CPLE_OutOfMemory, "Cannot allocate result buffer");
 
307
        return CE_Failure;
 
308
    }
 
309
    char *data = PyBytes_AsString( (PyObject *)*buf ); 
 
310
%#else 
 
311
    *buf = (void *)PyString_FromStringAndSize( NULL, buf_size ); 
 
312
    if (*buf == NULL)
 
313
    {
 
314
        CPLError(CE_Failure, CPLE_OutOfMemory, "Cannot allocate result buffer");
 
315
        return CE_Failure;
 
316
    }
 
317
    char *data = PyString_AsString( (PyObject *)*buf ); 
 
318
%#endif
 
319
 
 
320
    CPLErr eErr = GDALDatasetRasterIO(self, GF_Read, xoff, yoff, xsize, ysize,
 
321
                               (void*) data, nxsize, nysize, ntype,
 
322
                               band_list, pband_list, pixel_space, line_space, band_space );
 
323
    if (eErr == CE_Failure)
 
324
    {
 
325
        Py_DECREF((PyObject*)*buf);
 
326
        *buf = NULL;
 
327
    }
 
328
    return eErr;
 
329
}
 
330
 
 
331
%clear (GDALDataType *buf_type);
 
332
%clear (int band_list, int *pband_list );
 
333
%clear (void **buf );
 
334
%clear (int*);
 
335
 
137
336
%pythoncode {
138
337
    def ReadAsArray(self, xoff=0, yoff=0, xsize=None, ysize=None, buf_obj=None ):
139
338
        import gdalnumeric
172
371
 
173
372
        if buf_type is None:
174
373
            buf_type = self.GetRasterBand(1).DataType;
175
 
        return _gdal.Dataset_ReadRaster(self, xoff, yoff, xsize, ysize,
176
 
                                           buf_xsize, buf_ysize, buf_type,
177
 
                                           band_list, buf_pixel_space, buf_line_space, buf_band_space )
 
374
 
 
375
        return _gdal.Dataset_ReadRaster1(self, xoff, yoff, xsize, ysize,
 
376
                                            buf_xsize, buf_ysize, buf_type,
 
377
                                            band_list, buf_pixel_space, buf_line_space, buf_band_space )
178
378
 
179
379
    def GetSubDatasets(self):
180
380
        sd_list = []
189
389
                              sd['SUBDATASET_'+str(i)+'_DESC'] ) )
190
390
            i = i + 1
191
391
        return sd_list
 
392
 
 
393
    def BeginAsyncReader(self, xoff, yoff, xsize, ysize, buf_obj = None, buf_xsize = None, buf_ysize = None, buf_type = None, band_list = None, options=[]):
 
394
        if band_list is None:
 
395
            band_list = range(1, self.RasterCount + 1)
 
396
        if buf_xsize is None:
 
397
            buf_xsize = 0;
 
398
        if buf_ysize is None:
 
399
            buf_ysize = 0;
 
400
        if buf_type is None:
 
401
            buf_type = GDT_Byte
 
402
 
 
403
        if buf_xsize <= 0:
 
404
            buf_xsize = xsize
 
405
        if buf_ysize <= 0:
 
406
            buf_ysize = ysize
 
407
 
 
408
        if buf_obj is None:
 
409
            from sys import version_info
 
410
            nRequiredSize = int(buf_xsize * buf_ysize * len(band_list) * (_gdal.GetDataTypeSize(buf_type) / 8))
 
411
            if version_info >= (3,0,0):
 
412
                buf_obj_ar = [ None ]
 
413
                exec("buf_obj_ar[0] = b' ' * nRequiredSize")
 
414
                buf_obj = buf_obj_ar[0]
 
415
            else:
 
416
                buf_obj = ' ' * nRequiredSize
 
417
        return _gdal.Dataset_BeginAsyncReader(self, xoff, yoff, xsize, ysize, buf_obj, buf_xsize, buf_ysize, buf_type, band_list,  0, 0, 0, options)
192
418
}
193
419
}
194
420
 
267
493
    return bContinue;    
268
494
}
269
495
%}
270
 
%import typemaps_python.i
 
496