In the map shown below, I have manually created the dummy lines (in green). Using python, and without using PostGIS or sextante, I would like to automate the creation of these dummy links between a point and a line layer, based on the following criteria:

  • Depending on the polygon size, I should be able to add an X amount of dummy links between the centroid (point) and its nearest projection(s) onto the roadway layer.
  • The script so that the largest polygons get the most amount of dummy links and the smallest get the least amount.
  • The script should connect the newly created dummy link to nearest highway based on a classifications scheme, i.e. connect to "busier" highways first
  • If a polygon does not have any roads/links in it, the script should allow the centroid of that polygon to connect to roads outside of its boundaries within Y units of distance
  • The dummy links must be connected to the roadway layer in such a way that this roadway layer could be potentially used for routing including the dummy links.
  • Therefore, the dummy link layer must have the same attributes as the road layer
  • Create a new layer with all the new dummy links

Question: Is this possible using Qgis and Python alone? any pointers as to where to start or can you help in providing a pseudocode to assist me to solve this problem?

enter image description here

1 Answer 1


I'd add my answer as a comment, but since I can't comment (no reputation enough), I'll add as an answer. It seems that you are trying to add centroid connectors to feed into a traffic assignment algorithm. Right? I should say that doing it automatically is not considered best practice in this industry, but it you are going to do it, I'd like to suggest you to make it proportional to the amount of network comprised within the polygon (roadway stock) and the displacement of this network as well.

I'll also mix some of the transportation modelling jargon with the GIS jargon...

First of all, are you considering splitting links or just connecting the new centroid connectors (dummy links) to the existing extremities of the links? Since I don't know what is the case, I'll assume that you want to connect them to the existing extremities. And it DOES make a huge difference in the algorithm if anybody is wondering.

Now is where I think there are two major questions you need to think about: 1st - How to weigh the criteria closest links versus busiest links 2nd - How to spread the connectors through the TAZ so you don't have them all clustered together?

I'll not answer thse questions, so when it comes to the part of the algorithm to actually add centroids, I'll add a single one.

The algorithm I am suggesting is:

  1. Compute the number of connectors to be added in each polygon (for each TAZ)
  2. Associate this number of connectors with the centroids file
  3. Extract all nodes from the road network (link extremities) to a new layer
  4. Tag TAZ (polygon) ID to each one of these nodes
  5. Loop through the centroids adding the connectors to a new layer

    Now let's put some codes to these steps (I hope the variables names are self explanatory):

Steps 1 & 2:

Let's assume there is TAZ field in both the centroids layer and the TAZ (polygon) layer. Something like this:


   features = taz_layer.getFeatures()
   for f in features:
        geom = f.geometry()
        area= geom.area()

   features = centroids_layer.getFeatures()
   for f in features:
        a=centroids_layer.dataProvider().changeAttributeValues({f.id(): {idx:connec[taz_id]}})


Step 3:

    #Add node layer as suggested in the PyQGIS cookbook
    writer = QgsVectorFileWriter("node_layer.shp", "CP1250", fields, \
         QGis.WKBPoint, None, "ESRI Shapefile")

    for feat in line_layer.getFeatures():
            new_feat = QgsFeature()
            new_feat.setAttributes(["how busy is the link", "name of the street", "etc", "etc])"#Here you need to capture and add all the characteristics of the links you want to consider when adding connectors.

Step 4:

#This code is very slow, but will suffice #You should look into using spatial index, but this is what I could write by heart...

    for feat in node_layer.getFeatures(): #The extremities
        for feature in taz_layer.getFeatures():
             enclosed = feature.geometry().contains(feat.geom())
             if enclosed:
                 a=node_layer.dataProvider().changeAttributeValues({feat.id(): {idx2:taz_id}})


Step 5:

     #Add link layer as suggested in the PyQGIS cookbook
    writer = QgsVectorFileWriter("node_layer.shp", "CP1250", fields, \
         QGis.WKBLinestring, None, "ESRI Shapefile")

    index = QgsSpatialIndex()
    for feature in node_layer.values():

    for feature in centroids_layer.values():
        pt = QgsPoint(geom.asPoint())
        nearest = index_from.nearestNeighbor(pt, 100)  #100 is a guess, you might need to do more...

        #Nearest contains the IDS of the nodes, so it is wise to build dictionaries or numpy arrays with all the relevant criteria (TAZ, if it is busy or not, etc) before you even start looping through centroids
        for i in nearest:  
           if criteria1(i) and criteria2(i) and ... and criterian(i):
              P=[pt, node_i.geometry().asPoint()]
              new_feat = QgsFeature()

Well... It ended up being an answer longer than I had anticipated, but it should get you started... And just as a warning, I did NOT test this code, so it is likely to have typos and other mistakes...

  • Thanks Pedro, I do lots of Transit and Traffic plans, but now moving to doing a lot of it through Python, ArcGIS, and QGIS. I will try and implement your answer in the next few weeks. To answer your questions in your post: I'm testing a traffic demand model on 1,000 small zones to see how it performs, and then I'll modify the dummy links manually where I see fit.
    – dassouki
    Aug 6, 2014 at 16:52
  • Good Luck! It occurred to me that you can divide the TAZ in equal slices (like a pie chart) and restric the existence of connectors to one per slice. It gets more complicated, but there will be no clustering of connectors Aug 6, 2014 at 19:42

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .