Uncomment the following line to install geemap if needed.
# !pip install geemap
import subprocess
try:
import geemap
except ImportError:
print("geemap package is not installed. Installing ...")
subprocess.check_call(["python", "-m", "pip", "install", "geemap"])
import ee
import geemap
import ipyleaflet
import ipywidgets as widgets
If you are using a VPN to access GEE, you might need to uncomment the following line and change the port to your Internet proxy.
# geemap.set_proxy(port=1080)
Map = geemap.Map(center=[37.71, 105.47], zoom=4)
Map
dem = ee.Image("USGS/SRTMGL1_003")
vis_params = {
"min": 0,
"max": 4000,
"palette": ["006633", "E5FFCC", "662A00", "D8D8D8", "F5F5F5"],
}
Map.addLayer(dem, vis_params, "DEM")
fc = ee.FeatureCollection("users/giswqs/public/chn_admin_level2")
Map.addLayer(fc, {}, "China admin level2")
Map
dem_layer = Map.find_layer("DEM")
dem_layer.interact(opacity=(0, 1, 0.1))
vector_layer = Map.find_layer("China admin level2")
vector_layer.interact(opacity=(0, 1, 0.1))
https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20List.html
int_slider = widgets.IntSlider(
value=2000, min=1984, max=2020, step=1, description="Year:"
)
int_slider
int_slider.value
float_slider = widgets.FloatSlider(
value=0, min=-1, max=1, step=0.05, description="Threshold:"
)
float_slider
float_slider.value
int_progress = widgets.IntProgress(
value=7,
min=0,
max=10,
step=1,
description="Loading:",
bar_style="", # 'success', 'info', 'warning', 'danger' or ''
orientation="horizontal",
)
int_progress
int_text = widgets.IntText(
value=7,
description="Any:",
)
int_text
float_text = widgets.FloatText(
value=7.5,
description="Any:",
)
float_text
toggle_button = widgets.ToggleButton(
value=False,
description="Click me",
disabled=False,
button_style="success", # 'success', 'info', 'warning', 'danger' or ''
tooltip="Description",
icon="check", # (FontAwesome names without the `fa-` prefix)
)
toggle_button
toggle_button.value
checkbox = widgets.Checkbox(
value=False, description="Check me", disabled=False, indent=False
)
checkbox
checkbox.value
dropdown = widgets.Dropdown(
options=["USA", "China", "India"], value="China", description="Country:"
)
dropdown
dropdown.value
radio_buttons = widgets.RadioButtons(
options=["USA", "China", "India"], value="China", description="Country:"
)
radio_buttons
radio_buttons.value
text = widgets.Text(
value="USA",
placeholder="Enter a country name",
description="Country:",
disabled=False,
)
text
text.value
widgets.Textarea(
value="Hello World",
placeholder="Type something",
description="String:",
disabled=False,
)
button = widgets.Button(
description="Click me",
button_style="info", # 'success', 'info', 'warning', 'danger' or ''
tooltip="Click me",
icon="check", # (FontAwesome names without the `fa-` prefix)
)
button
widgets.DatePicker(description="Pick a Date", disabled=False)
widgets.ColorPicker(
concise=False, description="Pick a color", value="blue", disabled=False
)
out = widgets.Output(layout={"border": "1px solid black"})
out
with out:
for i in range(10):
print(i, "Hello world!")
from IPython.display import YouTubeVideo
out.clear_output()
with out:
display(YouTubeVideo("7qRtsTCnnSM"))
out
out.clear_output()
with out:
display(widgets.IntSlider())
out
Map = geemap.Map(center=[37.71, 105.47], zoom=4)
dem = ee.Image("USGS/SRTMGL1_003")
fc = ee.FeatureCollection("users/giswqs/public/chn_admin_level2")
vis_params = {
"min": 0,
"max": 4000,
"palette": ["006633", "E5FFCC", "662A00", "D8D8D8", "F5F5F5"],
}
Map.addLayer(dem, vis_params, "DEM")
Map.addLayer(fc, {}, "China admin level2")
Map
output_widget = widgets.Output(layout={"border": "1px solid black"})
output_control = ipyleaflet.WidgetControl(widget=output_widget, position="bottomright")
Map.add_control(output_control)
with output_widget:
print("Nice map!")
output_widget.clear_output()
def handle_interaction(**kwargs):
latlon = kwargs.get("coordinates")
if kwargs.get("type") == "click":
Map.default_style = {"cursor": "wait"}
xy = ee.Geometry.Point(latlon[::-1])
selected_fc = fc.filterBounds(xy)
with output_widget:
output_widget.clear_output()
try:
admin1_id = selected_fc.first().get("ADM1_ZH").getInfo()
admin2_id = selected_fc.first().get("ADM2_ZH").getInfo()
Map.layers = Map.layers[:4]
geom = selected_fc.geometry()
layer_name = admin1_id + "-" + admin2_id
Map.addLayer(
ee.Image().paint(geom, 0, 2), {"palette": "red"}, layer_name
)
print(layer_name)
except Exception as e:
print("No feature could be found")
Map.layers = Map.layers[:4]
Map.default_style = {"cursor": "pointer"}
Map.on_interaction(handle_interaction)