Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _check_src(self, src):
"""Basic check of source elements (sequence of points or polygons).
- array cast of source elements
- create ogr_src datasource/layer holding src points/polygons
- transforming source grid points/polygons to ogr.geometries
on ogr.layer
"""
tmpfile = tempfile.NamedTemporaryFile(mode='w+b').name
ogr_src = io.gdal.gdal_create_dataset('ESRI Shapefile',
os.path.join('/vsimem', tmpfile),
gdal_type=gdal.OF_VECTOR)
src = np.array(src)
# create memory datasource, layer and create features
if src.ndim == 2:
geom_type = ogr.wkbPoint
# no Polygons, just Points
else:
geom_type = ogr.wkbPolygon
fields = [('index', ogr.OFTInteger)]
georef.vector.ogr_create_layer(ogr_src, self._name, srs=self._srs,
geom_type=geom_type, fields=fields)
georef.vector.ogr_add_feature(ogr_src, src, name=self._name)
return ogr_src
def _create_dst_datasource(self, silent):
"""Create destination target gdal.Dataset
Creates one layer for each target polygon, consisting of
the needed source data attributed with index and weights fields
Returns
-------
ds_mem : object
gdal.Dataset object
"""
progress = None if (silent or isWindows) else gdal.TermProgress
# create mem-mapped temp file dataset
tmpfile = tempfile.NamedTemporaryFile(mode='w+b').name
ds_out = io.gdal.gdal_create_dataset('ESRI Shapefile',
os.path.join('/vsimem', tmpfile),
gdal_type=gdal.OF_VECTOR)
# create intermediate mem dataset
ds_mem = io.gdal.gdal_create_dataset('Memory', 'out',
gdal_type=gdal.OF_VECTOR)
# get src geometry layer
src_lyr = self.src.ds.GetLayerByName('src')
src_lyr.ResetReading()
src_lyr.SetSpatialFilter(None)
geom_type = src_lyr.GetGeomType()
# get trg geometry layer
trg_lyr = self.trg.ds.GetLayerByName('trg')
trg_lyr.ResetReading()
def load_vector(self, filename, source=0, driver='ESRI Shapefile'):
"""Read Layer from OGR Vector File
Parameters
----------
filename : string
path to shape-filename
source : int or string
number or name of wanted layer, defaults to 0
driver : string
driver string
"""
tmpfile = tempfile.NamedTemporaryFile(mode='w+b').name
self.ds = io.gdal.gdal_create_dataset('ESRI Shapefile',
os.path.join('/vsimem', tmpfile),
gdal_type=gdal.OF_VECTOR)
# get input file handles
ds_in, tmp_lyr = io.gdal.open_vector(filename, driver=driver,
layer=source)
# copy layer
ogr_src_lyr = self.ds.CopyLayer(tmp_lyr, self._name)
# get spatial reference object
srs = ogr_src_lyr.GetSpatialRef()
if srs is not None:
self._srs = ogr_src_lyr.GetSpatialRef()
# flush everything
del ds_in
def dump_vector(self, filename, driver='ESRI Shapefile', remove=True):
"""Output layer to OGR Vector File
Parameters
----------
filename : string
path to shape-filename
driver : string
driver string
remove : bool
if True removes existing output file
"""
ds_out = io.gdal.gdal_create_dataset(driver, filename,
gdal_type=gdal.OF_VECTOR,
remove=remove)
georef.vector.ogr_copy_layer(self.ds, 0, ds_out)
# flush everything
del ds_out