Exemplo n.º 1
0
class VectorWindowMemoryFile():
    """Context manager around fiona.io.MemoryFile."""

    def __init__(
        self, tile=None, features=None, schema=None, driver=None
    ):
        """Prepare data & profile."""
        self.tile = tile
        self.schema = schema
        self.driver = driver
        self.features = features

    def __enter__(self):
        """Open MemoryFile, write data and return."""
        self.fio_memfile = MemoryFile()
        with self.fio_memfile.open(
            schema=self.schema,
            driver=self.driver,
            crs=self.tile.crs
        ) as dst:
            dst.writerecords(self.features)
        return self.fio_memfile

    def __exit__(self, *args):
        """Make sure MemoryFile is closed."""
        self.fio_memfile.close()
Exemplo n.º 2
0
 def fp_reader(fp):
     memfile = MemoryFile(fp.read())
     dataset = memfile.open()
     try:
         yield dataset
     finally:
         dataset.close()
         memfile.close()
Exemplo n.º 3
0
 def __enter__(self):
     """Open MemoryFile, write data and return."""
     self.fio_memfile = MemoryFile()
     with self.fio_memfile.open(schema=self.schema,
                                driver=self.driver,
                                crs=self.tile.crs) as dst:
         dst.writerecords(self.features)
     return self.fio_memfile
Exemplo n.º 4
0
 def fp_reader(fp):
     memfile = MemoryFile(fp.read())
     dataset = memfile.open()
     try:
         yield dataset
     finally:
         dataset.close()
         memfile.close()
Exemplo n.º 5
0
def test_write_memoryfile(profile_first_coutwildrnp_shp):
    """In-memory GeoJSON can be written"""
    profile, first = profile_first_coutwildrnp_shp
    profile['driver'] = 'GeoJSON'
    with MemoryFile() as memfile:
        with memfile.open(**profile) as col:
            col.write(first)
        memfile.seek(0)
        data = memfile.read()

    with MemoryFile(data) as memfile:
        with memfile.open() as col:
            assert len(col) == 1
Exemplo n.º 6
0
 def fp_reader(fp):
     memfile = MemoryFile(fp.read())
     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.close()
Exemplo n.º 7
0
def test_memoryfile(path_coutwildrnp_json):
    """In-memory GeoJSON file can be read"""
    with open(path_coutwildrnp_json, 'rb') as f:
        data = f.read()
    with MemoryFile(data) as memfile:
        with memfile.open() as collection:
            assert len(collection) == 67
Exemplo n.º 8
0
def test_memoryfile_incr_init(data_coutwildrnp_json):
    """In-memory GeoJSON file written in 2 parts can be read"""
    with MemoryFile() as memfile:
        memfile.write(data_coutwildrnp_json[:1000])
        memfile.write(data_coutwildrnp_json[1000:])
        with memfile.open() as collection:
            assert len(collection) == 67
Exemplo n.º 9
0
def test_memoryfile_write_extension(profile_first_coutwildrnp_shp):
    """In-memory shapefile gets an .shp extension by default"""
    profile, first = profile_first_coutwildrnp_shp
    profile['driver'] = 'ESRI Shapefile'
    with MemoryFile() as memfile:
        with memfile.open(**profile) as col:
            col.write(first)
        assert memfile.name.endswith(".shp")
Exemplo n.º 10
0
 def __enter__(self):
     """Open MemoryFile, write data and return."""
     if self.driver.lower() == "geobuf":
         import geobuf
         return geobuf.encode(
             dict(
                 type="FeatureCollection",
                 features=[dict(f, type="Feature") for f in self.features]
             )
         )
     else:
         self.fio_memfile = MemoryFile()
         with self.fio_memfile.open(
             schema=self.schema,
             driver=self.driver,
             crs=self.tile.crs
         ) as dst:
             dst.writerecords(self.features)
         return self.fio_memfile.getbuffer()
Exemplo n.º 11
0
 def validate(self):
     """
     if schema exists we run shape file validation code of fiona by trying to save to in MemoryFile
     """
     if self._schema is not None:
         with MemoryFile() as memfile:
             with memfile.open(driver="ESRI Shapefile", schema=self.schema) as target:
                 for _item in self._results:
                     # getting rid of the assets that don't behave well becasue of in memroy rasters
                     item = GeoFeature(_item.geometry, _item.properties)
                     target.write(item.to_record(item.crs))
Exemplo n.º 12
0
 def fp_writer(fp):
     memfile = MemoryFile()
     dataset = memfile.open(
         driver=driver, crs=crs, schema=this_schema, layer=layer,
         encoding=encoding, enabled_drivers=enabled_drivers,
         crs_wkt=crs_wkt, **kwargs)
     try:
         yield dataset
     finally:
         dataset.close()
         memfile.seek(0)
         fp.write(memfile.read())
         memfile.close()
