def array_to_raster(a, gt, wkt, xoff=None, yoff=None, dtype=None): ''' Creates a raster from a given array, with optional x- and y-offsets if the array was clipped. Arguments: a A NumPy array gt A GDAL GeoTransform tuple wkt Well-Known Text projection xoff The offset in the x-direction; should be provided when clipped yoff The offset in the y-direction; should be provided when clipped dtype The data type to coerce on the array ''' if dtype is not None: a = a.astype(dtype) try: rast = gdal_array.OpenNumPyArray(a) except AttributeError: # For backwards compatibility with older version of GDAL rast = gdal.Open(gdalnumeric.GetArrayFilename(a)) except: rast = gdal_array.OpenArray(a) rast.SetGeoTransform(gt) rast.SetProjection(wkt) if xoff is not None and yoff is not None: # Bit of a hack; essentially, re-create the raster but with the # correct X and Y offsets (don't know how to do this without the # use of CopyDatasetInfo()) return array_to_raster_clone(a, rast, xoff, yoff) return rast
def array_to_raster_clone(a, proto, xoff=None, yoff=None): ''' Creates a raster from a given array and a prototype raster dataset, with optional x- and y-offsets if the array was clipped. Arguments: a A NumPy array proto A prototype dataset xoff The offset in the x-direction; should be provided when clipped yoff The offset in the y-direction; should be provided when clipped ''' try: rast = gdal_array.OpenNumPyArray(a) except AttributeError: # For backwards compatibility with older version of GDAL rast = gdal.Open(gdalnumeric.GetArrayFilename(a)) except: rast = gdal_array.OpenArray(a) kwargs = dict() if xoff is not None and yoff is not None: kwargs = dict(xoff=xoff, yoff=yoff) # Copy the projection info and metadata from a prototype dataset if type(proto) == str: proto = gdal.Open(proto) gdalnumeric.CopyDatasetInfo(proto, rast, **kwargs) return rast
def array_to_raster(a, gt, wkt, xoff=None, yoff=None, dtype=None): ''' Creates a raster from a given array, with optional x- and y-offsets if the array was clipped. Arguments: a A NumPy array gt A GDAL GeoTransform tuple wkt Well-Known Text projection xoff The offset in the x-direction; should be provided when clipped yoff The offset in the y-direction; should be provided when clipped dtype The data type to coerce on the array ''' if dtype is not None: a = a.astype(dtype) try: rast = gdal_array.OpenNumPyArray(a) except AttributeError: # For backwards compatibility with older version of GDAL rast = gdal.Open(gdalnumeric.GetArrayFilename(a)) kwargs = dict() if xoff is not None and yoff is not None: kwargs = dict(xoff=xoff, yoff=yoff) rast.SetGeoTransform(gt) rast.SetProjection(wkt) return rast
def OpenArray(array, prototype_ds=None, xoff=0, yoff=0): ds = gdal.Open(gdalnumeric.GetArrayFilename(array)) if ds is not None and prototype_ds is not None: if type(prototype_ds).__name__ == 'str': prototype_ds = gdal.Open(prototype_ds) if prototype_ds is not None: gdalnumeric.CopyDatasetInfo(prototype_ds, ds, xoff=xoff, yoff=yoff) return ds