Generating an XYZ ASCII file from a QGIS raster
Posted by: Gavin Fleming | in FOSSGIS, QGIS | 4 years, 8 months ago | 7 comments

Someone wrote to me asking if it would be possible to generate an XYZ ASCII file from a single band raster layer in QGIS. No doubt there are more efficient ways (this approach is pretty slow but it works), but I thought it would be fun to show how you can iterate over a raster, writing out the value of each cell into a text file (along with the centroid coordinates for that cell).

 

 

 

 

To use the script, you should save it to your local machine, then open the python console and load the script in the python editor. Next select a single band raster and then run the script in the editor. If your raster is quite large, it will take some time to run. I have spent zero time trying to optimise the script - if someone has an idea for doing it faster, send me a patch and I will update the example above.

The generated output dataset will look something like this:

 

Longitude,Latitude,VI
8.31259406548,7.86128343221,10
8.31264849753,7.86128343221,16
8.31270292958,7.86128343221,18
8.31248520138,7.8613378416,15
8.31253963343,7.8613378416,17
8.31259406548,7.8613378416,24
8.31264849753,7.8613378416,46
8.31270292958,7.8613378416,47
...


The resulting script can be used with programs like gdal_grid or loaded back into QGIS as a vector layer using the Delimited Text provider:

 

Current rating: 2.5

Comments

Stefan Keller 4 years, 8 months ago

Hi Tim!

That blog post is a fun Python code snippet! I'd like to add following comments and questions:

Why not simply use QGIS menu "Raster->Conversion->Translate"? (which in fact to me should be moved to layer "Save as...")

Given one prefers scripting, I would point explicitly to GDAL gdal_translate.

And given one prefers more a GUI what one could add a QtGui.QFileDialog(...) before line 9 "f = file('/tmp/test.xyz', 'w')"?

Yours, Stefan

Link | Reply
Currently unrated

Comment awaiting approval 2 weeks, 4 days ago

Comment awaiting approval 2 weeks, 4 days ago

Comment awaiting approval 4 days, 22 hours ago

Comment awaiting approval 4 days, 22 hours ago

Tim Sutton 4 years, 8 months ago

Hi Stefan

Yes gdal_translate is muuuuuuch faster so if you are only interested in performance you should definately use it. If you are interested in a knowing how to step wise iterate through a raster and do something with each, cell's value using the QGIS api then this snippet if aimed at you. Note that it would be faster also to use QGIS raster block API - you can see I actually get the block out in line 9:

> b = p.block(0, p.extent(), w, h)



but then don't use it (sloppy code I know :-) ). It would be a nice topic for a future post - how to read and use raster blocks from QGIS in python.


In this specific case, the person wanted all the 0 values excluded from the output file, and specific headers written (both are probably possible using gdal if I dug a little around the options).


As for the GUI file selector, I would probably make a proper plugin of this snippet and have an icon and a file dialog if I wanted to spend more time on it.


Thanks for your comments!


Tim

Link | Reply
Currently unrated

FFXIV 4 years, 4 months ago

<strong>FFXIV</strong>

If he shows up, we stick with him.

Link | Reply
Currently unrated

New Comment

required

required (not published)

optional

required

Have any questions? Contact us.
We would love to hear from you!