download external icon resources

pull/21/head
Hartmut Holzgraefe 2018-02-21 23:00:23 +00:00
rodzic b77f44c0b2
commit 4a2f946f1b
3 zmienionych plików z 43 dodań i 15 usunięć

Wyświetl plik

@ -26,6 +26,7 @@ from __future__ import print_function
import os
import tempfile
import shutil
from string import Template
import cairo
import gi
@ -729,19 +730,21 @@ class SinglePageRenderer(Renderer):
# apply UMAP file
umap_filename = None
if self.rc.umap_file:
json_tmpfile = tempfile.NamedTemporaryFile(suffix='.json', delete=False, mode='w')
json_filename = json_tmpfile.name
json_tmpfile.write(umap_utils.umap_preprocess(self.rc.umap_file))
json_tmpfile.close()
tmpdir = tempfile.mkdtemp(prefix='ocitysmap', suffix='.d')
template_dir = os.path.realpath(
os.path.join(
os.path.dirname(__file__),
'../../templates/umap'))
json_filename = os.path.join(tmpdir, 'geo.json')
json_tmpfile = open(json_filename, 'w')
json_tmpfile.write(umap_utils.umap_preprocess(self.rc.umap_file, tmpdir))
json_tmpfile.close()
template_file = os.path.join(template_dir, 'template.xml')
style_tmpfile = tempfile.NamedTemporaryFile(suffix='.xml', delete=False, mode='w')
style_filename = style_tmpfile.name
style_filename = os.path.join(tmpdir, 'style.xml')
style_tmpfile = open(style_filename, 'w')
with open(template_file, 'r') as style_template:
tmpstyle = Template(style_template.read())
@ -766,8 +769,7 @@ class SinglePageRenderer(Renderer):
mapnik.render(umap_overlay, ctx, scale_factor, 0, 0)
ctx.restore()
os.unlink(style_filename)
os.unlink(json_filename)
shutil.rmtree(tmpdir)
cairo_surface.flush()

Wyświetl plik

@ -1,8 +1,10 @@
# -*- coding: utf-8 -*-
import json, re
import os, json, re, urllib3, tempfile, logging
def umap_preprocess(umap_file):
LOG = logging.getLogger('ocitysmap')
def umap_preprocess(umap_file, tmpdir):
umap_defaults = {
'color' : 'blue',
'opacity' : 0.5,
@ -24,6 +26,13 @@ def umap_preprocess(umap_file):
'Ball' : -16
}
icon_dir = os.path.realpath(
os.path.join(
os.path.dirname(__file__),
'../templates/umap/maki/icons'))
http = urllib3.PoolManager()
fp = open(umap_file, 'r')
umap = json.load(fp)
@ -36,6 +45,8 @@ def umap_preprocess(umap_file):
new_features = []
icon_cache = {}
for layer in layers:
for feature in layer['features']:
layer_defaults = umap_defaults
@ -72,11 +83,26 @@ def umap_preprocess(umap_file):
new_props['iconClass'] = iconClass
if iconClass == 'Square' or iconClass == 'Drop':
m = re.match(r'/uploads/pictogram/(.*)-24.*png', iconUrl)
m = re.match(r'/uploads/pictogram/(.*)-24(.*)\.png', iconUrl)
if m:
new_props['iconUrl'] = m.group(1)
new_props['iconUrl'] = icon_dir + '/' + m.group(1) + "-15.svg"
if m.group(2) == '':
new_props['iconFill'] = 'black'
else:
new_props['iconFill'] = 'white'
else:
new_props['iconUrl'] = iconUrl
if iconUrl in icon_cache:
new_props['iconUrl'] = icon_cache[iconUrl]
else:
response = http.request('GET', iconUrl)
iconFile = tempfile.NamedTemporaryFile(suffix='.png', delete=False, mode='wb', dir=tmpdir)
iconFile.write(response.data)
iconFile.close()
iconPath = os.path.realpath(iconFile.name)
new_props['iconUrl'] = iconPath
icon_cache[iconUrl] = iconPath
try:
new_props['offset'] = marker_offsets[iconClass]

Wyświetl plik

@ -1,6 +1,6 @@
<?xml version='1.0' encoding='utf-8'?>
<Map xmlns:xi='http://www.w3.org/2001/XInclude' background-color='transparent'>
<FileSource name="root">/</FileSource>
<Style name="polygon">
<Rule>
<Filter>[mapnik::geometry_type]=polygon and [fill]='yes'</Filter>
@ -26,7 +26,7 @@
</Rule>
<Rule>
<Filter>(not ([iconUrl] = null or [iconUrl] = '')) and ([mapnik::geometry_type]=point)</Filter>
<MarkersSymbolizer file="${basedir}/maki/icons/[iconUrl]-15.svg" allow-overlap="true" transform='translate(0,-23)' fill='white'/>
<MarkersSymbolizer base="root" file="[iconUrl]" allow-overlap="true" transform='translate(0,-23)' fill='[iconFill]' width='20'/>
</Rule>
<Rule>
<Filter>(not ([name] = null or [name] = '')) and ([mapnik::geometry_type]=point)</Filter>