6

How do I get the extent out of the GeoTiff format? I want something like this:

Extent(293518.1886150768,5680494.194041155,890338.5054657329,6267530.571271311)

AKA the xmin, xmax, ymin, ymax coordinates from the GeoTiff?

Edit: I'm implementing my own reader so I will need to know how the file format tells me this information.

  • 1
    Do you have a particular software or API in mind? – artwork21 Jul 7 '14 at 17:52
  • Sorry, I was unclear, editing the question. – Johan S Jul 7 '14 at 17:55
  • Listgeo gives you a rather low level access to geotiff tags remotesensing.org/geotiff/listgeo.html – user30184 Jul 8 '14 at 7:24
  • hi, yes the problem is that I don't know which tags give me the information. Do you know? – Johan S Jul 8 '14 at 9:38
4
+50

The tags you're interested in are: ModelTiepointTag, ModelPixelScaleTag, and ModelTransformationTag. The specification describes how they stored the information:

http://www.remotesensing.org/geotiff/spec/geotiff2.6.html#2.6.1

You could have a look at how GDAL implements them in this file:

http://trac.osgeo.org/gdal/browser/trunk/gdal/frmts/gtiff/geotiff.cpp

  • 1
    I'm done with the implementation and these are the tags I used. What happened was that my reader didn't have to do the actual projections so the extent was all I needed, and to get them I used the tags you mentioned above. – Johan S Aug 3 '14 at 20:07
12

Building on what @David mentioned you may use open source gdal library using python module to get image extent like this:

import gdal
from gdalconst import GA_ReadOnly

data = gdal.Open('C:/Temp/myimage.tif', GA_ReadOnly)
geoTransform = data.GetGeoTransform()
minx = geoTransform[0]
maxy = geoTransform[3]
maxx = minx + geoTransform[1] * data.RasterXSize
miny = maxy + geoTransform[5] * data.RasterYSize
print [minx, miny, maxx, maxy]
data = None

[-86.20782844487971, 40.7246942570317, -84.5446284448797, 41.8290942570317]

Reference: Find Extents of GDAL Raster

  • Hi, thanks. As I have mentioned before I need the Keys/Tags that correspond to these values. The GeoTiff has a ModelTransformationTag, is that the transformation tag your using? – Johan S Jul 8 '14 at 9:41
3

Using the arcpy site package in Python, you can accomplish this by converting your geotiff to a raster object and using the extent and *max&*min classes.

import arcpy

arcpy.env.workspace = r'C:\path\to\ws'

rasters = arcpy.ListRasters()

for ras in rasters:
    f = arcpy.Raster(ras)
    xmin = f.extent.XMin
    ymin = f.extent.YMin
    xmax = f.extent.XMax
    ymax = f.extent.YMax
    rectangle = "%s %s %s %s" % (xmin, xmax, ymin, ymax)
  • 1
    It's going to be faster if you set f equal to the extent, instead of accessing it each time. I had code similar to this in a tight loop (building raster indices), and it cut execution time in half by making that simple change. It's actually a two liner with getattr(). – Paul Jul 31 '14 at 19:17
  • Thanks for the comment @Paul. My benchmark tests indicate 0.05 seconds to extract the extent using this method. Two-liners are not necessarily better than larger sections of code--after all "Readability counts." (from the Zen of Python: import this) – Aaron Jul 31 '14 at 19:27
  • Huh, I noticed a substantial difference in my benchmarks over 1000 rasters. But for a few, yeah there won't be any noticeable difference. – Paul Jul 31 '14 at 19:30
  • The source for import this is amusing indeed. ;) I find [getattr(extent, x) for x in ("XMin", "YMin", "XMax", "YMax")] pretty readable, but that's just me. – Paul Jul 31 '14 at 19:36
  • I'm sorry but I want to know how the arcpy library does this, I'm writing my own reader in Scala. – Johan S Jul 31 '14 at 19:43
0

You can use Rasterio to get the bounding box as follows:

>>> import rasterio

>>> dataset = rasterio.open('example.tif')

>>> dataset.bounds
BoundingBox(left=358485.0, bottom=4028985.0, right=590415.0, top=4265115.0)

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.