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_write_numeric_layer(self):
with pytest.raises(ValueError):
Collection("foo", mode='w', layer=1)
def setup(self):
self.tempdir = tempfile.mkdtemp()
schema = {'geometry': 'Point', 'properties': {'title': 'str'}}
self.c = Collection(os.path.join(self.tempdir, "foo.shp"),
"w", driver="ESRI Shapefile", schema=schema)
def test_write_geojson_layer(self):
with pytest.raises(ValueError):
Collection("foo", mode='w', driver='GeoJSON', layer='foo')
def test_append_geojson(self):
with pytest.raises(ValueError):
Collection("foo", mode='w', driver='ARCGEN')
def test_archive(self):
with pytest.raises(TypeError):
Collection("foo", mode='r', archive=1)
def test_driver(self):
with pytest.raises(TypeError):
Collection("foo", mode='w', driver=1)
def test_layer(self):
with pytest.raises(TypeError):
Collection("foo", mode='r', layer=0.5)
return fp_writer(fp)
else:
# If a pathlib.Path instance is given, convert it to a string path.
if isinstance(fp, Path):
fp = str(fp)
if vfs:
warnings.warn("The vfs keyword argument is deprecated. Instead, pass a URL that uses a zip or tar (for example) scheme.", FionaDeprecationWarning, stacklevel=2)
path, scheme, archive = vfs_parse_paths(fp, vfs=vfs)
path = ParsedPath(path, archive, scheme)
else:
path = parse_path(fp)
if mode in ('a', 'r'):
c = Collection(path, mode, driver=driver, encoding=encoding,
layer=layer, enabled_drivers=enabled_drivers, **kwargs)
elif mode == 'w':
if schema:
# Make an ordered dict of schema properties.
this_schema = schema.copy()
this_schema['properties'] = OrderedDict(schema['properties'])
else:
this_schema = None
c = Collection(path, mode, crs=crs, driver=driver, schema=this_schema,
encoding=encoding, layer=layer, enabled_drivers=enabled_drivers, crs_wkt=crs_wkt,
**kwargs)
else:
raise ValueError(
"mode string must be one of 'r', 'w', or 'a', not %s" % mode)
return c
valid_types.add("Multi" + geom_type)
return valid_types
def get_filetype(bytesbuf):
"""Detect compression type of bytesbuf.
ZIP only. TODO: add others relevant to GDAL/OGR."""
if bytesbuf[:4].startswith(b'PK\x03\x04'):
return 'zip'
else:
return ''
class BytesCollection(Collection):
"""BytesCollection takes a buffer of bytes and maps that to
a virtual file that can then be opened by fiona.
"""
def __init__(self, bytesbuf, sharing=True, **kwds):
"""Takes buffer of bytes whose contents is something we'd like
to open with Fiona and maps it to a virtual file.
"""
if not isinstance(bytesbuf, binary_type):
raise ValueError("input buffer must be bytes")
# Hold a reference to the buffer, as bad things will happen if
# it is garbage collected while in use.
self.bytesbuf = bytesbuf
# Map the buffer to a file. If the buffer contains a zipfile
# we take extra steps in naming the buffer and in opening
# Trying first the GeoJSON driver, then the Shapefile driver,
# the following succeeds:
fiona.open(
'example.shp', enabled_drivers=['GeoJSON', 'ESRI Shapefile'])
"""
# Parse the vfs into a vsi and an archive path.
path, vsi, archive = parse_paths(path, vfs)
if mode in ('a', 'r'):
if archive:
if not os.path.exists(archive):
raise IOError("no such archive file: %r" % archive)
elif path != '-' and not os.path.exists(path):
raise IOError("no such file or directory: %r" % path)
c = Collection(path, mode, driver=driver, encoding=encoding,
layer=layer, vsi=vsi, archive=archive,
enabled_drivers=enabled_drivers)
elif mode == 'w':
if schema:
# Make an ordered dict of schema properties.
this_schema = schema.copy()
this_schema['properties'] = OrderedDict(schema['properties'])
else:
this_schema = None
c = Collection(path, mode, crs=crs, driver=driver, schema=this_schema,
encoding=encoding, layer=layer, vsi=vsi, archive=archive,
enabled_drivers=enabled_drivers, crs_wkt=crs_wkt)
else:
raise ValueError(
"mode string must be one of 'r', 'w', or 'a', not %s" % mode)
return c