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 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 process(self, obj_data): """ Project data in an image wrapper @param obj_data: Image wrapper """ for index, (label, data) in enumerate(obj_data.getIterator()): wkt = obj_data.info(label)['WKT'] geotransform = obj_data.info(label)['GeoTransform'] if (self.center_coords.lower() == 'first' and index == 0) or \ self.center_coords.lower() == 'all': center_lon, center_lat = self._get_center_coords( wkt, geotransform, data.shape) ds = gdal_array.OpenNumPyArray(data) ds.SetGeoTransform(geotransform) ds.SetProjection(obj_data.info(label)['WKT']) gdal_dtype = get_gdal_dtype(data.dtype) reprojected_ds = project_insar_data(ds, center_lon, center_lat, data_type=gdal_dtype) obj_data.updateData(label, reprojected_ds.ReadAsArray()) obj_data.info(label)['WKT'] = reprojected_ds.GetProjection() obj_data.info( label)['GeoTransform'] = reprojected_ds.GetGeoTransform()
def OpenArray(array, prototype_ds=None, xoff=0, yoff=0): #ds = gdal.Open( gdalnumeric.GetArrayFilename(array) ) ds = gdal_array.OpenNumPyArray(array) #ds=gdal_array.OpenArray(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
def __init__(self, array: np.ndarray, geotransform: GeoTransform, epsg: Union[int, EPSG], nodata: float = None): """ @param nodata: which value should be no data """ self.data = array self.geotransform = geotransform self.epsg = epsg self.nodata = nodata self.raster = gdal_array.OpenNumPyArray(array, True) self.raster.SetGeoTransform(geotransform.to_gdal()) srs = osr.SpatialReference() srs.ImportFromEPSG(epsg) self.raster.SetSpatialRef(srs)
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 ''' rast = gdal_array.OpenNumPyArray(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 process(self, obj_data): """ Project data in an image wrapper @param obj_data: Image wrapper """ egmdf = FILEDF([ AutoList([ 'http://download.osgeo.org/proj/vdatum/egm96_15/egm96_15.gtx' ]) ]) egmdw = egmdf.output() egmurl, egmfilename = next(egmdw.getIterator()) for index, (label, data) in enumerate(obj_data.getIterator()): wkt = obj_data.info(label)['WKT'] geotransform = obj_data.info(label)['GeoTransform'] ds = gdal_array.OpenNumPyArray(data) ds.SetGeoTransform(geotransform) ds.SetProjection(obj_data.info(label)['WKT']) gdal_dtype = get_gdal_dtype(data.dtype) reprojected_ds = georaster_vertical_datum_shift( georaster=ds, old_datum_proj4= '+proj=longlat +datum=WGS84 +no_defs +geoidgrids=' + egmfilename, new_datum_proj4='+proj=longlat +datum=WGS84 +no_defs') obj_data.updateData(label, reprojected_ds.ReadAsArray())
# %% b_all=np.dstack((im_b4.get_data(),im_b8.get_data(),im.get_data())) # %% segments_slic = slic(b_all, n_segments=round(math.sqrt(ma.count(im.get_data()))*1.5), max_iter=100, compactness=0.5, sigma=1, multichannel=True, convert2lab=True, enforce_connectivity=True, min_size_factor=0.03, max_size_factor=5, slic_zero=True) # %% segments=Imagee(segments_slic,im.get_metadata()) # %% segments.export_as_tif('vyrez_segmenty.tif') # %% c=np.dstack((b_all,segments.get_data())) #vectorize segment data segments_polygons=gdal_array.OpenNumPyArray(segments.get_data(),binterleave=True) segments_polygons.SetGeoTransform(segments.get_metadata()['affine_transformation']) segments_polygons.SetProjection(segments.get_metadata()['proj_wkt']) srs=osr.SpatialReference() srs.ImportFromWkt(segments.get_metadata()['proj_wkt']) #outDriver=ogr.GetDriverByName('MEMORY') outDriver=ogr.GetDriverByName('ESRI Shapefile') outDataSource=outDriver.CreateDataSource('segments.shp') outLayer = outDataSource.CreateLayer("data", srs,geom_type=ogr.wkbPolygon) pixel_value = ogr.FieldDefn("pixel_value", ogr.OFTInteger) outLayer.CreateField(pixel_value) gdal.Polygonize( segments_polygons.GetRasterBand(1) , None, outLayer, 0) # %% del(outLayer,outDataSource,outDriver)