Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was wondering if anyone has some experience in getting elevation data from a raster without using ArcGIS, but rather get the information as a python list or dict?

I get my XY data as a list of tuples:

xy34 =[perp_obj[j].CalcPnts(float(i.dist), orientation) for j in range (len(perp_obj))]

I'd like to loop through the list or pass it to a function or class-method to get the corresponding elevation for the xy-pairs.

I did some research on the topic and the gdal API sounds promising. Can anyone advice me how to go about things, pitfalls, sample code?

EDIT GDAL is not an option as I can't edit the system path variable on the machine I'm working on! Does anyone know about a different approach?

Thanks for your efforts, LarsVegas

share|improve this question
unfortunately, you really do need to get GDAL running on your system to do anything with a raster in Python. With "can't edit the system path variable on the machine", are you referring to these instructions? I find this installation method very poor, and I don't use it nor recommend it. If you are using Windows, install GDAL/Python the simple way. – Mike T Jul 18 '12 at 22:12
Yes, I was indeed. I'm not at work right now but I'll check out the link you posted. Looks promising! Thanks for coming back to my question! – LarsVegas Jul 23 '12 at 10:19
I've used the installer by Christoph Gohlke (linked above) on many work computers, and it is really simple. You only need to ensure that you match the version of Python and either 32- or 64-bit Windows. While you are at it, you should also get NumPy from the same place, since that is needed by GDAL, as shown in the answers below. – Mike T Jul 23 '12 at 20:11

Here's a more programmatic way of using GDAL than @Aragon's answer. I've not tested it, but it is mostly boiler-plate code that has worked for me in the past. It relies on Numpy and GDAL bindings, but that's about it.

import osgeo.gdal as gdal
import osgeo.osr as osr
import numpy as np
from numpy import ma

def maFromGDAL(filename):
    dataset = gdal.Open(filename, gdal.GA_ReadOnly)

    if dataset is None:
        raise Exception()

    # Get the georeferencing metadata.
    # We don't need to know the CRS unless we want to specify coordinates
    # in a different CRS.
    #projection = dataset.GetProjection()
    geotransform = dataset.GetGeoTransform()

    # We need to know the geographic bounds and resolution of our dataset.
    if geotransform is None:
        dataset = None
        raise Exception()

    # Get the first band.
    band = dataset.GetRasterBand(1)
    # We need to nodata value for our MaskedArray later.
    nodata = band.GetNoDataValue()
    # Load the entire dataset into one numpy array.
    image = band.ReadAsArray(0, 0, band.XSize, band.YSize)
    # Close the dataset.
    dataset = None

    # Create a numpy MaskedArray from our regular numpy array.
    # If we want to be really clever, we could subclass MaskedArray to hold
    # our georeference metadata as well.
    # see here:
    # For details.
    masked_image = ma.masked_values(image, nodata, copy=False)
    masked_image.fill_value = nodata

    return masked_image, geotransform

def pixelToMap(gt, pos):
    return (gt[0] + pos[0] * gt[1] + pos[1] * gt[2],
            gt[3] + pos[0] * gt[4] + pos[1] * gt[5])

def mapToPixel(gt, pos):
    return (-(gt[0] + pos[1] * gt[2]) / gt[1],
            -(gt[3] + pos[0] * gt[5]) / gt[4])

def valueAtMapPos(image, gt, pos):
    pp = mapToPixel(gt, pos)
    x = int(pp[0])
    y = int(pp[1])

    if x < 0 or y < 0 or x >= image.shape[1] or y >= image.shape[0]:
        raise Exception()

    # Note how we reference the y column first. This is the way numpy arrays
    # work by default. But GDAL assumes x first.
    return image[y, x]

    image, geotransform = maFromGDAL('myimage.tif')
    val = valueAtMapPos(image, geotransform, (434323.0, 2984745.0))
    print val
    print('Something went wrong.')
share|improve this answer
see the edit to my question...thanks for posting anyway! I upvoted it. – LarsVegas Jul 16 '12 at 13:19
Ah damn! Well at least it's here for posterity. TBH, the maths in mapToPixel() and pixelToMap() are the important bit, as long as you can create a numpy array (or a regular Python one, but they're generally not as efficient for this sort of thing), and get the array's geographical bounding box. – MerseyViking Jul 16 '12 at 13:28
+1 for the comment (and code) about reversing the parameters to the numpy array. I was searching everywhere for a bug in my code, and this swap fixed it! – aldo Feb 19 '15 at 23:15

checkout my answer here... and read here for some information. the following info have taken from Geotips:

With gdallocationinfo, we can query the elevation at one point :

$ gdallocationinfo gmted/all075.vrt -geoloc 2 49 Report: Location: (87360P,19679L) Band 1: gmted/30N000E_20101117_gmted_bln075.vrt Value: 183

i hope it helps you...

share|improve this answer
Just found out I can't use GDAL as I'm not able to edit my system variable on the machine I'm working on. Thanks for the input though. – LarsVegas Jul 16 '12 at 13:03
up vote 0 down vote accepted

The provided python code extracts the value data of a raster cell based on given x,y coords. It is a slightly alterd version of an example from this excellent source. It is based on GDAL and numpy which are not part of the standard python distribution. Thanks to @Mike Toews for pointing out the Unofficial Windows Binaries for Python Extension Packages to make installation and use quick and easy.

import os, sys, time, gdal
from gdalconst import *

# coordinates to get pixel values for
xValues = [122588.008]
yValues = [484475.146]

# set directory

# register all of the drivers
# open the image
ds = gdal.Open('i25gn1_131.img', GA_ReadOnly)

if ds is None:
    print 'Could not open image'

# get image size
rows = ds.RasterYSize
cols = ds.RasterXSize
bands = ds.RasterCount

# get georeference info
transform = ds.GetGeoTransform()
xOrigin = transform[0]
yOrigin = transform[3]
pixelWidth = transform[1]
pixelHeight = transform[5]

# loop through the coordinates
for xValue,yValue in zip(xValues,yValues):
    # get x,y
    x = xValue
    y = yValue

    # compute pixel offset
    xOffset = int((x - xOrigin) / pixelWidth)
    yOffset = int((y - yOrigin) / pixelHeight)
    # create a string to print out
    s = "%s %s %s %s " % (x, y, xOffset, yOffset)

    # loop through the bands
    for i in xrange(1,bands):
        band = ds.GetRasterBand(i) # 1-based index
        # read data and add the value to the string
        data = band.ReadAsArray(xOffset, yOffset, 1, 1)
        value = data[0,0]
        s = "%s%s " % (s, value) 
    # print out the data string
    print s
    # figure out how long the script took to run
share|improve this answer
It seems like this is just a less generic, less flexible version of what MerseyViking offered above? – WileyB Feb 17 at 14:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.