Computer Science Mojo

~ David's Notes on coding, software and computer science




Post

How to use GDAL Python to transform geotiff pixels to lat lon real-word coordinates

Category: GDAL     Tag: GDAL   Linux   Python  
By: David     On: Thu 06 August 2015     

GDAL gdaltransform is a easy way to transform a pixel of a tiff image to its corresponding lat/lon coordinate.

Software

Reading

1. In terminal

Running the following in the terminal will give you the (lon, lat) coordinate of (12, 6) pixel (12th column and 6th row) of "myImage.tiff" :

gdaltransform myImage.tiff
12 6

this returns (lon, lat, z value) :

85.1234567890123 27.1234567890123 0

2. In Python Script

To achieve this through a python script and therefore transform an lot of pixels at once, you will have to first write the pixels to a file "pix.txt" and then run:

import os
os.system("gdaltransform " + imagePath + " <pix.txt "+ ">cor.txt")

This will produce "cor.txt" containing the (lon, lat, z value)

Example "pix.txt" and "cor.txt": pix.txt:

12 6
24 9

cor.txt:

85.1234567890123 27.1234567890123 0
86.3234565432233 26.9876543345677 0