summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--chimere/models.py41
-rw-r--r--chimere/views.py66
2 files changed, 79 insertions, 28 deletions
diff --git a/chimere/models.py b/chimere/models.py
index e99a9cf..825b4c0 100644
--- a/chimere/models.py
+++ b/chimere/models.py
@@ -1548,6 +1548,47 @@ class Route(GeographicItem):
dct['length'] = self.route.length
return dct
+ @classmethod
+ def getGeoJSONs(self, queryset, color="#000",
+ limit_to_categories=[]):
+ vals, default_color = [], color
+ q = queryset.select_related('categories').extra(
+ select={'json': 'ST_AsGeoJSON(route)'}).values(
+ 'json', 'name', 'pk', 'color', 'categories__pk')
+ added = []
+ current_categories = {}
+ for item in q.all():
+ if item['pk'] in added:
+ continue
+ if limit_to_categories and \
+ item["categories__pk"] not in limit_to_categories:
+ continue
+ color = default_color
+ if item["color"]:
+ color = item['color']
+ elif item["categories__pk"]:
+ if item["categories__pk"] not in current_categories:
+ cat = SubCategory.objects.get(pk=item["categories__pk"])
+ # [index, color list]
+ current_categories[item["categories__pk"]] = \
+ [0, list(Color.objects.filter(
+ color_theme=cat.color_theme))]
+ idx, colors = current_categories[item["categories__pk"]]
+ # category have a color theme
+ if colors:
+ c = colors[idx % len(colors)]
+ color = c.code
+ # index += 1
+ current_categories[item["categories__pk"]][0] += 1
+ vals.append({
+ "type": "Feature",
+ "geometry": json.loads(item['json']),
+ "properties": {"pk": item['pk'], "name": item['name'],
+ 'key': "route-{}".format(item['pk']),
+ 'color': color}})
+ added.append(item['pk'])
+ return vals
+
pre_save_route_values = {}
diff --git a/chimere/views.py b/chimere/views.py
index c713d6c..12ae9bd 100644
--- a/chimere/views.py
+++ b/chimere/views.py
@@ -820,35 +820,45 @@ def _getGeoObjects(area_name, category_ids, status='A', getjson=True,
# routes
if 'Route' in item_types:
# TODO: manage non aggregated and bounding box in non-aggregated
- query = AggregatedRoute.objects.filter(
- status__in=status, subcategory__in=category_ids).order_by(
- 'subcategory', '-pk')
- if getjson:
- current_cat, colors, idx = None, None, 0
- for route in query.all():
- color = ""
- # aggregated view has no color and no categories
- if hasattr(route, 'color') and route.color:
- color = route.color
- elif hasattr(route, 'categories'):
- c_cat = None
- for cat in route.categories.all():
- if cat.id in category_ids:
- c_cat = cat
- break
- if c_cat and not current_cat or current_cat != c_cat:
- idx = 0
- current_cat = c_cat
- colors = list(Color.objects.filter(
- color_theme=c_cat.color_theme))
- if colors:
- color = colors[idx % len(colors)].code
- idx += 1
- else:
- color = "#000"
- items.append(json.loads(route.getGeoJSON(color=color)))
+ if aggregated_category_ids:
+ query = AggregatedRoute.objects.filter(
+ status__in=status, subcategory__in=category_ids).order_by(
+ 'subcategory', '-pk')
+ if getjson:
+ current_cat, colors, idx = None, None, 0
+ for route in query.all():
+ color = ""
+ # aggregated view has no color and no categories
+ if hasattr(route, 'color') and route.color:
+ color = route.color
+ elif hasattr(route, 'categories'):
+ c_cat = None
+ for cat in route.categories.all():
+ if cat.id in category_ids:
+ c_cat = cat
+ break
+ if c_cat and not current_cat or current_cat != c_cat:
+ idx = 0
+ current_cat = c_cat
+ colors = list(Color.objects.filter(
+ color_theme=c_cat.color_theme))
+ if colors:
+ color = colors[idx % len(colors)].code
+ idx += 1
+ else:
+ color = "#000"
+ items.append(json.loads(route.getGeoJSON(color=color)))
+ else:
+ items += list(query)
else:
- items += list(query)
+ query = Route.objects.filter(
+ status__in=status, categories__pk__in=category_ids)
+ if getjson:
+ current_cat, colors, idx = None, None, 0
+ items += Route.getGeoJSONs(
+ query, limit_to_categories=category_ids)
+ else:
+ items += list(query)
if not items:
return empty, zoom_need_reload