Creating HTML labels in QGIS

QGIS map tips allow you to hover the mouse cursor over a feature in order to create a popup that displays information. This information is normally a data field, but you can also display other types of information using a subset of HTML tags. In this recipe, we'll create an HTML map tip that displays a Google Street View image at the feature's location.

Getting ready

In your qgis_data directory, create a directory named tmp.

You will also need to download the following zipped shapefile and place it in your qgis_data/nyc directory:

https://geospatialpython.googlecode.com/files/NYC_MUSEUMS_GEO.zip

How to do it...

We will create a function to process the Google data and register it as a QGIS function. Then, we'll load the layer and set its map tip display field. To do this, we need to perform the following steps:

  1. First, we need to import the Python libraries we'll need:
    from qgis.utils import qgsfunction
    from qgis.core import QGis
    import urllib    
    
  2. Next, we'll set a special QGIS Python decorator that registers our function as a QGIS function. The first argument, 0, means that the function won't accept any arguments itself. The second argument, Python, defines the group in which the function will appear when you use the expression builder:
    @qgsfunction(0, "Python")
    
  3. We'll create a function that accepts a feature and uses its geometry to pull down a Google Street View image. We must cache the images locally because the Qt widget that displays the map tips only allows you to use local images:
    def googleStreetView(values, feature, parent):
    x,y = feature.geometry().asPoint()
    baseurl = "https://maps.googleapis.com/maps/api/streetview?"
    w = 400
    h = 400
    fov = 90
    heading = 235
    pitch = 10
    params = "size=%sx%s&" % (w,h)
    params += "location=%s,%s&" % (y,x)
    params += "fov=%s&heading=%s&pitch=%s" % (fov, heading, pitch) 
    url = baseurl + params
    tmpdir = "/qgis_data/tmp/"
    img = tmpdir + str(feature.id()) + ".jpg"
    urllib.urlretrieve(url, img)
    return img
    
  4. Now, we can load the layer:
    pth = "/qgis_data/nyc/nyc_museums_geo.shp"
    lyr = QgsVectorLayer(pth, "New York City Museums", "ogr")
    
  5. Next, we can set the display field using a special QGIS tag with the name of our function:
    lyr.setDisplayField('<img src="[% $googleStreetView %]"/>')
    
  6. Finally, we add it to the map:
    QgsMapLayerRegistry.instance().addMapLayer(lyr)
    
  7. Select the map tips tool and hover over the different points to see the Google Street View images.

How it works...

The key to this recipe is the @qgsfunction decorator. When you register the function in this way, it shows up in the menus for Python functions in expressions. The function must also have the parent and value parameters, but we didn't need them in this case.

The following screenshot shows a Google Street View map tip:

How it works...

There's more...

If you don't need the function any more, you must unregister it for the function to go away. The unregister command uses the following convention, referencing the function name with a dollar sign:

QgsExpression.unregisterFunction("$googleStreetView")
..................Content has been hidden....................

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