52 lines
1.4 KiB
Python
52 lines
1.4 KiB
Python
import urllib.request
|
|
import zipfile
|
|
import os
|
|
|
|
SRTM_DIR = '/home/slin/enduro-trails/data/srtm'
|
|
BASE_URL = 'https://srtm.kurviger.de/SRTM3/Eurasia'
|
|
|
|
# Smolensk ~32E to Solnechnogorsk ~37E
|
|
# Latitudes: N53-N56 (Smolensk is at 54.8N, Moscow at 55.7N)
|
|
tiles = []
|
|
for lat in [53, 54, 55, 56]:
|
|
for lon in range(32, 37):
|
|
tiles.append(f'N{lat}E{lon:03d}')
|
|
|
|
os.chdir(SRTM_DIR)
|
|
count = 0
|
|
|
|
opener = urllib.request.build_opener()
|
|
opener.addheaders = [('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36')]
|
|
urllib.request.install_opener(opener)
|
|
|
|
for tile in tiles:
|
|
hgt = f'{tile}.hgt'
|
|
if os.path.exists(hgt):
|
|
print(f' EXISTS: {hgt}')
|
|
continue
|
|
|
|
url = f'{BASE_URL}/{tile}.hgt.zip'
|
|
tmp = f'/tmp/{tile}.hgt.zip'
|
|
|
|
try:
|
|
print(f' Downloading {tile}...', end=' ', flush=True)
|
|
urllib.request.urlretrieve(url, tmp)
|
|
|
|
if os.path.getsize(tmp) > 0:
|
|
with zipfile.ZipFile(tmp, 'r') as z:
|
|
z.extractall('.')
|
|
os.remove(tmp)
|
|
size = os.path.getsize(hgt) if os.path.exists(hgt) else 0
|
|
print(f'OK ({size // 1024}KB)')
|
|
count += 1
|
|
else:
|
|
print('EMPTY')
|
|
os.remove(tmp)
|
|
except Exception as e:
|
|
print(f'FAIL: {e}')
|
|
if os.path.exists(tmp):
|
|
os.remove(tmp)
|
|
|
|
print(f'\nDownloaded: {count} new files')
|
|
print(f'Total HGT: {len([f for f in os.listdir(".") if f.endswith(".hgt")])}')
|