A First Glimpse at pgRouting Layer for QGIS

PgRouting is great. But (Yes, there is always a “but”.) those queries are not easy to remember. That’s why I started work on a pgRouting GUI today. It’s actually a QGIS plugin which I’ll call “pgRouting Layer” and it is based on Pablo T. Carreira’s “Fast SQL Layer” plugin which can execute arbitrary SQL statements against a PostGIS or SpatiaLite database and add the results in a map layer.

This first prototype supports pgRouting’s shortest_path() function as described in “A Beginners Guide to pgRouting”. Once supplied with the necessary information about attribute field names, the plugin allows you to route between pairs of nodes. For convenience, the resulting layer is named after its start and end node.

some shortest paths using "pgRouting Layer" plugin

Besides normal routing capabilities, I’d like to develop this plugin towards a user friendly tool for catchment zone analysis. If you are interested in teaming up to work towards this goal, let me know.

  1. mattwigway said:

    I’m not sure how much I’ll be able to help (things are pretty hectic right now), but maybe a little. I’d love to be an alpha/beta tester; proper network analysis from the QGIS GUI is something I’ve been wanting for a long time.

    Is development in C/C++ or Python?

    • underdark said:

      Thanks Matt! The plugin is written in Python. Since the actual work load is handled by the database, I don’t think there is a reason to dive into C++.

  2. alexbruy said:

    Great work Anita! I’ll be happy to help with documentation and translation in Russian and Ukrainian

    • underdark said:

      Thanks Alex! Source code is now available at https://github.com/anitagraser/pgRoutingLayer. I’ve never done translations for plugins so I’d appreciate if you could set up whatever is necessary.

  3. Another interested tester/user, here. I am learning Python but would love to help as I can. I completely agree with mattwigway about having this in QGIS. Please let us know how we can help.

    • underdark said:

      Hi Joe,
      If you want to give pgRoutingLayer a try, you can download it from https://github.com/anitagraser/pgRoutingLayer. Please use the Github issue tracker to report any problems you encounter.

  4. Hi Anita, good work! Is the plugin available in a repository?

    • underdark said:

      Not yet. I’m planning to upload it this weekend. I’ll have to clean up first after yesterday’s hacking.

      • Ok, thanks! By the way… I guess that Peter (he was at the hackfest in Zurique) and his pal Saber would be very interested in working on catchments.

  5. Dassouki said:

    Hey Anita, I’ll be interested in helping out. this is the repo i created a year or so ago. https://github.com/dassouki/QgisPgRouting . Get your account and I can transfer the ownership to you

      • Dassouki said:

        No ill delete mine then.

  6. Ryan said:

    I would definitely be interested in helping out too. Although I haven’t created a plugin before, this would be a good project to work on. I think adding in buttons for choosing from-node and to-node via “point-and-click” would be an awesome addition.

    • underdark said:

      That would be great Ryan. You can simply fork the repository on Github and when you are done with your changes, you can send me a pull request. Choosing from and to node with clicks would be nice.

  7. Morten said:

    I have no knowledge on python, so I can not offer to participate in development. But I would like to test it. Some test data would be nice – the required data structure is not clear to me.

    • underdark said:

      Hi Morten,
      pgRouting doesn’t require a specific data structure. All you need is a table with the attributes listed in the GUI and docs, e.g. the order in which attributes appear does not matter. For test data, you can load some OSM file using osm2po as described in one of my latest posts.

  8. Saad said:

    How can i add this GUI in qgis…???

  9. Brian said:

    Hi there,
    I’ve downloaded and installed pgRoutingLayer, however as soon as I load the plugin in QGIS, this error comes up:

    “Couldn’t load plugin anitagraser-pgRoutingLayer-96e9f7c due an error when calling its initGui() method”

    I am using Mac OS 10.7 and QGIS v1.8. Any help would be appreciated.

      • Brian said:

        i hope this helps… thanks for your time.

        Couldn’t load plugin anitagraser-pgRoutingLayer-96e9f7c due an error when calling its initGui() method

        Traceback (most recent call last):

        import psycopg2
        File “/Applications/QGIS-1.8.app/Contents/MacOS/../Resources/python/qgis/utils.py”, line 283, in _import
        mod = _builtin_import(name, globals, locals, fromlist, level)
        ImportError: No module named psycopg2

        Python version:
        2.7.1 (r271:86832, Jun 16 2011, 16:59:05)
        [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)]

        QGIS version:
        1.8.0-Trunk Trunk, 48d0817

      • underdark said:

        Your Python installation lacks the psycopg2 module. I have no idea how to reinstall Python modules on MacOS, sorry.

  10. I’m having the exact same problem as “Brian” (OS X 10.6), however I’ve installed the psycopg2 Python module.

    I think the problem is that when I load QGIS, the pgRouting layer is looking at the wrong version of Python to use (based on file paths in the error message). Do you have idea how to get QGIS / pgRouting Layer to look for psycopg2 in these directories?


    Right now it is looking in these directories:



    • underdark said:

      Sorry, I have no OSX experience. My best guess would be to ask kyngchaos for help.

      • And indeed kyngchaos’ web site had the answer. In the QGIS readme file, I noticed the following:

        “Remember that QGIS is using the system Python, and it can’t use any other, so modules must be installed for the system Python.”

        So, you need to install the psycopg2 module from: http://www.kyngchaos.com/software/python
        which by default installs psycopg2 to the system version of Python (located in /Library/Python/2.6/site-packages ). Restart QGIS, and the error disappears.
        (OS X 10.6.8)

        Thanks for the tip-

  11. Hi again,

    After attempting to connect to my PostGIS database, I received the following error (OS X 10.6.8) from pgRouting Layer:

    An error has occured while executing Python code:


