Skip to content

Commit

Permalink
added function for removing temp files
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewPlayer3 committed Feb 21, 2024
1 parent ab4e511 commit 8eb65f0
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/asf_tools/watermasking/generate_osm_tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np
from osgeo import gdal

from hyp3_testing.watermasking.utils import lat_lon_to_tile_string
from asf_tools.watermasking.utils import lat_lon_to_tile_string


def process_pbf(planet_file: str, output_file: str):
Expand All @@ -32,6 +32,19 @@ def process_pbf(planet_file: str, output_file: str):
os.system(merge_command)


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:
print('Error removing temporary file. Skipping it...')


def process_ocean_tiles(ocean_polygons_path, lat, lon, tile_width_deg, tile_height_deg):
"""Process and crop OSM ocean polygons into a tif tile.
Expand Down Expand Up @@ -65,11 +78,8 @@ def process_ocean_tiles(ocean_polygons_path, lat, lon, tile_width_deg, tile_heig
creationOptions=['COMPRESS=LZW']
)

clipped_dbf = tile + '.dbf'
clipped_shx = tile + '.shx'
clipped_prj = tile + '.prj'
clipped_cpg = tile + '.cpg'
os.system(f'rm {clipped_polygons_path} {clipped_dbf} {clipped_shx} {clipped_prj} {clipped_cpg} > /dev/null 2>&1')
temp_files = [tile + '.dbf', tile + '.cpg', tile + '.prj', tile + '.shx']
remove_temp_files(temp_files)


def extract_water(water_file, lat, lon, tile_width_deg, tile_height_deg):
Expand Down Expand Up @@ -99,6 +109,7 @@ def extract_water(water_file, lat, lon, tile_width_deg, tile_height_deg):
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:
# When there are no islands to remove, an error will be occur, but we don't care about it.
pass
water_gdf.to_file(tile_shp, mode='w', engine='pyogrio')
water_gdf = None
Expand All @@ -118,11 +129,8 @@ def extract_water(water_file, lat, lon, tile_width_deg, tile_height_deg):
creationOptions=['COMPRESS=LZW']
)

tile_dbf = tile + '.dbf'
tile_cpg = tile + '.cpg'
tile_prj = tile + '.prj'
tile_shx = tile + '.shx'
os.system(f'rm {tile_geojson} {tile_pbf} {tile_shp} {tile_dbf} {tile_prj} {tile_cpg} {tile_shx}')
temp_files = [tile + '.dbf', tile + '.cpg', tile + '.prj', tile + '.shx']
remove_temp_files(temp_files)


def merge_tiles(internal_tile_dir, ocean_tile_dir, finished_tile_dir, translate_to_cog: bool = False):
Expand Down Expand Up @@ -161,6 +169,7 @@ def merge_tiles(internal_tile_dir, ocean_tile_dir, finished_tile_dir, translate_


def setup_directories():
"""Setup the directories necessary for running the script."""
dirs = ['interior_tiles/', 'ocean_tiles/', 'merged_tiles/', 'finished_tiles/']
for dir in dirs:
try:
Expand Down

0 comments on commit 8eb65f0

Please sign in to comment.