Projet

Général

Profil

0002-visualization-adapt-geojson-view-to-change-in-data-s.patch

Benjamin Dauvergne, 20 janvier 2020 12:31

Télécharger (8,36 ko)

Voir les différences:

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(-)
bijoe/visualization/views.py
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17
from __future__ import unicode_literals
18

  
17 19
from collections import OrderedDict
18 20
import hashlib
19 21
import json
......
260 262
        instance = self.get_object()
261 263
        visualization = Visualization.from_json(instance.parameters)
262 264
        visualization.measure = visualization.cube.measures['geolocation']
263
        drilldown = visualization.drilldown
264 265
        geojson = []
265 266

  
266 267
        for row in visualization.data():
267 268
            properties = {}
268
            for cell in row[:len(drilldown)]:
269
                properties[cell.label] = unicode(cell)
269
            for cell in row.dimensions:
270
                properties[cell.dimension.label] = '%s' % (cell,)
270 271
            geojson.append({
271 272
                'type': 'Feature',
272 273
                'geometry': {
273 274
                    'type': 'MultiPoint',
274
                    'coordinates': [unicode(cell) for cell in row[len(drilldown)]]
275
                    'coordinates': [[coord for coord in point] for point in row.measures[0].value],
275 276
                },
276 277
                'properties': properties,
277 278
            })
tests/fixtures/schema1/01_schema.json
230 230
                    "label": "d\u00e9lai moyen depuis 2000",
231 231
                    "name": "duration",
232 232
                    "type": "duration"
233
                },
234
                {
235
                    "expression": "ARRAY_AGG({fact_table}.geo) FILTER (WHERE {fact_table}.geo IS NOT NULL)",
236
                    "label": "geolocation",
237
                    "name": "geolocation",
238
                    "type": "point"
233 239
                }
234 240
            ],
235 241
            "name": "facts1",
......
246 252
    "search_path": [
247 253
        "schema1"
248 254
    ]
249
}
255
}
tests/fixtures/schema1/01_schema.sql
26 26
    leftsubcategory_id integer references schema1.subcategory(id),
27 27
    rightsubcategory_id integer references schema1.subcategory(id),
28 28
    outersubcategory_id integer references schema1.subcategory(id),
29
    "String" varchar
29
    "String" varchar,
30
    geo point
30 31
);
31 32

  
32 33
INSERT INTO category (ord, label) VALUES
......
46 47
   (3, 0, 'subé9');
47 48

  
48 49

  
49
INSERT INTO "Facts" (date, datetime, integer, boolean, cnt, innersubcategory_id, leftsubcategory_id, rightsubcategory_id, outersubcategory_id, "String") VALUES
50
   ('2017-01-01', '2017-01-01 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a'),
51
   ('2017-01-02', '2017-01-02 10:00', 1, TRUE, 10, 3, 3, 3, 3, 'b'),
52
   ('2017-01-03', '2017-01-03 10:00', 1, FALSE, 10, NULL, NULL, NULL, NULL, 'a'),
53
   ('2017-01-04', '2017-01-04 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a'),
