4

I'm working with QGIS 2.16.2 and I'm trying to use the MMQGIS hub lines plugin from the python console. For some reason the output of the process only connects a few point in the hubs shapefile with a few point in the spokes shapefile. Both shapefiles share point locations. 'centroids_distr1' contains a 'from_district' column with unique values from 1 to 12 and 'centroids_distr2' contains a 'to_district' column with repeated values from 1 to 12.

I'm new using python in QGIS so I don't know if it's a bug or it is that I am doing something the wrong way. This is the code lines I'm using:

import processing
processing.runalg('qgis:hublines', 'C:/BUS/centroids_distr1.shp', 'from_district', 'C:/Documents_AGP/TUS/centroids_distr2.shp', 'to_district', 'C:/BUS/HubLines_TEST.shp')

Is there anything wrong?

  • BTW, it works as expected when executed from the tool's interface. – DMS02 Sep 6 '16 at 7:39
3

You are not calling the algorithm from the MMQGIS plugin, instead you are calling a similar algorithm from the Processing plugin. Two separate and very useful plugins!

The algorithm from the Processing plugin does seem a little buggy as I can replicate your issue (might be a good idea to submit a bug report). An alternative could be to call the Distance to nearest hub algorithm which has an option to create lines (however it only takes the fields from the source layer).

So you could use something like:

import processing
processing.runalg('qgis:distancetonearesthub', 'C:/BUS/centroids_distr1.shp', 'C:/Documents_AGP/TUS/centroids_distr2.shp', 'from_district', 1, 0, 'C:/BUS/HubLines_TEST.shp')
  • 1
    Definitely I was doing something wrong. Thanks for making me notice. – DMS02 Sep 6 '16 at 10:39
  • 1
    @DMS02 - Most welcome, glad it was helpful :) – Joseph Sep 6 '16 at 10:44
  • I just checked Distance to nearest hub. It will only create lines linking each source point with its nearest destination point, and I want lines from each source point to all destination points. Anyway, the good news is that hub lines from the mmqgis plugin may still work. I'll search for that. – DMS02 Sep 6 '16 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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