3

I have raster files in the good and old EPSG:4326 (WGS-84) and I would like to create two other raster files with the exact same extent and resolution of it, but with the first raster having the the values of the latitude of each pixel while the second has the longitude.

It seems that in the past the QGIS Field Calculator accepted "$x" to represent the longitude, but today when I try to do it in the current QGIS Raster Calculator I get the message "Expression invalid" and cannot perform it.

So the final question is: How to represent latitude/longitude in the current (v 3.14) QGIS's Raster Calculator?

2
  • As far as I know, the field calculator is for vector layers. And the '$x' expression gives the X of a point for instance. Are you trying to create a raster using the field calculator?
    – Techie_Gus
    Aug 12 '20 at 20:01
  • Thanks for clarifying thar Field Calculator is for vector files. I am trying to work only with rasters. Mainly: how to get the longitude or x value of a raster pixel in the Raster Calculator. Aug 12 '20 at 22:34
3

QGIS Field Calculator indeed accepts $x and many other geometry functions. I do not think QGIS Raster Calculator ever supported x, y, column or row. But SAGA raster calculator does support position functions and can be used from within QGIS (Processing Toolbox):

xpos(), ypos() Get the x/y coordinates for the current cell

row(), col() Get the current cell's column/row index

Full description: http://www.saga-gis.org/saga_tool_doc/2.3.0/grid_calculus_1.html

Also have a look at Creating direction raster in QGIS?

1
  • Interesting. Thanks for pointing out the SAGA raster calculator. It did the job! Aug 13 '20 at 14:07
1

Not exactly what you are asking, but a way to get a longitudes or latitudes raster.

  • Use the gdal2xyz process to export the raster data as a CSV XYZ file:

1

For a single band, 1000x1000 pixels in Float64 format, the CSV file weighs 34 MB.

  • Import the CSV file to the map:

2

I don't like to work over this kind of spatial CSV, so export it as a Geopackage vector layer.

  • Rasterize (vector to raster) the vector layer, using the extent from the source raster data, burning the field_1 values to get longitudes or field_2 values to get latitudes:

3

That's all:

4

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.