Adding a layer to geojson.io

Cloud services have become common and geospatial maps are no exception. This recipe uses a service named geojson.io, which serves vector layers online, which you can upload from QGIS using Python.

Getting ready

For this recipe, you will need to install the qgisio plugin using the QGIS Plugin Manager.

You will also need a shapefile in a geodetic coordinate system (WGS84) from https://geospatialpython.googlecode.com/svn/union.zip.

Decompress the ZIP file and place it in your qgis_data directory named shapes.

How to do it...

We will convert our shapefile to GeoJSON using a temporary file. We'll then use Python to call the qgisio plugin in order to upload the data to be displayed online. To do this, we need to perform the following steps:

  1. First, we need to import all the relevant Python libraries:
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    from qgis.core import *
    from tempfile import mkstemp
    import os
    from qgisio import geojsonio
    
  2. Now, we set up the layer and get the layer's name:
    layer = QgsVectorLayer("/qgis_data/shapes/building.shp", "Building", "ogr")
    name = layer.name()
    
  3. Next, we establish a temporary file using the Python tempfile module for the GeoJSON conversion:
    handle, tmpfile = mkstemp(suffix='.geojson')
    os.close(handle)
    
  4. Now, we'll establish the coordinate reference system needed for the conversion, which must be WGS84 Geographic, to work with the cloud service:
    crs = QgsCoordinateReferenceSystem(4326,
    QgsCoordinateReferenceSystem.PostgisCrsId)
    
  5. Next, we can write out the layer as GeoJSON:
    error = QgsVectorFileWriter.writeAsVectorFormat(layer, tmpfile,
        "utf-8", crs, "GeoJSON", onlySelected=False)
    
  6. Then, we can make sure that the conversion didn't have any problems:
    if error != QgsVectorFileWriter.NoError:
      print "Unable to write geoJSON!"
    
  7. Now, we can read the GeoJSON content:
    with open(str(tmpfile), 'r') as f:
      contents = f.read()
    
  8. We then need to remove the temporary file:
    os.remove(tmpfile)
    
  9. We are ready to upload our GeoJSON to geojson.io using the qgisio module:
    url = geojsonio._create_gist(contents, "Layer exported from QGIS", name + ".geojson")
    
  10. We can then use the Qt library to open the map in a browser:
    QDesktopServices.openUrl(QUrl(url))
    

How it works...

This recipe actually uses two cloud services. The GeoJSON data is stored on a https://github.com service named Gist that allows you to store code snippets such as JSON. The geojson.io service can read data from Gist.

Note

Note that sometimes it can take several seconds to several minutes for the generated URL to become available online.

This screenshot shows the building layer on an OSM map on geojson.io, with the GeoJSON displayed next to the map:

How it works...

There's more...

There are additional advanced services that can serve QGIS maps, including www.QGISCloud.com and www.CartoDB.com, which can also display raster maps. Both of these services have free options and QGIS plugins. However, they are far more difficult to script from Python if you are trying to automate publishing maps to the Web as part of a workflow.

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

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