Skip to content

Instantly share code, notes, and snippets.

@jdherman
Created October 7, 2014 18:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdherman/dbbe3e16d488450515ac to your computer and use it in GitHub Desktop.
Save jdherman/dbbe3e16d488450515ac to your computer and use it in GitHub Desktop.
Extract raster data value at a point
from __future__ import division
from osgeo import gdal
from geopy.geocoders import Nominatim
def get_value_at_point(rasterfile, pos):
gdata = gdal.Open(rasterfile)
gt = gdata.GetGeoTransform()
data = gdata.ReadAsArray().astype(np.float)
gdata = None
x = int((pos[0] - gt[0])/gt[1])
y = int((pos[1] - gt[3])/gt[5])
return data[y, x]
city = 'Ithaca, NY'
g = Nominatim().geocode(city, timeout=5)
p = (g.longitude,g.latitude)
print get_value_at_point('path/to/my/raster/file', p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment