Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
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 '13 at 15:54
add comment

3 Answers

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

share|improve this answer
add comment

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
xoff, a, b, yoff, d, e = ds.GetGeoTransform()

def pixel2coord(x, y):
    """Returns global coordinates from pixel x, y coords"""
    xp = a * x + b * y + xoff
    yp = d * x + e * y + yoff
    return(xp, yp)

For example, if there is a pixel at x=10, y=22, then it's real-world coordinates are:

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

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

share|improve this answer
add comment

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.
share|improve this answer
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 '13 at 16:32
    
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 '13 at 22:55
add comment

Your Answer

 
discard

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.