def download_srtm(LLLON, LLLAT, URLON, ULLAT): # TODO # - Add docstring, comments and useful exceptions. import elevation run_command(['eio', 'selfcheck'], verbose=True) print('Downloading SRTM DEM data.') bare.io.create_dir('./reference_dem') cache_dir = './reference_dem/' product = 'SRTM3' dem_bounds = (LLLON, LLLAT, URLON, ULLAT) elevation.seed(bounds=dem_bounds, cache_dir=cache_dir, product=product, max_download_tiles=999) call = [ 'gdalbuildvrt', './reference_dem/elevation/SRTM3/cache/srtm.vrt', './reference_dem/SRTM3/cache/*.tif' ] run_command(call, verbose=True) ds = gdal.Open('./reference_dem/SRTM3/cache/srtm.vrt') ds = gdal.Translate('./reference_dem/SRTM3/cache/srtm_URb_subset.vrt', ds, projWin=[LLLON, ULLAT, URLON, LLLAT]) return './reference_dem/SRTM3/cache/srtm_subset.vrt'
def download_srtm(LLLON, LLLAT, URLON, URLAT, output_directory='./data/reference_dem/', verbose=True): # TODO # - Add function to determine extent automatically from input cameras # - Make geoid adjustment and converstion to UTM optional import elevation run_command(['eio', 'selfcheck'], verbose=verbose) print('Downloading SRTM DEM data...') hsfm.io.create_dir(output_dir) cache_dir = output_dir product = 'SRTM3' dem_bounds = (LLLON, LLLAT, URLON, URLAT) elevation.seed(bounds=dem_bounds, cache_dir=cache_dir, product=product, max_download_tiles=999) tifs = glob.glob(os.path.join(output_dir, 'SRTM3/cache/', '*tif')) vrt_file_name = os.path.join(output_dir, 'SRTM3/cache/srtm.vrt') call = ['gdalbuildvrt', vrt_file_name] call.extend(tifs) run_command(call, verbose=verbose) ds = gdal.Open(vrt_file_name) vrt_subset_file_name = os.path.join(output_dir, 'SRTM3/cache/srtm_subset.vrt') ds = gdal.Translate(vrt_subset_file_name, ds, projWin=[LLLON, URLAT, URLON, LLLAT]) # Adjust from EGM96 geoid to WGS84 ellipsoid adjusted_vrt_subset_file_name_prefix = os.path.join( output_dir, 'SRTM3/cache/srtm_subset') call = [ 'dem_geoid', '--reverse-adjustment', vrt_subset_file_name, '-o', adjusted_vrt_subset_file_name_prefix ] run_command(call, verbose=verbose) adjusted_vrt_subset_file_name = adjusted_vrt_subset_file_name_prefix + '-adj.tif' # Get UTM EPSG code epsg_code = hsfm.geospatial.wgs_lon_lat_to_epsg_code(LLLON, LLLAT) # Convert to UTM utm_vrt_subset_file_name = os.path.join( output_dir, 'SRTM3/cache/srtm_subset_utm_geoid_adj.tif') call = 'gdalwarp -co COMPRESS=LZW -co TILED=YES -co BIGTIFF=IF_SAFER -dstnodata -9999 -r cubic -t_srs EPSG:' + epsg_code call = call.split() call.extend([adjusted_vrt_subset_file_name, utm_vrt_subset_file_name]) run_command(call, verbose=verbose) # Cleanup print('Cleaning up...', 'Reference DEM available at', out) out = os.path.join(output_dir, os.path.split(utm_vrt_subset_file_name)[-1]) os.rename(utm_vrt_subset_file_name, out) shutil.rmtree(os.path.join(output_dir, 'SRTM3/')) return out
def seed(**kwargs): elevation.seed(**kwargs)
def seed(ctx, **kwargs): if ctx.parent and ctx.parent.params: kwargs.update(ctx.parent.params) elevation.seed(**kwargs)
def download_srtm(bounds, output_directory='./input_data/reference_dem/', utm=False, verbose=False, cleanup=True): """ bounds = (ULLON, ULLAT, LRLON, LRLAT) """ # TODO # - Add function to determine extent automatically from input cameras # - Make geoid adjustment and converstion to UTM optional # - Preserve wgs84 dem import elevation run_command(['eio', 'selfcheck'], verbose=verbose) if verbose: print('Downloading SRTM DEM data...') hsfm.io.create_dir(output_directory) cache_dir = output_directory product = 'SRTM3' elevation.seed(bounds=(bounds[0], bounds[3], bounds[2], bounds[1]), cache_dir=cache_dir, product=product, max_download_tiles=999) tifs = glob.glob(os.path.join(output_directory, 'SRTM3/cache/', '*tif')) vrt_file_name = os.path.join(output_directory, 'SRTM3/cache/srtm.vrt') call = ['gdalbuildvrt', vrt_file_name] call.extend(tifs) run_command(call, verbose=verbose) ds = gdal.Open(vrt_file_name) vrt_subset_file_name = os.path.join(output_directory, 'SRTM3/cache/srtm_subset.vrt') ds = gdal.Translate(vrt_subset_file_name, ds, projWin=[bounds[0], bounds[1], bounds[2], bounds[3]]) # Adjust from EGM96 geoid to WGS84 ellipsoid adjusted_vrt_subset_file_name_prefix = os.path.join( output_directory, 'SRTM3/cache/srtm_subset') call = [ 'dem_geoid', '--reverse-adjustment', vrt_subset_file_name, '-o', adjusted_vrt_subset_file_name_prefix ] run_command(call, verbose=verbose) adjusted_vrt_subset_file_name = adjusted_vrt_subset_file_name_prefix + '-adj.tif' if utm: # Get UTM EPSG code epsg_code = hsfm.geospatial.lon_lat_to_utm_epsg_code( bounds[0], bounds[3]) # Convert to UTM utm_vrt_subset_file_name = os.path.join( output_directory, 'SRTM3/cache/srtm_subset_utm_geoid_adj.tif') call = 'gdalwarp -co COMPRESS=LZW -co TILED=YES -co BIGTIFF=IF_SAFER -dstnodata -9999 -r cubic -t_srs EPSG:' + epsg_code call = call.split() call.extend([adjusted_vrt_subset_file_name, utm_vrt_subset_file_name]) run_command(call, verbose=verbose) if cleanup == True: out = os.path.join(output_directory, os.path.split(utm_vrt_subset_file_name)[-1]) os.rename(utm_vrt_subset_file_name, out) shutil.rmtree(os.path.join(output_directory, 'SRTM3/')) return out else: return utm_vrt_subset_file_name else: if cleanup == True: out = os.path.join( output_directory, os.path.split(adjusted_vrt_subset_file_name)[-1]) os.rename(adjusted_vrt_subset_file_name, out) shutil.rmtree(os.path.join(output_directory, 'SRTM3/')) return out else: return adjusted_vrt_subset_file_name