In QGIS 3.10 I want to order 22 polygons north to south based on their position using their centroids. I have used the centroids tool to determine where their middle is. But now I'm not sure now how to automate figuring out their north to south order.
QGIS – Ordering Polygons from North to South
centroidsfield-calculatororderpolygonqgis
Related Solutions
This is pretty straightforward, but a question first. Is the data in UTM format, decimal degrees or some-other? Doesn't matter, but if you understand that then the sorting will be obvious.
You will create a new column in the attribute table. It will be autofilled with data for the y coordinate. It can then be sorted. The final step would be to use a plugin MMQGIS to keep that sort order for the other software.
Firstly load the layer. Then open the attribute table. Turn on editing. That will make the Attribute table Field Calculator available.
Then you will create a new field (column). If your coodinate system is decimal degrees you must use
Decimal number (real)
with a precision of 5 or so. Regardless of the coordinate system when I do this I use real and width of 10 and precision of 10, cause it works with everything.Then in the expression box at the bottom you want
$y
to be in there. You type it straight in or select it from the tree of functions on the left, go to Geometry and it is there.OK that dialog and you should have a new column with the data. Then click on the column header to sort it. However that won't be permanent, even when you save it. But save the changes though and close the attribute table.
Load the plugin MMQGIS and use the menu item MMQGIS|MODIFY|SORT.
ciao
EDIT
My apologies.
OK, for polygons, use the tool in the vector menu first
VECTOR|GEOMETRY TOOLS|POLYGON CENTROIDS
which will create a new layer. Then do as above.
If you understand correctly you can create a grid, you choose a size that covers the smallest polygon ("vector grid" in toolbox) and then extracts the raster value using the grid, so use the tool "add raster values to features" in qgis toolbox. So you have a grid with raster values. Then you can extract the value of the raster using the centroid of the polygon (to compare)
We just have to think of a way to find the "luminous centroid of the polygon", I think you can do a thematic classification using a color ramp with grid values. This way you will have the most luminous points.
To get the brightest point of each polygon, you have to trim the grid with the values using the polygons and then sort them individually. then see what brightest value, filter it and save it as the centroid. I do not know if there is a fully automatic way of doing this.
Best Answer
You can create a new field with this expression :
Using features ID instead of a list of Y coordinates is to eliminate the case when 2 features have exactly the same Y coordinate (even if it isn't very probable).
The expression
y(centroid($geometry))
can be directly written asy($geometry)
because of the definition of they()
function :