Skip to content

Commit 5ee0480

Browse files
committed
Refactor variable names for more clarity
1 parent ab502bb commit 5ee0480

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

geospatial_tools/raster.py

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -262,38 +262,54 @@ def create_merged_raster_bands_metadata(
262262

263263

264264
def merge_raster_bands(
265-
merged_filename: Union[pathlib.Path, str],
266265
raster_file_list: list[Union[pathlib.Path, str]],
266+
merged_filename: Union[pathlib.Path, str],
267+
merged_band_names: list[str] = None,
267268
metadata: dict = None,
268-
band_names: list[str] = None,
269269
logger: logging.Logger = LOGGER,
270270
) -> Optional[pathlib.Path]:
271+
"""
272+
273+
Parameters
274+
----------
275+
merged_filename
276+
raster_file_list
277+
metadata
278+
merged_band_names
279+
logger
280+
281+
Returns
282+
-------
283+
284+
"""
271285
if not metadata:
272286
metadata = create_merged_raster_bands_metadata(raster_file_list)
287+
273288
merged_image_index = 1
274-
band_index = 0
289+
band_names_index = 0
290+
275291
logger.info(f"Merging asset [{merged_filename}] ...")
276292
with rasterio.open(merged_filename, "w", **metadata) as merged_asset_image:
277-
for asset_sub_item in raster_file_list:
278-
asset_name = pathlib.Path(asset_sub_item).name
293+
for raster_file in raster_file_list:
294+
asset_name = pathlib.Path(raster_file).name
279295
logger.info(f"Writing band image: {asset_name}")
280-
with rasterio.open(asset_sub_item) as asset_band_image:
281-
num_of_bands = asset_band_image.count
282-
for asset_band_image_index in range(1, num_of_bands + 1):
296+
with rasterio.open(raster_file) as source_image:
297+
num_of_bands = source_image.count
298+
for source_image_band_index in range(1, num_of_bands + 1):
283299
logger.info(
284-
f"Writing asset sub item band {asset_band_image_index} to merged index band {merged_image_index}"
300+
f"Writing asset sub item band {source_image_band_index} to merged index band {merged_image_index}"
285301
)
286-
merged_asset_image.write_band(merged_image_index, asset_band_image.read(asset_band_image_index))
287-
asset_description_index = asset_band_image_index - 1
288-
description = asset_band_image.descriptions[asset_description_index]
289-
if band_names:
290-
description = band_names[band_index]
302+
merged_asset_image.write_band(merged_image_index, source_image.read(source_image_band_index))
303+
source_description_index = source_image_band_index - 1
304+
description = source_image.descriptions[source_description_index]
305+
if merged_band_names:
306+
description = merged_band_names[band_names_index]
291307
if num_of_bands > 1:
292-
description = f"{description}-{asset_band_image_index}"
308+
description = f"{description}-{source_image_band_index}"
293309
merged_asset_image.set_band_description(merged_image_index, description)
294-
merged_asset_image.update_tags(merged_image_index, **asset_band_image.tags(asset_band_image_index))
310+
merged_asset_image.update_tags(merged_image_index, **source_image.tags(source_image_band_index))
295311
merged_image_index += 1
296-
band_index += 1
312+
band_names_index += 1
297313

298314
if not merged_filename.exists():
299315
return None

geospatial_tools/stac.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ def merge_asset(self, base_directory: Optional[Union[str, pathlib.Path]] = None,
121121
meta = self._create_merged_asset_metadata()
122122

123123
merge_raster_bands(
124-
merged_filename=merged_filename, raster_file_list=asset_filename_list, metadata=meta, band_names=self.bands
124+
merged_filename=merged_filename,
125+
raster_file_list=asset_filename_list,
126+
metadata=meta,
127+
merged_band_names=self.bands,
125128
)
126129

127130
if merged_filename.exists():

0 commit comments

Comments
 (0)