Instantly share code, notes, and snippets.

@NathanW2 /svg.py
Last active Feb 12, 2018

Embed
What would you like to do?
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

This comment has been minimized.

Show comment
Hide comment
@mhugo

mhugo Feb 4, 2016

Nice trick here ! :)

mhugo commented Feb 4, 2016

Nice trick here ! :)

@NathanW2

This comment has been minimized.

Show comment
Hide comment
@NathanW2
Owner

NathanW2 commented Feb 4, 2016

:)

@d3lmo

This comment has been minimized.

Show comment
Hide comment
@d3lmo

d3lmo 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?

d3lmo 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

This comment has been minimized.

Show comment
Hide comment
@galderantzuna

galderantzuna Feb 12, 2018

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 commented Feb 12, 2018

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

This comment has been minimized.

Show comment
Hide comment
@galderantzuna

galderantzuna 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)

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