Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_options(tmpdir, path_coutwildrnp_shp):
"""Test that setting CPL_DEBUG=ON works and that a warning is raised."""
logfile = str(tmpdir.mkdir('tests').join('test_options.log'))
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler(logfile)
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
# fiona.drivers() will be deprecated.
with pytest.warns(FionaDeprecationWarning):
with fiona.drivers(CPL_DEBUG=True):
c = fiona.open(path_coutwildrnp_shp)
c.close()
with open(logfile, "r") as f:
log = f.read()
if fiona.gdal_version.major >= 2:
assert "GDALOpen" in log
else:
assert "OGROpen" in log
def _read_spatial_file(filepath):
try:
with fiona.drivers():
with fiona.open(filepath) as src:
data = []
for f in src:
element = {
'name': f['properties']['name'],
'feature': f
}
data.append(element)
return data
except NameError as ex:
msg = "Could not read spatial dimension definition. Please install fiona to read"
msg += "geographic data files. Try running: \n"
msg += " pip install smif[spatial]\n"
msg += "or:\n"
msg += " conda install fiona shapely rtree\n"
raise SmifDataReadError(msg) from ex
def _read_spatial_file(filepath) -> List[Dict]:
try:
with fiona.Env():
return _read_spatial_data(filepath)
except AttributeError:
# older fiona versions
with fiona.drivers():
return _read_spatial_data(filepath)
except NameError as ex:
msg = "Could not read spatial dimension definition '%s' " % (filepath)
msg += "Please install fiona to read geographic data files. Try running: \n"
msg += " pip install smif[spatial]\n"
msg += "or:\n"
msg += " conda install fiona shapely rtree\n"
raise SmifDataReadError(msg) from ex
except IOError as ex:
msg = "Could not read spatial dimension definition '%s' " % (filepath)
msg += "Please verify that the path is correct and "
msg += "that the file is present on this location."
raise SmifDataNotFoundError(msg) from ex
def import_with_fiona(fpath, source):
"""
Use fiona to import a parcel file.
Return a list of dict objects containing WKT-formatted geometries in
addition to any metadata.
"""
shapes = []
try:
with fiona.drivers():
data = fiona.open(fpath)
for obj in data:
try:
shape = scrape_fiona_metadata(obj, source)
geom = to_shapely_obj(obj)
if geom:
shape['geom'] = dumps(geom)
shapes.append(shape)
except Exception as e:
_L.warning('error loading shape from fiona. {}'.format(e))
except Exception as e:
_L.warning('error importing file. {}'.format(e))
return shapes
-------
list
A list of data from the specified file in a fiona formatted dict
"""
# Find filename for this region_definition_name
region_definition = self._region_definition_exists(region_definition_name)
if region_definition is None:
raise DataNotFoundError(
"Region definition '{}' not found".format(region_definition_name))
else:
filename = region_definition['filename']
# Read the region data from file
filepath = os.path.join(self.file_dir['region_definitions'], filename)
with fiona.drivers():
with fiona.open(filepath) as src:
data = [f for f in src]
return data
def get_locations(self, bbox=None):
if not bbox:
bbox = self.metadata['bbox']
demo_dir = util.get_dsl_demo_dir()
path = os.path.join(demo_dir, 'iraq', 'ALIDAR_20101015_Index.shp')
features = []
with fiona.drivers():
with fiona.open(path, 'r') as source:
for feature in source:
features.append(feature)
return FeatureCollection(features)
def get_locations(self, bbox=None):
if not bbox:
bbox = self.metadata['bbox']
demo_dir = util.get_dsl_demo_dir()
path = os.path.join(demo_dir, 'iraq', 'TLIDAR_20101015_Index.shp')
features = []
with fiona.drivers():
with fiona.open(path, 'r') as source:
for feature in source:
features.append(feature)
return FeatureCollection(features)