{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " \n", " \n", " \n", "
View source on GitHubNotebook Viewer Run in Google Colab
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Oil palm concessions in southern Myanmar consist mostly of unconverted forest\n", "\n", "Credits to [Keiko Nomura](https://github.com/nkeikon)\n", "\n", "GEE App: https://nkeikon.users.earthengine.app/view/tanintharyi-oil-palm-and-rubber-map\n", "\n", "Source code: https://github.com/nkeikon/tanintharyi\n", "\n", "Reference: https://go.nature.com/2QBIrYe\n", "\n", "Demo: https://i.imgur.com/VrYKmVG.gifv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Install Earth Engine API and geemap\n", "Install the [Earth Engine Python API](https://developers.google.com/earth-engine/python_install) and [geemap](https://github.com/giswqs/geemap). The **geemap** Python package is built upon the [ipyleaflet](https://github.com/jupyter-widgets/ipyleaflet) and [folium](https://github.com/python-visualization/folium) packages and implements several methods for interacting with Earth Engine data layers, such as `Map.addLayer()`, `Map.setCenter()`, and `Map.centerObject()`.\n", "The following script checks if the geemap package has been installed. If not, it will install geemap, which automatically installs its [dependencies](https://github.com/giswqs/geemap#dependencies), including earthengine-api, folium, and ipyleaflet.\n", "\n", "**Important note**: A key difference between folium and ipyleaflet is that ipyleaflet is built upon ipywidgets and allows bidirectional communication between the front-end and the backend enabling the use of the map to capture user input, while folium is meant for displaying static data only ([source](https://blog.jupyter.org/interactive-gis-in-jupyter-with-ipyleaflet-52f9657fa7a)). Note that [Google Colab](https://colab.research.google.com/) currently does not support ipyleaflet ([source](https://github.com/googlecolab/colabtools/issues/60#issuecomment-596225619)). Therefore, if you are using geemap with Google Colab, you should use [`import geemap.eefolium`](https://github.com/giswqs/geemap/blob/master/geemap/eefolium.py). If you are using geemap with [binder](https://mybinder.org/) or a local Jupyter notebook server, you can use [`import geemap`](https://github.com/giswqs/geemap/blob/master/geemap/geemap.py), which provides more functionalities for capturing user input (e.g., mouse-clicking and moving)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Installs geemap package\n", "import subprocess\n", "\n", "try:\n", " import geemap\n", "except ImportError:\n", " print('geemap package not installed. Installing ...')\n", " subprocess.check_call([\"python\", '-m', 'pip', 'install', 'geemap'])\n", " import geemap\n", "\n", "\n", "# Authenticates and initializes Earth Engine\n", "import ee\n", "\n", "try:\n", " ee.Initialize()\n", "except Exception as e:\n", " ee.Authenticate()\n", " ee.Initialize() " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create an interactive map \n", "The default basemap is `Google Satellite`. [Additional basemaps](https://github.com/giswqs/geemap/blob/master/geemap/geemap.py#L13) can be added using the `Map.add_basemap()` function. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40,-100], zoom=4)\n", "Map.add_basemap('ROADMAP') # Add Google Map\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Add Earth Engine Python script " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Add Earth Engine data\n", "area1 = ee.Image(\"users/nkeikon/myanmar_sr/area1_map\")\n", "area2 = ee.Image(\"users/nkeikon/myanmar_sr/area2_map\")\n", "tanintharyi = ee.FeatureCollection(\"users/nkeikon/myanmar_sr/TNI\")\n", "Map.centerObject(tanintharyi,10)\n", "total = ee.ImageCollection([area1,area2]).mosaic()\n", "palette =['ff0000',# palm (red)\n", " '9933ff',#rubber (purple)\n", " '008000',#other trees (green)\n", " 'lime',#shrub (lime)\n", " 'yellow',#bare (yellow)\n", " '0000ff',#river (blue)\n", "]\n", "viz = {'min':1, 'max':6, 'palette':palette}\n", "MAIN = 'Default map'\n", "OILPALM = 'Oil palm only'\n", "RUBBER = 'Rubber only'\n", "OILPALM_RUBBER = 'Oil palm and rubber'\n", "mainVis = total.visualize(**viz)\n", "oilpalmVis = total.eq(1).selfMask().visualize(**{'palette':'red'})\n", "rubberVis = total.eq(2).selfMask().visualize(**{'palette':'purple'})\n", "oilpalm_rubberVis = oilpalmVis.blend(rubberVis)\n", "Map.addLayer(mainVis, {}, 'Land cover')\n", "Map.addLayer(oilpalmVis, {}, 'Oil palm only')\n", "Map.addLayer(rubberVis, {}, 'Rubber only')\n", "Map.addLayer(oilpalm_rubberVis, {}, 'Oil palm and rubber')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Add widgets to the map\n", "import ipywidgets as widgets\n", "from ipyleaflet import WidgetControl\n", "legend = widgets.HTML(\n", " value='',\n", " placeholder='Land cover',\n", " descripition='Land cover'\n", ")\n", "\n", "citation = widgets.HTML(\n", " value='Reference: https://go.nature.com/2QBIrYe',\n", " placeholder='Land cover',\n", " descripition='Land cover'\n", ")\n", "\n", "dropdown = widgets.Dropdown(\n", " options=[('Land cover', 1), ('Old palm only', 2), ('Rubber only', 3), ('Oil palm and bubber only', 4), ('Show all layers', 5)],\n", " value=5,\n", " description='Select layer',\n", ")\n", "\n", "legend_control = WidgetControl(widget=legend, position='bottomleft')\n", "citation_control = WidgetControl(widget=citation, position='bottomright')\n", "dropdown_control = WidgetControl(widget=dropdown, position='topright')\n", "\n", "all_layers = Map.layers\n", "\n", "def on_change(change):\n", " if change['type'] == 'change' and change['name'] == 'value':\n", " if change['new'] == 5: \n", " Map.layers = all_layers\n", " else:\n", " Map.layers = Map.layers[:3] \n", " layer_index = change['new'] + 2\n", " Map.add_layer(all_layers[layer_index])\n", "\n", "dropdown.observe(on_change)\n", "\n", "Map.add_control(legend_control)\n", "Map.add_control(citation_control)\n", "Map.add_control(dropdown_control)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Display Earth Engine data layers " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map" ] } ], "metadata": { "anaconda-cloud": {}, "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.8.2" } }, "nbformat": 4, "nbformat_minor": 1 }