Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# remove by name
fiona.remove(filename, layer="layer2")
assert fiona.listlayers(filename) == ["layer1", "layer4"]
# remove by negative index
fiona.remove(filename, layer=-1)
assert fiona.listlayers(filename) == ["layer1"]
# invalid layer name
with pytest.raises(ValueError):
fiona.remove(filename, layer="invalid_layer_name")
# invalid layer index
with pytest.raises(DatasetDeleteError):
fiona.remove(filename, layer=999)
def test_remove(tmpdir, kind, driver, specify_driver):
"""Test various dataset removal operations"""
extension = {"ESRI Shapefile": "shp", "GeoJSON": "json"}[driver]
filename = "delete_me.{extension}".format(extension=extension)
output_filename = str(tmpdir.join(filename))
create_sample_data(output_filename, driver=driver)
if kind == "collection":
to_delete = fiona.open(output_filename, "r")
else:
to_delete = output_filename
assert os.path.exists(output_filename)
if specify_driver:
fiona.remove(to_delete, driver=driver)
else:
fiona.remove(to_delete)
assert not os.path.exists(output_filename)
def test_remove_layer_geojson(tmpdir):
"""Removal of layers is not supported by GeoJSON driver
The reason for failure is slightly different between GDAL 2.2+ and < 2.2.
With < 2.2 the datasource will fail to open in write mode (IOError), while
with 2.2+ the datasource will open but the removal operation will fail (not
supported).
"""
filename = str(tmpdir.join("a_filename.geojson"))
create_sample_data(filename, "GeoJSON")
with pytest.raises((RuntimeError, IOError)):
fiona.remove(filename, layer=0)
assert os.path.exists(filename)
def test_remove_layer_shapefile(tmpdir):
"""Removal of layer in shapefile actually deletes the datasource"""
filename = str(tmpdir.join("a_filename.shp"))
create_sample_data(filename, "ESRI Shapefile")
fiona.remove(filename, layer=0)
assert not os.path.exists(filename)
"""Test various dataset removal operations"""
extension = {"ESRI Shapefile": "shp", "GeoJSON": "json"}[driver]
filename = "delete_me.{extension}".format(extension=extension)
output_filename = str(tmpdir.join(filename))
create_sample_data(output_filename, driver=driver)
if kind == "collection":
to_delete = fiona.open(output_filename, "r")
else:
to_delete = output_filename
assert os.path.exists(output_filename)
if specify_driver:
fiona.remove(to_delete, driver=driver)
else:
fiona.remove(to_delete)
assert not os.path.exists(output_filename)
def test_remove_nonexistent(tmpdir):
"""Attempting to remove a file that does not exist results in an IOError"""
filename = str(tmpdir.join("does_not_exist.shp"))
assert not os.path.exists(filename)
with pytest.raises(IOError):
fiona.remove(filename)
if os.path.isfile(self.path):
logger.debug("read existing entries")
with fiona.open(self.path, "r") as src:
self._existing = {f["properties"]["tile_id"]: f for f in src}
self.sink = fiona.open(self.path, "a")
else:
self.sink = fiona.open(
self.path, "w", driver=self.driver, crs=crs, schema=schema
)
self._existing = {}
else:
if os.path.isfile(self.path):
logger.debug("read existing entries")
with fiona.open(self.path, "r") as src:
self._existing = {f["properties"]["tile_id"]: f for f in src}
fiona.remove(self.path, driver=driver)
else:
self._existing = {}
self.sink = fiona.open(
self.path, "w", driver=self.driver, crs=crs, schema=schema
)
self.sink.writerecords(self._existing.values())
def convertLabelStringToPoly(shapeFileSrc, outGeoJSon, labelType='Airplane', aspectRatio=3, labelName=''):
source_layer_gdf = gpd.read_file(shapeFileSrc)
# Create the output Layer
if os.path.exists(outGeoJSon):
fiona.remove(outGeoJSon, 'GeoJSON').DeleteDataSource(outGeoJSon)
source_layer_gdf["geometry"], \
source_layer_gdf["Length_m"], \
source_layer_gdf["Width_m"], \
source_layer_gdf["Aspect(L/W)"], \
source_layer_gdf["compassDeg"] = source_layer_gdf.apply(
lambda x: evaluateLineStringFeature(x['geometry'],
labelType=labelType,
aspectRatio=aspectRatio,
labelName=labelName
),
axis=1)
source_layer_gdf.to_file(outGeoJSon, driver='GeoJSON')
def rm(ctx, input, layer, yes):
if layer is None:
kind = "datasource"
else:
kind = "layer"
if not yes:
click.confirm("The {} will be removed. Are you sure?".format(kind), abort=True)
try:
fiona.remove(input, layer=layer)
except Exception:
logger.exception("Failed to remove {}.".format(kind))
raise click.Abort()