[GIS] Loop over GeoTIFF image pixels, extract coordinates and pixel color

databasedigital image processinggeotiff-tiffPHPpython

I have a very large GeoTIFF image (~650MB) with color-coded landcover data from here. I need to extract the latitude, longitude, and pixel color for each pixel in the image, so I can import it into a MySQL database for my application.

I'd prefer solutions in PHP or Python, but anything that I can run on Linux would be fine.

Best Answer

The code below is made for R and requires the raster library:

setwd("C:/Data/AVHRR_LandCover/")
library(raster)

classificationfile <- raster("AVHRR_1deg_LANDCOVER_1981_1994.GLOBAL.tif")
xcoords <- xFromCol(classificationfile)
ycoords <- yFromRow(classificationfile)

values <- values(classificationfile)
NewRow <- rep(ycoords,each=NCOL(classificationfile))
NewCol <- rep(xcoords,NROW(classificationfile))

dNewCol <- round(NewCol,digits=8)
dNewRow <- round(NewRow,digits=8)
dvalues <- round(values,digits=0)

classframe <- data.frame(dNewCol,dNewRow,dvalues)

colnames(classframe) <- c("X","Y","Z")
write.table(classframe,file="Classification_XYZ.txt",sep=";",dec=".",row.names=F,col.names=T,quote=F)

Storing your data as XYZ is rather inefficient, so expect a somewhat large file. You can potentially save some space by reducing the "accuracy" on the round() function, which is used to determine the number of digits in the X and Y coordinates.

Another option is doing it with GDAL:

gdal_translate -of XYZ input.tif output.asc

See http://www.gdal.org/frmt_xyz.html for more information on how to change the format of the output file.