From 46f3403398ddb9cb458be394840fb46d9550cafa Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Mon, 20 Jan 2020 12:30:37 +0100 Subject: [PATCH 2/2] visualization: adapt geojson view to change in data structures (#38965) --- bijoe/visualization/views.py | 9 ++--- tests/fixtures/schema1/01_schema.json | 8 ++++- tests/fixtures/schema1/01_schema.sql | 39 ++++++++++----------- tests/test_schema1.py | 49 +++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 24 deletions(-) diff --git a/bijoe/visualization/views.py b/bijoe/visualization/views.py index a9b113a..e07cc0f 100644 --- a/bijoe/visualization/views.py +++ b/bijoe/visualization/views.py @@ -14,6 +14,8 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +from __future__ import unicode_literals + from collections import OrderedDict import hashlib import json @@ -260,18 +262,17 @@ class VisualizationGeoJSONView(generics.GenericAPIView): instance = self.get_object() visualization = Visualization.from_json(instance.parameters) visualization.measure = visualization.cube.measures['geolocation'] - drilldown = visualization.drilldown geojson = [] for row in visualization.data(): properties = {} - for cell in row[:len(drilldown)]: - properties[cell.label] = unicode(cell) + for cell in row.dimensions: + properties[cell.dimension.label] = '%s' % (cell,) geojson.append({ 'type': 'Feature', 'geometry': { 'type': 'MultiPoint', - 'coordinates': [unicode(cell) for cell in row[len(drilldown)]] + 'coordinates': [[coord for coord in point] for point in row.measures[0].value], }, 'properties': properties, }) diff --git a/tests/fixtures/schema1/01_schema.json b/tests/fixtures/schema1/01_schema.json index 8f28554..b5b0b17 100644 --- a/tests/fixtures/schema1/01_schema.json +++ b/tests/fixtures/schema1/01_schema.json @@ -230,6 +230,12 @@ "label": "d\u00e9lai moyen depuis 2000", "name": "duration", "type": "duration" + }, + { + "expression": "ARRAY_AGG({fact_table}.geo) FILTER (WHERE {fact_table}.geo IS NOT NULL)", + "label": "geolocation", + "name": "geolocation", + "type": "point" } ], "name": "facts1", @@ -246,4 +252,4 @@ "search_path": [ "schema1" ] -} \ No newline at end of file +} diff --git a/tests/fixtures/schema1/01_schema.sql b/tests/fixtures/schema1/01_schema.sql index 5d983d8..3dc1dc2 100644 --- a/tests/fixtures/schema1/01_schema.sql +++ b/tests/fixtures/schema1/01_schema.sql @@ -26,7 +26,8 @@ CREATE TABLE "Facts" ( leftsubcategory_id integer references schema1.subcategory(id), rightsubcategory_id integer references schema1.subcategory(id), outersubcategory_id integer references schema1.subcategory(id), - "String" varchar + "String" varchar, + geo point ); INSERT INTO category (ord, label) VALUES @@ -46,21 +47,21 @@ INSERT INTO subcategory (category_id, ord, label) VALUES (3, 0, 'subé9'); -INSERT INTO "Facts" (date, datetime, integer, boolean, cnt, innersubcategory_id, leftsubcategory_id, rightsubcategory_id, outersubcategory_id, "String") VALUES - ('2017-01-01', '2017-01-01 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a'), - ('2017-01-02', '2017-01-02 10:00', 1, TRUE, 10, 3, 3, 3, 3, 'b'), - ('2017-01-03', '2017-01-03 10:00', 1, FALSE, 10, NULL, NULL, NULL, NULL, 'a'), - ('2017-01-04', '2017-01-04 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a'), - ('2017-01-05', '2017-01-05 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'c'), - ('2017-01-06', '2017-01-06 10:00', 1, FALSE, 10, 1, 1, 1, 1, NULL), - ('2017-01-07', '2017-01-07 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'a'), - ('2017-01-08', '2017-01-08 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a'), - ('2017-01-09', '2017-01-09 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'a'), - ('2017-01-10', '2017-01-10 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a'), - ('2017-02-01', '2017-02-01 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'a'), - ('2017-03-01', '2017-03-01 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'c'), - ('2017-04-01', '2017-04-01 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'a'), - ('2017-05-01', '2017-05-01 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a'), - ('2017-06-01', '2017-06-01 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'c'), - ('2017-07-01', '2017-07-01 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a'), - ('2017-08-01', '2017-08-01 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'b'); +INSERT INTO "Facts" (date, datetime, integer, boolean, cnt, innersubcategory_id, leftsubcategory_id, rightsubcategory_id, outersubcategory_id, "String", geo) VALUES + ('2017-01-01', '2017-01-01 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a', '(1, 1)'), + ('2017-01-02', '2017-01-02 10:00', 1, TRUE, 10, 3, 3, 3, 3, 'b', '(1, 1)'), + ('2017-01-03', '2017-01-03 10:00', 1, FALSE, 10, NULL, NULL, NULL, NULL, 'a', '(1, 1)'), + ('2017-01-04', '2017-01-04 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a', '(1, 1)'), + ('2017-01-05', '2017-01-05 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'c', '(1, 1)'), + ('2017-01-06', '2017-01-06 10:00', 1, FALSE, 10, 1, 1, 1, 1, NULL, '(1, 1)'), + ('2017-01-07', '2017-01-07 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'a', '(1, 1)'), + ('2017-01-08', '2017-01-08 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a', '(1, 1)'), + ('2017-01-09', '2017-01-09 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'a', '(1, 1)'), + ('2017-01-10', '2017-01-10 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a', '(1, 1)'), + ('2017-02-01', '2017-02-01 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'a', '(1, 1)'), + ('2017-03-01', '2017-03-01 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'c', '(1, 1)'), + ('2017-04-01', '2017-04-01 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'a', '(1, 1)'), + ('2017-05-01', '2017-05-01 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a', '(1, 1)'), + ('2017-06-01', '2017-06-01 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'c', '(1, 1)'), + ('2017-07-01', '2017-07-01 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a', '(1, 1)'), + ('2017-08-01', '2017-08-01 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'b', '(1, 1)'); diff --git a/tests/test_schema1.py b/tests/test_schema1.py index 5ef27e4..5788034 100644 --- a/tests/test_schema1.py +++ b/tests/test_schema1.py @@ -5,6 +5,7 @@ import json from utils import login, get_table, get_ods_table, get_ods_document from bijoe.visualization.ods import OFFICE_NS, TABLE_NS +from bijoe.visualization.models import Visualization as VisualizationModel from bijoe.visualization.utils import Visualization @@ -222,3 +223,51 @@ def test_none_percent_json_data(schema1, app, admin): 'measures': [{'value': 5.882352941176471}] } ] + + +def test_geoloc(schema1, app, admin): + # test conversion to Javascript declaration + visu = VisualizationModel.objects.create( + slug='visu', + name='Visu', + parameters={ + 'warehouse': 'schema1', + 'cube': 'facts1', + 'representation': 'graphical', + 'measure': 'percent', + 'drilldown_y': 'leftcategory', + 'drilldown_x': 'date__year', + }) + response = app.get('/visualization/%d/geojson/' % visu.pk) + assert response.json == [ + { + u'geometry': + { + u'coordinates': [ + [1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0], + [1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0], + [1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0], + [1.0, 1.0] + ], + u'type': u'MultiPoint' + }, + u'properties': { + u'Left Category': u'cat\xe91', + u'ann\xe9e (Date)': u'2017' + }, + u'type': u'Feature', + }, + { + u'geometry': { + u'coordinates': [ + [1.0, 1.0] + ], + u'type': u'MultiPoint' + }, + u'properties': { + u'Left Category': u'Aucun(e)', + u'ann\xe9e (Date)': u'2017' + }, + u'type': u'Feature' + } + ] -- 2.24.0