[QGIS-Developer] Reusing network graph

Prem Kumar prem.netgis at gmail.com
Mon Jan 3 08:53:34 PST 2022


Hi Felix,

Appreciate your alternative solution. I could make some degree of
performance improvement.

Thanks a lot for your help, mate.

Prem

On Fri, 31 Dec 2021 at 7:15 PM, flatoram <felix.vons at gmail.com> wrote:

> Hi Prem,
>
> I experienced those performance issues too.
> When i process a lot of routing operations between points I will define an
> routing area first on the source network layer (e.g. osm_lines).
> I create a tempory layer sometimes in this way with pyqgis:
>
> # define bounding box and make it a big larger (just an example)
> rectangle = QgsRectangle(point_0, point_1)
> rectangle = rectangle.scaled(2)   # depends on your use case
>
> # build request
> request = QgsFeatureRequest().setFilterRect(rectangle)
> # request = request.setNoAttributes()  # when you do not need any
> attributes
> # request = request.setSubsetOfAttributes(["speed"], layer.fields())  #
> use only some fields
> # add conditions to request to use only features you need, e.g. ignore
> specific street types
> new_layer = layer.materialize(request)  # create a memory copy of source
> layer
>
> In my use cases the new_layer is definitive smaller and more perfomant
> than the main layer for those operations. Use the new layer for routing.
> But maybe you have not the full available path in your new layer, when your
> bbox was to small.
>
> An other possibility for path finding is QgsTracer class, but it is
> different to classic dijsktra/shortestpath when dealing with vertices or
> line intersections
>
> # use qgis tracer, but it is a bit different from classic dijkstra
> tracer = QgsTracer()
> tracer.setLayers([network_layer])
> # maybe you have to define the destinationCrs
> poly_line = tracer.findShortestPath(start_point, end_point)[0]
>
>
> Depends on the use cases I saved a lot of calculation time.
>
> - Felix
>
> Am Do., 30. Dez. 2021 um 21:01 Uhr schrieb <
> qgis-developer-request at lists.osgeo.org>:
>
>> Send QGIS-Developer mailing list submissions to
>>         qgis-developer at lists.osgeo.org
>>
>> To subscribe or unsubscribe via the World Wide Web, visit
>>         https://lists.osgeo.org/mailman/listinfo/qgis-developer
>> or, via email, send a message with subject or body 'help' to
>>         qgis-developer-request at lists.osgeo.org
>>
>> You can reach the person managing the list at
>>         qgis-developer-owner at lists.osgeo.org
>>
>> When replying, please edit your Subject line so it is more specific
>> than "Re: Contents of QGIS-Developer digest..."
>>
>>
>> Today's Topics:
>>
>>    1. Reusing network graph (Prem Kumar)
>>
>>
>> ----------------------------------------------------------------------
>>
>> Message: 1
>> Date: Thu, 30 Dec 2021 19:16:41 +0530
>> From: Prem Kumar <prem.netgis at gmail.com>
>> To: qgis-developer <qgis-developer at lists.osgeo.org>
>> Subject: [QGIS-Developer] Reusing network graph
>> Message-ID:
>>         <CAP2_p600CpiF=
>> eJfRsCKby7yB1siX4vW7faWbCKSrROtB9cLUg at mail.gmail.com>
>> Content-Type: text/plain; charset="utf-8"
>>
>> Hi Team,
>>
>> I have been trying to get the shortest path for a lot of pairs of start
>> and
>> end points for a given road layer. Although I'm able to achieve this by
>> creating the *QgsVectorLayerDirector *and Graph object for every pair of
>> start and end points.
>>
>> However, I could not reuse the graph object or director object for all the
>> pairs of start and end points, eventually the code is as slow as executing
>> the* 'native:shortestpathpointtopoint'* processing tool
>> programmatically for each pair of start and end points.
>>
>> Essentially, I'm trying to improve the performance by reusing the graph
>> objects. Can someone help me whether there is a possibility to reuse the
>> graph object for large sets of pairs? Here is the code snippet I have
>> used.
>>
>> vectorLayer =
>> QgsVectorLayer('testdata/network.gpkg|layername=network_lines',
>> 'lines')director = QgsVectorLayerDirector(vectorLayer, -1, '', '', '',
>> QgsVectorLayerDirector.DirectionBoth)strategy =
>> QgsNetworkDistanceStrategy()director.addStrategy(strategy)builder =
>> QgsGraphBuilder(vectorLayer.sourceCrs())startPoint =
>> QgsPointXY(1179661.925139,5419188.074362)endPoint =
>> QgsPointXY(1180942.970617,5420040.097560)tiedPoints =
>> director.makeGraph(builder, [startPoint, endPoint])tStart, tStop =
>> tiedPointsgraph = builder.graph()idxStart =
>> graph.findVertex(tStart)idxEnd = graph.findVertex(tStop)(tree, costs)
>> = QgsGraphAnalyzer.dijkstra(graph, idxStart, 0)
>>
>> -Prem
>> -------------- next part --------------
>> An HTML attachment was scrubbed...
>> URL: <
>> http://lists.osgeo.org/pipermail/qgis-developer/attachments/20211230/acf709c0/attachment-0001.html
>> >
>>
>> ------------------------------
>>
>> Subject: Digest Footer
>>
>> _______________________________________________
>> QGIS-Developer mailing list
>> QGIS-Developer at lists.osgeo.org
>> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>>
>>
>> ------------------------------
>>
>> End of QGIS-Developer Digest, Vol 194, Issue 18
>> ***********************************************
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20220103/cd64c88e/attachment.html>


More information about the QGIS-Developer mailing list