@@ -262,38 +262,54 @@ def create_merged_raster_bands_metadata(
262
262
263
263
264
264
def merge_raster_bands (
265
- merged_filename : Union [pathlib .Path , str ],
266
265
raster_file_list : list [Union [pathlib .Path , str ]],
266
+ merged_filename : Union [pathlib .Path , str ],
267
+ merged_band_names : list [str ] = None ,
267
268
metadata : dict = None ,
268
- band_names : list [str ] = None ,
269
269
logger : logging .Logger = LOGGER ,
270
270
) -> 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
+ """
271
285
if not metadata :
272
286
metadata = create_merged_raster_bands_metadata (raster_file_list )
287
+
273
288
merged_image_index = 1
274
- band_index = 0
289
+ band_names_index = 0
290
+
275
291
logger .info (f"Merging asset [{ merged_filename } ] ..." )
276
292
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
279
295
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 ):
283
299
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 } "
285
301
)
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 ]
291
307
if num_of_bands > 1 :
292
- description = f"{ description } -{ asset_band_image_index } "
308
+ description = f"{ description } -{ source_image_band_index } "
293
309
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 ))
295
311
merged_image_index += 1
296
- band_index += 1
312
+ band_names_index += 1
297
313
298
314
if not merged_filename .exists ():
299
315
return None
0 commit comments