giswqs commited on
Commit
0c6097c
·
unverified ·
1 Parent(s): d85c700

Add notebook (#2)

Browse files
Files changed (2) hide show
  1. notebooks/dashboard.ipynb +126 -56
  2. notebooks/leafmap.ipynb +0 -51
notebooks/dashboard.ipynb CHANGED
@@ -17,8 +17,16 @@
17
  "metadata": {},
18
  "outputs": [],
19
  "source": [
 
 
 
20
  "import leafmap.maplibregl as leafmap\n",
21
- "import ipywidgets as widgets"
 
 
 
 
 
22
  ]
23
  },
24
  {
@@ -28,70 +36,132 @@
28
  "metadata": {},
29
  "outputs": [],
30
  "source": [
31
- "tw = leafmap.TabWidget(\n",
32
- " title=\"Interactive Dashboard\",\n",
33
- " tabs=(\"Home\", \"Map\", \"Population\", \"Settings\"),\n",
34
- " icons=(\"mdi-home\", \"mdi-airplane\", \"mdi-account-box\", \"mdi-cog\"),\n",
35
- " show_panel_titles=False,\n",
36
- ")\n",
 
37
  "\n",
38
- "# Customize dialog\n",
39
- "tw.set_help_title(\"About this dashboard\")\n",
40
- "tw.set_help_content(\n",
41
- " widgets.HTML(\n",
42
- " \"\"\"\n",
43
- " <p><b>User Guide</b></p>\n",
44
- " <ul>\n",
45
- " <li>Step 1: Click on the tab</li>\n",
46
- " <li>Navigate the map</li>\n",
47
- " <li>? — help</li>\n",
48
- " </ul>\n",
49
- " \"\"\"\n",
50
- " )\n",
51
  ")\n",
52
  "\n",
53
- "m = leafmap.Map(projection=\"globe\", sidebar_visible=True, height=\"600px\")\n",
54
- "m.add_overture_3d_buildings()\n",
55
- "m.create_container()\n",
56
  "\n",
57
- "home_tab = widgets.HTML(\n",
58
- " \"\"\"\n",
59
- " <h1>Welcome to the Leafmap Visualization Dashboard</h1>\n",
60
- " <p>This is the home tab.</p>\n",
61
- " <img src=\"https://assets.gishub.org/images/geog-312.png\" width=\"100%\">\n",
62
- " \"\"\"\n",
63
  ")\n",
64
- "\n",
65
- "settings_tab = widgets.HTML(\n",
66
- " \"\"\"\n",
67
- " <h1>Settings</h1>\n",
68
- " <p>This is the settings tab.</p>\n",
69
- " \"\"\"\n",
70
  ")\n",
71
  "\n",
72
- "data = \"https://github.com/opengeos/datasets/releases/download/vector/countries.geojson\"\n",
73
- "\n",
74
- "m2 = leafmap.Map(style=\"liberty\", projection=\"globe\")\n",
75
- "first_symbol_id = m2.find_first_symbol_layer()[\"id\"]\n",
76
- "m2.add_data(\n",
77
- " data,\n",
78
- " column=\"POP_EST\",\n",
79
- " scheme=\"Quantiles\",\n",
80
- " cmap=\"Blues\",\n",
81
- " legend_title=\"Population\",\n",
82
- " name=\"population\",\n",
83
- " # before_id=first_symbol_id,\n",
84
- " extrude=True,\n",
85
- " scale_factor=1000,\n",
86
  ")\n",
87
- "container = m2.create_container()\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  "\n",
89
- "tw.set_tab_content(0, home_tab)\n",
90
- "tw.set_tab_content(1, m.container)\n",
91
- "tw.set_tab_content(3, settings_tab)\n",
92
- "tw.set_tab_content(2, m2.container)\n",
93
  "\n",
94
- "tw.widget"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  ]
96
  }
97
  ],
@@ -111,7 +181,7 @@
111
  "name": "python",
112
  "nbconvert_exporter": "python",
113
  "pygments_lexer": "ipython3",
114
- "version": "3.12.2"
115
  }
116
  },
117
  "nbformat": 4,
 
17
  "metadata": {},
18
  "outputs": [],
