Rendering web images as markers in QGIS


So here is an idea.  Say you have a point layer that has a link to a static image from a web cam. Lets say it is a traffic camera for this use case.

Selection_0272016-02-04 21_30_36-QGIS 0a64c16

We can use that feed to see the image in a browser. Cool. But what would be even cooler is if we could get the images into QGIS as markers without having to download each image each time for a update ourselves.

Turns out it’s pretty easy – could be easier no doubt but lets just go with this route for now.  For this you will need: a custom expression function, and data defined SVG path locations. (We have to use SVG markers because QGIS doesn’t have image markers just yet)

Lets write that custom expression function.  We need a SVG marker so lets set that for the layer and also edit the data defined path

svg.png

Hit the New file button and define a new function called show_camera. 

import requests
import base64

@qgsfunction(args='auto', group='Custom')
def show_camera(feed, feature, parent):
    svg = """
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg>
  <g>
    <image xlink:href="data:image/jpeg;base64,{0}" height="256" width="320" />
  </g>
</svg>
"""
    data = requests.get(feed, stream=True).content
    name = feed[-16:]
    b64response = base64.b64encode(data)
    newsvg = svg.format(b64response).replace('\n','')
    path = r"C:\temp\camera\{0}.svg".format(name)
    with open(path, 'w') as f:
        f.write(newsvg)
    return path.replace("\\", "/")

This will take the image feed, load it with the Python request library, base64 the image, stick the data in to a SVG, save the SVG to disk, and return the path to the new SVG.

We have to save to disk because QGIS can’t load SVGS from strings, although that would be a cool feature. I am also using base64 because a linked file SVG didn’t render in QGIS.

Once we have defined that we can use the function like so

show_camera( "url" )

looking at the output preview we have

'C:/temp/camera/MRMETRO-1216.jpg.svg'

Which looks right. Hit OK on the expression.

Before hitting Apply just make the size of the marker a little bigger as it will make the image to small to see, I used 35 for the demo. You should also pick a SVG marker from the list that is used as the default one if there is no valid path returned.

Hit Apply. Magic :)

qgis

The images are getting download and saved into C:\temp\camera for this demo but you could add some more magic around that.

Something to note is that each time you refresh the map you are making a bunch of web requests to get the new images. You could avoid this by tracking the image times and only grabbing new ones if a set time has elapsed.  I will leave that up the reader, however here is a link to a Gist with the code if you wish to fork it

Big hat tip to QGIS and its multi threaded rendering that still allows you to continue to work while it renders and downloads the images in the background.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s