Skip to content

Commit

Permalink
Add notebook 138 for using draw control
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs committed Oct 25, 2023
1 parent 0fda46b commit 0f4f13b
Show file tree
Hide file tree
Showing 2 changed files with 346 additions and 0 deletions.
173 changes: 173 additions & 0 deletions docs/notebooks/138_draw_control.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/examples/notebooks/138_draw_control.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
"\n",
"**Clip Earth Engine images interactively with the Draw Control**\n",
"\n",
"Uncomment the following line to install [geemap](https://geemap.org) if needed."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# %pip install -U geemap"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import ee\n",
"import geemap\n",
"import ipywidgets as widgets"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Add some Earth Engine data to the map, then you can use the draw control and a button widget to clip the Earth Engine data by the drawn polygons. \n",
"\n",
"The drawn geometries can be retrieved as either an ee.Geometry object (`m.user_roi`) or ee.FeatureCollection (`m._user_rois`)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"self = geemap.Map(center=[0, 9.31], zoom=3)\n",
"image = ee.Image('USGS/SRTMGL1_003')\n",
"vis_params = {\n",
" 'min': 0,\n",
" 'max': 6000,\n",
" 'palette': 'terrain',\n",
"}\n",
"self.add_layer(image, vis_params, 'SRTM')\n",
"\n",
"clip_btn = widgets.Button(description='Clip Image')\n",
"reset_btn = widgets.Button(description='Reset')\n",
"\n",
"def on_clip_btn_clicked(b):\n",
" if self.user_roi is not None:\n",
" try:\n",
" clipped_image = image.clip(self.user_roi)\n",
" self.add_layer(clipped_image, vis_params, 'Clipped Image')\n",
" self.find_layer('SRTM').visible = False\n",
" except:\n",
" pass\n",
"\n",
"clip_btn.on_click(on_clip_btn_clicked)\n",
"\n",
"def on_reset_btn_clicked(b):\n",
" self._draw_control.clear()\n",
" self.find_layer('SRTM').visible = True\n",
" self.layers = self.layers[:3]\n",
"\n",
"reset_btn.on_click(on_reset_btn_clicked)\n",
"\n",
"widget = widgets.VBox([clip_btn, reset_btn])\n",
"self.add_widget(widget, position='bottomright')\n",
"self"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a solara web app."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import ee\n",
"import geemap\n",
"import solara\n",
"import ipywidgets as widgets\n",
"\n",
"\n",
"class Map(geemap.Map):\n",
" def __init__(self, **kwargs):\n",
" super().__init__(**kwargs)\n",
"\n",
" image = ee.Image('USGS/SRTMGL1_003')\n",
" vis_params = {\n",
" 'min': 0,\n",
" 'max': 6000,\n",
" 'palette': 'terrain',\n",
" }\n",
" self.add_layer(image, vis_params, 'SRTM')\n",
"\n",
" clip_btn = widgets.Button(description='Clip Image')\n",
" reset_btn = widgets.Button(description='Reset')\n",
"\n",
" def on_clip_btn_clicked(b):\n",
" if self.user_roi is not None:\n",
" try:\n",
" clipped_image = image.clip(self.user_roi)\n",
" self.add_layer(clipped_image, vis_params, 'Clipped Image')\n",
" self.find_layer('SRTM').visible = False\n",
" except:\n",
" pass\n",
"\n",
" clip_btn.on_click(on_clip_btn_clicked)\n",
"\n",
" def on_reset_btn_clicked(b):\n",
" self._draw_control.clear()\n",
" self.find_layer('SRTM').visible = True\n",
" self.layers = self.layers[:3]\n",
"\n",
" reset_btn.on_click(on_reset_btn_clicked)\n",
"\n",
" widget = widgets.VBox([clip_btn, reset_btn])\n",
" self.add_widget(widget, position='bottomright')\n",
"\n",
"\n",
"@solara.component\n",
"def Page():\n",
" with solara.Column(style={\"min-width\": \"500px\"}):\n",
" Map.element(\n",
" center=[0, 9.31],\n",
" zoom=3,\n",
" height=\"600px\",\n",
" )\n",
"Page()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
173 changes: 173 additions & 0 deletions examples/notebooks/138_draw_control.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/examples/notebooks/138_draw_control.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
"\n",
"**Clip Earth Engine images interactively with the Draw Control**\n",
"\n",
"Uncomment the following line to install [geemap](https://geemap.org) if needed."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# %pip install -U geemap"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import ee\n",
"import geemap\n",
"import ipywidgets as widgets"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Add some Earth Engine data to the map, then you can use the draw control and a button widget to clip the Earth Engine data by the drawn polygons. \n",
"\n",
"The drawn geometries can be retrieved as either an ee.Geometry object (`m.user_roi`) or ee.FeatureCollection (`m._user_rois`)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"self = geemap.Map(center=[0, 9.31], zoom=3)\n",
"image = ee.Image('USGS/SRTMGL1_003')\n",
"vis_params = {\n",
" 'min': 0,\n",
" 'max': 6000,\n",
" 'palette': 'terrain',\n",
"}\n",
"self.add_layer(image, vis_params, 'SRTM')\n",
"\n",
"clip_btn = widgets.Button(description='Clip Image')\n",
"reset_btn = widgets.Button(description='Reset')\n",
"\n",
"def on_clip_btn_clicked(b):\n",
" if self.user_roi is not None:\n",
" try:\n",
" clipped_image = image.clip(self.user_roi)\n",
" self.add_layer(clipped_image, vis_params, 'Clipped Image')\n",
" self.find_layer('SRTM').visible = False\n",
" except:\n",
" pass\n",
"\n",
"clip_btn.on_click(on_clip_btn_clicked)\n",
"\n",
"def on_reset_btn_clicked(b):\n",
" self._draw_control.clear()\n",
" self.find_layer('SRTM').visible = True\n",
" self.layers = self.layers[:3]\n",
"\n",
"reset_btn.on_click(on_reset_btn_clicked)\n",
"\n",
"widget = widgets.VBox([clip_btn, reset_btn])\n",
"self.add_widget(widget, position='bottomright')\n",
"self"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a solara web app."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import ee\n",
"import geemap\n",
"import solara\n",
"import ipywidgets as widgets\n",
"\n",
"\n",
"class Map(geemap.Map):\n",
" def __init__(self, **kwargs):\n",
" super().__init__(**kwargs)\n",
"\n",
" image = ee.Image('USGS/SRTMGL1_003')\n",
" vis_params = {\n",
" 'min': 0,\n",
" 'max': 6000,\n",
" 'palette': 'terrain',\n",
" }\n",
" self.add_layer(image, vis_params, 'SRTM')\n",
"\n",
" clip_btn = widgets.Button(description='Clip Image')\n",
" reset_btn = widgets.Button(description='Reset')\n",
"\n",
" def on_clip_btn_clicked(b):\n",
" if self.user_roi is not None:\n",
" try:\n",
" clipped_image = image.clip(self.user_roi)\n",
" self.add_layer(clipped_image, vis_params, 'Clipped Image')\n",
" self.find_layer('SRTM').visible = False\n",
" except:\n",
" pass\n",
"\n",
" clip_btn.on_click(on_clip_btn_clicked)\n",
"\n",
" def on_reset_btn_clicked(b):\n",
" self._draw_control.clear()\n",
" self.find_layer('SRTM').visible = True\n",
" self.layers = self.layers[:3]\n",
"\n",
" reset_btn.on_click(on_reset_btn_clicked)\n",
"\n",
" widget = widgets.VBox([clip_btn, reset_btn])\n",
" self.add_widget(widget, position='bottomright')\n",
"\n",
"\n",
"@solara.component\n",
"def Page():\n",
" with solara.Column(style={\"min-width\": \"500px\"}):\n",
" Map.element(\n",
" center=[0, 9.31],\n",
" zoom=3,\n",
" height=\"600px\",\n",
" )\n",
"Page()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 0f4f13b

Please sign in to comment.