|
10 | 10 | # NumPy to generate the random scores from 0 to 1 that we are using so far, as well as the classes
|
11 | 11 | import numpy as np
|
12 | 12 |
|
13 |
| -# Useful imports to open and read the GeoJSON file |
| 13 | +# Useful imports to open and read the GeoJSON file, and to get risk data from the API |
14 | 14 | from pathlib import Path
|
15 | 15 | import json
|
| 16 | +import requests |
| 17 | +import config as cfg |
16 | 18 |
|
17 | 19 | # Various modules provided by Dash to build the page layout
|
18 | 20 | import dash_core_components as dcc
|
|
34 | 36 | with open(Path(__file__).parent.joinpath('data', 'departements.geojson'), 'rb') as response:
|
35 | 37 | departments = json.load(response)
|
36 | 38 |
|
| 39 | +# We fetch the deparment risk score json and store it in the risk_json variable |
| 40 | +# When everything is validated, we'll request the data directly from the API |
| 41 | +risk_json = requests.get(cfg.PYRORISK_FALLBACK).json() |
37 | 42 |
|
38 |
| -# We add to each department in the geojson a new property called "score" that corresponds to the random risk level |
| 43 | +# We add to each department in the geojson a new property called "score" that corresponds to the risk level |
39 | 44 | for department in departments['features']:
|
40 |
| - department['properties']['score'] = np.random.rand() |
| 45 | + dpt_name = department['properties']['nom'] |
| 46 | + geocode_list = [dpt['geocode'] for dpt in risk_json] |
| 47 | + if dpt_name in geocode_list: |
| 48 | + risk_json_index = geocode_list.index(dpt_name) |
| 49 | + department['properties']['score'] = risk_json[risk_json_index]['score'] |
| 50 | + else: |
| 51 | + department['properties']['score'] = 0 |
41 | 52 |
|
42 | 53 |
|
43 | 54 | # Preparing the choropleth map, fetching the departments GeoJSON and building the related map attribute
|
|
0 commit comments