def test_gdal_config_accessers(): """Low level GDAL config access.""" assert get_gdal_config('foo') is None set_gdal_config('foo', 'bar') assert get_gdal_config('foo') == 'bar' del_gdal_config('foo') assert get_gdal_config('foo') is None
def test_gdal_config_accessors_no_normalize(): """Disables casting keys to upper case and normalizing values to boolean Python values. """ assert get_gdal_config('foo') is None set_gdal_config('foo', 'ON', normalize=False) assert get_gdal_config('foo', normalize=False) == 'ON' del_gdal_config('foo') assert get_gdal_config('foo') is None
def __exit__(self, exc_type=None, exc_val=None, exc_tb=None): log.debug("Exiting env context: %r", self) delenv() if self._has_parent_env: defenv() setenv(**self.context_options) else: log.debug("Exiting outermost env") # See note directly above where _discovered_options is globally # defined. while local._discovered_options: key, val = local._discovered_options.popitem() set_gdal_config(key, val, normalize=False) local._discovered_options = None log.debug("Exited env context: %r", self)
def test_gdal_config_accessors_capitalization(): """GDAL normalizes config names to upper case so Rasterio does not need to do it on its own. This test serves as a canary in case GDAL changes its behavior, which is an important part of reinstating discovered environment variables when ``rasterio.Env()`` starts. GDAL does not alter config values. """ assert get_gdal_config('foo') is None assert get_gdal_config('FOO') is None set_gdal_config('foo', 'bar') assert get_gdal_config('foo') == 'bar' assert get_gdal_config('FOO') == 'bar' del_gdal_config('foo') assert get_gdal_config('foo') is None assert get_gdal_config('FOO') is None set_gdal_config('upper', 'UPPER') assert get_gdal_config('upper') == 'UPPER' del_gdal_config('upper')
def test_env_discovery(key, val): """When passing options to ``rasterio.Env()`` Rasterio first checks to see if they were set in the environment and reinstates on exit. The discovered environment should only be reinstated when the outermost environment exits. It's really important that this test use an environment default. """ assert rasterio.env._discovered_options is None, \ "Something has gone horribly wrong." try: # This should persist when all other environment managers exit. set_gdal_config(key, val) # Start an environment and overwrite the value that should persist with rasterio.Env(**{key: True}): assert get_gdal_config(key) is True assert rasterio.env._discovered_options == {key: val} # Start another nested environment, again overwriting the value # that should persist with rasterio.Env(**{key: False}): assert rasterio.env._discovered_options == {key: val} assert get_gdal_config(key) is False # Ensure the outer state is restored. assert rasterio.env._discovered_options == {key: val} assert get_gdal_config(key) is True # Ensure the discovered value remains unchanged. assert rasterio.env._discovered_options is None assert get_gdal_config(key, normalize=False) == val # Leaving this option in the GDAL environment could cause a problem # for other tests. finally: del_gdal_config(key)
def test_env_discovery(key, val): """When passing options to ``rasterio.Env()`` Rasterio first checks to see if they were set in the environment and reinstates on exit. The discovered environment should only be reinstated when the outermost environment exits. It's really important that this test use an environment default. """ assert rasterio.env.local._discovered_options is None, \ "Something has gone horribly wrong." try: # This should persist when all other environment managers exit. set_gdal_config(key, val) # Start an environment and overwrite the value that should persist with rasterio.Env(**{key: True}): assert get_gdal_config(key) is True assert rasterio.env.local._discovered_options == {key: val} # Start another nested environment, again overwriting the value # that should persist with rasterio.Env(**{key: False}): assert rasterio.env.local._discovered_options == {key: val} assert get_gdal_config(key) is False # Ensure the outer state is restored. assert rasterio.env.local._discovered_options == {key: val} assert get_gdal_config(key) is True # Ensure the discovered value remains unchanged. assert rasterio.env.local._discovered_options is None assert get_gdal_config(key, normalize=False) == val # Leaving this option in the GDAL environment could cause a problem # for other tests. finally: del_gdal_config(key)
def test_gdal_cachemax(): """``GDAL_CACHEMAX`` is a special case.""" original_cachemax = get_gdal_config('GDAL_CACHEMAX') assert original_cachemax != 4321 # GDALSetCacheMax() has a limit of somewhere between 2 and 3 GB. # We use GDALSetCacheMax64(), so use a value that is outside the 32 bit # range to verify it is not being truncated. set_gdal_config('GDAL_CACHEMAX', 4321) assert get_gdal_config('GDAL_CACHEMAX', 4321) == 4321 # On first read the number will be in bytes. Drop to MB if necessary. try: set_gdal_config('GDAL_CACHEMAX', original_cachemax) except OverflowError: set_gdal_config('GDAL_CACHEMAX', int(original_cachemax / 1000000))
return f(*args, **kwds) return wrapper return decorator # Patch the environment if needed, such as in the installed wheel case. if 'GDAL_DATA' not in os.environ: path = GDALDataFinder().search_wheel() if path: set_gdal_config("GDAL_DATA", path) log.debug("GDAL data found in package, GDAL_DATA set to %r.", path) # See https://github.com/mapbox/rasterio/issues/1631. elif GDALDataFinder().find_file("header.dxf"): log.debug("GDAL data files are available at built-in paths") else: path = GDALDataFinder().search() if path: set_gdal_config("GDAL_DATA", path) log.debug("GDAL_DATA not found in environment, set to %r.", path) if "PROJ_LIB" in os.environ: path = os.environ["PROJ_LIB"]