1
2
3
4
5
6
7 from sys import version_info as _swig_python_version_info
8 if _swig_python_version_info >= (2, 7, 0):
10 import importlib
11 pkg = __name__.rpartition('.')[0]
12 mname = '.'.join((pkg, '_gdal_array')).lstrip('.')
13 try:
14 return importlib.import_module(mname)
15 except ImportError:
16 return importlib.import_module('_gdal_array')
17 _gdal_array = swig_import_helper()
18 del swig_import_helper
19 elif _swig_python_version_info >= (2, 6, 0):
21 from os.path import dirname
22 import imp
23 fp = None
24 try:
25 fp, pathname, description = imp.find_module('_gdal_array', [dirname(__file__)])
26 except ImportError:
27 import _gdal_array
28 return _gdal_array
29 try:
30 _mod = imp.load_module('_gdal_array', fp, pathname, description)
31 finally:
32 if fp is not None:
33 fp.close()
34 return _mod
35 _gdal_array = swig_import_helper()
36 del swig_import_helper
37 else:
38 import _gdal_array
39 del _swig_python_version_info
40
41 try:
42 _swig_property = property
43 except NameError:
44 pass
45
46 try:
47 import builtins as __builtin__
48 except ImportError:
49 import __builtin__
50
52 if (name == "thisown"):
53 return self.this.own(value)
54 if (name == "this"):
55 if type(value).__name__ == 'SwigPyObject':
56 self.__dict__[name] = value
57 return
58 method = class_type.__swig_setmethods__.get(name, None)
59 if method:
60 return method(self, value)
61 if (not static):
62 if _newclass:
63 object.__setattr__(self, name, value)
64 else:
65 self.__dict__[name] = value
66 else:
67 raise AttributeError("You cannot add attributes to %s" % self)
68
69
72
73
75 if (name == "thisown"):
76 return self.this.own()
77 method = class_type.__swig_getmethods__.get(name, None)
78 if method:
79 return method(self)
80 raise AttributeError("'%s' object has no attribute '%s'" % (class_type.__name__, name))
81
82
84 try:
85 strthis = "proxy of " + self.this.__repr__()
86 except __builtin__.Exception:
87 strthis = ""
88 return "<%s.%s; %s >" % (self.__class__.__module__, self.__class__.__name__, strthis,)
89
90 try:
91 _object = object
92 _newclass = 1
93 except __builtin__.Exception:
96 _newclass = 0
97
98 from sys import version_info as _swig_python_version_info
99 if _swig_python_version_info >= (2, 7, 0):
100 from . import gdal
101 else:
102 import gdal
103 del _swig_python_version_info
105 """Proxy of C++ CPLVirtualMemShadow class."""
106
107 __swig_setmethods__ = {}
108 __setattr__ = lambda self, name, value: _swig_setattr(self, VirtualMem, name, value)
109 __swig_getmethods__ = {}
110 __getattr__ = lambda self, name: _swig_getattr(self, VirtualMem, name)
111
113 raise AttributeError("No constructor defined")
114 __repr__ = _swig_repr
115 __swig_destroy__ = _gdal_array.delete_VirtualMem
116 __del__ = lambda self: None
117
119 """GetAddr(VirtualMem self)"""
120 return _gdal_array.VirtualMem_GetAddr(self)
121
122
123 - def Pin(self, start_offset=0, nsize=0, bWriteOp=0):
124 """
125 Pin(VirtualMem self, size_t start_offset=0, size_t nsize=0, int bWriteOp=0)
126 Pin(VirtualMem self, size_t start_offset=0, size_t nsize=0)
127 Pin(VirtualMem self, size_t start_offset=0)
128 Pin(VirtualMem self)
129 """
130 return _gdal_array.VirtualMem_Pin(self, start_offset, nsize, bWriteOp)
131
132 VirtualMem_swigregister = _gdal_array.VirtualMem_swigregister
133 VirtualMem_swigregister(VirtualMem)
134
135
139
141 """TermProgress_nocb(double dfProgress, char const * pszMessage=None, void * pData=None) -> int"""
142 return _gdal_array.TermProgress_nocb(dfProgress, pszMessage, pData)
143 TermProgress = _gdal_array.TermProgress
144
146 """OpenNumPyArray(PyArrayObject * psArray, bool binterleave) -> Dataset"""
147 return _gdal_array.OpenNumPyArray(psArray, binterleave)
148
152
154 """GetArrayFilename(PyArrayObject * psArray) -> retStringAndCPLFree *"""
155 return _gdal_array.GetArrayFilename(psArray)
156
157 -def BandRasterIONumPy(band, bWrite, xoff, yoff, xsize, ysize, psArray, buf_type, resample_alg, callback=0, callback_data=None):
158 """BandRasterIONumPy(Band band, int bWrite, double xoff, double yoff, double xsize, double ysize, PyArrayObject * psArray, GDALDataType buf_type, GDALRIOResampleAlg resample_alg, GDALProgressFunc callback=0, void * callback_data=None) -> CPLErr"""
159 return _gdal_array.BandRasterIONumPy(band, bWrite, xoff, yoff, xsize, ysize, psArray, buf_type, resample_alg, callback, callback_data)
160
161 -def DatasetIONumPy(ds, bWrite, xoff, yoff, xsize, ysize, psArray, buf_type, resample_alg, callback=0, callback_data=None, binterleave=True, band_list=0):
162 """DatasetIONumPy(Dataset ds, int bWrite, double xoff, double yoff, double xsize, double ysize, PyArrayObject * psArray, GDALDataType buf_type, GDALRIOResampleAlg resample_alg, GDALProgressFunc callback=0, void * callback_data=None, bool binterleave=True, int band_list=0) -> CPLErr"""
163 return _gdal_array.DatasetIONumPy(ds, bWrite, xoff, yoff, xsize, ysize, psArray, buf_type, resample_alg, callback, callback_data, binterleave, band_list)
164
165 -def MDArrayIONumPy(bWrite, mdarray, psArray, nDims1, nDims3, buffer_datatype):
166 """MDArrayIONumPy(bool bWrite, GDALMDArrayHS * mdarray, PyArrayObject * psArray, int nDims1, int nDims3, GDALExtendedDataTypeHS * buffer_datatype) -> CPLErr"""
167 return _gdal_array.MDArrayIONumPy(bWrite, mdarray, psArray, nDims1, nDims3, buffer_datatype)
168
170 """VirtualMemGetArray(VirtualMem virtualmem)"""
171 return _gdal_array.VirtualMemGetArray(virtualmem)
172
174 """RATValuesIONumPyWrite(RasterAttributeTable poRAT, int nField, int nStart, PyArrayObject * psArray) -> CPLErr"""
175 return _gdal_array.RATValuesIONumPyWrite(poRAT, nField, nStart, psArray)
176
178 """RATValuesIONumPyRead(RasterAttributeTable poRAT, int nField, int nStart, int nLength) -> PyObject *"""
179 return _gdal_array.RATValuesIONumPyRead(poRAT, nField, nStart, nLength)
180
181 import numpy
182
183 from osgeo import gdalconst
184 from osgeo import gdal
185 gdal.AllRegister()
186
187 codes = {gdalconst.GDT_Byte: numpy.uint8,
188 gdalconst.GDT_UInt16: numpy.uint16,
189 gdalconst.GDT_Int16: numpy.int16,
190 gdalconst.GDT_UInt32: numpy.uint32,
191 gdalconst.GDT_Int32: numpy.int32,
192 gdalconst.GDT_UInt64: numpy.uint64,
193 gdalconst.GDT_Int64: numpy.int64,
194 gdalconst.GDT_Float32: numpy.float32,
195 gdalconst.GDT_Float64: numpy.float64,
196 gdalconst.GDT_CInt16: numpy.complex64,
197 gdalconst.GDT_CInt32: numpy.complex64,
198 gdalconst.GDT_CFloat32: numpy.complex64,
199 gdalconst.GDT_CFloat64: numpy.complex128}
200
201
202 -def OpenArray(array, prototype_ds=None, interleave='band'):
203
204 interleave = interleave.lower()
205 if interleave == 'band':
206 interleave = True
207 elif interleave == 'pixel':
208 interleave = False
209 else:
210 raise ValueError('Interleave should be band or pixel')
211
212 ds = OpenNumPyArray(array, interleave)
213
214 if ds is not None and prototype_ds is not None:
215 if type(prototype_ds).__name__ == 'str':
216 prototype_ds = gdal.Open(prototype_ds)
217 if prototype_ds is not None:
218 CopyDatasetInfo(prototype_ds, ds)
219
220 return ds
221
222
241
243 if not isinstance(numeric_type, (numpy.dtype, type)):
244 raise TypeError("Input must be a type")
245 return flip_code(numeric_type)
246
249
254
255 -def LoadFile(filename, xoff=0, yoff=0, xsize=None, ysize=None,
256 buf_xsize=None, buf_ysize=None, buf_type=None,
257 resample_alg=gdal.GRIORA_NearestNeighbour,
258 callback=None, callback_data=None, interleave='band',
259 band_list=None):
260 ds = gdal.Open(filename)
261 if ds is None:
262 raise ValueError("Can't open "+filename+"\n\n"+gdal.GetLastErrorMsg())
263
264 return DatasetReadAsArray(ds, xoff, yoff, xsize, ysize,
265 buf_xsize=buf_xsize, buf_ysize=buf_ysize, buf_type=buf_type,
266 resample_alg=resample_alg,
267 callback=callback, callback_data=callback_data,
268 interleave=interleave,
269 band_list=band_list)
270
271 -def SaveArray(src_array, filename, format="GTiff", prototype=None, interleave='band'):
272 driver = gdal.GetDriverByName(format)
273 if driver is None:
274 raise ValueError("Can't find driver "+format)
275
276 return driver.CreateCopy(filename, OpenArray(src_array, prototype, interleave))
277
278
279 -def DatasetReadAsArray(ds, xoff=0, yoff=0, win_xsize=None, win_ysize=None, buf_obj=None,
280 buf_xsize=None, buf_ysize=None, buf_type=None,
281 resample_alg=gdal.GRIORA_NearestNeighbour,
282 callback=None, callback_data=None, interleave='band',
283 band_list=None):
284 """Pure python implementation of reading a chunk of a GDAL file
285 into a numpy array. Used by the gdal.Dataset.ReadAsArray method."""
286
287 if win_xsize is None:
288 win_xsize = ds.RasterXSize
289 if win_ysize is None:
290 win_ysize = ds.RasterYSize
291
292 if band_list is None:
293 band_list = list(range(1, ds.RasterCount + 1))
294
295 interleave = interleave.lower()
296 if interleave == 'band':
297 interleave = True
298 xdim = 2
299 ydim = 1
300 banddim = 0
301 elif interleave == 'pixel':
302 interleave = False
303 xdim = 1
304 ydim = 0
305 banddim = 2
306 else:
307 raise ValueError('Interleave should be band or pixel')
308
309 nbands = len(band_list)
310 if nbands == 0:
311 return None
312
313 if nbands == 1:
314 return BandReadAsArray(ds.GetRasterBand(band_list[0]), xoff, yoff, win_xsize, win_ysize,
315 buf_xsize=buf_xsize, buf_ysize=buf_ysize, buf_type=buf_type,
316 buf_obj=buf_obj,
317 resample_alg=resample_alg,
318 callback=callback,
319 callback_data=callback_data)
320
321 if buf_obj is None:
322 if buf_xsize is None:
323 buf_xsize = win_xsize
324 if buf_ysize is None:
325 buf_ysize = win_ysize
326 if buf_type is None:
327 buf_type = ds.GetRasterBand(band_list[0]).DataType
328 for idx in range(1, nbands):
329 band_index = band_list[idx]
330 if buf_type != ds.GetRasterBand(band_index).DataType:
331 buf_type = gdalconst.GDT_Float32
332
333 typecode = GDALTypeCodeToNumericTypeCode(buf_type)
334 if typecode is None:
335 buf_type = gdalconst.GDT_Float32
336 typecode = numpy.float32
337 else:
338 buf_type = NumericTypeCodeToGDALTypeCode(typecode)
339
340 if buf_type == gdalconst.GDT_Byte and ds.GetRasterBand(1).GetMetadataItem('PIXELTYPE', 'IMAGE_STRUCTURE') == 'SIGNEDBYTE':
341 typecode = numpy.int8
342 buf_shape = (nbands, buf_ysize, buf_xsize) if interleave else (buf_ysize, buf_xsize, nbands)
343 buf_obj = numpy.empty(buf_shape, dtype=typecode)
344
345 else:
346 if len(buf_obj.shape) != 3:
347 raise ValueError('Array should have 3 dimensions')
348
349 shape_buf_xsize = buf_obj.shape[xdim]
350 shape_buf_ysize = buf_obj.shape[ydim]
351 if buf_xsize is not None and buf_xsize != shape_buf_xsize:
352 raise ValueError('Specified buf_xsize not consistent with array shape')
353 if buf_ysize is not None and buf_ysize != shape_buf_ysize:
354 raise ValueError('Specified buf_ysize not consistent with array shape')
355 if buf_obj.shape[banddim] != nbands:
356 raise ValueError('Dimension %d of array should have size %d to store bands)' % (banddim, nbands))
357
358 datatype = NumericTypeCodeToGDALTypeCode(buf_obj.dtype.type)
359 if not datatype:
360 raise ValueError("array does not have corresponding GDAL data type")
361 if buf_type is not None and buf_type != datatype:
362 raise ValueError("Specified buf_type not consistent with array type")
363 buf_type = datatype
364
365 if DatasetIONumPy(ds, 0, xoff, yoff, win_xsize, win_ysize,
366 buf_obj, buf_type, resample_alg, callback, callback_data,
367 interleave, band_list) != 0:
368 _RaiseException()
369 return None
370
371 return buf_obj
372
373
379 """Pure python implementation of writing a chunk of a GDAL file
380 from a numpy array. Used by the gdal.Dataset.WriteArray method."""
381
382 if band_list is None:
383 band_list = list(range(1, ds.RasterCount + 1))
384
385 interleave = interleave.lower()
386 if interleave == 'band':
387 interleave = True
388 xdim = 2
389 ydim = 1
390 banddim = 0
391 elif interleave == 'pixel':
392 interleave = False
393 xdim = 1
394 ydim = 0
395 banddim = 2
396 else:
397 raise ValueError('Interleave should be band or pixel')
398
399 if len(band_list) == 1:
400 if array is None or (len(array.shape) != 2 and len(array.shape) != 3):
401 raise ValueError("expected array of dim 2 or 3")
402 if len(array.shape) == 3:
403 if array.shape[banddim] != 1:
404 raise ValueError("expected size of dimension %d should be 1" % banddim)
405 array = array[banddim]
406
407 return BandWriteArray(ds.GetRasterBand(band_list[0]),
408 array,
409 xoff=xoff, yoff=yoff, resample_alg=resample_alg,
410 callback=callback, callback_data=callback_data)
411
412 if array is None or len(array.shape) != 3:
413 raise ValueError("expected array of dim 3")
414
415 xsize = array.shape[xdim]
416 ysize = array.shape[ydim]
417
418 if xsize + xoff > ds.RasterXSize or ysize + yoff > ds.RasterYSize:
419 raise ValueError("array larger than output file, or offset off edge")
420 if array.shape[banddim] != len(band_list):
421 raise ValueError('Dimension %d of array should have size %d to store bands)' % (banddim, len(band_list)))
422
423 datatype = NumericTypeCodeToGDALTypeCode(array.dtype.type)
424
425
426
427 if not datatype:
428 gdal.Debug('gdal_array', 'force array to float64')
429 array = array.astype(numpy.float64)
430 datatype = NumericTypeCodeToGDALTypeCode(array.dtype.type)
431
432 if not datatype:
433 raise ValueError("array does not have corresponding GDAL data type")
434
435 ret = DatasetIONumPy(ds, 1, xoff, yoff, xsize, ysize,
436 array, datatype, resample_alg, callback, callback_data,
437 interleave, band_list)
438 if ret != 0:
439 _RaiseException()
440 return ret
441
442
443 -def BandReadAsArray(band, xoff=0, yoff=0, win_xsize=None, win_ysize=None,
444 buf_xsize=None, buf_ysize=None, buf_type=None, buf_obj=None,
445 resample_alg=gdal.GRIORA_NearestNeighbour,
446 callback=None, callback_data=None):
447 """Pure python implementation of reading a chunk of a GDAL file
448 into a numpy array. Used by the gdal.Band.ReadAsArray method."""
449
450 if win_xsize is None:
451 win_xsize = band.XSize
452 if win_ysize is None:
453 win_ysize = band.YSize
454
455 if buf_obj is None:
456 if buf_xsize is None:
457 buf_xsize = win_xsize
458 if buf_ysize is None:
459 buf_ysize = win_ysize
460 if buf_type is None:
461 buf_type = band.DataType
462
463 typecode = GDALTypeCodeToNumericTypeCode(buf_type)
464 if typecode is None:
465 buf_type = gdalconst.GDT_Float32
466 typecode = numpy.float32
467 else:
468 buf_type = NumericTypeCodeToGDALTypeCode(typecode)
469
470 if buf_type == gdalconst.GDT_Byte and band.GetMetadataItem('PIXELTYPE', 'IMAGE_STRUCTURE') == 'SIGNEDBYTE':
471 typecode = numpy.int8
472 buf_obj = numpy.empty([buf_ysize, buf_xsize], dtype=typecode)
473
474 else:
475 if len(buf_obj.shape) not in (2, 3):
476 raise ValueError("expected array of dimension 2 or 3")
477
478 if len(buf_obj.shape) == 2:
479 shape_buf_xsize = buf_obj.shape[1]
480 shape_buf_ysize = buf_obj.shape[0]
481 else:
482 if buf_obj.shape[0] != 1:
483 raise ValueError("expected size of first dimension should be 0")
484 shape_buf_xsize = buf_obj.shape[2]
485 shape_buf_ysize = buf_obj.shape[1]
486 if buf_xsize is not None and buf_xsize != shape_buf_xsize:
487 raise ValueError('Specified buf_xsize not consistent with array shape')
488 if buf_ysize is not None and buf_ysize != shape_buf_ysize:
489 raise ValueError('Specified buf_ysize not consistent with array shape')
490
491 datatype = NumericTypeCodeToGDALTypeCode(buf_obj.dtype.type)
492 if not datatype:
493 raise ValueError("array does not have corresponding GDAL data type")
494 if buf_type is not None and buf_type != datatype:
495 raise ValueError("Specified buf_type not consistent with array type")
496 buf_type = datatype
497
498 if BandRasterIONumPy(band, 0, xoff, yoff, win_xsize, win_ysize,
499 buf_obj, buf_type, resample_alg, callback, callback_data) != 0:
500 _RaiseException()
501 return None
502
503 return buf_obj
504
508 """Pure python implementation of writing a chunk of a GDAL file
509 from a numpy array. Used by the gdal.Band.WriteArray method."""
510
511 if array is None or len(array.shape) != 2:
512 raise ValueError("expected array of dim 2")
513
514 xsize = array.shape[1]
515 ysize = array.shape[0]
516
517 if xsize + xoff > band.XSize or ysize + yoff > band.YSize:
518 raise ValueError("array larger than output file, or offset off edge")
519
520 datatype = NumericTypeCodeToGDALTypeCode(array.dtype.type)
521
522
523
524 if not datatype:
525 gdal.Debug('gdal_array', 'force array to float64')
526 array = array.astype(numpy.float64)
527 datatype = NumericTypeCodeToGDALTypeCode(array.dtype.type)
528
529 if not datatype:
530 raise ValueError("array does not have corresponding GDAL data type")
531
532 ret = BandRasterIONumPy(band, 1, xoff, yoff, xsize, ysize,
533 array, datatype, resample_alg, callback, callback_data)
534 if ret != 0:
535 _RaiseException()
536 return ret
537
570
574
575 -def MDArrayReadAsArray(mdarray,
576 array_start_idx = None,
577 count = None,
578 array_step = None,
579 buffer_datatype = None,
580 buf_obj = None):
604
605 -def MDArrayWriteArray(mdarray, array,
606 array_start_idx = None,
607 array_step = None):
634
636 """
637 Pure Python implementation of writing a chunk of the RAT
638 from a numpy array. Type of array is coerced to one of the types
639 (int, double, string) supported. Called from RasterAttributeTable.WriteArray
640 """
641 if array is None:
642 raise ValueError("Expected array of dim 1")
643
644
645 if not isinstance(array, numpy.ndarray):
646 array = numpy.array(array)
647
648 if array.ndim != 1:
649 raise ValueError("Expected array of dim 1")
650
651 if (start + array.size) > rat.GetRowCount():
652 raise ValueError("Array too big to fit into RAT from start position")
653
654 if numpy.issubdtype(array.dtype, numpy.integer):
655
656
657
658 array = array.astype(numpy.int32)
659 elif numpy.issubdtype(array.dtype, numpy.floating):
660
661 array = array.astype(numpy.double)
662 elif numpy.issubdtype(array.dtype, numpy.character):
663
664 array = array.astype(bytes)
665 else:
666 raise ValueError("Array not of a supported type (integer, double or string)")
667
668 ret = RATValuesIONumPyWrite(rat, field, start, array)
669 if ret != 0:
670 _RaiseException()
671 return ret
672
674 """
675 Pure Python implementation of reading a chunk of the RAT
676 into a numpy array. Called from RasterAttributeTable.ReadAsArray
677 """
678 if length is None:
679 length = rat.GetRowCount() - start
680
681 ret = RATValuesIONumPyRead(rat, field, start, length)
682 if ret is None:
683 _RaiseException()
684 return ret
685
687 """
688 Copy georeferencing information and metadata from one dataset to another.
689 src: input dataset
690 dst: output dataset - It can be a ROI -
691 xoff, yoff: dst's offset with respect to src in pixel/line.
692
693 Notes: Destination dataset must have update access. Certain formats
694 do not support creation of geotransforms and/or gcps.
695
696 """
697
698 dst.SetMetadata(src.GetMetadata())
699
700
701
702
703 gt = src.GetGeoTransform()
704 if gt != (0, 1, 0, 0, 0, 1):
705 dst.SetProjection(src.GetProjectionRef())
706
707 if xoff == 0 and yoff == 0:
708 dst.SetGeoTransform(gt)
709 else:
710 ngt = [gt[0], gt[1], gt[2], gt[3], gt[4], gt[5]]
711 ngt[0] = gt[0] + xoff*gt[1] + yoff*gt[2]
712 ngt[3] = gt[3] + xoff*gt[4] + yoff*gt[5]
713 dst.SetGeoTransform((ngt[0], ngt[1], ngt[2], ngt[3], ngt[4], ngt[5]))
714
715
716 elif src.GetGCPCount() > 0:
717
718 if (xoff == 0) and (yoff == 0):
719 dst.SetGCPs(src.GetGCPs(), src.GetGCPProjection())
720 else:
721 gcps = src.GetGCPs()
722
723 new_gcps = []
724 for gcp in gcps:
725 ngcp = gdal.GCP()
726 ngcp.GCPX = gcp.GCPX
727 ngcp.GCPY = gcp.GCPY
728 ngcp.GCPZ = gcp.GCPZ
729 ngcp.GCPPixel = gcp.GCPPixel - xoff
730 ngcp.GCPLine = gcp.GCPLine - yoff
731 ngcp.Info = gcp.Info
732 ngcp.Id = gcp.Id
733 new_gcps.append(ngcp)
734
735 try:
736 dst.SetGCPs(new_gcps, src.GetGCPProjection())
737 except:
738 print("Failed to set GCPs")
739 return
740
741 return
742
743
744