Thursday 2 February 2017

Sequentially renumber point feature based on distance along line in QGIS


I have a point layer with features that are along a separate line layer, and have calculated the points' distance along the line using line_locate_point(aggregate('Line','collect',$geometry),$geometry). The values are stored in a virtual field named loc in the point layer.


Now I want to be able to sequentially renumber the points along the line based on loc. How can I do this purely in QGIS, without creating a new layer (i.e. so not using something like Sort and Number)?


I tried using scale_linear() but this is not the appropriate tool (see results below - I would like testno to go 1,2,3,4...)


enter image description here


I'm aware there is an easy solution in PostGIS using rank() OVER PARTITION BY but I'm using spatialite and I don't think rank() exists. I can't get it to work.



The quick and dirty alternative is to save loc as a proper field, save as shapefile and renumber appropriately by opening the .dbf in Excel, but I'd prefer to find a solution that works entirely in QGIS without extra plugins if possible.



Answer



If you are in QGIS 3 (3.0+), there is a new tool Add autoincremental field which will do the job.


Sometimes the output records are ordered in a weird way, so it is better to set Advanced parameters | Sort expression option.


If you are in QGIS 2.18 or earlier versions, common approach would involve SQL such as PostGIS or SpatiaLite. But unfortunately Virtual Layer will not work for your case as the data is stored in a virtual field.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...