diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2016-09-20 16:44:31 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2016-09-20 16:45:04 +0200 |
commit | caad2c7f6e200d3dd1b2a0c3bb3a36c5311b442c (patch) | |
tree | a2f771312e0653d13febae27e69566669f9696c8 /chimere/models.py | |
parent | ea953629c654b6264ef2e4932307bbccaf8c41d9 (diff) | |
download | Chimère-caad2c7f6e200d3dd1b2a0c3bb3a36c5311b442c.tar.bz2 Chimère-caad2c7f6e200d3dd1b2a0c3bb3a36c5311b442c.zip |
Bulk geojson marker performance improvement
Diffstat (limited to 'chimere/models.py')
-rw-r--r-- | chimere/models.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/chimere/models.py b/chimere/models.py index e0b401b..e99a9cf 100644 --- a/chimere/models.py +++ b/chimere/models.py @@ -905,6 +905,51 @@ class Marker(GeographicItem): return json.dumps(jsons) + @classmethod + def getGeoJSONs(self, queryset, limit_to_categories=[]): + vals = [] + q = queryset.select_related('categories').extra( + select={'json': 'ST_AsGeoJSON(point)'}).values( + 'json', 'name', 'pk', 'categories__pk') + added, cats = [], {} + 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 + if item['categories__pk'] not in cats: + try: + cat = SubCategory.objects.get( + available=True, pk=item['categories__pk']) + except SubCategory.DoesNotExist: + continue + cats[item['categories__pk']] = { + 'icon_path': unicode(cat.icon.image), + 'icon_hover_path': unicode(cat.hover_icon.image) + if cat.hover_icon else '', + 'icon_offset_x': cat.icon.offset_x, + 'icon_offset_y': cat.icon.offset_y, + 'icon_popup_offset_x': cat.icon.popup_offset_x, + 'icon_popup_offset_y': cat.icon.popup_offset_y, + 'category_name': cat.name} + try: + cats[item['categories__pk']].update( + {'icon_width': cat.icon.image.width, + 'icon_height': cat.icon.image.height, + }) + except IOError: + pass + dct = { + "type": "Feature", + "geometry": json.loads(item['json']), + "properties": {"pk": item['pk'], "name": item['name'], + 'key': "marker-{}".format(item['pk'])}} + dct['properties'].update(cats[item['categories__pk']]) + vals.append(dct) + added.append(item['pk']) + return vals + @property def default_category(self): # Should we select only available ones ? |