From 3b52dc4099a33c481db644e6d0230f5d2a7c1dcb Mon Sep 17 00:00:00 2001 From: bacetiner Date: Mon, 15 Jan 2024 15:02:46 -0800 Subject: [PATCH] Removing multithreaded image writing until racing condition is resolved. Raster reads are still parallelized --- brails/rapidtools/imutils.py | 40 ++++++++++++------------------------ 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/brails/rapidtools/imutils.py b/brails/rapidtools/imutils.py index 5b268ef..e772415 100644 --- a/brails/rapidtools/imutils.py +++ b/brails/rapidtools/imutils.py @@ -47,7 +47,6 @@ from shapely.geometry import box, Polygon import os import numpy as np -import concurrent.futures from PIL import Image from tqdm import tqdm @@ -119,44 +118,31 @@ def get_image_for_fp(dataset,fp): results = None return results + # Read the aerial imagery GeoTIFF into a dataset object: dataset = rasterio.open(rasterMosaicFile, driver='GTiff', num_threads='all_cpus') + + # Compute the coordinates of the bounding box for the dataset bbox_wgs84 = orgcrs2wgs84_bbox(box(*dataset.bounds),dataset.crs) + # Get the footprints contained in the determined bounding box: fpHandler = FootprintHandler() fpHandler.fetch_footprint_data(bbox_wgs84,fpSource=fpData) + # Create the folder to extract the aerial imagery: os.makedirs('images',exist_ok=True) os.makedirs('images/aerial',exist_ok=True) - # Download building-wise aerial imagery: + # Extract building-wise aerial imagery: footprints = fpHandler.footprints - print('\n)') - results = [] - """ + print('\n') for fp in tqdm(footprints, desc='Extracting aerial imagery...'): res = get_image_for_fp(dataset,fp) - results.append(res) - """ - pbar = tqdm(total=len(footprints), desc='Extracting aerial imagery...') - with concurrent.futures.ThreadPoolExecutor() as executor: - future_to_url = { - executor.submit(get_image_for_fp, dataset, fp): fp - for fp in footprints - } - for future in concurrent.futures.as_completed(future_to_url): - fp = future_to_url[future] - pbar.update(n=1) - try: - results.append(future.result()) - except Exception as exc: - print("%r generated an exception: %s" % (fp, exc)) - - for res in results: - if res is not None: - (fp,imout,fp_cent) = res - self.footprints.append(fp) - self.aerialImageList.append(imout) - self.centroids.append(fp_cent) + if res is not None: + (fp,imout,fp_cent) = res + self.footprints.append(fp) + self.aerialImageList.append(imout) + self.centroids.append(fp_cent) + # Report the total number of images extracted and their directory: print(f'\nExtracted aerial imagery for a total of {len(self.footprints)} buildings.') print(f'You can access the extracted images at {os.getcwd()}/images/aerial') \ No newline at end of file