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 _details_of_file(path): with Env(_osgeo_use_exceptions=False): gdal_ds = gdal.OpenEx(path, conv.of_of_str('raster')) if gdal_ds is not None: aff = affine.Affine.from_gdal(*gdal_ds.GetGeoTransform()) w, h = gdal_ds.RasterXSize, gdal_ds.RasterYSize cx, cy = aff * [w / 2, h / 2] return gdal_ds.GetProjection(), (cx, cy) gdal_ds = gdal.OpenEx(path, conv.of_of_str('vector')) if gdal_ds is not None: lyr = gdal_ds.GetLayerByIndex(0) if lyr is None: raise ValueError('Could not open file layer') extent = lyr.GetExtent() if extent is None: raise ValueError('Could not compute extent') minx, maxx, miny, maxy = extent cx, cy = (maxx + minx) / 2, (maxy + miny) / 2 return lyr.GetSpatialRef().ExportToWkt(), (cx, cy) raise ValueError('Could not open file')
def wkt_of_any(string): """Wkt of user input""" out = osr.GetUserInputAsWKT(string) if isinstance(out, str): return out else: prj = None with Env(_osgeo_use_exceptions=False): gdal_ds = gdal.OpenEx(string, conv.of_of_str('raster')) if gdal_ds is not None: prj = gdal_ds.GetProjection() gdal_ds = gdal.OpenEx(string, conv.of_of_str('vector')) if gdal_ds is not None: lyr = gdal_ds.GetLayerByIndex(0) if lyr is not None: prj = lyr.GetSpatialRef() if prj is not None: return prj.ExportToWkt() raise ValueError('Could not convert to wkt ({})'.format( gdal.GetLastErrorMsg()))
def get_data(self, *args, **kwargs): with Env(_gdal_trust_buzzard=True): return Raster.get_data(self, *args, **kwargs)
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