3

Is there a way to export all values from a raster to for example a CSV or other text file or DBF or XLS and later import (changed) values from that file back into the raster?

For vector-files this is no problem but for raster-files I have not found anything like this. (Important here: the export-file should be a table, not a grid).

The idea is to process raster-data with some external program like excel of statistics-software, to add channels (=columns) based on the data already there or create a new channel by linking data with data from a table. All this can be easily done for vector-layers but apparently not for raster-layers.

3 Answers 3

4

As mentioned in the other topic, you could use gdal2xyz. For each pixel in a raster this will save an xyz triplet in a CSV text file. It's included with the GDAL that comes with QGIS, run it from an OSGeo4W command line window:

gdal2xyz -csv infile.asc outfile.csv

Under Linux you could try running it in a terminal. If this doesn't work 'locate' gdal2xyz.py and add its path to the command line. It will work with one-band rasters in all of the common formats, not just ASCs. Come back if you have rasters with more than one band, gdal2xyz can handle these too.

4
  • Oops, forgot about that one :-) Yes, that solves part of the problem. Now I only have to get the data back in after external processing, along with the original coordinates, datum etc. One thing surprised me: Why does gdal2xyz handle only one band at a time? It could just as well output x and y and one addtional column for each band.
    – stn
    Commented Apr 3, 2012 at 18:54
  • 1
    OK, so gdal2xyz does handle multiple bands. You find that out after googling: gdal2xyz -band 1 -band 2 scrfile.tif
    – stn
    Commented Apr 3, 2012 at 19:24
  • Yup! It will even handle images. You have to drop the -csv if there is more than one band and then you get a space-delimited text file.
    – nhopton
    Commented Apr 3, 2012 at 19:54
  • If "the other topic" is a GIS SE Q&A can you provide a link to it, please?
    – PolyGeo
    Commented Dec 11, 2021 at 8:53
0

The latest version of GDAL includes a program named "gdal_calc.py" that looks like it can do more on the commandline than the version that is in the QGIS-menu.

With Ubuntu this tool does not show up unless you completely uninstall the "stable" version of QGIS and all its dependencies and then reinstall from the "unstable" repository.

0

Consider raster calculator as an alternative

An alternative, depending on what you want to do, is using Raster calculator.

Create attribute values from raster pixel values and convert them back to rasters

If, however, you want to stick to the workflow you asked for, another option is to create a point grid that corresponds to your pixels:

  1. Create a vector point grid (Menu Vector / Research Tools / Create Grid) with the same extent and the same distance as the pixel size to get one point per pixel. Be sure to use the same CRS as the raster layer.

  2. Then get the pixel value(s) from the raster to the attribute table of the point layer, using Field calculator. Use this expression: raster_value ('raster', 1, $geometry), where raster is the name of your raster layer and 1 is the no. of the band.

  3. Then calculate / change / add values in the attribute table and/or export the point grid to a CSV or any other type of file you want for external calculation. You could also import a CSV file with data processed in an external application and join it to the point grid.

  4. Use Menu Raster / Conversion /Rasterize (Vector to raster) to convert the values in the attribute table to a raster with pixel values from attribute fields.

  5. If you have more then one band, then stick the diffent single-band output rasters together, creating a multiband raster using Menu Raster / Miscellaneous / Merge, select Input layers and check the box next to Place each input file into a separate band.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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