diff --git a/ohsome_quality_api/raster/__init__.py b/ohsome_quality_api/raster/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/ohsome_quality_api/raster/client.py b/ohsome_quality_api/raster/client.py deleted file mode 100644 index 7ddf37dce..000000000 --- a/ohsome_quality_api/raster/client.py +++ /dev/null @@ -1,57 +0,0 @@ -"""A client to raster datasets existing as files on disk.""" - -import os -from copy import deepcopy - -import geojson -from pyproj import Transformer -from rasterstats import zonal_stats - -from ohsome_quality_api.config import get_config_value -from ohsome_quality_api.raster.definitions import RasterDataset -from ohsome_quality_api.utils.exceptions import RasterDatasetNotFoundError - - -def get_zonal_stats( - feature: geojson.Feature, - raster: RasterDataset, - *args, - **kwargs, -): - """Wrapper around the function `zonal_stats` of the package `rasterstats`. - - All arguments are passed directly to `zonal_stats`. - - The only difference is that the arguments `vectors` and `raster` of `zonal_stats` - are expected to be a `geojson.Feature` object and a member of the `RasterDataset` - class respectively. - """ - return zonal_stats( - transform(feature, raster), - get_raster_path(raster), - *args, - nodata=raster.nodata, - **kwargs, - ) - - -def transform(feature: geojson.Feature, raster: RasterDataset): - """Convert Feature to RasterDataset CRS. - - GeoJSON Feature CRS/SRID is expected to be EPSG:4326. - """ - if raster.crs == "EPSG:4326": - return feature - transformer = Transformer.from_crs("EPSG:4326", raster.crs, always_xy=True) - return geojson.utils.map_tuples( - lambda coordinates: transformer.transform(coordinates[0], coordinates[1]), - deepcopy(feature), - ) - - -def get_raster_path(raster: RasterDataset) -> str: - """Get the path of the raster file on disk.""" - path = os.path.join(get_config_value("data_dir"), raster.filename) - if not os.path.exists(path): - raise RasterDatasetNotFoundError(raster) - return path diff --git a/ohsome_quality_api/raster/definitions.py b/ohsome_quality_api/raster/definitions.py deleted file mode 100644 index fcb934abd..000000000 --- a/ohsome_quality_api/raster/definitions.py +++ /dev/null @@ -1,69 +0,0 @@ -from dataclasses import dataclass - -from ohsome_quality_api.utils.exceptions import RasterDatasetUndefinedError - - -@dataclass(frozen=True) -class RasterDataset: - """Raster datasets available on disk. - - Args: - name: Name of raster - filename: Filename of raster on disk - crs: An authority string (i.e. `EPSG:4326` or `ESRI:54009`) - """ - - name: str - filename: str - crs: str - nodata: int | None - - -RASTER_DATASETS = ( - RasterDataset( - "GHS_BUILT_R2018A", - "GHS_BUILT_LDS2014_GLOBE_R2018A_54009_1K_V2_0.tif", - "ESRI:54009", - -200, - ), - RasterDataset( - "GHS_POP_R2019A", - "GHS_POP_E2015_GLOBE_R2019A_54009_1K_V1_0.tif", - "ESRI:54009", - -200, - ), - RasterDataset( - "GHS_SMOD_R2019A", - "GHS_SMOD_POP2015_GLOBE_R2019A_54009_1K_V2_0.tif", - "ESRI:54009", - -200, - ), - RasterDataset( - "VNL", - "VNL_v2_npp_2020_global_vcmslcfg_c202102150000.average_masked.tif", - "EPSG:4326", - -999, - ), -) - - -def get_raster_dataset_names() -> list[str]: - return [r.name for r in RASTER_DATASETS] - - -def get_raster_dataset(name: str) -> RasterDataset: - """Get a instance of the `RasterDataset` class by the raster name. - - Args: - name: Name of the raster as defined by `RASTER_DATASETS`. - - Returns - An instance of the `RasterDataset` class with matching name. - - Raises: - RasterDatasetUndefinedError: If no matching `RasterDataset` class is found. - """ - try: - return next(filter(lambda r: r.name == name, RASTER_DATASETS)) - except StopIteration as e: - raise RasterDatasetUndefinedError(name) from e diff --git a/tests/fixtures/raster/GHS_BUILT_LDS2014_GLOBE_R2018A_54009_1K_V2_0.tif b/tests/fixtures/raster/GHS_BUILT_LDS2014_GLOBE_R2018A_54009_1K_V2_0.tif deleted file mode 100644 index b758e750d..000000000 Binary files a/tests/fixtures/raster/GHS_BUILT_LDS2014_GLOBE_R2018A_54009_1K_V2_0.tif and /dev/null differ diff --git a/tests/fixtures/raster/GHS_POP_E2015_GLOBE_R2019A_54009_1K_V1_0.tif b/tests/fixtures/raster/GHS_POP_E2015_GLOBE_R2019A_54009_1K_V1_0.tif deleted file mode 100644 index a94a4be7b..000000000 Binary files a/tests/fixtures/raster/GHS_POP_E2015_GLOBE_R2019A_54009_1K_V1_0.tif and /dev/null differ diff --git a/tests/fixtures/raster/GHS_SMOD_POP2015_GLOBE_R2019A_54009_1K_V2_0.tif b/tests/fixtures/raster/GHS_SMOD_POP2015_GLOBE_R2019A_54009_1K_V2_0.tif deleted file mode 100644 index a50f94776..000000000 Binary files a/tests/fixtures/raster/GHS_SMOD_POP2015_GLOBE_R2019A_54009_1K_V2_0.tif and /dev/null differ diff --git a/tests/fixtures/raster/VNL_v2_npp_2020_global_vcmslcfg_c202102150000.average_masked.tif b/tests/fixtures/raster/VNL_v2_npp_2020_global_vcmslcfg_c202102150000.average_masked.tif deleted file mode 100644 index 1bdd7a2fa..000000000 Binary files a/tests/fixtures/raster/VNL_v2_npp_2020_global_vcmslcfg_c202102150000.average_masked.tif and /dev/null differ diff --git a/tests/integrationtests/test_raster.py b/tests/integrationtests/test_raster.py deleted file mode 100644 index 320cb125c..000000000 --- a/tests/integrationtests/test_raster.py +++ /dev/null @@ -1,29 +0,0 @@ -import ohsome_quality_api.raster.client as raster_client -from ohsome_quality_api.raster.definitions import get_raster_dataset - -from .utils import get_geojson_fixture - - -def test_get_raster_path(mock_env_oqapi_data_dir): - raster = get_raster_dataset("GHS_BUILT_R2018A") - path = raster_client.get_raster_path(raster) - assert isinstance(path, str) - - -def test_get_zonal_stats(mock_env_oqapi_data_dir): - raster = get_raster_dataset("GHS_BUILT_R2018A") - feature = get_geojson_fixture("algeria-touggourt-feature.geojson") - - expected = [ - { - "min": 0.0, - "max": 46.7505989074707, - "mean": 1.6491034092047276, - "count": 234, - } - ] - result = raster_client.get_zonal_stats( - feature, - raster, - ) - assert expected == result diff --git a/tests/unittests/test_raster.py b/tests/unittests/test_raster.py deleted file mode 100644 index d3a80dc3d..000000000 --- a/tests/unittests/test_raster.py +++ /dev/null @@ -1,112 +0,0 @@ -import os -import tempfile -import unittest -from unittest import mock - -import geojson - -import ohsome_quality_api.raster.client as raster_client -from ohsome_quality_api.raster.definitions import get_raster_dataset -from ohsome_quality_api.utils.exceptions import RasterDatasetNotFoundError - - -class TestRaster(unittest.TestCase): - def setUp(self): - self.raster_dataset = get_raster_dataset("GHS_BUILT_R2018A") - path = os.path.join( - os.path.dirname(os.path.abspath(__file__)), - "fixtures/heidelberg-altstadt-feature.geojson", - ) - with open(path, "r") as f: - self.feature = geojson.load(f) - - def test_get_raster_path(self): - with tempfile.TemporaryDirectory() as tmpdirname: - path = os.path.join( - tmpdirname, - "GHS_BUILT_LDS2014_GLOBE_R2018A_54009_1K_V2_0.tif", - ) - with open(path, "w") as f: - f.write("") - with mock.patch( - "ohsome_quality_api.raster.client.get_config_value", - return_value=tmpdirname, - ): - self.assertEqual( - raster_client.get_raster_path(self.raster_dataset), - path, - ) - - @mock.patch("ohsome_quality_api.raster.client.os") - def test_get_raster_path_error(self, mock_os): - mock_os.path.exists.return_value = False - self.assertRaises( - RasterDatasetNotFoundError, - raster_client.get_raster_path, - self.raster_dataset, - ) - - @mock.patch( - "ohsome_quality_api.raster.client.get_raster_path", - return_value=os.path.join( - os.path.dirname(os.path.abspath(__file__)), - "fixtures", - "GHS_BUILT_R2018A-Heidelberg.tif", - ), - ) - def test_get_zonal_stats(self, *args): - expected = [ - { - "count": 2, - "max": 73.3844985961914, - "mean": 70.12319946289062, - "min": 66.86190032958984, - } - ] - result = raster_client.get_zonal_stats( - self.feature, - self.raster_dataset, - ) - self.assertEqual(expected, result) - - @mock.patch( - "ohsome_quality_api.raster.client.get_raster_path", - return_value=os.path.join( - os.path.dirname(os.path.abspath(__file__)), - "fixtures", - "nodata.tif", - ), - ) - def test_get_zonal_stats_nodata(self, *args): - """Test on a raster file with only pixel values of -222 (nodata)""" - expected = [{"count": 0, "sum": None}] - result = raster_client.get_zonal_stats( - self.feature, self.raster_dataset, stats=["count", "sum"] - ) - self.assertEqual(expected, result) - - def test_transform_different_crs(self): - expected = { - "type": "Feature", - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - (664903.2658458926, 5810944.6089190915), - (666571.1021172021, 5810944.6089190915), - (666474.5092985737, 5812131.278240253), - (664806.9147134189, 5812131.278240253), - (664903.2658458926, 5810944.6089190915), - ] - ], - }, - "properties": {}, - } - result = raster_client.transform(self.feature, self.raster_dataset) - self.assertDictEqual(expected, result) - self.assertNotEqual(self.feature, result) - - def test_transform_same_crs(self): - raster_dataset = get_raster_dataset("VNL") - result = raster_client.transform(self.feature, raster_dataset) - self.assertDictEqual(self.feature, result) diff --git a/tests/unittests/test_raster_definitions.py b/tests/unittests/test_raster_definitions.py deleted file mode 100644 index 673a7635b..000000000 --- a/tests/unittests/test_raster_definitions.py +++ /dev/null @@ -1,19 +0,0 @@ -import unittest - -from ohsome_quality_api.raster import definitions -from ohsome_quality_api.utils.exceptions import RasterDatasetUndefinedError - - -class TestDefinitions(unittest.TestCase): - def test_get_raster_dataset_names(self): - names = definitions.get_raster_dataset_names() - self.assertIsInstance(names, list) - self.assertTrue(names) - - def test_get_raster_dataset(self): - raster = definitions.get_raster_dataset("GHS_BUILT_R2018A") - self.assertIsInstance(raster, definitions.RasterDataset) - - def test_get_raster_dataset_undefined(self): - with self.assertRaises(RasterDatasetUndefinedError): - definitions.get_raster_dataset("foo")