54
   ('2017-01-05', '2017-01-05 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'c'),
55
   ('2017-01-06', '2017-01-06 10:00', 1, FALSE, 10, 1, 1, 1, 1, NULL),
56
   ('2017-01-07', '2017-01-07 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'a'),
57
   ('2017-01-08', '2017-01-08 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a'),
58
   ('2017-01-09', '2017-01-09 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'a'),
59
   ('2017-01-10', '2017-01-10 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a'),
60
   ('2017-02-01', '2017-02-01 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'a'),
61
   ('2017-03-01', '2017-03-01 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'c'),
62
   ('2017-04-01', '2017-04-01 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'a'),
63
   ('2017-05-01', '2017-05-01 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a'),
64
   ('2017-06-01', '2017-06-01 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'c'),
65
   ('2017-07-01', '2017-07-01 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a'),
66
   ('2017-08-01', '2017-08-01 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'b');
50
INSERT INTO "Facts" (date, datetime, integer, boolean, cnt, innersubcategory_id, leftsubcategory_id, rightsubcategory_id, outersubcategory_id, "String", geo) VALUES
51
   ('2017-01-01', '2017-01-01 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a', '(1, 1)'),
52
   ('2017-01-02', '2017-01-02 10:00', 1, TRUE, 10, 3, 3, 3, 3, 'b', '(1, 1)'),
53
   ('2017-01-03', '2017-01-03 10:00', 1, FALSE, 10, NULL, NULL, NULL, NULL, 'a', '(1, 1)'),
54
   ('2017-01-04', '2017-01-04 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a', '(1, 1)'),
55
   ('2017-01-05', '2017-01-05 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'c', '(1, 1)'),
56
   ('2017-01-06', '2017-01-06 10:00', 1, FALSE, 10, 1, 1, 1, 1, NULL, '(1, 1)'),
57
   ('2017-01-07', '2017-01-07 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'a', '(1, 1)'),
58
   ('2017-01-08', '2017-01-08 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a', '(1, 1)'),
59
   ('2017-01-09', '2017-01-09 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'a', '(1, 1)'),
60
   ('2017-01-10', '2017-01-10 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a', '(1, 1)'),
61
   ('2017-02-01', '2017-02-01 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'a', '(1, 1)'),
62
   ('2017-03-01', '2017-03-01 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'c', '(1, 1)'),
63
   ('2017-04-01', '2017-04-01 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'a', '(1, 1)'),
64
   ('2017-05-01', '2017-05-01 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a', '(1, 1)'),
65
   ('2017-06-01', '2017-06-01 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'c', '(1, 1)'),
66
   ('2017-07-01', '2017-07-01 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a', '(1, 1)'),
67
   ('2017-08-01', '2017-08-01 10:00', 1, TRUE, 10, 1, 1, 1, 1, 'b', '(1, 1)');
tests/test_schema1.py
5 5
from utils import login, get_table, get_ods_table, get_ods_document
6 6

  
7 7
from bijoe.visualization.ods import OFFICE_NS, TABLE_NS
8
from bijoe.visualization.models import Visualization as VisualizationModel
8 9
from bijoe.visualization.utils import Visualization
9 10

  
10 11

  
......
222 223
            'measures': [{'value': 5.882352941176471}]
223 224
        }
224 225
    ]
226

  
227

  
228
def test_geoloc(schema1, app, admin):
229
    # test conversion to Javascript declaration
230
    visu = VisualizationModel.objects.create(
231
        slug='visu',
232
        name='Visu',
233
        parameters={
234
            'warehouse': 'schema1',
235
            'cube': 'facts1',
236
            'representation': 'graphical',
237
            'measure': 'percent',
238
            'drilldown_y': 'leftcategory',
239
            'drilldown_x': 'date__year',
240
        })
241
    response = app.get('/visualization/%d/geojson/' % visu.pk)
242
    assert response.json == [
243
        {
244
            u'geometry':
245
            {
246
                u'coordinates': [
247
                    [1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0],
248
                    [1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0],
249
                    [1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0],
250
                    [1.0, 1.0]
251
                ],
252
                u'type': u'MultiPoint'
253
            },
254
            u'properties': {
255
                u'Left Category': u'cat\xe91',
256
                u'ann\xe9e (Date)': u'2017'
257
            },
258
            u'type': u'Feature',
259
        },
260
        {
261
            u'geometry': {
262
                u'coordinates': [
263
                    [1.0, 1.0]
264
                ],
265
                u'type': u'MultiPoint'
266
            },
267
            u'properties': {
268
                u'Left Category': u'Aucun(e)',
269
                u'ann\xe9e (Date)': u'2017'
270
            },
271
            u'type': u'Feature'
272
        }
273
    ]
225
-