2016-11-26 13:48:44 +01:00
|
|
|
import os
|
2016-12-02 18:53:16 +01:00
|
|
|
import subprocess
|
2016-11-26 13:48:44 +01:00
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.db.models import Max, Min
|
|
|
|
from shapely.affinity import scale
|
|
|
|
|
|
|
|
from c3nav.mapdata.models import Package
|
|
|
|
|
|
|
|
|
|
|
|
class LevelRenderer():
|
|
|
|
def __init__(self, level):
|
|
|
|
self.level = level
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_dimensions():
|
|
|
|
aggregate = Package.objects.all().aggregate(Max('right'), Min('left'), Max('top'), Min('bottom'))
|
|
|
|
return (
|
2016-12-02 18:50:19 +01:00
|
|
|
float(aggregate['right__max'] - aggregate['left__min']) * settings.RENDER_SCALE,
|
|
|
|
float(aggregate['top__max'] - aggregate['bottom__min']) * settings.RENDER_SCALE
|
2016-11-26 13:48:44 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
2016-11-28 19:35:53 +01:00
|
|
|
def polygon_svg(geometry, fill_color=None, fill_opacity=None, stroke_width=0.0, stroke_color=None, filter=None):
|
2016-12-02 18:51:42 +01:00
|
|
|
scaled = scale(geometry, xfact=settings.RENDER_SCALE, yfact=settings.RENDER_SCALE, origin=(0, 0))
|
2016-12-02 18:50:19 +01:00
|
|
|
element = ET.fromstring(scaled.svg(0, fill_color or '#FFFFFF'))
|
2016-11-26 13:48:44 +01:00
|
|
|
if element.tag != 'g':
|
|
|
|
new_element = ET.Element('g')
|
|
|
|
new_element.append(element)
|
|
|
|
element = new_element
|
|
|
|
|
|
|
|
for path in element.findall('path'):
|
|
|
|
path.attrib.pop('opacity')
|
2016-12-02 18:50:19 +01:00
|
|
|
path.set('stroke-width', str(stroke_width * settings.RENDER_SCALE))
|
2016-11-26 13:48:44 +01:00
|
|
|
|
|
|
|
if fill_color is None and 'fill' in path.attrib:
|
|
|
|
path.attrib.pop('fill')
|
|
|
|
path.set('fill-opacity', '0')
|
|
|
|
|
2016-11-28 19:35:53 +01:00
|
|
|
if fill_opacity is not None:
|
|
|
|
path.set('fill-opacity', str(fill_opacity))
|
|
|
|
|
2016-11-26 13:48:44 +01:00
|
|
|
if stroke_color is not None:
|
|
|
|
path.set('stroke', stroke_color)
|
|
|
|
elif 'stroke' in path.attrib:
|
|
|
|
path.attrib.pop('stroke')
|
|
|
|
|
|
|
|
if filter is not None:
|
|
|
|
path.set('filter', filter)
|
|
|
|
|
|
|
|
return element
|
|
|
|
|
|
|
|
def get_svg(self):
|
|
|
|
width, height = self.get_dimensions()
|
|
|
|
|
|
|
|
svg = ET.Element('svg', {
|
|
|
|
'width': str(width),
|
|
|
|
'height': str(height),
|
|
|
|
'xmlns:svg': 'http://www.w3.org/2000/svg',
|
|
|
|
'xmlns': 'http://www.w3.org/2000/svg',
|
|
|
|
})
|
|
|
|
|
|
|
|
contents = ET.Element('g', {
|
|
|
|
'transform': 'scale(1 -1) translate(0 -%d)' % (height),
|
|
|
|
})
|
|
|
|
svg.append(contents)
|
|
|
|
|
2016-11-29 00:47:37 +01:00
|
|
|
contents.append(self.polygon_svg(self.level.geometries.buildings_with_holes,
|
2016-11-26 18:19:55 +01:00
|
|
|
fill_color='#D5D5D5'))
|
|
|
|
|
2016-11-28 19:15:36 +01:00
|
|
|
contents.append(self.polygon_svg(self.level.geometries.outsides,
|
|
|
|
fill_color='#DCE6DC'))
|
|
|
|
|
2016-11-26 18:19:55 +01:00
|
|
|
contents.append(self.polygon_svg(self.level.geometries.walls_shadow,
|
2016-11-28 19:35:53 +01:00
|
|
|
fill_color='#000000',
|
2016-11-29 17:47:05 +01:00
|
|
|
fill_opacity=0.06))
|
|
|
|
|
|
|
|
contents.append(self.polygon_svg(self.level.geometries.elevatorlevels,
|
|
|
|
fill_color='#9EF8FB'))
|
2016-11-26 18:19:55 +01:00
|
|
|
|
|
|
|
contents.append(self.polygon_svg(self.level.geometries.doors,
|
|
|
|
fill_color='#FFFFFF',
|
2016-11-29 17:47:05 +01:00
|
|
|
stroke_color='#3c3c3c',
|
2016-12-02 18:50:19 +01:00
|
|
|
stroke_width=0.05))
|
2016-11-26 18:19:55 +01:00
|
|
|
|
|
|
|
contents.append(self.polygon_svg(self.level.geometries.obstacles,
|
|
|
|
fill_color='#BDBDBD',
|
|
|
|
stroke_color='#9E9E9E',
|
2016-12-02 18:50:19 +01:00
|
|
|
stroke_width=0.05))
|
2016-11-28 20:56:52 +01:00
|
|
|
|
2016-11-29 00:47:37 +01:00
|
|
|
contents.append(self.polygon_svg(self.level.geometries.walls,
|
2016-11-26 13:48:44 +01:00
|
|
|
fill_color='#949494',
|
2016-11-29 17:47:05 +01:00
|
|
|
stroke_color='#3c3c3c',
|
2016-12-02 18:50:19 +01:00
|
|
|
stroke_width=0.05))
|
2016-11-26 13:48:44 +01:00
|
|
|
|
|
|
|
return ET.tostring(svg).decode()
|
|
|
|
|
2016-12-02 18:50:49 +01:00
|
|
|
def _get_render_path(self, filename):
|
|
|
|
return os.path.join(settings.RENDER_ROOT, filename)
|
|
|
|
|
2016-11-26 13:48:44 +01:00
|
|
|
def write_svg(self):
|
2016-12-02 18:50:49 +01:00
|
|
|
filename = self._get_render_path('level-%s.svg' % self.level.name)
|
2016-11-26 13:48:44 +01:00
|
|
|
with open(filename, 'w') as f:
|
|
|
|
f.write(self.get_svg())
|
2016-12-02 18:50:49 +01:00
|
|
|
return filename
|
|
|
|
|
|
|
|
def render_png(self):
|
|
|
|
svg_filename = self.write_svg()
|
|
|
|
filename = self._get_render_path('level-%s.png' % self.level.name)
|
|
|
|
subprocess.call(['rsvg-convert', svg_filename, '-o', filename])
|