def open_avector(self, path, layer=None, driver='ESRI Shapefile', options=(), mode='r'): """Open a vector file anonymously in this DataSource. Only metadata are kept in memory. See DataSource.open_vector """ gdal_ds, lyr = Vector._open_file(path, layer, driver, options, mode) options = [str(arg) for arg in options] _ = conv.of_of_mode(mode) consts = Vector._Constants( self, gdal_ds=gdal_ds, lyr=lyr, open_options=options, mode=mode, layer=layer, ) prox = Vector(self, consts, gdal_ds, lyr) self._register([], prox) self._register_new_activated(prox) return prox
def awrap_numpy_raster(self, fp, array, band_schema=None, sr=None, mode='w'): """Register a numpy array as a raster anonymously in this DataSource. See DataSource.wrap_numpy_raster """ # Parameter checking *************************************************** if not isinstance(fp, Footprint): # pragma: no cover raise TypeError('`fp` should be a Footprint') array = np.asarray(array) if array.shape[:2] != tuple(fp.shape): # pragma: no cover raise ValueError('Incompatible shape between `array` and `fp`') if array.ndim not in [2, 3]: # pragma: no cover raise ValueError('Array should have 2 or 3 dimensions') band_count = 1 if array.ndim == 2 else array.shape[-1] band_schema = _tools.sanitize_band_schema(band_schema, band_count) if sr is not None: sr = osr.GetUserInputAsWKT(sr) _ = conv.of_of_mode(mode) if sr is not None: fp = self._back.convert_footprint(fp, sr) # Construction ********************************************************* prox = NumpyRaster(self, fp, array, band_schema, sr, mode) # DataSource Registering *********************************************** self._register([], prox) return prox
def open_raster(self, key, path, driver='GTiff', options=(), mode='r'): """Open a raster file in this DataSource under `key`. Only metadata are kept in memory. Parameters ---------- key: hashable (like a string) File identifier within DataSource path: string driver: string gdal driver to use when opening the file http://www.gdal.org/formats_list.html options: sequence of str options for gdal mode: one of ('r', 'w') Example ------- >>> ds.open_raster('ortho', '/path/to/ortho.tif') >>> ortho = ds.open_araster('/path/to/ortho.tif') >>> ds.open_raster('dem', '/path/to/dem.tif', mode='w') """ self._validate_key(key) gdal_ds = RasterPhysical._open_file(path, driver, options, mode) options = [str(arg) for arg in options] _ = conv.of_of_mode(mode) consts = RasterPhysical._Constants(self, gdal_ds=gdal_ds, open_options=options, mode=mode) prox = RasterPhysical(self, consts, gdal_ds) self._register([key], prox) self._register_new_activated(prox) return prox
def open_file(path, layer, driver, options, mode): """Open a vector datasource""" options = [str(arg) for arg in options] if len(options) else [] gdal_ds = gdal.OpenEx( path, conv.of_of_mode(mode) | conv.of_of_str('vector'), [driver], options, ) if gdal_ds is None: # pragma: no cover raise ValueError( 'Could not open `{}` with `{}` (gdal error: `{}`)'.format( path, driver, str(gdal.GetLastErrorMsg()).strip('\n'))) if layer is None: layer = 0 if isinstance(layer, numbers.Integral): lyr = gdal_ds.GetLayer(layer) else: lyr = gdal_ds.GetLayerByName(layer) if lyr is None: # pragma: no cover count = gdal_ds.GetLayerCount() raise Exception( 'Could not open layer `{}` ({} layers available: {}) (gdal error: `{}`)' .format( layer, count, { i: gdal_ds.GetLayerByIndex(i).GetName() for i in range(count) }, str(gdal.GetLastErrorMsg()).strip('\n'), )) return gdal_ds, lyr
def aopen_raster(self, path, driver='GTiff', options=(), mode='r'): """Open a raster file anonymously in this DataSource. Only metadata are kept in memory. See DataSource.open_raster Example ------ >>> ortho = ds.aopen_raster('/path/to/ortho.tif') >>> file_wkt = ds.ortho.wkt_stored """ # Parameter checking *************************************************** path = str(path) driver = str(driver) options = [str(arg) for arg in options] _ = conv.of_of_mode(mode) # Construction dispatch ************************************************ if driver.lower() == 'mem': # pragma: no cover raise ValueError("Can't open a MEM raster, user acreate_raster") elif True: allocator = lambda: BackGDALFileRaster.open_file( path, driver, options, mode) prox = GDALFileRaster(self, allocator, options, mode) else: pass # DataSource Registering *********************************************** self._register([], prox) return prox
def create_file(cls, path, geometry, fields, layer, driver, options, sr): """Create a vector datasource""" with Env(_osgeo_use_exceptions=False): dr = gdal.GetDriverByName(driver) gdal_ds = gdal.OpenEx( path, conv.of_of_mode('w') | conv.of_of_str('vector'), [driver], options, ) if gdal_ds is None: gdal_ds = dr.Create(path, 0, 0, 0, 0, options) else: if gdal_ds.GetLayerByName( layer) is not None: # pragma: no cover err = gdal_ds.DeleteLayer(layer) if err: raise Exception('Could not delete %s' % path) # See TODO on deletion of existing file # if gdal_ds.GetLayerCount() == 0: # del gdal_ds # err = dr.DeleteDataSource(path) # if err: # raise Exception('Could not delete %s' % path) # gdal_ds = dr.CreateDataSource(path, options) if gdal_ds is None: # pragma: no cover raise Exception('Could not create gdal dataset (%s)' % str(gdal.GetLastErrorMsg()).strip('\n')) if sr is not None: sr = osr.SpatialReference(osr.GetUserInputAsWKT(sr)) geometry = conv.wkbgeom_of_str(geometry) lyr = gdal_ds.CreateLayer(layer, sr, geometry, options) if lyr is None: # pragma: no cover raise Exception('Could not create layer (%s)' % str(gdal.GetLastErrorMsg()).strip('\n')) for field in fields: flddef = ogr.FieldDefn(field['name'], field['type']) if field['precision'] is not None: flddef.SetPrecision(field['precision']) if field['width'] is not None: flddef.SetWidth(field['width']) if field['nullable'] is not None: flddef.SetNullable(field['nullable']) if field['default'] is not None: flddef.SetDefault(field['default']) lyr.CreateField(flddef) lyr.SyncToDisk() gdal_ds.FlushCache() return gdal_ds, lyr
def _open_file(cls, path, driver, options, mode): """Open a raster datasource""" options = [str(arg) for arg in options] gdal_ds = gdal.OpenEx( path, conv.of_of_mode(mode) | conv.of_of_str('raster'), [driver], options, ) if gdal_ds is None: raise ValueError( 'Could not open `{}` with `{}` (gdal error: `{}`)'.format( path, driver, gdal.GetLastErrorMsg())) return gdal_ds
def _create_vrt(cls, fp, dtype, band_count, band_schema, sr): band_schema = cls._sanitize_band_schema(band_schema, band_count) vrt_xml = cls._create_vrt_xml_str(fp, dtype, band_count, band_schema, sr) gdal_ds = gdal.OpenEx( vrt_xml, conv.of_of_mode('w') | conv.of_of_str('raster'), ['VRT'], ) if gdal_ds is None: raise Exception('Could not create gdal dataset (%s)' % gdal.GetLastErrorMsg()) gdal_ds.FlushCache() return gdal_ds
def open_file(path, driver, options, mode): """Open a raster dataset""" gdal_ds = gdal.OpenEx( path, conv.of_of_mode(mode) | conv.of_of_str('raster'), [driver], options, ) if gdal_ds is None: # pragma: no cover raise ValueError( 'Could not open `{}` with `{}` (gdal error: `{}`)'.format( path, driver, str(gdal.GetLastErrorMsg()).strip('\n'))) return gdal_ds
def open_raster(self, key, path, driver='GTiff', options=(), mode='r'): """Open a raster file in this DataSource under `key`. Only metadata are kept in memory. Parameters ---------- key: hashable (like a string) File identifier within DataSource path: string driver: string gdal driver to use when opening the file http://www.gdal.org/formats_list.html options: sequence of str options for gdal mode: one of {'r', 'w'} Returns ------- GDALFileRaster Example ------- >>> ds.open_raster('ortho', '/path/to/ortho.tif') >>> file_proj4 = ds.ortho.proj4_stored >>> ds.open_raster('dem', '/path/to/dem.tif', mode='w') >>> nodata_value = ds.dem.nodata """ # Parameter checking *************************************************** self._validate_key(key) path = str(path) driver = str(driver) options = [str(arg) for arg in options] _ = conv.of_of_mode(mode) # Construction dispatch ************************************************ if driver.lower() == 'mem': # pragma: no cover raise ValueError("Can't open a MEM raster, user create_raster") elif True: allocator = lambda: BackGDALFileRaster.open_file( path, driver, options, mode) prox = GDALFileRaster(self, allocator, options, mode) else: pass # DataSource Registering *********************************************** self._register([key], prox) return prox
def open_file(path, driver, options, mode): """Open a raster dataset""" success, payload = GDALErrorCatcher(gdal.OpenEx, none_is_error=True)( path, conv.of_of_mode(mode) | conv.of_of_str('raster'), [driver], options, ) if not success: raise RuntimeError( 'Could not open `{}` using driver `{}` (gdal error: `{}`)'. format(path, driver, payload[1])) gdal_ds = payload return gdal_ds
def open_araster(self, path, driver='GTiff', options=(), mode='r'): """Open a raster file anonymously in this DataSource. Only metadata are kept in memory. See DataSource.open_raster """ gdal_ds = RasterPhysical._open_file(path, driver, options, mode) options = [str(arg) for arg in options] _ = conv.of_of_mode(mode) consts = RasterPhysical._Constants(self, gdal_ds=gdal_ds, open_options=list(options), mode=mode) prox = RasterPhysical(self, consts, gdal_ds) self._register([], prox) self._register_new_activated(prox) return prox
def _read_cache_data(self, cache_tile, produce_fp, produced_data, bands): """ reads cache data """ # print(self.h, "reading") filepaths = self._get_cache_tile_path(cache_tile) assert len(filepaths) == 1, len(filepaths) filepath = filepaths[0] # Open a raster datasource options = () gdal_ds = gdal.OpenEx( filepath, conv.of_of_mode('r') | conv.of_of_str('raster'), ['GTiff'], options, ) if gdal_ds is None: raise ValueError( 'Could not open `{}` with `{}` (gdal error: `{}`)'.format( filepath, 'GTiff', gdal.GetLastErrorMsg())) assert produce_fp.same_grid(cache_tile) to_read_fp = produce_fp.intersection(cache_tile) rtlx, rtly = cache_tile.spatial_to_raster(to_read_fp.tl) assert rtlx >= 0 and rtlx < cache_tile.rsizex assert rtly >= 0 and rtly < cache_tile.rsizey for band in bands: a = gdal_ds.GetRasterBand(band).ReadAsArray( int(rtlx), int(rtly), int(to_read_fp.rsizex), int(to_read_fp.rsizey), buf_obj=produced_data[ to_read_fp.slice_in(produce_fp, clip=True) + (band - 1, )]) if a is None: raise ValueError( 'Could not read array (gdal error: `{}`)'.format( gdal.GetLastErrorMsg()))
def open_vector(self, key, path, layer=None, driver='ESRI Shapefile', options=(), mode='r'): """Open a vector file in this DataSource under `key`. Only metadata are kept in memory. Parameters ---------- key: hashable (like a string) File identifier within DataSource path: string layer: None or int or string driver: string ogr driver to use when opening the file http://www.gdal.org/ogr_formats.html options: sequence of str options for ogr mode: one of ('r', 'w') Example ------- >>> ds.open_vector('trees', '/path/to.shp') >>> trees = ds.open_avector('/path/to.shp') >>> ds.open_vector('roofs', '/path/to.json', driver='GeoJSON', mode='w') """ self._validate_key(key) gdal_ds, lyr = Vector._open_file(path, layer, driver, options, mode) options = [str(arg) for arg in options] _ = conv.of_of_mode(mode) consts = Vector._Constants( self, gdal_ds=gdal_ds, lyr=lyr, open_options=options, mode=mode, layer=layer, ) prox = Vector(self, consts, gdal_ds, lyr) self._register([key], prox) self._register_new_activated(prox) return prox
def aopen_vector(self, path, layer=None, driver='ESRI Shapefile', options=(), mode='r'): """Open a vector file anonymously in this DataSource. Only metadata are kept in memory. See DataSource.open_vector Example ------- >>> trees = ds.aopen_vector('/path/to.shp') >>> features_bounds = trees.bounds """ path = str(path) if layer is None: layer = 0 elif isinstance(layer, numbers.Integral): layer = int(layer) else: layer = str(layer) driver = str(driver) options = [str(arg) for arg in options] _ = conv.of_of_mode(mode) # Construction dispatch ************************************************ if driver.lower() == 'memory': # pragma: no cover raise ValueError("Can't open a MEMORY vector, user create_vector") elif True: allocator = lambda: BackGDALFileVector.open_file( path, layer, driver, options, mode) prox = GDALFileVector(self, allocator, options, mode) else: pass # DataSource Registering *********************************************** self._register([], prox) return prox
def open_file(path, layer, driver, options, mode): """Open a vector dataset""" options = [str(arg) for arg in options] if len(options) else [] success, payload = GDALErrorCatcher(gdal.OpenEx, none_is_error=True)( path, conv.of_of_mode(mode) | conv.of_of_str('vector'), [driver], options, ) if not success: raise RuntimeError( 'Could not open `{}` using driver `{}` (gdal error: `{}`)'. format(path, driver, payload[1])) gdal_ds = payload if layer is None: layer = 0 if np.all(np.isreal(layer)): success, payload = GDALErrorCatcher(gdal_ds.GetLayer)(int(layer)) else: success, payload = GDALErrorCatcher(gdal_ds.GetLayerByName)(layer) if not success: # pragma: no cover count = gdal_ds.GetLayerCount() raise Exception( 'Could not open layer `{}` of `{}` ({} layers available: {}) (gdal error: `{}`)' .format( layer, path, count, { i: gdal_ds.GetLayerByIndex(i).GetName() for i in range(count) }, payload[1], )) lyr = payload return gdal_ds, lyr
def _open_file(cls, path, layer, driver, options, mode): """Open a vector datasource""" options = [str(arg) for arg in options] if len(options) else [] gdal_ds = gdal.OpenEx( path, conv.of_of_mode(mode) | conv.of_of_str('vector'), [driver], options, ) if gdal_ds is None: raise ValueError('Could not open `{}` with `{}` (gdal error: `{}`)'.format( path, driver, gdal.GetLastErrorMsg() )) if layer is None: layer = 0 if isinstance(layer, numbers.Integral): lyr = gdal_ds.GetLayer(layer) else: lyr = gdal_ds.GetLayerByName(layer) if lyr is None: raise Exception('Could not open layer (gdal error: %s)' % gdal.GetLastErrorMsg()) return gdal_ds, lyr
def wrap_numpy_raster(self, key, fp, array, band_schema=None, sr=None, mode='w'): """Register a numpy array as a raster under `key` in this DataSource. Parameters ---------- key: hashable (like a string) File identifier within DataSource fp: Footprint of shape (Y, X) Description of the location and size of the raster to create. array: ndarray of shape (Y, X) or (Y, X, B) band_schema: dict or None Band(s) metadata. (see `Band fields` below) sr: string or None Spatial reference of the new file if None: don't set a spatial reference if string: if path: Use same projection as file at `path` if textual spatial reference: http://gdal.org/java/org/gdal/osr/SpatialReference.html#SetFromUserInput-java.lang.String- mode: one of {'r', 'w'} Returns ------- NumpyRaster Band fields ----------- Fields: 'nodata': None or number 'interpretation': None or str 'offset': None or number 'scale': None or number 'mask': None or one of ('') Interpretation values: undefined, grayindex, paletteindex, redband, greenband, blueband, alphaband, hueband, saturationband, lightnessband, cyanband, magentaband, yellowband, blackband Mask values: all_valid, per_dataset, alpha, nodata A field missing or None is kept to default value. A field can be passed as: a value: All bands are set to this value a sequence of length `band_count` of value: All bands will be set to respective state """ # Parameter checking *************************************************** self._validate_key(key) if not isinstance(fp, Footprint): # pragma: no cover raise TypeError('`fp` should be a Footprint') array = np.asarray(array) if array.shape[:2] != tuple(fp.shape): # pragma: no cover raise ValueError('Incompatible shape between `array` and `fp`') if array.ndim not in [2, 3]: # pragma: no cover raise ValueError('Array should have 2 or 3 dimensions') band_count = 1 if array.ndim == 2 else array.shape[-1] band_schema = _tools.sanitize_band_schema(band_schema, band_count) if sr is not None: sr = osr.GetUserInputAsWKT(sr) _ = conv.of_of_mode(mode) if sr is not None: fp = self._back.convert_footprint(fp, sr) # Construction ********************************************************* prox = NumpyRaster(self, fp, array, band_schema, sr, mode) # DataSource Registering *********************************************** self._register([key], prox) return prox
def open_vector(self, key, path, layer=None, driver='ESRI Shapefile', options=(), mode='r'): """Open a vector file in this DataSource under `key`. Only metadata are kept in memory. Parameters ---------- key: hashable (like a string) File identifier within DataSource path: string layer: None or int or string driver: string ogr driver to use when opening the file http://www.gdal.org/ogr_formats.html options: sequence of str options for ogr mode: one of {'r', 'w'} Returns ------- GDALFileVector Example ------- >>> ds.open_vector('trees', '/path/to.shp') >>> feature_count = len(ds.trees) >>> ds.open_vector('roofs', '/path/to.json', driver='GeoJSON', mode='w') >>> fields_list = ds.roofs.fields """ # Parameter checking *************************************************** self._validate_key(key) path = str(path) if layer is None: layer = 0 elif isinstance(layer, numbers.Integral): layer = int(layer) else: layer = str(layer) driver = str(driver) options = [str(arg) for arg in options] _ = conv.of_of_mode(mode) # Construction dispatch ************************************************ if driver.lower() == 'memory': # pragma: no cover raise ValueError("Can't open a MEMORY vector, user create_vector") elif True: allocator = lambda: BackGDALFileVector.open_file( path, layer, driver, options, mode) prox = GDALFileVector(self, allocator, options, mode) else: pass # DataSource Registering *********************************************** self._register([key], prox) return prox
def _create_file(cls, path, geometry, fields, layer, driver, options, sr): """Create a vector datasource""" if layer is None: layer = '.'.join(ntpath.basename(path).split('.')[:-1]) elif not isinstance(layer, str): raise TypeError('layer should be None or str') options = [str(arg) for arg in options] if len(options) else [] with Env(_osgeo_use_exceptions=False): dr = gdal.GetDriverByName(driver) gdal_ds = gdal.OpenEx( path, conv.of_of_mode('w') | conv.of_of_str('vector'), [driver], options, ) if gdal_ds is None: gdal_ds = dr.Create(path, 0, 0, 0, 0, options) else: if gdal_ds.GetLayerByName(layer) is not None: err = gdal_ds.DeleteLayer(layer) if err: raise Exception('Could not delete %s' % path) # See todo on deletion of existing file # if gdal_ds.GetLayerCount() == 0: # del gdal_ds # err = dr.DeleteDataSource(path) # if err: # raise Exception('Could not delete %s' % path) # gdal_ds = dr.CreateDataSource(path, options) if gdal_ds is None: raise Exception('Could not create gdal dataset (%s)' % gdal.GetLastErrorMsg()) if sr is not None: sr = osr.SpatialReference(osr.GetUserInputAsWKT(sr)) geometry = conv.wkbgeom_of_str(geometry) lyr = gdal_ds.CreateLayer(layer, sr, geometry, options) if lyr is None: raise Exception('Could not create layer (%s)' % gdal.GetLastErrorMsg()) fields = cls._normalize_fields_defn(fields) for field in fields: flddef = ogr.FieldDefn(field['name'], field['type']) if field['precision'] is not None: flddef.SetPrecision(field['precision']) if field['width'] is not None: flddef.SetWidth(field['width']) if field['nullable'] is not None: flddef.SetNullable(field['nullable']) if field['default'] is not None: flddef.SetDefault(field['default']) lyr.CreateField(flddef) lyr.SyncToDisk() gdal_ds.FlushCache() return gdal_ds, lyr