Traceback (most recent call last):

    File “/Users/kgoulding/.qgis/python/plugins/anitagraser-pgRoutingLayer-758f1f4/connectors/postgis.py”, line 150, in __init__

        raise DbError(e)

    DbError: missing “=” after “‘” in connection info string

    Python version:

    2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)]

    QGIS version:

    1.8.0-Trunk Trunk, 48d0817

    • underdark said:

      That’s interesting. I didn’t even write postgis.py myself. It was taken from “RT Sql Layer” plugin by Giuseppe Sucameli who in turn used “PostGIS Manager” by Martin Dobias. Do those plugins work for you?

      • Thanks for responding — No, those plugins do not work either! I get the same error: “Couldn’t connect to database: missing “=” after “‘” in connection info string”.

  12. Marco Giana said:

    Hi ya I am getting the same as Kevin. Pity would like to give it spin. I am running QGIS on windows 7.

    • underdark said:

      All three plugins mentioned above don’t work for you? That’s weird because I developed pgRouting Layer on Win7 and it definitely works. Are you sure about your PostGIS setup working correctly?

      • Funny; I sometimes don’t get the error (and pgRouting layer works!) — sometimes — if I leave the password field blank when connecting to my PostGIS database. Odd. When I enter the password it reliably spits out the aforementioned error. Let me know If I can help debug your plugin for Mac OS X-

      • underdark said:

        Thanks for your offer Kevin! I’m afraid I wouldn’t even know where to start looking for the error. Since PostGIS Manager plugin is affected too, Martin Dobias might be the best contact person.

  13. Hi Anita!

    Anita, i’ve forked your repository in github and I’ll try to add support for other routing methods (a-star and shooting star).

    I’ve been working on a Django application that will make heavy use of pgRouting functions, so I’ll also try to separate the logic code in another class.

    I’ll let you know when done.

    • underdark said:

      Great! Looking forward to testing any new features.
      Best wishes,

  14. Dear Anita,

    After some days following instructions in this blog and others, I managed to complete installation of postgreSQL, postGIS, pgrouting and pgRoutingLayer. I am a total beginner to Linux, so I wanted to thank you for your posts and your patience answering all the questions in this blog and in stackexchange.com.

    I still have everything to learn about these applications, so I will keep coming regularly to check your blog.

    Thanks again,


    • Thanks Tao! I really appreciate your feedback.

  15. Emmanuel said:

    Hi Anita,

    Thanks a lot for your tutorials, they’re really useful. I’d love to use pgrouting but, like others, postgis.py is not properly executed when I launch it. The same issue arises with PostGIS Manager and not entering the password never solves it. I know you haven’t written postgis.py yourself but in case you have had any thoughts about it recently, I would greatly appreciate any help. I am on Windows 7, with PostgreSQL 8.4 and PostGIS 1.5. Everything else works fine.

    Here is the log file:
    An error has occured while executing Python code:

    Traceback (most recent call last):
    File “C:/Users/e/.qgis/python/plugins\anitagraser-pgRoutingLayer-758f1f4\pgRoutingLayer.py”, line 84, in run
    self.db = self.actionsDb[dados].connect()
    File “C:/Users/e/.qgis/python/plugins\anitagraser-pgRoutingLayer-758f1f4\dbConnection.py”, line 128, in connect
    conn = ConnectionManager.getConnection( self.type ).connect( selected, self.parent() )
    File “C:/Users/e/.qgis/python/plugins\anitagraser-pgRoutingLayer-758f1f4\connectors\postgis.py”, line 132, in connect
    return Connection(uri)
    File “C:/Users/e/.qgis/python/plugins\anitagraser-pgRoutingLayer-758f1f4\connectors\postgis.py”, line 150, in __init__
    raise DbError(e)
    File “C:/Users/e/.qgis/python/plugins\anitagraser-pgRoutingLayer-758f1f4\connectors\postgis.py”, line 76, in __init__
    msg = unicode( error.args[0], ‘utf-8’)
    UnicodeDecodeError: ‘utf8’ codec can’t decode byte 0xe9 in position 42: invalid continuation byte

    Thanks a lot,

  16. John Steedman said:

    V. useful plugin. Many thanks and look forward to any extensions.

%d bloggers like this: