Routing along streets

Sometimes, you may want to find the best driving route between two addresses. Street routing has now become so commonplace that we take it for granted. However, if you explore the recipes on geocoding and network analysis in this book, you will begin to see what a complex challenge street routing truly is. To perform routing operations in QGIS, we'll use the QGIS GeoSearch plugin, which is written in Python, so that we can access it from the console.

Getting ready

You will need to install the QGIS Python GeoSearch plugin for this exercise in order to do the routing, as well as the QGIS OpenLayers Plugin to overlay the result on a Google map, as follows:

  1. From the QGIS Plugins menu, select Manage and Install Plugins….
  2. If you have the QGIS GeoCoding Plugin installed, then you must uninstall it, as sometimes it conflicts with the GeoSearch plugin. So, select this in the plugin list and click on the Uninstall plugin button.
  3. In the Plugins dialog search box, search for GeoSearch.
  4. Select the GeoSearch plugin and click on the Install plugin button.
  5. Next, in the Plugins search dialog, search for OpenLayers.
  6. Select the OpenLayers plugin and click on the Install plugin button.

How to do it...

We will invoke the GeoSearch plugin's routing function, which uses Google's routing engine, and display the result over a Google map from the OpenLayers plugin. To do this, we need to perform the following steps:

  1. In the QGIS Python Console, we first import the QGIS utils library as well as the required portions of the GeoSearch plugin:
    import qgis.utils
    from GeoSearch import geosearchdialog, GoogleMapsApi
    
  2. Next, we'll use the QGIS utils library to access the OpenLayers plugin:
    openLyrs = qgis.utils.plugins['openlayers_plugin']
    
  3. The GeoSearch plugin isn't really designed for programmatic use, so in order to invoke this plugin, we must invoke it through the GUI interface, but then we need to pass blank values so that it doesn't trigger the GUI plugin interface:
    g = geosearchdialog.GeoSearchDialog(iface)
    g.SearchRoute([])
    
  4. Now, using the following code, we can safely create our routing engine object:
    d = GoogleMapsApi.directions.Directions()
    
  5. Next, we create our origin and destination addresses:
    origin = "Boston, MA"
    dest = "2517 Main Rd, Dedham, ME 04429"
    
  6. Then, we can calculate the route using the simplest possible options, as shown here:
    route = d.GetDirections(origin, dest, mode = "driving",  waypoints=None, avoid=None, units="imperial")
    
  7. Now, we use the OpenLayers plugin to add the Google Maps base map to the QGIS map:
    layerType = openLyrs._olLayerTypeRegistry.getById(4)
    openLyrs.addLayer(layerType)
    
  8. Finally, we use the GeoSearch plugin to create a QGIS layer on top of the base map for our route:
    g.CreateVectorLayerGeoSearch_Route(route)
    

How it works...

Even though they are built in Python, neither the GeoSearch nor OpenLayers plugins are designed to be used with Python by a programmer. However, we are still able to use the tools in a script without much trouble. To take advantage of some of the routing options available with the GeoSearch plugin, you can use its GUI to see what is available and then add those options to your script. Beware that most plugins don't have a true API, so a slight change to the plugin in a future version can break your script.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset