Skip to content

Commit

Permalink
remove blind exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewPlayer3 committed Feb 28, 2024
1 parent 45e55c9 commit be30e48
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 92 deletions.
120 changes: 57 additions & 63 deletions src/asf_tools/watermasking/generate_osm_tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ def extract_water(water_file, lat, lon, tile_width_deg, tile_height_deg, interio
try:
water_gdf = water_gdf.drop(water_gdf[water_gdf['place'] == 'island'].index)
water_gdf = water_gdf.drop(water_gdf[water_gdf['place'] == 'islet'].index)
except Exception as e:
# When there are no islands to remove, an error will be occur, but we don't care about it.
except AttributeError:
# When there are no islands to remove, an AttributeError should throw, but we don't care about it.
pass
water_gdf.to_file(tile_shp, mode='w', engine='pyogrio')
water_gdf = None
Expand Down Expand Up @@ -136,47 +136,44 @@ def merge_interior_and_ocean(internal_tile_dir, ocean_tile_dir, finished_tile_di
num_tiles = len([f for f in os.listdir(internal_tile_dir) if f.endswith('tif')])
for filename in os.listdir(internal_tile_dir):
if filename.endswith('.tif'):
try:
start_time = time.time()

internal_tile = internal_tile_dir + filename
external_tile = ocean_tile_dir + filename
output_tile = finished_tile_dir + filename
command = ' '.join([
'gdal_calc.py',
'-A',
internal_tile,
'-B',
external_tile,
'--format',
'GTiff',
'--outfile',
output_tile,
'--calc',
'"logical_or(A, B)"'
])
start_time = time.time()

internal_tile = internal_tile_dir + filename
external_tile = ocean_tile_dir + filename
output_tile = finished_tile_dir + filename
command = ' '.join([
'gdal_calc.py',
'-A',
internal_tile,
'-B',
external_tile,
'--format',
'GTiff',
'--outfile',
output_tile,
'--calc',
'"logical_or(A, B)"'
])
os.system(command)

if translate_to_cog:
cogs_dir = finished_tile_dir + 'cogs/'
try:
os.mkdir(cogs_dir)
except FileExistsError:
pass
out_file = cogs_dir + filename
command = f'gdal_translate -ot Byte -of COG -co NUM_THREADS=all_cpus {output_tile} {out_file}'
os.system(command)
os.remove(output_tile)

if translate_to_cog:
cogs_dir = finished_tile_dir + 'cogs/'
try:
os.mkdir(cogs_dir)
except FileExistsError:
pass
out_file = cogs_dir + filename
command = f'gdal_translate -ot Byte -of COG -co NUM_THREADS=all_cpus {output_tile} {out_file}'
os.system(command)
os.remove(output_tile)
end_time = time.time()
total_time = end_time - start_time

end_time = time.time()
total_time = end_time - start_time
print(f'Elapsed Time: {total_time}(s)')
print(f'Completed {index} of {num_tiles}')

print(f'Elapsed Time: {total_time}(s)')
print(f'Completed {index} of {num_tiles}')

index += 1
except Exception as e:
print(f'Caught error while processing {filename}. Continuing anyways...\n{e}')
index += 1


def main():
Expand Down Expand Up @@ -218,30 +215,27 @@ def main():
for lat in lat_range:
for lon in lon_range:
tile_name = lat_lon_to_tile_string(lat, lon, is_worldcover=False)
try:
start_time = time.time()
extract_water(
processed_pbf_path,
lat,
lon,
tile_width,
tile_height,
interior_tile_dir=INTERIOR_TILE_DIR
)
process_ocean_tiles(
args.ocean_polygons_path,
lat,
lon,
tile_width,
tile_height,
output_dir=OCEAN_TILE_DIR
)
end_time = time.time()
total_time = end_time - start_time
print(f'Finished initial creation of {tile_name} in {total_time}(s). {index} of {num_tiles}')
index += 1
except Exception as e:
print(f'Caught error while processing {tile_name}. Continuing anyways...\n{e}')
start_time = time.time()
extract_water(
processed_pbf_path,
lat,
lon,
tile_width,
tile_height,
interior_tile_dir=INTERIOR_TILE_DIR
)
process_ocean_tiles(
args.ocean_polygons_path,
lat,
lon,
tile_width,
tile_height,
output_dir=OCEAN_TILE_DIR
)
end_time = time.time()
total_time = end_time - start_time
print(f'Finished initial creation of {tile_name} in {total_time}(s). {index} of {num_tiles}')
index += 1

print('Merging processed tiles...')
merge_interior_and_ocean(INTERIOR_TILE_DIR, OCEAN_TILE_DIR, FINISHED_TILE_DIR, translate_to_cog=True)
Expand Down
41 changes: 19 additions & 22 deletions src/asf_tools/watermasking/generate_worldcover_tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,28 +174,25 @@ def crop_tile(tile):
Args:
tile: The filename of the desired tile to crop.
"""
try:
ref_image = TILE_DIR + tile
out_filename = CROPPED_TILE_DIR + tile
pixel_size = gdal.Warp('tmp_px_size.tif', ref_image, dstSRS='EPSG:4326').GetGeoTransform()[1]

shapefile_command = ' '.join(['gdaltindex', 'tmp.shp', ref_image])
os.system(shapefile_command)

gdal.Warp(
out_filename,
tile,
cutlineDSName='tmp.shp',
cropToCutline=True,
xRes=pixel_size,
yRes=pixel_size,
targetAlignedPixels=True,
dstSRS='EPSG:4326',
format='COG'
)
remove_temp_files(['tmp_px_size.tif', 'tmp.shp'])
except Exception as e: # When a tile fails to process, we don't necessarily want the program to stop.
print(f'Could not process {tile} due to {e}. Skipping...')
ref_image = TILE_DIR + tile
out_filename = CROPPED_TILE_DIR + tile
pixel_size = gdal.Warp('tmp_px_size.tif', ref_image, dstSRS='EPSG:4326').GetGeoTransform()[1]

shapefile_command = ' '.join(['gdaltindex', 'tmp.shp', ref_image])
os.system(shapefile_command)

gdal.Warp(
out_filename,
tile,
cutlineDSName='tmp.shp',
cropToCutline=True,
xRes=pixel_size,
yRes=pixel_size,
targetAlignedPixels=True,
dstSRS='EPSG:4326',
format='COG'
)
remove_temp_files(['tmp_px_size.tif', 'tmp.shp'])


def build_dataset(worldcover_tile_dir, lat_range, lon_range, out_degrees):
Expand Down
14 changes: 7 additions & 7 deletions src/asf_tools/watermasking/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def lat_lon_to_tile_string(lat, lon, is_worldcover: bool = False, postfix: str =
lon: The minimum longitude of the tile.
is_worldcover: Wheter the tile is Worldcover or OSM.
postfix: A postfix to append to the tile name to make it a filename.
Returns:
The name of the tile.
"""
Expand Down Expand Up @@ -45,22 +45,22 @@ def merge_tiles(tiles, out_format, out_filename):

def remove_temp_files(temp_files: list):
"""Remove each file in a list of files.
Args:
temp_files: The list of temporary files to remove.
"""
for file in temp_files:
try:
os.remove(file)
except Exception as e:
print(f'Caught {e} while removing temporary file: {file}. Skipping it...')
except FileNotFoundError:
print(f'Temp file {file} was not found, skipping removal...')


def setup_directories(dirs: list[str]):
"""Setup the directories necessary for running the script."""
for dir in dirs:
for directory in dirs:
try:
os.mkdir(dir)
except FileExistsError as e:
os.mkdir(directory)
except FileExistsError:
# Directories already exists.
pass

0 comments on commit be30e48

Please sign in to comment.