Skip to content

Commit

Permalink
Cleaned up and expanded resolution testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kcartier-wri committed Sep 4, 2024
1 parent f2f5b94 commit 8e88425
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 69 deletions.
5 changes: 3 additions & 2 deletions city_metrix/layers/high_land_surface_temperature.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@
class HighLandSurfaceTemperature(Layer):
THRESHOLD_ADD = 3

def __init__(self, start_date="2013-01-01", end_date="2023-01-01", **kwargs):
def __init__(self, start_date="2013-01-01", end_date="2023-01-01", spatial_resolution=30, **kwargs):
super().__init__(**kwargs)
self.start_date = start_date
self.end_date = end_date
self.spatial_resolution = spatial_resolution

def get_data(self, bbox):
hottest_date = self.get_hottest_date(bbox)
start_date = (hottest_date - datetime.timedelta(days=45)).strftime("%Y-%m-%d")
end_date = (hottest_date + datetime.timedelta(days=45)).strftime("%Y-%m-%d")

lst = LandSurfaceTemperature(start_date, end_date).get_data(bbox)
lst = LandSurfaceTemperature(start_date, end_date, self.spatial_resolution).get_data(bbox)
lst_mean = lst.mean(dim=['x', 'y'])
high_lst = lst.where(lst >= (lst_mean + self.THRESHOLD_ADD))
return high_lst
Expand Down
4 changes: 3 additions & 1 deletion city_metrix/layers/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,12 @@ def get_image_collection(
:param name: optional name to print while reporting progress
:return:
"""
if scale is None:
raise Exception("Spatial_resolution cannot be None.")

crs = get_utm_zone_epsg(bbox)

# See link regarding bug in crs specification shttps://github.com/google/Xee/issues/118
# See link regarding bug in crs specification https://github.com/google/Xee/issues/118
ds = xr.open_dataset(
image_collection,
engine='ee',
Expand Down
12 changes: 10 additions & 2 deletions tests/resources/layer_dumps_for_br_lauro_de_freitas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
# Values should normally be set to False in order to avoid unnecessary execution.
RUN_DUMPS = False

# Specify None to write to a temporary default folder otherwise specify a valid custom target path.
CUSTOM_DUMP_DIRECTORY = None
# Multiplier applied to the default spatial_resolution of the layer
# Use value of 1 for default resolution.
SPATIAL_RESOLUTION_MULTIPLIER = 1

# Both the tests and QGIS file are implemented for the same bounding box in Brazil.
COUNTRY_CODE_FOR_BBOX = 'BRA'
BBOX = BBOX_BRA_LAURO_DE_FREITAS_1

# Specify None to write to a temporary default folder otherwise specify a valid custom target path.
CUSTOM_DUMP_DIRECTORY = None

def pytest_configure(config):
qgis_project_file = 'layers_for_br_lauro_de_freitas.qgz'

Expand All @@ -36,6 +40,10 @@ def pytest_configure(config):
def target_folder():
return get_target_folder_path()

@pytest.fixture
def target_spatial_resolution_multiplier():
return SPATIAL_RESOLUTION_MULTIPLIER

@pytest.fixture
def bbox_info():
bbox = namedtuple('bbox', ['bounds', 'country'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,122 +24,138 @@
WorldPop, Layer
)
from .conftest import RUN_DUMPS, prep_output_path, verify_file_is_populated
from ...tools.general_tools import get_class_default_spatial_resolution


@pytest.mark.skipif(RUN_DUMPS == False, reason='Skipping since RUN_DUMPS set to False')
def test_write_albedo(target_folder, bbox_info):
def test_write_albedo(target_folder, bbox_info, target_spatial_resolution_multiplier):
file_path = prep_output_path(target_folder, 'albedo.tif')
Albedo().write(bbox_info.bounds, file_path, tile_degrees=None)
target_resolution = target_spatial_resolution_multiplier * get_class_default_spatial_resolution(Albedo())
Albedo(spatial_resolution=target_resolution).write(bbox_info.bounds, file_path, tile_degrees=None)
assert verify_file_is_populated(file_path)

@pytest.mark.skipif(RUN_DUMPS == False, reason='Skipping since RUN_DUMPS set to False')
def test_write_alos_dsm(target_folder, bbox_info):
def test_write_alos_dsm(target_folder, bbox_info, target_spatial_resolution_multiplier):
file_path = prep_output_path(target_folder, 'alos_dsm.tif')
AlosDSM().write(bbox_info.bounds, file_path, tile_degrees=None)
target_resolution = target_spatial_resolution_multiplier * get_class_default_spatial_resolution(AlosDSM())
AlosDSM(spatial_resolution=target_resolution).write(bbox_info.bounds, file_path, tile_degrees=None)
assert verify_file_is_populated(file_path)

@pytest.mark.skipif(RUN_DUMPS == False, reason='Skipping since RUN_DUMPS set to False')
def test_write_average_net_building_height(target_folder, bbox_info):
def test_write_average_net_building_height(target_folder, bbox_info, target_spatial_resolution_multiplier):
file_path = prep_output_path(target_folder, 'average_net_building_height.tif')
AverageNetBuildingHeight().write(bbox_info.bounds, file_path, tile_degrees=None)
target_resolution = target_spatial_resolution_multiplier * get_class_default_spatial_resolution(AverageNetBuildingHeight())
AverageNetBuildingHeight(spatial_resolution=target_resolution).write(bbox_info.bounds, file_path, tile_degrees=None)
assert verify_file_is_populated(file_path)

@pytest.mark.skipif(RUN_DUMPS == False, reason='Skipping since RUN_DUMPS set to False')
def test_write_esa_world_cover(target_folder, bbox_info):
def test_write_esa_world_cover(target_folder, bbox_info, target_spatial_resolution_multiplier):
file_path = prep_output_path(target_folder, 'esa_world_cover.tif')
EsaWorldCover().write(bbox_info.bounds, file_path, tile_degrees=None)
target_resolution = target_spatial_resolution_multiplier * get_class_default_spatial_resolution(EsaWorldCover())
EsaWorldCover(spatial_resolution=target_resolution).write(bbox_info.bounds, file_path, tile_degrees=None)
assert verify_file_is_populated(file_path)

@pytest.mark.skipif(RUN_DUMPS == False, reason='Skipping since RUN_DUMPS set to False')
def test_write_high_land_surface_temperature(target_folder, bbox_info):
def test_write_high_land_surface_temperature(target_folder, bbox_info, target_spatial_resolution_multiplier):
file_path = prep_output_path(target_folder, 'high_land_surface_temperature.tif')
HighLandSurfaceTemperature().write(bbox_info.bounds, file_path, tile_degrees=None)
target_resolution = target_spatial_resolution_multiplier * get_class_default_spatial_resolution(HighLandSurfaceTemperature())
HighLandSurfaceTemperature(spatial_resolution=target_resolution).write(bbox_info.bounds, file_path, tile_degrees=None)
assert verify_file_is_populated(file_path)

@pytest.mark.skipif(RUN_DUMPS == False, reason='Skipping since RUN_DUMPS set to False')
def test_write_land_surface_temperature(target_folder, bbox_info):
def test_write_land_surface_temperature(target_folder, bbox_info, target_spatial_resolution_multiplier):
file_path = prep_output_path(target_folder, 'land_surface_temperature.tif')
LandSurfaceTemperature().write(bbox_info.bounds, file_path, tile_degrees=None)
target_resolution = target_spatial_resolution_multiplier * get_class_default_spatial_resolution(LandSurfaceTemperature())
LandSurfaceTemperature(spatial_resolution=target_resolution).write(bbox_info.bounds, file_path, tile_degrees=None)
assert verify_file_is_populated(file_path)

# TODO Class is no longer used, but may be useful later
# @pytest.mark.skipif(RUN_DUMPS == False, reason='Skipping since RUN_DUMPS set to False')
# def test_write_landsat_collection_2(target_folder, bbox_info):
# def test_write_landsat_collection_2(target_folder, bbox_info, target_spatial_resolution_multiplier):
# file_path = prep_output_path(target_folder, 'landsat_collection2.tif')
# bands = ['green']
# LandsatCollection2(bands).write(bbox_info.bounds, file_path, tile_degrees=None)
# assert verify_file_is_populated(file_path)

@pytest.mark.skipif(RUN_DUMPS == False, reason='Skipping since RUN_DUMPS set to False')
def test_write_nasa_dem(target_folder, bbox_info):
def test_write_nasa_dem(target_folder, bbox_info, target_spatial_resolution_multiplier):
file_path = prep_output_path(target_folder, 'nasa_dem.tif')
NasaDEM().write(bbox_info.bounds, file_path, tile_degrees=None)
target_resolution = target_spatial_resolution_multiplier * get_class_default_spatial_resolution(NasaDEM())
NasaDEM(spatial_resolution=target_resolution).write(bbox_info.bounds, file_path, tile_degrees=None)
assert verify_file_is_populated(file_path)

@pytest.mark.skipif(RUN_DUMPS == False, reason='Skipping since RUN_DUMPS set to False')
def test_write_natural_areas(target_folder, bbox_info):
def test_write_natural_areas(target_folder, bbox_info, target_spatial_resolution_multiplier):
file_path = prep_output_path(target_folder, 'natural_areas.tif')
NaturalAreas().write(bbox_info.bounds, file_path, tile_degrees=None)
target_resolution = target_spatial_resolution_multiplier * get_class_default_spatial_resolution(NaturalAreas())
NaturalAreas(spatial_resolution=target_resolution).write(bbox_info.bounds, file_path, tile_degrees=None)
assert verify_file_is_populated(file_path)

@pytest.mark.skipif(RUN_DUMPS == False, reason='Skipping since RUN_DUMPS set to False')
def test_write_ndvi_sentinel2_gee(target_folder, bbox_info):
def test_write_ndvi_sentinel2_gee(target_folder, bbox_info, target_spatial_resolution_multiplier):
file_path = prep_output_path(target_folder, 'ndvi_sentinel2_gee.tif')
NdviSentinel2(year=2023).write(bbox_info.bounds, file_path, tile_degrees=None)
target_resolution = target_spatial_resolution_multiplier * get_class_default_spatial_resolution(NdviSentinel2())
NdviSentinel2(year=2023, spatial_resolution=target_resolution).write(bbox_info.bounds, file_path, tile_degrees=None)
assert verify_file_is_populated(file_path)

@pytest.mark.skipif(RUN_DUMPS == False, reason='Skipping since RUN_DUMPS set to False')
def test_write_openbuildings(target_folder, bbox_info):
def test_write_openbuildings(target_folder, bbox_info, target_spatial_resolution_multiplier):
file_path = prep_output_path(target_folder, 'open_buildings.tif')
OpenBuildings(bbox_info.country).write(bbox_info.bounds, file_path, tile_degrees=None)
assert verify_file_is_populated(file_path)

# TODO Class write is not functional. Is class still needed or have we switched to overture?
# @pytest.mark.skipif(RUN_DUMPS == False, reason='Skipping since RUN_DUMPS set to False')
# def test_write_open_street_map(target_folder, bbox_info):
# def test_write_open_street_map(target_folder, bbox_info, target_spatial_resolution_multiplier):
# file_path = prep_output_path(target_folder, 'open_street_map.tif')
# OpenStreetMap().write(bbox_info.bounds, file_path, tile_degrees=None)
# assert verify_file_is_populated(file_path)

@pytest.mark.skipif(RUN_DUMPS == False, reason='Skipping since RUN_DUMPS set to False')
def test_write_overture_buildings(target_folder, bbox_info):
def test_write_overture_buildings(target_folder, bbox_info, target_spatial_resolution_multiplier):
file_path = prep_output_path(target_folder, 'overture_buildings.tif')
OvertureBuildings().write(bbox_info.bounds, file_path, tile_degrees=None)
assert verify_file_is_populated(file_path)

# TODO Class is no longer used, but may be useful later
# @pytest.mark.skipif(RUN_DUMPS == False, reason='Skipping since RUN_DUMPS set to False')
# def test_write_sentinel_2_level2(target_folder, bbox_info):
# def test_write_sentinel_2_level2(target_folder, bbox_info, target_spatial_resolution_multiplier):
# file_path = prep_output_path(target_folder, 'sentinel_2_level2.tif')
# sentinel_2_bands = ["green"]
# Sentinel2Level2(sentinel_2_bands).write(bbox_info.bounds, file_path, tile_degrees=None)
# assert verify_file_is_populated(file_path)

@pytest.mark.skipif(RUN_DUMPS == False, reason='Skipping since RUN_DUMPS set to False')
def test_write_smart_surface_lulc(target_folder, bbox_info):
def test_write_smart_surface_lulc(target_folder, bbox_info, target_spatial_resolution_multiplier):
# Note: spatial_resolution not implemented for this raster class
file_path = prep_output_path(target_folder, 'smart_surface_lulc.tif')
SmartSurfaceLULC().write(bbox_info.bounds, file_path, tile_degrees=None)
assert verify_file_is_populated(file_path)

@pytest.mark.skipif(RUN_DUMPS == False, reason='Skipping since RUN_DUMPS set to False')
def test_write_tree_canopy_height(target_folder, bbox_info):
def test_write_tree_canopy_height(target_folder, bbox_info, target_spatial_resolution_multiplier):
file_path = prep_output_path(target_folder, 'tree_canopy_height.tif')
TreeCanopyHeight().write(bbox_info.bounds, file_path, tile_degrees=None)
target_resolution = target_spatial_resolution_multiplier * get_class_default_spatial_resolution(TreeCanopyHeight())
TreeCanopyHeight(spatial_resolution=target_resolution).write(bbox_info.bounds, file_path, tile_degrees=None)
assert verify_file_is_populated(file_path)

@pytest.mark.skipif(RUN_DUMPS == False, reason='Skipping since RUN_DUMPS set to False')
def test_write_tree_cover(target_folder, bbox_info):
def test_write_tree_cover(target_folder, bbox_info, target_spatial_resolution_multiplier):
file_path = prep_output_path(target_folder, 'tree_cover.tif')
TreeCover().write(bbox_info.bounds, file_path, tile_degrees=None)
target_resolution = target_spatial_resolution_multiplier * get_class_default_spatial_resolution(TreeCover())
TreeCover(spatial_resolution=target_resolution).write(bbox_info.bounds, file_path, tile_degrees=None)
assert verify_file_is_populated(file_path)

@pytest.mark.skipif(RUN_DUMPS == False, reason='Skipping since RUN_DUMPS set to False')
def test_write_urban_land_use(target_folder, bbox_info):
def test_write_urban_land_use(target_folder, bbox_info, target_spatial_resolution_multiplier):
file_path = prep_output_path(target_folder, 'urban_land_use.tif')
UrbanLandUse().write(bbox_info.bounds, file_path, tile_degrees=None)
target_resolution = target_spatial_resolution_multiplier * get_class_default_spatial_resolution(UrbanLandUse())
UrbanLandUse(spatial_resolution=target_resolution).write(bbox_info.bounds, file_path, tile_degrees=None)
assert verify_file_is_populated(file_path)

@pytest.mark.skipif(RUN_DUMPS == False, reason='Skipping since RUN_DUMPS set to False')
def test_write_world_pop(target_folder, bbox_info):
def test_write_world_pop(target_folder, bbox_info, target_spatial_resolution_multiplier):
file_path = prep_output_path(target_folder, 'world_pop.tif')
WorldPop().write(bbox_info.bounds, file_path, tile_degrees=None)
target_resolution = target_spatial_resolution_multiplier * get_class_default_spatial_resolution(WorldPop())
WorldPop(spatial_resolution=target_resolution).write(bbox_info.bounds, file_path, tile_degrees=None)
assert verify_file_is_populated(file_path)
Loading

0 comments on commit 8e88425

Please sign in to comment.