Skip to content

Instantly share code, notes, and snippets.

@NathanW2
Last active February 12, 2018 18:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save NathanW2/1933f13bebe548db1104 to your computer and use it in GitHub Desktop.
Save NathanW2/1933f13bebe548db1104 to your computer and use it in GitHub Desktop.
Web images as QGIS markers
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("\\", "/")
@mhugo
Copy link

mhugo commented Feb 4, 2016

Nice trick here ! :)

@NathanW2
Copy link
Author

NathanW2 commented Feb 4, 2016

:)

@l1n00
Copy link

l1n00 commented Mar 7, 2016

Cool, but it don't work for me.. When i try to test the function qgis tell me:

Traceback (most recent call last):
File "", line 1, in
File "C:/PROGRA1/QGISLY1/apps/qgis/./python\qgis\utils.py", line 572, in _import
mod = _builtin_import(name, globals, locals, fromlist, level)
ImportError: No module named requests

where am I wrong?

@galderantzuna
Copy link

Hi. JR from (https://gis.stackexchange.com/questions/271096) directed me to your solution. The idea is perfect for what I need to do, but my images are local jpg. As I know nothing of python I have been blindly trying to modify your function to no avail. Can you help? What should be the function to use locally stored jpg images?

@galderantzuna
Copy link

galderantzuna commented Feb 12, 2018

This is were I did arrive

import requests
import base64

@qgsfunction(args='auto', group='Custom')
def show_image(pSourceFile, pFileName, feature, parent):
svg =
.
.
.
fullorigpath = "D:\MDKD\MOZA\60 INFRA\09 software\2018-02 GPSfotolapse_1_5_6\v01\vdf200"
fullorigpath = fullorigpath.replace("\", "/")
pSourceFile = fullorigpath + pSourceFile[1:]

vFileName = pFileName[:-4]
vSourceFileSVG = r"D:\temp\{0}.svg".format(vFileName)
origdata = pSourceFile
b64data = base64.b64encode(origdata)
newsvg = svg.format(b64data).replace('\n','')
with open(vSourceFileSVG, 'w') as f:
	f.write(newsvg)
return vSourceFileSVG

and this the error I get
'ascii' codec can't decode byte 0x81 in position 30: ordinal not in range(128)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment