Ejemplo n.º 1
0
    def open(self) -> Iterator[GeoRasterReader]:
        """Context manager which returns a :class:`BandDataSource`"""

        activate_from_config()  # check if settings changed and apply new

        lock = self._lock
        locked = False if lock is None else lock.acquire(blocking=True)

        try:
            _LOG.debug("opening %s", self.filename)
            with rasterio.DatasetReader(rasterio.path.parse_path(
                    str(self.filename)),
                                        sharing=False) as src:
                override = False

                transform = src.transform
                if transform.is_identity:
                    override = True
                    transform = self.get_transform(src.shape)

                try:
                    crs = _rasterio_crs(src)
                except ValueError:
                    override = True
                    crs = self.get_crs()

                bandnumber = self.get_bandnumber(src)
                band = rasterio.band(src, bandnumber)
                nodata = src.nodatavals[band.bidx - 1] if src.nodatavals[
                    band.bidx - 1] is not None else self.nodata
                nodata = num2numpy(nodata, band.dtype)

                if locked:
                    locked = False
                    lock.release()

                if override:
                    warnings.warn(
                        f"""Broken/missing geospatial data was found in file:
"{self.filename}"
Will use approximate metadata for backwards compatibility reasons (#673).
This behaviour is deprecated. Future versions will raise an error.""",
                        category=DeprecationWarning)
                    yield OverrideBandDataSource(band,
                                                 nodata=nodata,
                                                 crs=crs,
                                                 transform=transform,
                                                 lock=lock)
                else:
                    yield BandDataSource(band, nodata=nodata, lock=lock)

        except Exception as e:
            _LOG.error("Error opening source dataset: %s", self.filename)
            raise e
        finally:
            if locked:
                lock.release()
Ejemplo n.º 2
0
    def open(self) -> Iterator[GeoRasterReader]:
        """Context manager which returns a :class:`BandDataSource`"""

        activate_from_config()  # check if settings changed and apply new

        lock = self._lock
        locked = False if lock is None else lock.acquire(blocking=True)

        try:
            _LOG.debug("opening %s", self.filename)
            with rasterio.open(self.filename, sharing=False) as src:
                override = False

                transform = src.transform
                if transform.is_identity:
                    override = True
                    transform = self.get_transform(src.shape)

                try:
                    crs = geometry.CRS(_rasterio_crs_wkt(src))
                except ValueError:
                    override = True
                    crs = self.get_crs()

                # The [1.0a1-1.0a8] releases of rasterio had a bug that means it
                # cannot read multiband data into a numpy array during reprojection
                # We override it here to force the reading and reprojection into separate steps
                # TODO: Remove when we no longer care about those versions of rasterio
                bandnumber = self.get_bandnumber(src)
                band = rasterio.band(src, bandnumber)
                nodata = src.nodatavals[band.bidx - 1] if src.nodatavals[
                    band.bidx - 1] is not None else self.nodata
                nodata = num2numpy(nodata, band.dtype)

                if locked:
                    locked = False
                    lock.release()

                if override:
                    yield OverrideBandDataSource(band,
                                                 nodata=nodata,
                                                 crs=crs,
                                                 transform=transform,
                                                 lock=lock)
                else:
                    yield BandDataSource(band, nodata=nodata, lock=lock)

        except Exception as e:
            _LOG.error("Error opening source dataset: %s", self.filename)
            raise e
        finally:
            if locked:
                lock.release()
Ejemplo n.º 3
0
def test_rio_env_via_config():
    ee = activate_from_config()
    assert ee is not None

    # Second call should not change anything
    assert activate_from_config() is None

    set_default_rio_config(aws=None, cloud_defaults=True)

    # config change should activate new env
    ee = activate_from_config()
    assert ee is not None
    assert 'GDAL_DISABLE_READDIR_ON_OPEN' in ee

    deactivate_rio_env()
    assert get_rio_env() == {}
Ejemplo n.º 4
0
def test_rio_configure_aws_access(monkeypatch, without_aws_env, dask_client):
    monkeypatch.setenv("AWS_ACCESS_KEY_ID", "fake-key-id")
    monkeypatch.setenv("AWS_SECRET_ACCESS_KEY", "fake-secret")
    monkeypatch.setenv("AWS_DEFAULT_REGION", "fake-region")

    creds = configure_s3_access()
    cc = creds.get_frozen_credentials()
    assert cc.access_key == 'fake-key-id'
    assert cc.secret_key == 'fake-secret'
    assert cc.token is None

    ee = activate_from_config()
    assert ee is not None
    assert 'AWS_ACCESS_KEY_ID' in ee
    assert 'AWS_SECRET_ACCESS_KEY' in ee
    assert 'AWS_REGION' in ee
    assert 'AWS_SESSION_TOKEN' not in ee

    ee = get_rio_env(sanitize=False)
    assert ee is not None
    assert ee['AWS_ACCESS_KEY_ID'] == 'fake-key-id'
    assert ee['AWS_SECRET_ACCESS_KEY'] == 'fake-secret'
    assert ee['AWS_REGION'] == 'fake-region'
    assert ee['GDAL_DISABLE_READDIR_ON_OPEN'] == 'EMPTY_DIR'

    ee_local = ee
    client = dask_client

    creds = configure_s3_access(client=client)
    cc = creds.get_frozen_credentials()
    assert cc.access_key == 'fake-key-id'
    assert cc.secret_key == 'fake-secret'
    assert cc.token is None

    ee = client.submit(activate_from_config).result()
    assert ee is not None
    assert 'AWS_ACCESS_KEY_ID' in ee
    assert 'AWS_SECRET_ACCESS_KEY' in ee
    assert 'AWS_REGION' in ee
    assert 'AWS_SESSION_TOKEN' not in ee

    def _activate_and_get(sanitize=True):
        activate_from_config()
        return get_rio_env(sanitize=sanitize)

    ee = client.submit(_activate_and_get, sanitize=False).result()
    assert ee == ee_local
Ejemplo n.º 5
0
 def _activate_and_get(sanitize=True):
     activate_from_config()
     return get_rio_env(sanitize=sanitize)