I am developing a script which uses OpenStreetMap data to calculate the distance (over the road network) between each building and the nearest hospital.

To do this, I mainly follow the official tutorial http://www.qgis.org/en/docs/pyqgis_developer_cookbook/network_analysis.html

My problem comes when calling QgsGraphBuilder. Because my buildings obviously don't lie exactly on the road network, I want to specify a tolerance value when creating the builder.

However, with

builder = QgsGraphBuilder(crs, True, 200.0, "WGS84")

it doesn't "snap" my buildings to the network... Is there anything wrong with the way I call the function QgsGraphBuilder? I haven't seen any example which actually implements this tolerance parameter...


I actually also tie all my buildings'centroids to the network:

builder = QgsGraphBuilder(crs, True, 200.0, "WGS84")

all_points = [] # A list that will contain the coordinates of all objects to be tied to the network
all_points.append(hospital) # I add the coordinates of the hospital

for building in coord_buildings.values():
    all_points.append(building) # I add the coordinates of all the buildings'centroids

    tiedPoints = director.makeGraph( builder, all_points ) # I tie the buildings'centroids to the graph

    graph = builder.graph() # I build the graph

    startId = graph.findVertex(hospital) # I take the id of the hospital in the graph

    print startId # Displays -1

If I understand correctly, this means that my hospital doesn't match to anything in the network because it hasn't been tied properly... maybe because of the tolerance value?

By the way, my data is in a PostGIS database, unprojected (EPSG 4326), in case it makes any difference


I just saw that some buildings are actually tied to the network when I use only one parameter

builder = QgsGraphBuilder(crs)


builder = QgsGraphBuilder(crs, True, 0.0)

But rougly 1000 are still not tied. So probably really to do with the tolerance factor?

When I specify the other parameters

builder = QgsGraphBuilder(crs, True, 200.0, "WGS84")

the dijkstra process fails (the tree only contains -1 values)

1 Answer 1


This doesn't actually answer the question but it still solved my problem:

What I did was "simply" to snap my points (hospitals) to the road network within PostGIS beforehand. Now that all my points lie exactly on the network, I no longer need to worry about the tolerance.

This is not very elegant though, so if anyone has a "real" solution, I would still be interested to read it! :-)

  • 1
    Well, My opinion is that your base data has to be correct before you start to network analysis. Process should be 1. Collect data 2. Correct data for archive/network analysis purpose 3. Save corrected data as your "master" data 4. Do analysis 5. report findings 6. Archive used version. When underlying data is updated start from 1. Do not do 1. 3. And create network analysis in some program and different format than "master" data, that causes data fragmentation. But that is just my opinion Feb 17, 2014 at 10:01
  • While I would in general agree with you, the process in my case is quite different. I don't want to save the corrected data as the master data, because it is no longer 100% correct data! I have to snap the buildings to the road network so that the routing can work out but it obviously doesn't mean that the buildings have moved in the real world! :-) Anyway, this issue is here not so important because I am not storing long-time data. Summary could be as such: 1) Download OSM data 2) Pre-process the data 3) Do the analysis / routing 4) Analyse results 5) Drop the data and keep only the results Feb 17, 2014 at 13:52
  • Agree that in this case it is ok do that, but my comment was more in general. If your data needs to have network topology is better to have 99% correct topology for analysis than network you cant use. (i do work with district heating / water systems) Feb 18, 2014 at 7:27

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.