{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "\n", " \n", " \n", " \n", "
View source on GitHubNotebook Viewer Run in Google Colab
" ] }, { "cell_type": "markdown", "id": "1", "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", "id": "2", "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/gee-community/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/gee-community/geemap#dependencies), including earthengine-api, folium, and ipyleaflet." ] }, { "cell_type": "code", "execution_count": null, "id": "3", "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\"])" ] }, { "cell_type": "code", "execution_count": null, "id": "4", "metadata": {}, "outputs": [], "source": [ "import ee\n", "import geemap" ] }, { "cell_type": "markdown", "id": "5", "metadata": {}, "source": [ "## Create an interactive map \n", "The default basemap is `Google Maps`. [Additional basemaps](https://github.com/gee-community/geemap/blob/master/geemap/geemap.py#L13) can be added using the `Map.add_basemap()` function." ] }, { "cell_type": "code", "execution_count": null, "id": "6", "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "Map.add_basemap(\"HYBRID\") # Add Google Map\n", "Map" ] }, { "cell_type": "markdown", "id": "7", "metadata": {}, "source": [ "## Add Earth Engine Python script" ] }, { "cell_type": "code", "execution_count": null, "id": "8", "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 = [\n", " \"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, "id": "9", "metadata": {}, "outputs": [], "source": [ "# Add widgets to the map\n", "import ipywidgets as widgets\n", "from ipyleaflet import WidgetControl\n", "\n", "legend = widgets.HTML(\n", " value='',\n", " placeholder=\"Land cover\",\n", " description=\"Land cover\",\n", ")\n", "\n", "citation = widgets.HTML(\n", " value='Reference: https://go.nature.com/2QBIrYe',\n", " placeholder=\"Land cover\",\n", " description=\"Land cover\",\n", ")\n", "\n", "dropdown = widgets.Dropdown(\n", " options=[\n", " (\"Land cover\", 1),\n", " (\"Old palm only\", 2),\n", " (\"Rubber only\", 3),\n", " (\"Oil palm and bubber only\", 4),\n", " (\"Show all layers\", 5),\n", " ],\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", "\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", "\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", "id": "10", "metadata": {}, "source": [ "## Display Earth Engine data layers" ] }, { "cell_type": "code", "execution_count": null, "id": "11", "metadata": {}, "outputs": [], "source": [ "Map" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }