Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _get_data(self):
""" Returns DataSource geometries as numpy ndarrays
"""
lyr = self.ds.GetLayer()
sources = []
for feature in lyr:
geom = feature.GetGeometryRef()
poly = georef.vector.ogr_to_numpy(geom)
sources.append(poly)
return np.array(sources)
lyr = self.trg.ds.GetLayerByName('trg')
feat = lyr.GetFeature(idx)
trg = feat.GetGeometryRef()
except Exception:
raise TypeError("No target polygon found at index {0}".
format(idx))
else:
raise TypeError('No target polygons found in object!')
# check for trg
if trg is None:
raise TypeError('Either *trg* or *idx* keywords must be given!')
# check for geometry
if not type(trg) == ogr.Geometry:
trg = georef.vector.numpy_to_ogr(trg, 'Polygon')
# apply Buffer value
trg = trg.Buffer(buf)
if idx is None:
intersecs = self.dst.get_data_by_geom(trg)
else:
intersecs = self.dst.get_data_by_att('trg_index', idx)
return intersecs
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
trg_lyr.ResetReading()
trg_lyr.SetSpatialFilter(None)
# buffer handling (time consuming)
if self._buffer > 0:
for i in range(trg_lyr.GetFeatureCount()):
feat = trg_lyr.GetFeature(i)
feat.SetGeometryDirectly(feat.GetGeometryRef().
Buffer(self._buffer))
trg_lyr.SetFeature(feat)
# reset target layer
trg_lyr.ResetReading()
# create tmp dest layer
self.tmp_lyr = georef.vector.ogr_create_layer(ds_mem, 'dst',
srs=self._srs,
geom_type=geom_type)
trg_lyr.Intersection(src_lyr, self.tmp_lyr,
options=['SKIP_FAILURES=YES',
'INPUT_PREFIX=trg_',
'METHOD_PREFIX=src_',
'PROMOTE_TO_MULTI=YES',
'USE_PREPARED_GEOMETRIES=YES',
'PRETEST_CONTAINMENT=YES'],
callback=progress)
georef.vector.ogr_copy_layer(ds_mem, 0, ds_out)
return ds_out
# create tmp dest layer
self.tmp_lyr = georef.vector.ogr_create_layer(ds_mem, 'dst',
srs=self._srs,
geom_type=geom_type)
trg_lyr.Intersection(src_lyr, self.tmp_lyr,
options=['SKIP_FAILURES=YES',
'INPUT_PREFIX=trg_',
'METHOD_PREFIX=src_',
'PROMOTE_TO_MULTI=YES',
'USE_PREPARED_GEOMETRIES=YES',
'PRETEST_CONTAINMENT=YES'],
callback=progress)
georef.vector.ogr_copy_layer(ds_mem, 0, ds_out)
return ds_out