def collection(path, mode='r', driver=None, schema=None, crs=None): """Open file at ``path`` in ``mode`` "r" (read), "a" (append), or "w" (write) and return a ``Collection`` object. In append or write mode, a driver name such as "ESRI Shapefile" or "GPX" (see OGR docs or ``ogr2ogr --help`` on the command line) and a schema mapping such as: {'geometry': 'Point', 'properties': { 'class': 'int', 'label': 'str', 'value': 'float'}} must be provided. The ``crs`` (coordinate reference system) parameter is currently ignored. """ if mode in ('a', 'r'): c = Collection(path, mode) elif mode == 'w': if not driver: raise ValueError("An OGR driver name must be specified") if not schema: raise ValueError("A collection schema must be specified") c = Collection(path, mode, driver, schema) else: raise ValueError("Invalid mode: %s" % mode) c.open() return c
def collection(path, mode='r', driver=None, schema=None, crs=None): """Open file at ``path`` in ``mode`` "r" (read), "a" (append), or "w" (write) and return a ``Collection`` object. In write mode, a driver name such as "ESRI Shapefile" or "GPX" (see OGR docs or ``ogr2ogr --help`` on the command line) and a schema mapping such as: {'geometry': 'Point', 'properties': { 'class': 'int', 'label': 'str', 'value': 'float'}} must be provided. A coordinate reference system for collections in write mode can be defined by the ``crs`` parameter. It takes Proj4 style mappings like {'proj': 'longlat', 'ellps': 'WGS84', 'datum': 'WGS84', 'no_defs': True} """ if mode in ('a', 'r'): if not os.path.exists(path): raise OSError("File or directory '%s' not found" % path) c = Collection(path, mode) elif mode == 'w': if not driver: raise ValueError("An OGR driver name must be specified") if not schema: raise ValueError("A collection schema must be specified") c = Collection(path, mode, driver, schema, crs) else: raise ValueError("Invalid mode: %s" % mode) c.open() return c
def open(self, driver=None, schema=None, crs=None, encoding=None, layer=None, vfs=None, enabled_drivers=None, crs_wkt=None, **kwargs): """Open the file and return a Fiona collection object. If data has already been written, the file is opened in 'r' mode. Otherwise, the file is opened in 'w' mode. Parameters ---------- Note well that there is no `path` parameter: a `MemoryFile` contains a single dataset and there is no need to specify a path. Other parameters are optional and have the same semantics as the parameters of `fiona.open()`. """ vsi_path = self.name if self.closed: raise IOError("I/O operation on closed file.") if self.exists(): return Collection(vsi_path, 'r', driver=driver, encoding=encoding, layer=layer, enabled_drivers=enabled_drivers, **kwargs) else: if schema: # Make an ordered dict of schema properties. this_schema = schema.copy() this_schema['properties'] = OrderedDict(schema['properties']) else: this_schema = None return Collection(vsi_path, 'w', crs=crs, driver=driver, schema=this_schema, encoding=encoding, layer=layer, enabled_drivers=enabled_drivers, crs_wkt=crs_wkt, **kwargs)
def open(self, driver=None, schema=None, crs=None, encoding=None, layer=None, vfs=None, enabled_drivers=None, crs_wkt=None, **kwargs): """Open the file and return a Fiona collection object. If data has already been written, the file is opened in 'r' mode. Otherwise, the file is opened in 'w' mode. Parameters ---------- Note well that there is no `path` parameter: a `MemoryFile` contains a single dataset and there is no need to specify a path. Other parameters are optional and have the same semantics as the parameters of `fiona.open()`. """ if self.closed: raise IOError("I/O operation on closed file.") if not self.exists(): self._ensure_extension(driver) this_schema = schema.copy() this_schema["properties"] = OrderedDict(schema["properties"]) return Collection(self.name, "w", crs=crs, driver=driver, schema=this_schema, encoding=encoding, layer=layer, enabled_drivers=enabled_drivers, crs_wkt=crs_wkt, **kwargs) elif self.mode in ("r", "r+"): return Collection(self.name, "r", driver=driver, encoding=encoding, layer=layer, enabled_drivers=enabled_drivers, **kwargs)
def setUp(self): self.tempdir = tempfile.mkdtemp() schema = {'geometry': 'LineString', 'properties': {'title': 'str'}} self.c = Collection(os.path.join(self.tempdir, "foo.shp"), "w", "ESRI Shapefile", schema=schema)
def open(self, path, driver=None, encoding=None, layer=None, enabled_drivers=None, **kwargs): """Open a dataset within the zipped stream. Parameters ---------- path : str Path to a dataset in the zip file, relative to the root of the archive. Returns ------- A Fiona collection object """ vsi_path = '/vsizip{0}/{1}'.format(self.name, path.lstrip('/')) if self.closed: raise IOError("I/O operation on closed file.") return Collection(vsi_path, 'r', driver=driver, encoding=encoding, layer=layer, enabled_drivers=enabled_drivers, **kwargs)
def test_crs(self): with pytest.raises(TypeError): Collection("foo", mode='w', driver="ESRI Shapefile", schema=0, crs=1)
def _get_data(self, layer, name): if name not in self.data: path = layer["file"] if layer.get("type", "shp_index") == "shp_index": self.data[name] = Collection(path) elif layer.get("type") == "gdal": self.data[name] = rasterio.open(path) return self.data[name]
def open(path, mode='r', driver=None, schema=None, crs=None, encoding=None): """Open file at ``path`` in ``mode`` "r" (read), "a" (append), or "w" (write) and return a ``Collection`` object. In write mode, a driver name such as "ESRI Shapefile" or "GPX" (see OGR docs or ``ogr2ogr --help`` on the command line) and a schema mapping such as: {'geometry': 'Point', 'properties': { 'class': 'int', 'label': 'str', 'value': 'float'}} must be provided. A coordinate reference system for collections in write mode can be defined by the ``crs`` parameter. It takes Proj4 style mappings like {'proj': 'longlat', 'ellps': 'WGS84', 'datum': 'WGS84', 'no_defs': True} The drivers used by Fiona will try to detect the encoding of data files. If they fail, you may provide the proper ``encoding``, such as 'Windows-1252' for the Natural Earth datasets. """ if mode in ('a', 'r'): if not os.path.exists(path): raise IOError("no such file or directory: %r" % path) c = Collection(path, mode, encoding=encoding) elif mode == 'w': c = Collection(path, mode=mode, crs=crs, driver=driver, schema=schema, encoding=encoding) else: raise ValueError( "mode string must be one of 'r', 'w', or 'a', not %s" % mode) return c
def _get_data(self, layer, name): if name not in self.data: path = layer["file"] if layer.get("type", "shp_index") == "shp_index": # Avoid loading if not needed from fiona.collection import Collection # pylint: disable=import-outside-toplevel self.data[name] = Collection(path) elif layer.get("type") == "gdal": # Avoid loading if not needed import rasterio # pylint: disable=import-outside-toplevel self.data[name] = rasterio.open(path) return self.data[name]
def test_encoding(self): with pytest.raises(TypeError): Collection("foo", mode='r', encoding=1)
def test_append_geojson(self): with pytest.raises(ValueError): Collection("foo", mode='w', driver='ARCGEN')
def test_write_geojson_layer(self): with pytest.raises(ValueError): Collection("foo", mode='w', driver='GeoJSON', layer='foo')
def test_write_numeric_layer(self): with pytest.raises(ValueError): Collection("foo", mode='w', layer=1)
def test_archive(self): with pytest.raises(TypeError): Collection("foo", mode='r', archive=1)
def test_vsi(self): with pytest.raises(TypeError): Collection("foo", mode='r', vsi='git')
def open(path, mode='r', driver=None, schema=None, crs=None, encoding=None, layer=None, vfs=None): """Open file at ``path`` in ``mode`` "r" (read), "a" (append), or "w" (write) and return a ``Collection`` object. In write mode, a driver name such as "ESRI Shapefile" or "GPX" (see OGR docs or ``ogr2ogr --help`` on the command line) and a schema mapping such as: {'geometry': 'Point', 'properties': { 'class': 'int', 'label': 'str', 'value': 'float'}} must be provided. A coordinate reference system for collections in write mode can be defined by the ``crs`` parameter. It takes Proj4 style mappings like {'proj': 'longlat', 'ellps': 'WGS84', 'datum': 'WGS84', 'no_defs': True} The drivers used by Fiona will try to detect the encoding of data files. If they fail, you may provide the proper ``encoding``, such as 'Windows-1252' for the Natural Earth datasets. When the provided path is to a file containing multiple named layers of data, a layer can be singled out by ``layer``. A virtual filesystem can be specified. The ``vfs`` parameter may be an Apache Commons VFS style string beginning with "zip://" or "tar://"". In this case, the ``path`` must be an absolute path within that container. """ # 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 not os.path.exists(path): raise IOError("no such file or directory: %r" % path) c = Collection(path, mode, encoding=encoding, layer=layer, vsi=vsi, archive=archive) elif mode == 'w': c = Collection(path, mode, crs=crs, driver=driver, schema=schema, encoding=encoding, layer=layer, vsi=vsi, archive=archive) else: raise ValueError( "mode string must be one of 'r', 'w', or 'a', not %s" % mode) return c
def test_driver(self): with pytest.raises(TypeError): Collection("foo", mode='w', driver=1)
def test_mode(self): with pytest.raises(TypeError): Collection("foo", mode=0)
def test_path(self): with pytest.raises(TypeError): Collection(0)
def open(path, mode='r', driver=None, schema=None, crs=None, encoding=None, layer=None, vfs=None, enabled_drivers=None, crs_wkt=None): """Open file at ``path`` in ``mode`` "r" (read), "a" (append), or "w" (write) and return a ``Collection`` object. In write mode, a driver name such as "ESRI Shapefile" or "GPX" (see OGR docs or ``ogr2ogr --help`` on the command line) and a schema mapping such as: {'geometry': 'Point', 'properties': [('class', 'int'), ('label', 'str'), ('value', 'float')]} must be provided. If a particular ordering of properties ("fields" in GIS parlance) in the written file is desired, a list of (key, value) pairs as above or an ordered dict is required. If no ordering is needed, a standard dict will suffice. A coordinate reference system for collections in write mode can be defined by the ``crs`` parameter. It takes Proj4 style mappings like {'proj': 'longlat', 'ellps': 'WGS84', 'datum': 'WGS84', 'no_defs': True} short hand strings like EPSG:4326 or WKT representations of coordinate reference systems. The drivers used by Fiona will try to detect the encoding of data files. If they fail, you may provide the proper ``encoding``, such as 'Windows-1252' for the Natural Earth datasets. When the provided path is to a file containing multiple named layers of data, a layer can be singled out by ``layer``. A virtual filesystem can be specified. The ``vfs`` parameter may be an Apache Commons VFS style string beginning with "zip://" or "tar://"". In this case, the ``path`` must be an absolute path within that container. The drivers enabled for opening datasets may be restricted to those listed in the ``enabled_drivers`` parameter. This and the ``driver`` parameter afford much control over opening of files. # Trying only the GeoJSON driver when opening to read, the # following raises ``DataIOError``: fiona.open('example.shp', driver='GeoJSON') # 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
def open(fp, mode='r', driver=None, schema=None, crs=None, encoding=None, layer=None, vfs=None, enabled_drivers=None, crs_wkt=None, **kwargs): """Open a collection for read, append, or write In write mode, a driver name such as "ESRI Shapefile" or "GPX" (see OGR docs or ``ogr2ogr --help`` on the command line) and a schema mapping such as: {'geometry': 'Point', 'properties': [('class', 'int'), ('label', 'str'), ('value', 'float')]} must be provided. If a particular ordering of properties ("fields" in GIS parlance) in the written file is desired, a list of (key, value) pairs as above or an ordered dict is required. If no ordering is needed, a standard dict will suffice. A coordinate reference system for collections in write mode can be defined by the ``crs`` parameter. It takes Proj4 style mappings like {'proj': 'longlat', 'ellps': 'WGS84', 'datum': 'WGS84', 'no_defs': True} short hand strings like EPSG:4326 or WKT representations of coordinate reference systems. The drivers used by Fiona will try to detect the encoding of data files. If they fail, you may provide the proper ``encoding``, such as 'Windows-1252' for the Natural Earth datasets. When the provided path is to a file containing multiple named layers of data, a layer can be singled out by ``layer``. The drivers enabled for opening datasets may be restricted to those listed in the ``enabled_drivers`` parameter. This and the ``driver`` parameter afford much control over opening of files. # Trying only the GeoJSON driver when opening to read, the # following raises ``DataIOError``: fiona.open('example.shp', driver='GeoJSON') # Trying first the GeoJSON driver, then the Shapefile driver, # the following succeeds: fiona.open( 'example.shp', enabled_drivers=['GeoJSON', 'ESRI Shapefile']) Parameters ---------- fp : URI (str or pathlib.Path), or file-like object A dataset resource identifier or file object. mode : str One of 'r', to read (the default); 'a', to append; or 'w', to write. driver : str In 'w' mode a format driver name is required. In 'r' or 'a' mode this parameter has no effect. schema : dict Required in 'w' mode, has no effect in 'r' or 'a' mode. crs : str or dict Required in 'w' mode, has no effect in 'r' or 'a' mode. encoding : str Name of the encoding used to encode or decode the dataset. layer : int or str The integer index or name of a layer in a multi-layer dataset. vfs : str This is a deprecated parameter. A URI scheme such as "zip://" should be used instead. enabled_drivers : list An optional list of driver names to used when opening a collection. crs_wkt : str An optional WKT representation of a coordinate reference system. kwargs : mapping Other driver-specific parameters that will be interpreted by the OGR library as layer creation or opening options. Returns ------- Collection """ if mode == 'r' and hasattr(fp, 'read'): @contextmanager def fp_reader(fp): memfile = MemoryFile(fp.read()) dataset = memfile.open() try: yield dataset finally: dataset.close() memfile.close() return fp_reader(fp) elif mode == 'w' and hasattr(fp, 'write'): if schema: # Make an ordered dict of schema properties. this_schema = schema.copy() this_schema['properties'] = OrderedDict(schema['properties']) else: this_schema = None @contextmanager def fp_writer(fp): memfile = MemoryFile() dataset = memfile.open(driver=driver, crs=crs, schema=schema, layer=layer, encoding=encoding, enabled_drivers=enabled_drivers, **kwargs) try: yield dataset finally: dataset.close() memfile.seek(0) fp.write(memfile.read()) memfile.close() 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: # Parse the vfs into a vsi and an archive path. 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
def test_layer(self): with pytest.raises(TypeError): Collection("foo", mode='r', layer=0.5)