diff --git a/city_metrix/layers/layer.py b/city_metrix/layers/layer.py index c86e9fe..18aa11b 100644 --- a/city_metrix/layers/layer.py +++ b/city_metrix/layers/layer.py @@ -9,7 +9,7 @@ from dask.diagnostics import ProgressBar from ee import ImageCollection from geocube.api.core import make_geocube -from shapely.geometry import box +from shapely.geometry import box, polygon from xrspatial import zonal_stats import geopandas as gpd import xarray as xr @@ -72,13 +72,26 @@ def write(self, bbox, output_path, tile_degrees=None, **kwargs): os.makedirs(output_path) file_names = [] + tile_serial_id = 1 + tile_dict = {"tile_name": [], "geometry": []} for tile in tiles["geometry"]: data = self.aggregate.get_data(tile.bounds) - file_name = f"{output_path}/{uuid4()}.tif" - file_names.append(file_name) + tile_suffix = str(tile_serial_id).zfill(3) + file_name = f'tile_{tile_suffix}.tif' + file_path = os.path.join(output_path, file_name) + file_names.append(file_path) + write_layer(file_path, data) - write_layer(file_name, data) + tile_dict['tile_name'].append(file_name) + tile_dict['geometry'].append(tile) + + tile_serial_id += 1 + + # Write tile polygons to geojson file + tile_grid_gdf = gpd.GeoDataFrame(tile_dict, crs='EPSG:4326') + tile_grid_file_path = os.path.join(output_path, 'tile_grid.geojson') + tile_grid_gdf.to_file(tile_grid_file_path) else: data = self.aggregate.get_data(bbox) write_layer(output_path, data) diff --git a/tests/resources/layer_dumps_for_br_lauro_de_freitas/test_write_layers_to_qgis_files.py b/tests/resources/layer_dumps_for_br_lauro_de_freitas/test_write_layers_to_qgis_files.py index 160d7bc..a73ab25 100644 --- a/tests/resources/layer_dumps_for_br_lauro_de_freitas/test_write_layers_to_qgis_files.py +++ b/tests/resources/layer_dumps_for_br_lauro_de_freitas/test_write_layers_to_qgis_files.py @@ -34,6 +34,13 @@ def test_write_albedo(target_folder, bbox_info, target_spatial_resolution_multip 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_albedo_tiled(target_folder, bbox_info, target_spatial_resolution_multiplier): + file_path = prep_output_path(target_folder, 'albedo_tiled.tif') + 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=0.01) + 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, target_spatial_resolution_multiplier): file_path = prep_output_path(target_folder, 'alos_dsm.tif')