Skip to content
This repository was archived by the owner on Apr 24, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions srtm4/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
import os

import requests
from requests.auth import HTTPBasicAuth
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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -102,8 +104,9 @@ def get_srtm_tile(srtm_tile, out_dir):
return

# download the zip file
srtm_tile_url = '{}/{}.zip'.format(SRTM_URL, srtm_tile)
zip_path = os.path.join(output_dir, '{}.zip'.format(srtm_tile))
x, y = srtm_tile
srtm_tile_url = f'{SRTM_URL}/{y}{x}.SRTMGL1.hgt.zip'
zip_path = os.path.join(output_dir, f'{y}_{x}.zip')

lock_zip = filelock.FileLock(srtm_zip_download_lock)
lock_zip.acquire()
Expand Down
19 changes: 17 additions & 2 deletions srtm4/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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_lat(lon, 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)
Expand Down