[GIS] Grid to Matrix in QGIS

grids-graticulesqgis

I generated a rectangular grid using the vector tools available and added a attribute in this grade (sum of lentghs of a line).
Now I want to convert this rectangular grid to a matrix (if possible in a text, because I would put it in my thesis, so I wouldn't need to type cell by cell), in which the values of the cells would be that attribute.
I couldn't find a tool in QGIS to do that.
I'm using QGIS 3.8.3 Zanzibar.
This is the grid that I have generated, I did set the style according to a attribute value.

This is the attribute table.
I want to extract the values of the last column in a matrix form, in which the value of each cell would be the value of the attribute in that cell.

As suggested by Gabriel De Luca, I exported the vector in a CSV format and opened it in LibreOffice Calc. Now I'm closer to what I need. I have the column with 2500 rowns, but need to convert it to a 50×50 matrix.

Best Answer

As you can see, you can Export the attribute table as a CSV file in a simple way in QGIS. But the problem turns to sort the values in LibreOffice. That task slightly exceeds our reach.

But I kept thinking that there really must be a way to export the data exactly as you wish, which is in the form of an array. And what has an array shape is a raster, so it seems to me that the first thing we should do is rasterize those polygons.

1

Here I had some polygons created from another test I was doing. You will notice that they are the result of a polygonization, but it is not a problem, let's rasterize them again.

My polygons have a field called DM that contains the values that I want to burn in the pixels of a raster.

We do not want to go crazy with the coordinates and the resolution, so all we know is how many pixels the image should be wide by how many high (5x5 in my case), and we will ask the raster to assume the extent of the polygon layer (by pressing the three dots button in the Output Extent section, and choosing the Use Layer Extent option).

2

The result is a raster that we can export to GeoTIFF format. Rendering is not a problem, the important thing is to notice that the values of the DN field are burned in each pixel.

3

We can now use the gdal_translate command (https://gdal.org/programs/gdal_translate.html) to convert the GeoTIFF file to a format that is almost exactly what we are looking for, an ASCII grid (https://gdal.org/drivers/raster/aaigrid.html).

My way of doing this is to run the OSGeo4W console, navigate to the folder that has the rasterized file, and run the following command:

gdal_translate -of AAIGrid Rasterized.tiff Rasterized.csv

Now, it only remains to import it into LibreOffice Calc, separating cells delimited by space, and remove the rows from the header to have the values grid.

4

Related Question