I want to a store GeoTIFF file pixel by pixel in the database. I need to store pixel values along with the lat/lon of that pixel on the GeoTIFF image. Is it possible to do that?

  • 1
    Is the goal to be able to query by Lat/Lon? You could accomplish this with GDAL using the geotransform attribute - I can provide a python example. The conversion could be pre-rendered into a 2 band geotiff (Band1 - LAT, Band2 - LON), but I wonder if it wouldn't be more efficient to do this on the fly. I guess a use case would help.
    – Jay Laura
    Mar 6, 2013 at 15:54

3 Answers 3


In a generic sense, use the affine transform parameters, which should be available with any raster file format. With GDAL, this is available with GetGeoTransform(), or PostGIS' ST_GeoReference() function. After finding these six parameters, one only needs to determine which ones they are, then a function can be made to transform in linear space.

E.g., with Python:

from osgeo import gdal
ds = gdal.Open('myfile.tif')

# unravel GDAL affine transform parameters
c, a, b, f, d, e = ds.GetGeoTransform()

def pixel2coord(col, row):
    """Returns global coordinates to pixel center using base-0 raster index"""
    xp = a * col + b * row + a * 0.5 + b * 0.5 + c
    yp = d * col + e * row + d * 0.5 + e * 0.5 + f
    return(xp, yp)

For example, if there is a pixel at col=10, row=22, then the real-world coordinates to the pixel center are:

>>> pixel2coord(10, 22)
(2780000.0, 6162300.0)

Something similar can be cooked up for PostGIS with ST_Affine().


The world file (.tfw) that accompanies a GeoTIFF contains:

  • pixel size in the x-direction (and y-direction) in map units/pixel and
  • x-coordinate (and y-coordinate) of the center of the upper left pixel. This is all you need to calculate position in Lat/Lon of every pixel.
  • 2
    In addition to what you describe, at a minimum you need to know the coordinate system! (A world file does not include such information.)
    – whuber
    Mar 6, 2013 at 16:32
  • 2
    Also, not every GeoTIFF contains a world file. A true GeoTIFF has the coordinate information stored in the header, not in a separate file. However, the term "GeoTIFF" is often applied to TIFF images with a world file. The point is, there won't always be a TFW.
    – user3461
    May 5, 2013 at 22:55

This may be a bit archaic: Convert the pixels to points, add lat and long fields, and populate with lat and long values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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