route from point to point and describe points
This commit is contained in:
parent
8a9406275b
commit
bbfca9f318
9 changed files with 127 additions and 19 deletions
|
@ -3,7 +3,7 @@ from collections import OrderedDict
|
|||
from django.db import models
|
||||
from django.utils.functional import cached_property
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from shapely.geometry import CAP_STYLE, JOIN_STYLE
|
||||
from shapely.geometry import CAP_STYLE, JOIN_STYLE, Point
|
||||
from shapely.geometry.geo import mapping, shape
|
||||
|
||||
from c3nav.mapdata.fields import GeometryField
|
||||
|
@ -68,6 +68,9 @@ class GeometryMapItem(MapItem, metaclass=GeometryMapItemMeta):
|
|||
def get_shadow_geojson(self):
|
||||
return None
|
||||
|
||||
def contains(self, x, y):
|
||||
return self.geometry.contains(Point(x, y))
|
||||
|
||||
|
||||
class GeometryMapItemWithLevel(GeometryMapItem):
|
||||
"""
|
||||
|
|
|
@ -172,8 +172,14 @@ class AreaLocation(LocationModelMixin, GeometryMapItemWithLevel):
|
|||
def subtitle(self):
|
||||
return self.get_subtitle()
|
||||
|
||||
def get_subtitle(self):
|
||||
items = [self.get_location_type_display()]
|
||||
@property
|
||||
def subtitle_without_type(self):
|
||||
return self.get_subtitle(with_type=False)
|
||||
|
||||
def get_subtitle(self, with_type=True):
|
||||
items = []
|
||||
if with_type:
|
||||
items += [self.get_location_type_display()]
|
||||
items += [area.title for area in self.get_in_areas()]
|
||||
return ', '.join(items)
|
||||
|
||||
|
@ -284,13 +290,37 @@ class PointLocation(Location):
|
|||
def location_id(self):
|
||||
return 'c:%s:%d:%d' % (self.level.name, self.x*100, self.y*100)
|
||||
|
||||
@cached_property
|
||||
def description(self):
|
||||
from c3nav.routing.graph import Graph
|
||||
graph = Graph.load()
|
||||
point = graph.get_nearest_point(self.level, self.x, self.y)
|
||||
if point is None:
|
||||
return _('Unreachable Coordinates'), ''
|
||||
|
||||
locations = sorted(AreaLocation.objects.filter(name__in=point.arealocations, can_describe=True),
|
||||
key=AreaLocation.get_sort_key, reverse=True)
|
||||
|
||||
if not locations:
|
||||
return _('Coordinates'), ''
|
||||
|
||||
location = locations[0]
|
||||
if location.contains(self.x, self.y):
|
||||
return (_('Coordinates in %(location)s') % {'location': location.title}), location.subtitle_without_type
|
||||
else:
|
||||
return (_('Coordinates near %(location)s') % {'location': location.title}), location.subtitle_without_type
|
||||
|
||||
@property
|
||||
def title(self) -> str:
|
||||
return 'Custom location'
|
||||
return self.description[0]
|
||||
|
||||
@property
|
||||
def subtitle(self) -> str:
|
||||
return 'Coordinates'
|
||||
add_subtitle = self.description[1]
|
||||
subtitle = '%s:%d:%d' % (self.level.name, self.x*100, self.y*100)
|
||||
if add_subtitle:
|
||||
subtitle += ' - '+add_subtitle
|
||||
return subtitle
|
||||
|
||||
def to_json(self):
|
||||
result = super().to_location_json()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue