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?
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().
This may be a bit archaic: Convert the pixels to points, add lat and long fields, and populate with lat and long values.
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.