I want to get just the Pixel size of a raster (in an automated workflow), and gdalinfo looks like the best tool for that; The trouble is that it gives a whole lot of info which I don't need.

So instead of using awk or some other logic for parsing the whole thing, is there a way of getting only the Pixel Size from gdalinfo?

  • 2
    There are some switches to limit the output and they are all documented in gdal.org/gdalinfo.html. I fear you must pipe the output through grep and sed. On Windows you can start with gdalinfo test.tif |find "Pixel Size" but grep and sed are also available for Windows. – user30184 Oct 9 '15 at 14:47
  • 1
    I think the traditional grep/awk workflow is going to be the only direct answer for your question...but there may be a better solution depending on what you are trying to do overall. For example, if you are trying to call this from a Python script, you could just use the Python binding to get the info you need directly. I expect it's not that simple though, since you are asking. – Evil Genius Oct 9 '15 at 15:21
  • 2
    The development version has json output from gdalinfo, which would (possibly) be easier to parse. – user10353 Oct 12 '15 at 15:02
  • @kyle: How do I download the developer version? I can't find anything like that in: download.osgeo.org/gdal – Devdatta Tengshe Oct 13 '15 at 4:15
  • Windows builds can be found at gisinternals.com/sdk. Linux you probably have to compile. – user10353 Oct 13 '15 at 14:51

Are you using Python for your automated workflow? Although you specify gdalinfo in your question, you can get the raster information using Python if you have the GDAL Python bindings set up.

# Imports
import gdal

# Open raster in ReadOnly mode
rast_src = ".../raster.tif"
rast_open = gdal.Open(rast_src, GA_ReadOnly)

# Get georeference information: this returns a list containing
# origin x, cell size (W-E),rotation, origin y, rotation, cell size (N-S)
rast_info = rast_open.GetGeoTransform()

# The origin is the top left of the image and the cell size is given in
# the x direction (W-E) and y direction (N-S).
res_x = rast_info[1]
res_y = rast_info[5]

Note that res_y will be negative as it is measured south from origin.

For more information on the GeoTransform object see the Getting Dataset Information section here: http://www.gdal.org/gdal_tutorial.html

The following tutorial essentially runs through this step by step and references some more useful online resources: http://geoinformaticstutorial.blogspot.co.uk/2012/09/reading-raster-data-with-python-and-gdal.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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