Skip to content

Instantly share code, notes, and snippets.

@rmolina
Last active June 20, 2016 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmolina/59304312de6dba6c06264fc8eac53636 to your computer and use it in GitHub Desktop.
Save rmolina/59304312de6dba6c06264fc8eac53636 to your computer and use it in GitHub Desktop.
Quick rasterio example on how to sample elevations from the EarthEnv Digital Elevation Model Version 1 by Natalie Robinson and the NCEAS Environment and Organisms (ENO) Working Group, http://www.earthenv.org/DEM
def EarthEnvDEM90(lat, lon):
import rasterio
tile = 'EarthEnv-DEM90_%s%02i%s%03i' % (lat < 0 and "S" or "N",
abs(lat - lat % 5),
lon < 0 and "W" or "E",
abs(lon - lon % 5))
# /vsitar/ allows on the fly decompression of the tiles :)
bil = r'/vsitar/F:\ruben\geodata\dem90\%s.tar.gz\%s.bil' % (tile, tile)
with rasterio.open(bil) as src:
r, c = src.index(lon, lat)
if not lat % 5 or not lon % 5: # tile edges return off-by-one indices
#print '==> [off-by-one indices: %i, %i] <==' % (r, c)
c = max(min(src.meta['width'] - 1, c), 0)
r = max(min(src.meta['height'] - 1, r), 0)
w = ((r, r + 1), (c, c + 1))
data = src.read(1, window=w)
return data
print 'Everest: %i m a.s.l.' % EarthEnvDEM90(27.988056, 86.925278)
print 'Aconcagua: %i m a.s.l.' % EarthEnvDEM90(-32.653431, -70.011083)
print 'Kilimanjaro: %i m a.s.l.' % EarthEnvDEM90(-3.075833, 37.353333)
print 'Edge case (off-by-one indices): %i m a.s.l.' % EarthEnvDEM90(-10, -75)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment