[GIS] How to read GeoTIFF using PHP

elevationgeotiff-tiffPHPsrtm

My current pet project involves creating a web service that uses the CGIAR SRTM worldwide elevation data set. Each request to the service will query for elevation data from a small geographic area.

Storing all the data in fashion that can be queried fast turns out to be difficult. My best idea is to keep the data in GeoTIFF files and create an index linking lat/lon coordinates to files. Which leads to my current main problem:

How do I get the elevation values out of a GeoTIFF file? I know the projection, scale and orientation of the GeoTIFF files (i.e. I know what lat/lon values each pixel represents). How can I figure out what elevation that pixel refers to? I have to use PHP and thought I could use the ImageMagick function for reading a pixel's color. Unfortunately, it returns R=2 G=2 B=2 for all pixels.

update

To answer someone else's question below, here is my solution.

I went with the library by Bob Osola suggested in the accepted answer below and made the following changes to adjust it to SRTMv4.1 file format (the one available from the CGIAR website). Update the constants to:

const NUM_DATA_ROWS = 6000;     // the number of data rows in the file ( = ImageLength tag value) 
const NUM_DATA_COLS = 6000;     // the number of data columns in the file ( = ImageWidth tag value)   
const DEGREES_PER_TILE = 5;     // each tile is 5 x 5 degrees of lat/lon
const PIXEL_DIST = 0.000833333; // the distance represented by one pixel (0 degrees 0 mins 3 secs of arc = 1/1200)
const STRIPOFFSETS = 0x44bbd5c; // the offset address of the 'StripOffsets' tag

In addition there was a bug in the computation of a longitude. To fix it, replace

$topleftLon = 180 - (($tileRefHoriz -1) * self::DEGREES_PER_TILE); 
if ($lon < 0) {
    $topleftLon = -$topleftLon;
}

by

$topleftLon = (($tileRefHoriz -1) * self::DEGREES_PER_TILE) - 180; 

That should be it.

Best Answer

Download the source code from here

http://www.osola.org.uk/elevations/index.htm

SRTMGeoTIFFReader.php is the clever file the reads the GeoTiff and converts the elevation values (in meters) into Lat/Lng coordinates.

I doubt this is exactly what you want but it does give you a solid base on understanding the process required to accomplish the task from your question.

(There is too much code to post here)

Related Question