19
  "source": [
20
+ "import json\n",
21
+ "import duckdb\n",
22
+ "import ipywidgets as widgets\n",
23
  "import leafmap.maplibregl as leafmap\n",
24
+ "import matplotlib.pyplot as plt\n",
25
+ "\n",
26
+ "m = leafmap.Map(sidebar_visible=True, layer_manager_expanded=False, height=\"800px\")\n",
27
+ "m.add_basemap(\"Esri.WorldImagery\", before_id=m.first_symbol_layer_id, visible=False)\n",
28
+ "m.add_draw_control(controls=[\"polygon\", \"trash\"])\n",
29
+ "m"
30
  ]
31
  },
32
  {
 
36
  "metadata": {},
37
  "outputs": [],
38
  "source": [
39
+ "con = duckdb.connect()\n",
40
+ "con.install_extension(\"httpfs\")\n",
41
+ "con.install_extension(\"spatial\")\n",
42
+ "con.install_extension(\"h3\", repository=\"community\")\n",
43
+ "con.load_extension(\"httpfs\")\n",
44
+ "con.load_extension(\"spatial\")\n",
45
+ "con.load_extension(\"h3\")\n",
46
  "\n",
47
+ "url = \"https://data.gishub.org/duckdb/h3_res4_geo.parquet\"\n",
48
+ "\n",
49
+ "con.sql(\n",
50
+ " f\"CREATE TABLE IF NOT EXISTS h3_res4_geo AS SELECT * FROM read_parquet('{url}');\"\n",
 
 
 
 
 
 
 
 
 
51
  ")\n",
52
  "\n",
53
+ "colormaps = sorted(plt.colormaps())\n",
 
 
54
  "\n",
55
+ "checkbox = widgets.Checkbox(\n",
56
+ " description=\"3D Map\",\n",
57
+ " value=True,\n",
58
+ " style={\"description_width\": \"initial\"},\n",
59
+ " layout=widgets.Layout(width=\"initial\"),\n",
 
60
  ")\n",
61
+ "outline_chk = widgets.Checkbox(\n",
62
+ " description=\"Add Hexagon Outline\",\n",
63
+ " value=False,\n",
64
+ " style={\"description_width\": \"initial\"},\n",
65
+ " layout=widgets.Layout(width=\"initial\"),\n",
 
66
  ")\n",
67
  "\n",
68
+ "colormap_dropdown = widgets.Dropdown(\n",
69
+ " options=colormaps,\n",
70
+ " description=\"Colormap:\",\n",
71
+ " value=\"inferno\",\n",
72
+ " style={\"description_width\": \"initial\"},\n",
 
 
 
 
 
 
 
 
 
73
  ")\n",
74
+ "class_slider = widgets.IntSlider(\n",
75
+ " description=\"Class:\",\n",
76
+ " min=1,\n",
77
+ " max=10,\n",
78
+ " step=1,\n",
79
+ " value=5,\n",
80
+ " style={\"description_width\": \"initial\"},\n",
81
+ ")\n",
82
+ "apply_btn = widgets.Button(description=\"Apply\")\n",
83
+ "close_btn = widgets.Button(description=\"Close\")\n",
84
+ "output_widget = widgets.Output()\n",
85
+ "with output_widget:\n",
86
+ " print(\"Draw a polygon on the map. Then, click \\non the 'Apply' button\")\n",
87
+ "\n",
88
+ "\n",
89
+ "def on_apply_btn_click(change):\n",
90
+ " with output_widget:\n",
91
+ " try:\n",
92
+ " output_widget.clear_output()\n",
93
+ " if len(m.draw_features_selected) > 0:\n",
94
+ " geojson = m.draw_features_selected[0][\"geometry\"]\n",
95
+ " df = con.sql(\n",
96
+ " f\"\"\"\n",
97
+ " SELECT * EXCLUDE (geometry), ST_AsText(geometry) AS geometry FROM h3_res4_geo\n",
98
+ " WHERE ST_Intersects(geometry, ST_GeomFromGeoJSON('{json.dumps(geojson)}'));\n",
99
+ " \"\"\"\n",
100
+ " ).df()\n",
101
+ " gdf = leafmap.df_to_gdf(df)\n",
102
+ " if \"H3 Hexagon\" in m.layer_dict:\n",
103
+ " m.remove_layer(\"H3 Hexagon\")\n",
104
  "\n",
105
+ " if outline_chk.value:\n",
106
+ " outline_color = \"rgba(255, 255, 255, 255)\"\n",
107
+ " else:\n",
108
+ " outline_color = \"rgba(255, 255, 255, 0)\"\n",
109
  "\n",
110
+ " if checkbox.value:\n",
111
+ " m.add_data(\n",
112
+ " gdf,\n",
113
+ " column=\"building_count\",\n",
114
+ " scheme=\"JenksCaspall\",\n",
115
+ " cmap=colormap_dropdown.value,\n",
116
+ " k=class_slider.value,\n",
117
+ " outline_color=outline_color,\n",
118
+ " name=\"H3 Hexagon\",\n",
119
+ " before_id=m.first_symbol_layer_id,\n",
120
+ " extrude=True,\n",
121
+ " fit_bounds=False,\n",
122
+ " add_legend=False,\n",
123
+ " )\n",
124
+ " else:\n",
125
+ " m.add_data(\n",
126
+ " gdf,\n",
127
+ " column=\"building_count\",\n",
128
+ " scheme=\"JenksCaspall\",\n",
129
+ " cmap=colormap_dropdown.value,\n",
130
+ " k=class_slider.value,\n",
131
+ " outline_color=outline_color,\n",
132
+ " name=\"H3 Hexagon\",\n",
133
+ " before_id=m.first_symbol_layer_id,\n",
134
+ " fit_bounds=False,\n",
135
+ " add_legend=False,\n",
136
+ " )\n",
137
+ "\n",
138
+ " m.remove_from_sidebar(name=\"Legend\")\n",
139
+ " m.add_legend_to_sidebar(\n",
140
+ " title=\"Building Count\",\n",
141
+ " legend_dict=m.legend_dict,\n",
142
+ " )\n",
143
+ " except Exception as e:\n",
144
+ " with output_widget:\n",
145
+ " print(e)\n",
146
+ "\n",
147
+ "\n",
148
+ "def on_close_btn_click(change):\n",
149
+ " m.remove_from_sidebar(name=\"H3 Hexagonal Grid\")\n",
150
+ "\n",
151
+ "\n",
152
+ "apply_btn.on_click(on_apply_btn_click)\n",
153
+ "close_btn.on_click(on_close_btn_click)\n",
154
+ "\n",
155
+ "widget = widgets.VBox(\n",
156
+ " [\n",
157
+ " widgets.HBox([checkbox, outline_chk]),\n",
158
+ " colormap_dropdown,\n",
159
+ " class_slider,\n",
160
+ " widgets.HBox([apply_btn, close_btn]),\n",
161
+ " output_widget,\n",
162
+ " ]\n",
163
+ ")\n",
164
+ "m.add_to_sidebar(widget, label=\"H3 Hexagonal Grid\", widget_icon=\"mdi-hexagon-multiple\")"
165
  ]
166
  }
