6

Using Qgis 2.18.3, I am looking for the quickest method of calculating the aggregated distance between multiple points. As the Crow flies would be enough...

I have a point layer in a small area. They have unique and incremented ID's. (in my case these are stops of a truck)

I was using the Points to path plugin to create lines. Then using $length to calculate the length and then basic statistic to aggregate the length. I could build a model to automate this a little. The thing is that I have hundreds of those seperate point layers...

Is there an other method which is quicker?

2 Answers 2

4

You could also make a shell script for all your files so you don't have to sit around waiting for QGIS to be ready. In this example the points are shape files and saga_cmd and ogr2ogr is used.

#!/bin/sh

for i in *.shp

do
    f=$(echo $i| cut -d'.' -f 1) 
    saga_cmd shapes_lines 1 -LINES ${f}_line.shp -POINTS ${f}
    ogrinfo ${f}_line.shp -sql "ALTER TABLE ${f}_line ADD COLUMN length integer(5)"
    ogrinfo ${f}_line.shp -dialect SQLite -sql "UPDATE ${f}_line SET length = ST_Length(geometry)"
done

Then you'll get polyline shape file for each point file with a new column showing length of the line. You could also add options to saga_cmd with order of points etc.

3

You could create a custom script (or a model but I prefer script) which runs the Field Calculator and the Convert points to lines to create an output of a single line joining all points with a Length field containing the aggregated length.

You can create one from:

Processing Toolbox > Scripts > Tools > Create new script

Then use something like the following:

##Example=name
##Point_layer=vector point
##ID_Field=Field Point_layer
##Result=output vector

output_fieldCalculator = processing.runalg("qgis:fieldcalculator", Point_layer, 'Separate', 1, 10, 0, True, '1', None)
output_pointToLine = processing.runalg("saga:convertpointstolines", output_fieldCalculator['OUTPUT_LAYER'], ID_Field, "Separate", None)
output_fieldLength = processing.runalg("qgis:fieldcalculator", output_pointToLine['LINES'], 'Length', 0, 10, 2, True, '$length', Result)

Make sure to save the script into your /.qgis2/processing/scripts directory.


Example:

When you run the script, you can select the point layer and the ID field:

Script

Result:

Example


Note: The "Separate" field is only used for the SAGA tool to create the line correctly.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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