diff options
Diffstat (limited to 'ishtar_common/views_item.py')
-rw-r--r-- | ishtar_common/views_item.py | 67 |
1 files changed, 49 insertions, 18 deletions
diff --git a/ishtar_common/views_item.py b/ishtar_common/views_item.py index d06613b19..b8cc24fb4 100644 --- a/ishtar_common/views_item.py +++ b/ishtar_common/views_item.py @@ -1337,7 +1337,7 @@ def _format_val(val): return str(val) -def _format_geojson(rows, link_template): +def _format_geojson(rows, link_template, display_polygon): data = { "type": "FeatureCollection", "crs": {"type": "name", "properties": {"name": "EPSG:4326"}}, @@ -1347,23 +1347,45 @@ def _format_geojson(rows, link_template): } if not rows: return data + """ + Columns: + base: ['id', 'cached_label', 'main_geodata__cached_x', + 'main_geodata__cached_y', 'point_x', 'point_y', + 'locked', 'lock_user_id'] + poly: ['id', 'cached_label', 'main_geodata__cached_x', + 'main_geodata__cached_y', + 'main_geodata__multi_line', 'main_geodata__multi_polygon', + 'point_x', 'point_y', + 'locked', 'lock_user_id'] + """ + delta = 2 if display_polygon else 0 for row in rows: - feat = {"id": row[0], "name": row[1]} - x, y = row[2], row[3] - if not x or not y or x < -180 or x > 180 or y < -90 or y > 90: - data["no-geo"].append(feat) - continue - feature = { + properties = {"id": row[0], "name": row[1]} + feature = None + base_feature = { "type": "Feature", - "properties": feat, - "geometry": {"type": "Point", "coordinates": [x, y]}, + "properties": properties, } + if display_polygon: + if row[4]: # lines + feature = base_feature + feature["geometry"] = json.loads(row[4].geojson) + elif row[5]: # polygons + feature = base_feature + feature["geometry"] = json.loads(row[5].geojson) + if not feature: + x, y = row[4 + delta], row[5 + delta] + if not x or not y or x < -180 or x > 180 or y < -90 or y > 90: + data["no-geo"].append(properties) + continue + feature = base_feature + feature["geometry"] = {"type": "Point", "coordinates": [x, y]} data["features"].append(feature) return data def _get_data_from_query(items, query_table_cols, extra_request_keys, - point_fields=None): + geo_fields=None): # TODO: manage data json field for query_keys in query_table_cols: if not isinstance(query_keys, (tuple, list)): @@ -1381,21 +1403,21 @@ def _get_data_from_query(items, query_table_cols, extra_request_keys, query_key.replace(".", "__") # class style to query values = ["id"] + query_table_cols - if point_fields: + if geo_fields: profile = get_current_profile() precision = profile.point_precision if precision is not None: exp_x = ExpressionWrapper( - Round(point_fields[0], precision), + Round(geo_fields[0], precision), output_field=FloatField(), ) exp_y = ExpressionWrapper( - Round(point_fields[1], precision), + Round(geo_fields[1], precision), output_field=FloatField(), ) else: - exp_x = F(point_fields[0]) - exp_y = F(point_fields[1]) + exp_x = F(geo_fields[0]) + exp_y = F(geo_fields[1]) items = items.annotate(point_x=exp_x) items = items.annotate(point_y=exp_y) values += ["point_x", "point_y"] @@ -1797,6 +1819,9 @@ def get_item( data_type == "json-map" and request_items.get("no_limit", False) ): row_nb = None + display_polygon = False + if data_type == "json-map" and request_items.get("display_polygon", False): + display_polygon = True dct_request_items = {} @@ -2105,6 +2130,9 @@ def get_item( base_query_key + "cached_y"] table_cols += [base_query_key + "cached_x", base_query_key + "cached_y"] + if display_polygon: + query_table_cols += [base_query_key + "multi_line", + base_query_key + "multi_polygon"] # manage sort tables manual_sort_key = None @@ -2194,10 +2222,13 @@ def get_item( items = [item.get_previous(old) for item in items] if data_type == "json-map": - point_fields = query_table_cols[-2:] + if display_polygon: + geo_fields = query_table_cols[-4:] + else: + geo_fields = query_table_cols[-2:] datas = _get_data_from_query( items, query_table_cols, my_extra_request_keys, - point_fields=point_fields + geo_fields=geo_fields ) elif data_type != "csv" and getattr(model, "NEW_QUERY_ENGINE", False): datas = _get_data_from_query(items, query_table_cols, my_extra_request_keys) @@ -2262,7 +2293,7 @@ def get_item( lnk = lnk.replace("999999", "<pk>") if not has_locks: lnk = lnk.replace("<lock>", "") - data = json.dumps(_format_geojson(datas, lnk)) + data = json.dumps(_format_geojson(datas, lnk, display_polygon)) return HttpResponse(data, content_type="application/json") for data in datas: res = { |