visualization: adapt geojson view to change in data structures (#38965)

This commit is contained in:
Benjamin Dauvergne 2020-01-20 12:30:37 +01:00
parent 86a8568cd6
commit f0f976c470
4 changed files with 81 additions and 24 deletions

View File

@ -14,6 +14,8 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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,
})

View File

@ -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"
]
}
}

View File

@ -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)');

View File

@ -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'
}
]