1

When you create a grid in QGIS, in the attribute table for each square, you have a corresponding column left, right, top, bottom in coordinates. Which of these four correspond to the X, Y, Z, M coordinates?

I am trying to import a grid from a CSV file with the left, right, top, bottom columns. I had to export the grid because I couldn't figure out how to add the values from multiple columns correctly in the field calculator in QGIS. So I exported it into a CSV, added the columns in Excel, now I want to bring the file back into QGIS.

Alternatively, how do I create a new column in the attribute table that adds the values in multiple columns by using the field calculator? I tried "Col1"+"Col2"+"Col3" and sum(col1,col2,col3) but couldn't get completely correct values for some reason. In the first case it only seemed to be adding some of the columns.

enter image description here

enter image description here

3
  • 1
    Create a grid from "Create grid" tool? Left and right is X, Top and bottom Y. – BERA Jul 23 '20 at 7:21
  • 4
    We definitely need more details. Currently any attempt to answering is just wild guessing, since we don't know, how your csv exactly looks etc. – Erik Jul 23 '20 at 7:28
  • 1
    Added pictures of attribute table and grid – MapDeath Jul 23 '20 at 17:37
2

Briefly saying there is no match, but you can make some of it yourself.

The resulting Attribute table after using the Create Grid contains five fields, namely:
"id", "left", "top", "right", and "bottom", see an image below.

example

where:

  • "id" is simply an integer starting from 1 until n-th cell. The order of cells is from the top-left feature to the bottom left each time moving one column right an so on, something like following the И-pattern.
  • "left" corresponds to the most western "X"-coordinate
  • "top" to the most northern "Y"-coordinate
  • "right" to the most eastern "X"-coordinate
  • "bottom" to the most southern "Y"-coordinate

coordinates

If your question was interpreted correctly, there are several possibilities to get the coordinates for each cell.

  1. Coordinates as a centroid of each cell:
SELECT "id", ST_X(st_centroid(geometry)) AS "X", ST_Y(st_centroid(geometry)) AS "Y"
FROM "Grid"

case_1

  1. Coordinates as a centroid of each cell in WKT-format:
SELECT "id", st_astext(st_centroid(geometry))
FROM "Grid"

case_2

  1. Coordinates as a polyline in WKT-format:
SELECT "id", st_astext(st_exteriorring(geometry))
FROM "Grid"

case_3

  1. Coordinates as a polygon in WKT-format:
SELECT "id", st_astext(geometry)
FROM "Grid"

case_4

All Options are based on the usage of a Virtual Layer through Layer > Add Layer > Add/Edit Virtual Layer....

So, my suggestion on this stage before exporting a grid in CSV-file is to create a new column in the Attribute table where you will store the geometry in a WKT-Format i.e. in the Field calculator use geom_to_wkt($geometry) for a text-field with unlimited length, see image below.

geom_wkt

So, afterwards you will be able freely read the wkt-geometry when importing a CSV-file into QGIS-Project, part of in described here Loading WKT polygons into QGIS.

If suddenly you have no opportunity to read a WKT from a CSV-file in QGIS, put eye on the following workflow.

  1. Drag&Drop your csv with grids into QGIS

    csv_in_qgis

  2. Deploy a Virtual Layer through Layer > Add Layer > Add/Edit Virtual Layer... to obtain a layer with geometry.

SELECT *, setsrid(make_polygon(make_line(
                                    make_point(left,bottom),
                                    make_point(left,top),
                                    make_point(right,top),
                                    make_point(right,bottom)
                                )), #here use your SRID, e.g. 25833)    
FROM "test"
  1. Get the Output

    result

Regarding the "Z" and "M" coordinates. If you have those Attributes in you CSV-file then extend the make_point(left,bottom) to make_point(left,bottom, "alltitude_field", "m_field"), e.g. make_point(1,2,3,4), as described in PostGIS Docs | ST_MakePoint.

1
  • 1
    I knew that the LRTB columns corresponded to the sides of each grid cell, but I am assuming the code you gave will help build a better CSV for re-import. Will try soon – MapDeath Jul 29 '20 at 3:02

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