Spaces:
Running
Running
Add google buildings
Browse files- pages/02_google_buildings.py +97 -0
pages/02_google_buildings.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import leafmap
|
| 2 |
+
import solara
|
| 3 |
+
import ipywidgets as widgets
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import geopandas as gpd
|
| 6 |
+
|
| 7 |
+
zoom = solara.reactive(2)
|
| 8 |
+
center = solara.reactive((20, 0))
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def get_datasets(m):
|
| 12 |
+
url = 'https://raw.githubusercontent.com/opengeos/ee-tile-layers/main/datasets.tsv'
|
| 13 |
+
df = pd.read_csv(url, sep='\t')
|
| 14 |
+
setattr(m, 'df', df)
|
| 15 |
+
return df
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def add_widget(m, position="topright"):
|
| 19 |
+
building_url = "https://sites.research.google/open-buildings/tiles.geojson"
|
| 20 |
+
country_url = (
|
| 21 |
+
"https://naciscdn.org/naturalearth/110m/cultural/ne_110m_admin_0_countries.zip"
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
building_gdf = gpd.read_file(building_url)
|
| 25 |
+
country_gdf = gpd.read_file(country_url)
|
| 26 |
+
countries = country_gdf["NAME"].values.tolist()
|
| 27 |
+
countries.sort()
|
| 28 |
+
|
| 29 |
+
style = {"description_width": "initial"}
|
| 30 |
+
padding = "0px 0px 0px 5px"
|
| 31 |
+
|
| 32 |
+
country = widgets.Dropdown(
|
| 33 |
+
options=countries,
|
| 34 |
+
description="Country:",
|
| 35 |
+
style=style,
|
| 36 |
+
layout=widgets.Layout(padding=padding, width="275px"),
|
| 37 |
+
)
|
| 38 |
+
country.value = None
|
| 39 |
+
|
| 40 |
+
m.add_gdf(building_gdf, layer_name="Coverage", zoom_to_layer=True, info_mode=None)
|
| 41 |
+
|
| 42 |
+
def country_changed(change):
|
| 43 |
+
if change["new"]:
|
| 44 |
+
selected = change["new"]
|
| 45 |
+
selected_gdf = country_gdf[country_gdf["NAME"] == selected]
|
| 46 |
+
gdf_style = {
|
| 47 |
+
"color": "#000000",
|
| 48 |
+
"weight": 2,
|
| 49 |
+
"opacity": 1,
|
| 50 |
+
"fill": False,
|
| 51 |
+
"fillColor": "#3388ff",
|
| 52 |
+
}
|
| 53 |
+
if selected not in m.get_layer_names():
|
| 54 |
+
m.add_gdf(
|
| 55 |
+
selected_gdf,
|
| 56 |
+
layer_name=selected,
|
| 57 |
+
zoom_to_layer=True,
|
| 58 |
+
info_mode=None,
|
| 59 |
+
style=gdf_style,
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
country.observe(country_changed, names="value")
|
| 63 |
+
|
| 64 |
+
m.add_ee_layer('GOOGLE/open-buildings', name='Buildings')
|
| 65 |
+
|
| 66 |
+
m.add_widget(country, position=position)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
class Map(leafmap.Map):
|
| 70 |
+
def __init__(self, **kwargs):
|
| 71 |
+
super().__init__(**kwargs)
|
| 72 |
+
# Add what you want below
|
| 73 |
+
self.add_basemap("SATELLITE", shown=False)
|
| 74 |
+
self.find_layer("Google Satellite").visible = False
|
| 75 |
+
self.add_layer_manager()
|
| 76 |
+
add_widget(self)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
@solara.component
|
| 80 |
+
def Page():
|
| 81 |
+
with solara.Column(style={"min-width": "500px"}):
|
| 82 |
+
# solara components support reactive variables
|
| 83 |
+
# solara.SliderInt(label="Zoom level", value=zoom, min=1, max=20)
|
| 84 |
+
# using 3rd party widget library require wiring up the events manually
|
| 85 |
+
# using zoom.value and zoom.set
|
| 86 |
+
Map.element( # type: ignore
|
| 87 |
+
zoom=zoom.value,
|
| 88 |
+
on_zoom=zoom.set,
|
| 89 |
+
center=center.value,
|
| 90 |
+
on_center=center.set,
|
| 91 |
+
scroll_wheel_zoom=True,
|
| 92 |
+
toolbar_ctrl=False,
|
| 93 |
+
data_ctrl=False,
|
| 94 |
+
height="780px",
|
| 95 |
+
)
|
| 96 |
+
solara.Text(f"Zoom: {zoom.value}")
|
| 97 |
+
solara.Text(f"Center: {center.value}")
|