Exemplo n.º 13
0
def test_open_closed():
    """Get an exception when opening a dataset on a closed MemoryFile"""
    memfile = MemoryFile()
    memfile.close()
    assert memfile.closed
    with pytest.raises(OSError):
        memfile.open()
Exemplo n.º 14
0
def test_write_bytesio(profile_first_coutwildrnp_shp):
    """GeoJSON can be written to BytesIO"""
    profile, first = profile_first_coutwildrnp_shp
    profile['driver'] = 'GeoJSON'
    with BytesIO() as fout:
        with fiona.open(fout, 'w', **profile) as col:
            col.write(first)
        fout.seek(0)
        data = fout.read()

    with MemoryFile(data) as memfile:
        with memfile.open() as col:
            assert len(col) == 1
Exemplo n.º 15
0
class VectorWindowMemoryFile():
    """Context manager around fiona.io.MemoryFile."""

    def __init__(
        self, tile=None, features=None, schema=None, driver=None
    ):
        """Prepare data & profile."""
        self.tile = tile
        self.schema = schema
        self.driver = driver
        self.features = features

    def __enter__(self):
        """Open MemoryFile, write data and return."""
        if self.driver.lower() == "geobuf":
            import geobuf
            return geobuf.encode(
                dict(
                    type="FeatureCollection",
                    features=[dict(f, type="Feature") for f in self.features]
                )
            )
        else:
            self.fio_memfile = MemoryFile()
            with self.fio_memfile.open(
                schema=self.schema,
                driver=self.driver,
                crs=self.tile.crs
            ) as dst:
                dst.writerecords(self.features)
            return self.fio_memfile.getbuffer()

    def __exit__(self, *args):
        """Make sure MemoryFile is closed."""
        try:
            self.fio_memfile.close()
        except AttributeError:
            pass
Exemplo n.º 16
0
 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()
Exemplo n.º 17
0
def listlayers(fp, vfs=None):
    """List layer names in their index order

    Parameters
    ----------
    fp : URI (str or pathlib.Path), or file-like object
        A dataset resource identifier or file object.
    vfs : str
        This is a deprecated parameter. A URI scheme such as "zip://"
        should be used instead.

    Returns
    -------
    list
        A list of layer name strings.

    """
    if hasattr(fp, 'read'):

        with MemoryFile(fp.read()) as memfile:
            return _listlayers(memfile.name)

    else:

        if isinstance(fp, Path):
            fp = str(fp)

        if not isinstance(fp, string_types):
            raise TypeError("invalid path: %r" % fp)
        if vfs and not isinstance(vfs, string_types):
            raise TypeError("invalid vfs: %r" % vfs)

        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)
            pobj_vfs = parse_path(vfs)
            pobj_path = parse_path(fp)
            pobj = ParsedPath(pobj_path.path, pobj_vfs.path, pobj_vfs.scheme)
        else:
            pobj = parse_path(fp)

        return _listlayers(vsi_path(pobj))
Exemplo n.º 18
0
def test_memoryfile_init(data_coutwildrnp_json):
    """In-memory GeoJSON file can be read"""
    with MemoryFile(data_coutwildrnp_json) as memfile:
        with memfile.open() as collection:
            assert len(collection) == 67
Exemplo n.º 19
0
def test_memoryfile_bare_ext():
    """File extensions without a leading . are handled"""
    assert MemoryFile(ext="geojson").name.endswith(".geojson")
Exemplo n.º 20
0
def test_memoryfile_ext():
    """File extensions are handled"""
    assert MemoryFile(ext=".geojson").name.endswith(".geojson")
Exemplo n.º 21
0
def test_memoryfile_tell(data_coutwildrnp_json):
    """Test MemoryFile.tell() """
    with MemoryFile() as memfile:
        assert memfile.tell() == 0
        memfile.write(data_coutwildrnp_json)
        assert memfile.tell() == len(data_coutwildrnp_json)
Exemplo n.º 22
0
def test_memoryfile_len(data_coutwildrnp_json):
    """Test MemoryFile.__len__ """
    with MemoryFile() as memfile:
        assert len(memfile) == 0
        memfile.write(data_coutwildrnp_json)
        assert len(memfile) == len(data_coutwildrnp_json)
Exemplo n.º 23
0
def test_memoryfile_open_file_or_bytes_read(path_coutwildrnp_json):
    """Test MemoryFile.open when file_or_bytes has a read attribute """
    with open(path_coutwildrnp_json, 'rb') as f:
        with MemoryFile(f) as memfile:
            with memfile.open() as collection:
                assert len(collection) == 67