167
  ],
 
181
  "name": "python",
182
  "nbconvert_exporter": "python",
183
  "pygments_lexer": "ipython3",
184
+ "version": "3.12.9"
185
  }
186
  },
187
  "nbformat": 4,
notebooks/leafmap.ipynb DELETED
@@ -1,51 +0,0 @@
1
- {
2
- "cells": [
3
- {
4
- "cell_type": "code",
5
- "execution_count": null,
6
- "metadata": {},
7
- "outputs": [],
8
- "source": [
9
- "import leafmap.maplibregl as leafmap"
10
- ]
11
- },
12
- {
13
- "cell_type": "code",
14
- "execution_count": null,
15
- "metadata": {},
16
- "outputs": [],
17
- "source": [
18
- "m = leafmap.Map(\n",
19
- " style=\"liberty\", projection=\"globe\", sidebar_visible=True, height=\"750px\"\n",
20
- ")\n",
21
- "m.add_basemap(\"USGS.Imagery\")\n",
22
- "cities = (\n",
23
- " \"https://github.com/opengeos/datasets/releases/download/world/world_cities.geojson\"\n",
24
- ")\n",
25
- "m.add_geojson(cities, name=\"Cities\")\n",
26
- "m"
27
- ]
28
- }
29
- ],
30
- "metadata": {
31
- "kernelspec": {
32
- "display_name": "geo",
33
- "language": "python",
34
- "name": "python3"
35
- },
36
- "language_info": {
37
- "codemirror_mode": {
38
- "name": "ipython",
39
- "version": 3
40
- },
41
- "file_extension": ".py",
42
- "mimetype": "text/x-python",
43
- "name": "python",
44
- "nbconvert_exporter": "python",
45
- "pygments_lexer": "ipython3",
46
- "version": "3.12.2"
47
- }
48
- },
49
- "nbformat": 4,
50
- "nbformat_minor": 2
51
- }