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.

  • Willing to work with R? Got a script in my stash to do that. – Mikkel Lydholm Rasmussen Aug 9 '16 at 8:59
  • @MikkelLydholmRasmussen Go ahead and post it, I'll give it a try! – Skylar Ittner Aug 9 '16 at 9:08

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


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")

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.

  • Well, I tried the R script. My computer with 16GB of RAM had to swap to the hard drive for extra RAM, so I went to bed and left it. In the morning I got "Error: cannot allocate vector of size 4.8 Gb". It probably works fine with smaller datasets, but I'm using the one with 1km resolution. I've just started with the GDAL version, I'll check it after work and comment if it worked. – Skylar Ittner Aug 9 '16 at 18:16
  • @SkylarMT if you want to try to work around the memory limits, take a look at stat.ethz.ch/R-manual/R-devel/library/base/html/… – Mikkel Lydholm Rasmussen Aug 9 '16 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.