|
2 | 2 | "cells": [
|
3 | 3 | {
|
4 | 4 | "cell_type": "code",
|
5 |
| - "execution_count": 12, |
| 5 | + "execution_count": null, |
6 | 6 | "id": "43ef0082-9552-44e9-82d3-c1e27209d2c5",
|
7 | 7 | "metadata": {
|
8 | 8 | "lines_to_next_cell": 2,
|
9 | 9 | "tags": []
|
10 | 10 | },
|
11 |
| - "outputs": [ |
12 |
| - { |
13 |
| - "data": { |
14 |
| - "application/vnd.jupyter.widget-view+json": { |
15 |
| - "model_id": "620b5ab499a04a69bf13d9bbe53f6907", |
16 |
| - "version_major": 2, |
17 |
| - "version_minor": 0 |
18 |
| - }, |
19 |
| - "text/plain": [ |
20 |
| - "Map(center=[47, 2], controls=(AttributionControl(options=['position', 'prefix'], position='bottomright'), Zoom…" |
21 |
| - ] |
22 |
| - }, |
23 |
| - "metadata": {}, |
24 |
| - "output_type": "display_data" |
25 |
| - } |
26 |
| - ], |
| 11 | + "outputs": [], |
27 | 12 | "source": [
|
28 | 13 | "import json\n",
|
29 | 14 | "import requests\n",
|
30 | 15 | "import random\n",
|
31 | 16 | "from ipyleaflet import (\n",
|
32 | 17 | " Map,\n",
|
| 18 | + " LayerGroup,\n", |
| 19 | + " SearchControl,\n", |
33 | 20 | " TileLayer,\n",
|
34 | 21 | " FullScreenControl,\n",
|
35 | 22 | " ScaleControl,\n",
|
|
66 | 53 | "m.add_layer(radon)\n",
|
67 | 54 | "\n",
|
68 | 55 | "# Create a function get_data which fetch the datas from the irsn application\n",
|
69 |
| - "def get_data():\n", |
| 56 | + "def get_data(lng, lat):\n", |
70 | 57 | " r = requests.get(\n",
|
71 | 58 | " \"https://cartoradon.irsn.fr/commune.py/communes/lonlat/{};{}\".format(lng, lat)\n",
|
72 | 59 | " )\n",
|
73 | 60 | " return r.json()\n",
|
74 | 61 | "\n",
|
75 | 62 | "\n",
|
| 63 | + "def generate_popup(coordinates):\n", |
| 64 | + " data = get_data(coordinates[1], coordinates[0])\n", |
| 65 | + " # Display geojson\n", |
| 66 | + " geometry = data[\"geometry\"]\n", |
| 67 | + " geojson = GeoJSON(\n", |
| 68 | + " data=geometry,\n", |
| 69 | + " style={\"opacity\": 1, \"dashArray\": \"9\", \"fillOpacity\": 0.1, \"weight\": 1},\n", |
| 70 | + " hover_style={\"color\": \"white\", \"dashArray\": \"0\", \"fillOpacity\": 0.5},\n", |
| 71 | + " )\n", |
| 72 | + " m.add_layer(geojson)\n", |
| 73 | + "\n", |
| 74 | + " # Create Popup\n", |
| 75 | + " message1 = HTML()\n", |
| 76 | + " dict_properties = data[\"properties\"]\n", |
| 77 | + " get_name = dict_properties.get(\"name\")\n", |
| 78 | + " get_class = dict_properties.get(\"classr\")\n", |
| 79 | + " message1.value = \"Ville: {}, Classe : {}\".format(get_name, get_class)\n", |
| 80 | + " popup = Popup(\n", |
| 81 | + " location=coordinates,\n", |
| 82 | + " child=message1,\n", |
| 83 | + " close_button=True,\n", |
| 84 | + " auto_close=True,\n", |
| 85 | + " close_on_escape_key=False,\n", |
| 86 | + " )\n", |
| 87 | + " m.add_layer(popup)\n", |
| 88 | + "\n", |
| 89 | + "\n", |
76 | 90 | "# take a method on_interation providing Map object to obtain the coordinates\n",
|
77 | 91 | "def on_interaction(event, type, coordinates):\n",
|
78 | 92 | "\n",
|
79 | 93 | " if type == \"click\":\n",
|
80 |
| - " data = get_data(coordinates[1], coordinates[0])\n", |
81 |
| - "\n", |
82 |
| - " # Display geojson\n", |
83 |
| - " geometry = data[\"geometry\"]\n", |
84 |
| - " geojson = GeoJSON(\n", |
85 |
| - " data=geometry,\n", |
86 |
| - " style={\"opacity\": 1, \"dashArray\": \"9\", \"fillOpacity\": 0.1, \"weight\": 1},\n", |
87 |
| - " hover_style={\"color\": \"white\", \"dashArray\": \"0\", \"fillOpacity\": 0.5},\n", |
88 |
| - " )\n", |
89 |
| - " m.add_layer(geojson)\n", |
90 |
| - "\n", |
91 |
| - " # Create Popup\n", |
92 |
| - " message1 = HTML()\n", |
93 |
| - " dict_properties = data[\"properties\"]\n", |
94 |
| - " get_name = dict_properties.get(\"name\")\n", |
95 |
| - " get_class = dict_properties.get(\"classr\")\n", |
96 |
| - " message1.value = \"Ville: {}, Classe : {}\".format(get_name, get_class)\n", |
97 |
| - " popup = Popup(\n", |
98 |
| - " location=coordinates,\n", |
99 |
| - " child=message1,\n", |
100 |
| - " close_button=True,\n", |
101 |
| - " auto_close=True,\n", |
102 |
| - " close_on_escape_key=False,\n", |
103 |
| - " )\n", |
104 |
| - " m.add_layer(popup)\n", |
| 94 | + " generate_popup(coordinates)\n", |
105 | 95 | "\n",
|
106 | 96 | "\n",
|
107 | 97 | "m.on_interaction(on_interaction)\n",
|
108 | 98 | "\n",
|
| 99 | + "\n", |
| 100 | + "def on_found(text, location, **kwargs):\n", |
| 101 | + " generate_popup(location)\n", |
| 102 | + "\n", |
| 103 | + "\n", |
| 104 | + "search = SearchControl(\n", |
| 105 | + " position=\"topleft\",\n", |
| 106 | + " url=\"https://cartoradon.irsn.fr/commune.py/communes/search/FR/{s}?\",\n", |
| 107 | + " zoom=8,\n", |
| 108 | + " property_name=\"display_name\",\n", |
| 109 | + ")\n", |
| 110 | + "search.on_location_found(on_found)\n", |
| 111 | + "\n", |
| 112 | + "m.add_control(search)\n", |
| 113 | + "\n", |
| 114 | + "\n", |
109 | 115 | "m"
|
110 | 116 | ]
|
111 |
| - }, |
112 |
| - { |
113 |
| - "cell_type": "code", |
114 |
| - "execution_count": null, |
115 |
| - "id": "b6e75898-89cf-44e1-826a-11e7b4e9b427", |
116 |
| - "metadata": { |
117 |
| - "tags": [] |
118 |
| - }, |
119 |
| - "outputs": [], |
120 |
| - "source": [] |
121 |
| - }, |
122 |
| - { |
123 |
| - "cell_type": "code", |
124 |
| - "execution_count": null, |
125 |
| - "id": "3312020d-cbd5-4f27-b9f0-845145da5811", |
126 |
| - "metadata": {}, |
127 |
| - "outputs": [], |
128 |
| - "source": [] |
129 | 117 | }
|
130 | 118 | ],
|
131 | 119 | "metadata": {
|
|
0 commit comments