diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-01-23 17:51:34 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-04-24 19:38:56 +0200 |
commit | b829a9fc77e2eca9a0132c78b996622a4ce4df42 (patch) | |
tree | 57bcd07237986b5d007384e99797724d90b34b8a /ishtar_common/views_item.py | |
parent | 9c0b3373f9ef2681b2aac34e3d0289a28f666d92 (diff) | |
download | Ishtar-b829a9fc77e2eca9a0132c78b996622a4ce4df42.tar.bz2 Ishtar-b829a9fc77e2eca9a0132c78b996622a4ce4df42.zip |
Map: geojson response - first display of items on map
Diffstat (limited to 'ishtar_common/views_item.py')
-rw-r--r-- | ishtar_common/views_item.py | 41 |
1 files changed, 39 insertions, 2 deletions
diff --git a/ishtar_common/views_item.py b/ishtar_common/views_item.py index e720a0437..f019fbcb3 100644 --- a/ishtar_common/views_item.py +++ b/ishtar_common/views_item.py @@ -12,7 +12,7 @@ import subprocess from tempfile import NamedTemporaryFile from django.conf import settings -from django.contrib.gis.geos import GEOSException +from django.contrib.gis.geos import GEOSException, GEOSGeometry from django.contrib.staticfiles.templatetags.staticfiles import static from django.core.cache import cache from django.core.exceptions import ObjectDoesNotExist @@ -991,6 +991,41 @@ def _format_val(val): return unicode(val) +def _format_geojson(rows): + data = { + 'type': 'FeatureCollection', + 'crs': { + 'type': 'name', + 'properties': { + 'name': 'EPSG:4326' + } + }, + 'features': [], + 'no-geo': [] + } + if not rows: + return data + geo_attr, name_attr = None, None + for attr in rows[0]: + if attr.endswith('point_2d'): + geo_attr = attr + if attr in ['name', 'cached_label']: + name_attr = attr + if not geo_attr or not name_attr: + return data + for row in rows: + feat = {'name': row[name_attr], 'id': row['id']} + if not row[geo_attr]: + data['no-geo'].append(feat) + continue + feature = {'type': 'Feature'} + feature['properties'] = feat + point = GEOSGeometry(row[geo_attr]) + feature['geometry'] = json.loads(point.json) + data['features'].append(feature) + return data + + DEFAULT_ROW_NUMBER = 10 # length is used by ajax DataTables requests EXCLUDED_FIELDS = ['length'] @@ -1608,7 +1643,9 @@ def get_item(model, func_name, default_name, extra_request_keys=None, elif 'name' in res: res['value'] = res.pop('name') rows.append(res) - if full == 'shortcut': + if data_type == 'json-map': + data = json.dumps(_format_geojson(rows)) + elif full == 'shortcut': data = json.dumps(rows) else: data = json.dumps({ |