From e0e0875e4b77c9b6a36bb0be4d7ddf829a903ce0 Mon Sep 17 00:00:00 2001 From: Tim Iles Date: Thu, 9 Mar 2023 10:12:09 +0000 Subject: [PATCH 1/2] updating srtm path --- srtm4/download.py | 5 +++-- srtm4/point.py | 19 +++++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/srtm4/download.py b/srtm4/download.py index ffdb9cb..42ece0c 100644 --- a/srtm4/download.py +++ b/srtm4/download.py @@ -17,7 +17,7 @@ from requests.adapters import HTTPAdapter, Retry, RetryError import filelock -SRTM_URL = 'https://srtm.csi.cgiar.org/wp-content/uploads/files/srtm_5x5/TIFF' +SRTM_URL = 'https://e4ftl01.cr.usgs.gov//DP133/SRTM/SRTMGL1.003/2000.02.11' def _requests_retry_session( @@ -102,7 +102,8 @@ def get_srtm_tile(srtm_tile, out_dir): return # download the zip file - srtm_tile_url = '{}/{}.zip'.format(SRTM_URL, srtm_tile) + x, y = srtm_tile + srtm_tile_url = f'{SRTM_URL}/{y}{x}.SRTMGL1.hgt.zip' zip_path = os.path.join(output_dir, '{}.zip'.format(srtm_tile)) lock_zip = filelock.FileLock(srtm_zip_download_lock) diff --git a/srtm4/point.py b/srtm4/point.py index 1fbfd3d..3009c2f 100644 --- a/srtm4/point.py +++ b/srtm4/point.py @@ -33,6 +33,21 @@ def lon_lats_str(lon, lat): return lon_lats +def tile_from_lon_lat(lon, lat): + if lon < 0: + lon_direction = "W" + else: + lon_direction = "E" + if lat < 0: + lat_direction = "S" + else: + lat_direction = "N" + + lon = int(lon) + lat = int(lat) + + return f"{lon_direction}{str(lon).zfill(3)}", f"{lat_direction}{str(lat).zfill(2)}" + def srtm4_which_tile(lon, lat): """ Determine the srtm tiles needed to cover the (list of) point(s) @@ -70,10 +85,10 @@ def srtm4(lon, lat): """ # get the names of srtm_tiles needed srtm_tiles = srtm4_which_tile(lon, lat) - + srtm_tile_lon, srtm_tile_lat = tile_from_lon_latlon, lat) # download the tiles if not already there for srtm_tile in set(srtm_tiles): - download.get_srtm_tile(srtm_tile, SRTM_DIR) + download.get_srtm_tile((srtm_tile_lon, srtm_tile_lat), SRTM_DIR) # run the srtm4 binary and feed it from stdin lon_lats = lon_lats_str(lon, lat) From 966a51aaf0ed64552ccad6c28013c0b547720b59 Mon Sep 17 00:00:00 2001 From: Tim Iles Date: Thu, 9 Mar 2023 10:44:27 +0000 Subject: [PATCH 2/2] adjustments to output name --- srtm4/download.py | 4 +++- srtm4/point.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/srtm4/download.py b/srtm4/download.py index 42ece0c..1a8dcb6 100644 --- a/srtm4/download.py +++ b/srtm4/download.py @@ -14,6 +14,7 @@ import os import requests +from requests.auth import HTTPBasicAuth from requests.adapters import HTTPAdapter, Retry, RetryError import filelock @@ -59,6 +60,7 @@ def download(to_file, from_url): # Use a requests session with retry logic because the server at # SRTM_URL sometimes returns 503 responses when overloaded session = _requests_retry_session() + r = session.get(from_url, stream=True, verify=False) if not r.ok: raise ConnectionError( @@ -104,7 +106,7 @@ def get_srtm_tile(srtm_tile, out_dir): # download the zip file x, y = srtm_tile srtm_tile_url = f'{SRTM_URL}/{y}{x}.SRTMGL1.hgt.zip' - zip_path = os.path.join(output_dir, '{}.zip'.format(srtm_tile)) + zip_path = os.path.join(output_dir, f'{y}_{x}.zip') lock_zip = filelock.FileLock(srtm_zip_download_lock) lock_zip.acquire() diff --git a/srtm4/point.py b/srtm4/point.py index 3009c2f..66c4086 100644 --- a/srtm4/point.py +++ b/srtm4/point.py @@ -85,7 +85,7 @@ def srtm4(lon, lat): """ # get the names of srtm_tiles needed srtm_tiles = srtm4_which_tile(lon, lat) - srtm_tile_lon, srtm_tile_lat = tile_from_lon_latlon, lat) + srtm_tile_lon, srtm_tile_lat = tile_from_lon_lat(lon, lat) # download the tiles if not already there for srtm_tile in set(srtm_tiles): download.get_srtm_tile((srtm_tile_lon, srtm_tile_lat), SRTM_DIR)