Created
September 21, 2014 11:36
-
-
Save ajdawson/dc712f1d2203aceedb95 to your computer and use it in GitHub Desktop.
SRTM parser for Cartopy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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