# Author - Anastasia Reed-Comeaux # Date - 11/28/2023 # Purpose - Created to be a tool to match summits to the tiles used in their summit relocation. Creates a list of summit # names and the TIFFs used to relocate the summit. import requests import os import shutil import json from osgeo import gdal, ogr def find_matching_tiles(x, y, out_loc, data_type): """ Args: x: x coordinate of summit y: y coordinate of summit out_loc: location of all saved TIFFs data_type: the type of data to be looking for tiles of i.e. 5m, 1/3 """ url = "https://tnmaccess.nationalmap.gov/api/v1/products" parameters = {"bbox": f"{x},{y},{x},{y}", "datasets": data_type} num_bad_response = 0 while True: try: response = requests.get(url, params=parameters, verify=False) if not response.ok: num_bad_response += 1 if num_bad_response > 20: raise TNMAPIException(response.status_code) else: try: data = response.json() except Exception as bad_response: print(bad_response) return None if data['total'] == 0: return [] rasters = [] for i in range(0, len(data['items'])): if not data['items'][i]['downloadURL'].endswith('.tif') and not data['items'][i]['downloadURL'].endswith( '.laz'): continue if float(data['items'][i]['boundingBox']['minX']) < float(x) < \ float(data['items'][i]['boundingBox']['maxX']) and \ float(data['items'][i]['boundingBox']['minY']) < float(y) < \ float(data['items'][i]['boundingBox']['maxY']): title = data['items'][i]['title'].replace(" ", "_").replace("_1_x_1_degree", "").replace("_arc-second", "") if '1/3' in title: title = title.replace('1/3', '13') if data_type == "Lidar Point Cloud (LPC)": downloaded_raster = os.path.join(out_loc, title.split('_')[-1], title + ".laz") else: downloaded_raster = os.path.join(out_loc, title.split('_')[-1], title + ".tif") if os.path.isfile(downloaded_raster): rasters.append(downloaded_raster) return rasters except Exception as e: print(e) else: if response.status_code == 400: time.sleep(30) break def gather_matched_tiffs(match_file, dest_folder): """ Args: match_file - file listing each summit and their respective tiles dest_folder - destination to move matched tiles to Result: populates a folder with the matched tiles """ with open(match_file, 'r') as file: matches = json.load(file) file.close() if not os.path.exists(dest_folder): os.mkdir(dest_folder) for val in matches.values(): for tiff in val: if not os.path.exists(dest_folder+tiff.split('/')[-1]): shutil.copy(tiff, dest_folder+tiff.split('/')[-1]) def main(): # need to use a set to make sure you're not grabbing files that have already been copied # then it should copy them to the destination folder. Then it should save the match file # to the destination folder out = "/tmp/.x2go-areed-comeaux/media/disk/_cygdrive_X_/13/TIFF/current/" dest = "/home/areed-comeaux/Desktop/Projects/summitsnapping/full_test/TIFFs/13/" out_file = "summits_and_their_tiles_13.txt" shapefile = "full_test/mosaic_test.shp" # "/tmp/.x2go-areed-comeaux/media/disk/_cygdrive_E_/full_test_mosaic_tiles/mosaic_test_305_sample.shp" dataSource = ogr.GetDriverByName("ESRI Shapefile").Open(shapefile) layer = dataSource.GetLayer() dataType = "National Elevation Dataset (NED) 1/3 arc-second" matches = {} """for summit in layer: print(summit.GetFieldCount()) for n in range(summit.GetFieldCount()): fdefn = summit.GetFieldDefnRef(n) print(f'{n}: {fdefn.GetName()}')""" """with open(out_file, 'w') as file: for summit in layer: x = summit.GetField(10) y = summit.GetField(11) name = summit.GetField(2) fid = summit.GetFID() print(f"Processing {name}...", end='') matches[name+' - ' + str(fid)] = find_matching_tiles(x, y, out, dataType) print("Done!") json.dump(matches, file)""" gather_matched_tiffs(out_file, dest) if __name__ == "__main__": main()