2016-11-27 14:03:39 +01:00
|
|
|
from collections import OrderedDict
|
|
|
|
|
2016-10-12 15:25:00 +02:00
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2016-10-13 13:55:02 +02:00
|
|
|
from shapely.geometry.geo import mapping, shape
|
2016-10-12 15:25:00 +02:00
|
|
|
|
|
|
|
from c3nav.mapdata.fields import GeometryField
|
2016-11-28 20:56:52 +01:00
|
|
|
from c3nav.mapdata.models import Elevator
|
2016-11-27 23:51:44 +01:00
|
|
|
from c3nav.mapdata.models.base import MapItem, MapItemMeta
|
2016-10-12 15:25:00 +02:00
|
|
|
from c3nav.mapdata.utils import format_geojson
|
|
|
|
|
2016-11-27 14:03:39 +01:00
|
|
|
GEOMETRY_MAPITEM_TYPES = OrderedDict()
|
|
|
|
|
|
|
|
|
2016-11-27 23:51:44 +01:00
|
|
|
class GeometryMapItemMeta(MapItemMeta):
|
2016-11-27 14:03:39 +01:00
|
|
|
def __new__(mcs, name, bases, attrs):
|
|
|
|
cls = super().__new__(mcs, name, bases, attrs)
|
|
|
|
if not cls._meta.abstract:
|
|
|
|
GEOMETRY_MAPITEM_TYPES[name.lower()] = cls
|
|
|
|
return cls
|
|
|
|
|
2016-10-12 15:25:00 +02:00
|
|
|
|
2016-11-27 14:03:39 +01:00
|
|
|
class GeometryMapItem(MapItem, metaclass=GeometryMapItemMeta):
|
2016-10-12 15:25:00 +02:00
|
|
|
"""
|
|
|
|
A map feature
|
|
|
|
"""
|
|
|
|
level = models.ForeignKey('mapdata.Level', on_delete=models.CASCADE, verbose_name=_('level'))
|
|
|
|
geometry = GeometryField()
|
|
|
|
|
2016-11-28 16:38:22 +01:00
|
|
|
geomtype = None
|
|
|
|
|
2016-10-12 15:25:00 +02:00
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2016-10-13 15:55:15 +02:00
|
|
|
@property
|
|
|
|
def title(self):
|
|
|
|
return self.name
|
|
|
|
|
2016-10-12 15:25:00 +02:00
|
|
|
@classmethod
|
|
|
|
def fromfile(cls, data, file_path):
|
|
|
|
kwargs = super().fromfile(data, file_path)
|
|
|
|
|
|
|
|
if 'geometry' not in data:
|
|
|
|
raise ValueError('missing geometry.')
|
|
|
|
try:
|
|
|
|
kwargs['geometry'] = shape(data['geometry'])
|
|
|
|
except:
|
|
|
|
raise ValueError(_('Invalid GeoJSON.'))
|
|
|
|
|
|
|
|
if 'level' not in data:
|
|
|
|
raise ValueError('missing level.')
|
|
|
|
kwargs['level'] = data['level']
|
|
|
|
|
|
|
|
return kwargs
|
|
|
|
|
2016-11-27 14:03:39 +01:00
|
|
|
@classmethod
|
|
|
|
def get_styles(cls):
|
|
|
|
return {
|
|
|
|
cls.__name__.lower(): cls.color
|
|
|
|
}
|
|
|
|
|
|
|
|
def get_geojson_properties(self):
|
|
|
|
return OrderedDict((
|
|
|
|
('type', self.__class__.__name__.lower()),
|
|
|
|
('name', self.name),
|
|
|
|
('package', self.package.name),
|
|
|
|
('level', self.level.name),
|
|
|
|
))
|
|
|
|
|
|
|
|
def to_geojson(self):
|
|
|
|
return [OrderedDict((
|
|
|
|
('type', 'Feature'),
|
|
|
|
('properties', self.get_geojson_properties()),
|
|
|
|
('geometry', format_geojson(mapping(self.geometry), round=False)),
|
|
|
|
))]
|
|
|
|
|
2016-10-12 15:25:00 +02:00
|
|
|
def tofile(self):
|
|
|
|
result = super().tofile()
|
|
|
|
result['level'] = self.level.name
|
|
|
|
result['geometry'] = format_geojson(mapping(self.geometry))
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2016-11-14 21:15:20 +01:00
|
|
|
class Building(GeometryMapItem):
|
2016-10-12 15:25:00 +02:00
|
|
|
"""
|
|
|
|
The outline of a building on a specific level
|
|
|
|
"""
|
|
|
|
geomtype = 'polygon'
|
|
|
|
|
|
|
|
class Meta:
|
2016-10-16 13:35:01 +02:00
|
|
|
verbose_name = _('Building')
|
|
|
|
verbose_name_plural = _('Buildings')
|
|
|
|
default_related_name = 'buildings'
|
2016-10-12 15:25:00 +02:00
|
|
|
|
|
|
|
|
2016-11-28 19:15:36 +01:00
|
|
|
class Room(GeometryMapItem):
|
2016-10-12 15:25:00 +02:00
|
|
|
"""
|
2016-11-28 19:15:36 +01:00
|
|
|
An accessible area like a room. Can overlap.
|
2016-10-12 15:25:00 +02:00
|
|
|
"""
|
|
|
|
geomtype = 'polygon'
|
|
|
|
|
|
|
|
class Meta:
|
2016-11-28 19:15:36 +01:00
|
|
|
verbose_name = _('Room')
|
|
|
|
verbose_name_plural = _('Rooms')
|
|
|
|
default_related_name = 'rooms'
|
|
|
|
|
|
|
|
|
|
|
|
class Outside(GeometryMapItem):
|
|
|
|
"""
|
|
|
|
An accessible outdoor area like a court. Can overlap.
|
|
|
|
"""
|
|
|
|
geomtype = 'polygon'
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _('Outside Area')
|
|
|
|
verbose_name_plural = _('Outside Areas')
|
|
|
|
default_related_name = 'outsides'
|
2016-10-16 13:20:34 +02:00
|
|
|
|
|
|
|
|
2016-11-14 21:15:20 +01:00
|
|
|
class Obstacle(GeometryMapItem):
|
2016-10-16 13:20:34 +02:00
|
|
|
"""
|
|
|
|
An obstacle
|
|
|
|
"""
|
|
|
|
height = models.DecimalField(_('height of the obstacle'), null=True, max_digits=4, decimal_places=2)
|
|
|
|
|
|
|
|
geomtype = 'polygon'
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _('Obstacle')
|
|
|
|
verbose_name_plural = _('Obstacles')
|
|
|
|
default_related_name = 'obstacles'
|
|
|
|
|
2016-11-27 14:03:39 +01:00
|
|
|
def get_geojson_properties(self):
|
|
|
|
result = super().get_geojson_properties()
|
|
|
|
result['height'] = float(self.height)
|
|
|
|
return result
|
|
|
|
|
2016-10-16 13:20:34 +02:00
|
|
|
@classmethod
|
|
|
|
def fromfile(cls, data, file_path):
|
|
|
|
kwargs = super().fromfile(data, file_path)
|
|
|
|
|
|
|
|
if 'height' in data:
|
|
|
|
if not isinstance(data['height'], (int, float)):
|
|
|
|
raise ValueError('altitude has to be int or float.')
|
|
|
|
kwargs['height'] = data['height']
|
|
|
|
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def tofile(self):
|
|
|
|
result = super().tofile()
|
|
|
|
if self.height is not None:
|
2016-11-28 16:33:31 +01:00
|
|
|
result['height'] = float(self.height)
|
2016-10-16 13:20:34 +02:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
2016-11-14 21:15:20 +01:00
|
|
|
class Door(GeometryMapItem):
|
2016-10-16 13:20:34 +02:00
|
|
|
"""
|
|
|
|
A connection between two rooms
|
|
|
|
"""
|
|
|
|
geomtype = 'polygon'
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _('Door')
|
|
|
|
verbose_name_plural = _('Doors')
|
|
|
|
default_related_name = 'doors'
|
2016-11-28 20:56:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ElevatorLevel(GeometryMapItem):
|
|
|
|
"""
|
|
|
|
An elevator Level
|
|
|
|
"""
|
|
|
|
elevator = models.ForeignKey(Elevator, on_delete=models.PROTECT, related_name='levels')
|
|
|
|
button = models.SlugField(_('Button label'), max_length=10)
|
|
|
|
|
|
|
|
geomtype = 'polygon'
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _('Elevator Level')
|
|
|
|
verbose_name_plural = _('Elevator Levels')
|
|
|
|
default_related_name = 'elevatorlevels'
|
|
|
|
|
|
|
|
def get_geojson_properties(self):
|
|
|
|
result = super().get_geojson_properties()
|
|
|
|
result['elevator'] = self.elevator.name
|
|
|
|
result['button'] = self.button
|
|
|
|
return result
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def fromfile(cls, data, file_path):
|
|
|
|
kwargs = super().fromfile(data, file_path)
|
|
|
|
|
|
|
|
if 'elevator' not in data:
|
|
|
|
raise ValueError('missing elevator.')
|
|
|
|
kwargs['elevator'] = data['elevator']
|
|
|
|
|
|
|
|
if 'button' not in data:
|
|
|
|
raise ValueError('missing button.')
|
|
|
|
kwargs['button'] = data['button']
|
|
|
|
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def tofile(self):
|
|
|
|
result = super().tofile()
|
|
|
|
result['elevator'] = self.elevator.name
|
|
|
|
result['button'] = self.button
|
|
|
|
return result
|