4

For a letter drop experiment in Germany, I am using QGIS Network Analysis tools to calculate the shortest distances from letter drop points to surrounding postboxes. Using the "Shortest path (point to layer)" tool I am able to do this with a roads network layer and a postboxes layer. However, I have to select the drop points manually each time.

Is there any way to automate the selection of drop points?

Taras
  • 32,823
  • 4
  • 66
  • 137
  • 1
    In processing menu > History, you can see the last processing command lines executed. You can after test it directly from the QGIS Python console to verify if it works, and after, do a loop on the layer features (see here : https://anitagraser.com/pyqgis-101-introduction-to-qgis-python-programming-for-non-programmers/pyqgis-101-filtering-features/), extract geometry feat.geometry().x() for the x coordinate and pass it in the processing code line as parameter. – J. Monticolo Feb 15 '22 at 11:06
  • 1
    You can also right-click on the processing tool and select : "batch process", and compute automatically all the batch for each points of the letter drop layer. – J. Monticolo Feb 15 '22 at 11:08

1 Answers1

5

A solution using the PyQGIS.

Let's assume there are three layers, see the image below:

  • start points : three red dots in the 'random_points_test' layer
  • end points : three blue dots in the 'points_test' layer
  • a network : 16 features in the 'lines_test2' layer

input

Firstly it is important to understand the format of the starting point as an input in the geoalgorithm. To achieve this, use the following command processing.algorithmHelp("qgis:shortestpathpointtolayer") in the Python Console. This command is also useful to get more information about the geoalgorithm "Shortest path (point to layer)" parameters.

START_POINT: Start point
Parameter type: QgsProcessingParameterPoint

Accepted data types:
    - str: as an 'x,y' string, e.g. '1.5,10.1'
    - QgsPointXY
    - QgsProperty
    - QgsReferencedPointXY
    - QgsGeometry: centroid of geometry is used 

Secondly, proceed with Plugins > Python Console > Show Editor > New Editor and paste the script below

# Here names of input layers must be specified 
start_points = QgsProject.instance().mapLayersByName('random_points_test')[0]
end_points = QgsProject.instance().mapLayersByName('points_test')[0]
network = QgsProject.instance().mapLayersByName('lines_test2')[0]

Looping through all start point features

for feat in start_points.getFeatures(): start_point_id = feat["id"] start_point_geom = feat.geometry()

parameters = {
    'DEFAULT_DIRECTION' : 2,
    'DEFAULT_SPEED' : 50,
    'DIRECTION_FIELD' : '',
    'END_POINTS' : end_points,
    'INPUT' : network,
    'OUTPUT' : 'TEMPORARY_OUTPUT',
    'SPEED_FIELD' : '',
    'START_POINT' : start_point_geom,
    'STRATEGY' : 0,
    'TOLERANCE' : 0,
    'VALUE_BACKWARD' : '',
    'VALUE_BOTH' : '',
    'VALUE_FORWARD' : ''
    }

result = processing.run("qgis:shortestpathpointtolayer", parameters)['OUTPUT']
result.setName(f'Shortest path from point {start_point_id}') # changing the output name
QgsProject.instance().addMapLayer(result) # adding output to the map

Press Run script run script and get the output like this

result


References:

Taras
  • 32,823
  • 4
  • 66
  • 137