Skip to content

Instantly share code, notes, and snippets.

@ajdawson
Created September 21, 2014 11:36
Show Gist options
  • Save ajdawson/dc712f1d2203aceedb95 to your computer and use it in GitHub Desktop.
Save ajdawson/dc712f1d2203aceedb95 to your computer and use it in GitHub Desktop.
SRTM parser for Cartopy
"""Construct an SRTM lookup file for Cartopy."""
from HTMLParser import HTMLParser
import json
import os
import urllib2
#: Base URL where SRTM data files (with extensions .hgt.zip) are located.
SRTM_URL = 'http://e4ftl01.cr.usgs.gov/SRTM/SRTMGL3.003/2000.02.11/'
#: Name of JSON file to write output to.
SRTM_JSON = 'srtm.json'
class SRTMParser(HTMLParser):
"""Parser for SRTM data download page."""
def __init__(self):
HTMLParser.__init__(self)
self.targets = {}
def handle_starttag(self, tag, attrs):
if tag == 'a':
target = None
for attribute, value in attrs:
if attribute == 'href':
target = value
if target is not None and target.endswith('.hgt.zip'):
self.targets[target.split('.')[0]] = os.path.join(
SRTM_URL, target)
if __name__ == '__main__':
resource = urllib2.urlopen(SRTM_URL)
html = resource.read()
resource.close()
parser = SRTMParser()
parser.feed(html)
with open(SRTM_JSON, 'w') as f:
json.dump(parser.targets, f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment