{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/gee-community/geemap/blob/master/examples/workshops/Japan_2022.ipynb)\n", "[![image](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/gee-community/geemap/master?urlpath=lab%2Ftree%2Fexamples%2Fworkshops)\n", "\n", "**Introduction to Earth Engine and Geemap**\n", "\n", "A workshop at Nagoya University, Japan.\n", "\n", "Useful links\n", "\n", "- [Google Earth Engine](https://earthengine.google.com/)\n", "- [Sign up for an Earth Engine account](https://earthengine.google.com/signup/)\n", "- [Awesome GEE Community Datasets](https://samapriya.github.io/awesome-gee-community-datasets)\n", "- [Geemap website](https://geemap.org/)\n", "- [Geemap book](https://book.geemap.org/)\n", "- [Geemap YouTube videos](https://youtube.com/@giswqs)\n", "- [Streamlit Web App](https://github.com/giswqs/streamlit-geospatial)\n", "\n", "## Installing geemap\n", "\n", "Press **Ctrl + /** to uncomment the following line to install geemap. Restart the kernel/runtime after installation." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# pip install geemap" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Earth Engine authentication" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import ee\n", "\n", "ee.Authenticate()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Following the instructions [here](https://book.geemap.org/chapters/01_introduction.html#earth-engine-authentication) to authenticate your Earth Engine account.\n", "\n", "## Creating interactive maps" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import geemap" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4, height=600)\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(data_ctrl=False, toolbar_ctrl=False, draw_ctrl=False)\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(lite_mode=True)\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.save('ipyleaflet.html')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Adding basemaps\n", "\n", "### Built-in basemaps" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import geemap" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(basemap='HYBRID')\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.add_basemap('OpenTopoMap')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for basemap in geemap.basemaps.keys():\n", " print(basemap)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "len(geemap.basemaps)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Basemap GUI" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Earth Engine data types\n", "\n", "### Image\n", "\n", "#### Loading Earth Engine images" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = ee.Image('USGS/SRTMGL1_003')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Visualizing Earth Engine images" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[21.79, 70.87], zoom=3)\n", "image = ee.Image('USGS/SRTMGL1_003')\n", "vis_params = {\n", " 'min': 0,\n", " 'max': 6000,\n", " 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],\n", "}\n", "Map.addLayer(image, vis_params, 'SRTM')\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ImageCollection\n", "\n", "#### Loading image collections" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "collection = ee.ImageCollection('COPERNICUS/S2_SR')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Visualizing image collections" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "collection = ee.ImageCollection('COPERNICUS/S2_SR')\n", "image = collection.median()\n", "\n", "vis = {\n", " 'min': 0.0,\n", " 'max': 3000,\n", " 'bands': ['B4', 'B3', 'B2'],\n", "}\n", "\n", "Map.setCenter(83.277, 17.7009, 12)\n", "Map.addLayer(image, vis, 'Sentinel-2')\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Filtering image collections" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "collection = (\n", " ee.ImageCollection('COPERNICUS/S2_SR')\n", " .filterDate('2021-01-01', '2022-01-01')\n", " .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 5))\n", ")\n", "image = collection.median()\n", "\n", "vis = {\n", " 'min': 0.0,\n", " 'max': 3000,\n", " 'bands': ['B4', 'B3', 'B2'],\n", "}\n", "\n", "Map.setCenter(83.277, 17.7009, 12)\n", "Map.addLayer(image, vis, 'Sentinel-2')\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Geometry\n", "\n", "#### Geometry types\n", "\n", "#### Creating Geometry objects" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "point = ee.Geometry.Point([1.5, 1.5])\n", "\n", "lineString = ee.Geometry.LineString([[-35, -10], [35, -10], [35, 10], [-35, 10]])\n", "\n", "linearRing = ee.Geometry.LinearRing(\n", " [[-35, -10], [35, -10], [35, 10], [-35, 10], [-35, -10]]\n", ")\n", "\n", "rectangle = ee.Geometry.Rectangle([-40, -20, 40, 20])\n", "\n", "polygon = ee.Geometry.Polygon([[[-5, 40], [65, 40], [65, 60], [-5, 60], [-5, 60]]])\n", "\n", "Map.addLayer(point, {}, 'Point')\n", "Map.addLayer(lineString, {}, 'LineString')\n", "Map.addLayer(linearRing, {}, 'LinearRing')\n", "Map.addLayer(rectangle, {}, 'Rectangle')\n", "Map.addLayer(polygon, {}, 'Polygon')\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "point = ee.Geometry.Point([1.5, 1.5])\n", "\n", "lineString = ee.Geometry.LineString(\n", " [[-35, -10], [35, -10], [35, 10], [-35, 10]], None, False\n", ")\n", "\n", "linearRing = ee.Geometry.LinearRing(\n", " [[-35, -10], [35, -10], [35, 10], [-35, 10], [-35, -10]], None, False\n", ")\n", "\n", "rectangle = ee.Geometry.Rectangle([-40, -20, 40, 20], None, False)\n", "\n", "polygon = ee.Geometry.Polygon(\n", " [[[-5, 40], [65, 40], [65, 60], [-5, 60], [-5, 60]]], None, False\n", ")\n", "\n", "Map.addLayer(point, {}, 'Point')\n", "Map.addLayer(lineString, {}, 'LineString')\n", "Map.addLayer(linearRing, {}, 'LinearRing')\n", "Map.addLayer(rectangle, {}, 'Rectangle')\n", "Map.addLayer(polygon, {}, 'Polygon')\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Using drawing tools" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if Map.user_roi is not None:\n", " print(Map.user_roi.getInfo())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Feature\n", "\n", "#### Creating Feature objects" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create an ee.Geometry.\n", "polygon = ee.Geometry.Polygon(\n", " [[[-35, -10], [35, -10], [35, 10], [-35, 10], [-35, -10]]], None, False\n", ")\n", "\n", "# Create a Feature from the Geometry.\n", "polyFeature = ee.Feature(polygon, {'foo': 42, 'bar': 'tart'})" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "polyFeature.getInfo()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "Map.addLayer(polyFeature, {}, 'feature')\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create a dictionary of properties, some of which may be computed values.\n", "props = {'foo': ee.Number(8).add(88), 'bar': 'nihao'}\n", "\n", "# Create a None geometry feature with the dictionary of properties.\n", "nowhereFeature = ee.Feature(None, props)\n", "\n", "nowhereFeature.getInfo()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Setting Feature properties" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Make a feature and set some properties.\n", "feature = (\n", " ee.Feature(ee.Geometry.Point([-122.22599, 37.17605]))\n", " .set('genus', 'Sequoia')\n", " .set('species', 'sempervirens')\n", ")\n", "\n", "# Overwrite the old properties with a new dictionary.\n", "newDict = {'genus': 'Brachyramphus', 'presence': 1, 'species': 'marmoratus'}\n", "feature = feature.set(newDict)\n", "\n", "# Check the result.\n", "feature.getInfo()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Getting Feature properties" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "prop = feature.get('species')\n", "prop.getInfo()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "props = feature.toDictionary()\n", "props.getInfo()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### FeatureCollection\n", "\n", "#### Loading feature collections" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "fc = ee.FeatureCollection('TIGER/2016/Roads')\n", "Map.setCenter(-73.9596, 40.7688, 12)\n", "Map.addLayer(fc, {}, 'Census roads')\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Creating feature collections" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Make a list of Features.\n", "features = [\n", " ee.Feature(ee.Geometry.Rectangle(30.01, 59.80, 30.59, 60.15), {'name': 'Voronoi'}),\n", " ee.Feature(ee.Geometry.Point(-73.96, 40.781), {'name': 'Thiessen'}),\n", " ee.Feature(ee.Geometry.Point(6.4806, 50.8012), {'name': 'Dirichlet'}),\n", "]\n", "\n", "# Create a FeatureCollection from the list and print it.\n", "fromList = ee.FeatureCollection(features)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fromList.getInfo()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Filtering feature collections" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "states = ee.FeatureCollection('TIGER/2018/States')\n", "feat = states.filter(ee.Filter.eq('NAME', 'Texas'))\n", "Map.addLayer(feat, {}, 'Texas')\n", "Map.centerObject(feat)\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "texas = feat.first()\n", "texas.toDictionary().getInfo()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "states = ee.FeatureCollection('TIGER/2018/States')\n", "fc = states.filter(ee.Filter.inList('NAME', ['California', 'Oregon', 'Washington']))\n", "Map.addLayer(fc, {}, 'West Coast')\n", "Map.centerObject(fc)\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "region = Map.user_roi\n", "if region is None:\n", " region = ee.Geometry.BBox(-88.40, 29.88, -77.90, 35.39)\n", "\n", "fc = ee.FeatureCollection('TIGER/2018/States').filterBounds(region)\n", "Map.addLayer(fc, {}, 'Southeastern U.S.')\n", "Map.centerObject(fc)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Visualizing feature collections" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "Map.addLayer(states, {}, \"US States\")\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "image = ee.Image().paint(states, 0, 3)\n", "Map.addLayer(image, {'palette': 'red'}, \"US States\")\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "style = {'color': '0000ffff', 'width': 2, 'lineType': 'solid', 'fillColor': 'FF000080'}\n", "Map.addLayer(states.style(**style), {}, \"US States\")\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "vis_params = {\n", " 'color': '000000',\n", " 'colorOpacity': 1,\n", " 'pointSize': 3,\n", " 'pointShape': 'circle',\n", " 'width': 2,\n", " 'lineType': 'solid',\n", " 'fillColorOpacity': 0.66,\n", "}\n", "palette = ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5']\n", "Map.add_styled_vector(\n", " states, column=\"NAME\", palette=palette, layer_name=\"Styled vector\", **vis_params\n", ")\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Styling by attribute" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[28.00142, -81.7424], zoom=13)\n", "Map.add_basemap('HYBRID')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "types = [\n", " \"Freshwater Forested/Shrub Wetland\",\n", " \"Freshwater Emergent Wetland\",\n", " \"Freshwater Pond\",\n", " \"Estuarine and Marine Wetland\",\n", " \"Riverine\",\n", " \"Lake\",\n", " \"Estuarine and Marine Deepwater\",\n", " \"Other\",\n", "]\n", "\n", "colors = [\n", " \"#008837\",\n", " \"#7FC31C\",\n", " \"#688CC0\",\n", " \"#66C2A5\",\n", " \"#0190BF\",\n", " \"#13007C\",\n", " \"#007C88\",\n", " \"#B28653\",\n", "]\n", "\n", "fillColor = [c + \"A8\" for c in colors]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fc = ee.FeatureCollection(\"projects/sat-io/open-datasets/NWI/wetlands/FL_Wetlands\")\n", "styled_fc = geemap.ee_vector_style(\n", " fc, column='WETLAND_TY', labels=types, fillColor=fillColor, color='00000000'\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.addLayer(styled_fc, {}, 'NWI')\n", "Map.add_legend(title='Wetland Type', labels=types, colors=colors)\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fuels = [\n", " 'Coal',\n", " 'Oil',\n", " 'Gas',\n", " 'Hydro',\n", " 'Nuclear',\n", " 'Solar',\n", " 'Waste',\n", " 'Wind',\n", " 'Geothermal',\n", " 'Biomass',\n", "]\n", "colors = [\n", " '000000',\n", " '593704',\n", " 'BC80BD',\n", " '0565A6',\n", " 'E31A1C',\n", " 'FF7F00',\n", " '6A3D9A',\n", " '5CA2D1',\n", " 'FDBF6F',\n", " '229A00',\n", "]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fc = ee.FeatureCollection(\"WRI/GPPD/power_plants\").filter(\n", " ee.Filter.inList('fuel1', fuels)\n", ")\n", "styled_fc = geemap.ee_vector_style(fc, column=\"fuel1\", labels=fuels, color=colors)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "Map.addLayer(styled_fc, {}, 'Power Plants')\n", "Map.add_legend(title=\"Power Plant Fuel Type\", labels=fuels, colors=colors)\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "types = ['I', 'U', 'S', 'M', 'C', 'O']\n", "labels = ['Interstate', 'U.S.', 'State recognized', 'Common Name', 'County', 'Other']\n", "colors = ['E31A1C', 'FF7F00', '6A3D9A', '000000', 'FDBF6F', '229A00']\n", "width = [8, 5, 4, 2, 1, 1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fc = ee.FeatureCollection('TIGER/2016/Roads')\n", "styled_fc = geemap.ee_vector_style(\n", " fc, column='rttyp', labels=types, color=colors, width=width\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40.7424, -73.9724], zoom=13)\n", "Map.addLayer(styled_fc, {}, 'Census Roads')\n", "Map.add_legend(title='Route Type', labels=labels, colors=colors)\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Earth Engine Data Catalog\n", "\n", "### Searching for datasets" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dataset_xyz = ee.Image('USGS/SRTMGL1_003')\n", "Map.addLayer(dataset_xyz, {}, \"USGS/SRTMGL1_003\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "dem = ee.Image('USGS/SRTMGL1_003')\n", "vis_params = {\n", " 'min': 0,\n", " 'max': 4000,\n", " 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],\n", "}\n", "Map.addLayer(dem, vis_params, 'SRTM DEM')\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using the datasets module" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from geemap.datasets import DATA" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "dataset = ee.Image(DATA.USGS_GAP_CONUS_2011)\n", "Map.addLayer(dataset, {}, 'GAP CONUS')\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from geemap.datasets import get_metadata\n", "\n", "get_metadata(DATA.USGS_GAP_CONUS_2011)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting image metadata" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = ee.Image('LANDSAT/LC09/C02/T1_L2/LC09_044034_20220503')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image.bandNames().getInfo()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image.select('SR_B1').projection().getInfo()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image.select('SR_B1').projection().nominalScale().getInfo()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image.propertyNames().getInfo()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image.get('CLOUD_COVER').getInfo()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image.get('DATE_ACQUIRED').getInfo()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image.get('system:time_start').getInfo()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "date = ee.Date(image.get('system:time_start'))\n", "date.format('YYYY-MM-dd').getInfo()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image.toDictionary().getInfo()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "props = geemap.image_props(image)\n", "props.getInfo()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calculating descriptive statistics" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = ee.Image('LANDSAT/LC09/C02/T1_L2/LC09_044034_20220503')\n", "geemap.image_min_value(image).getInfo()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.image_max_value(image).getInfo()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.image_mean_value(image).getInfo()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.image_stats(image).getInfo()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using the inspector tool" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create an interactive map\n", "Map = geemap.Map(center=(40, -100), zoom=4)\n", "\n", "# Add Earth Engine datasets\n", "dem = ee.Image('USGS/SRTMGL1_003')\n", "landsat7 = ee.Image('LANDSAT/LE7_TOA_5YEAR/1999_2003').select(\n", " ['B1', 'B2', 'B3', 'B4', 'B5', 'B7']\n", ")\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "\n", "# Set visualization parameters.\n", "vis_params = {\n", " 'min': 0,\n", " 'max': 4000,\n", " 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],\n", "}\n", "\n", "# Add Earth Engine layers to the map\n", "Map.addLayer(dem, vis_params, 'SRTM DEM')\n", "Map.addLayer(\n", " landsat7,\n", " {'bands': ['B4', 'B3', 'B2'], 'min': 20, 'max': 200, 'gamma': 2.0},\n", " 'Landsat 7',\n", ")\n", "Map.addLayer(states, {}, \"US States\")\n", "\n", "# Display the map\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using the plotting tool" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "\n", "landsat7 = ee.Image('LANDSAT/LE7_TOA_5YEAR/1999_2003').select(\n", " ['B1', 'B2', 'B3', 'B4', 'B5', 'B7']\n", ")\n", "\n", "landsat_vis = {'bands': ['B4', 'B3', 'B2'], 'gamma': 1.4}\n", "Map.addLayer(landsat7, landsat_vis, \"Landsat\")\n", "\n", "hyperion = ee.ImageCollection('EO1/HYPERION').filter(\n", " ee.Filter.date('2016-01-01', '2017-03-01')\n", ")\n", "\n", "hyperion_vis = {\n", " 'min': 1000.0,\n", " 'max': 14000.0,\n", " 'gamma': 2.5,\n", "}\n", "Map.addLayer(hyperion, hyperion_vis, 'Hyperion')\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.set_plot_options(add_marker_cluster=True, overlay=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Changing layer opacity" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=(40, -100), zoom=4)\n", "\n", "dem = ee.Image('USGS/SRTMGL1_003')\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "\n", "vis_params = {\n", " 'min': 0,\n", " 'max': 4000,\n", " 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],\n", "}\n", "\n", "Map.addLayer(dem, vis_params, 'SRTM DEM', True, 1)\n", "Map.addLayer(states, {}, \"US States\", True)\n", "\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Visualizing raster data\n", "\n", "### Single-band images" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[12, 69], zoom=3)\n", "dem = ee.Image('USGS/SRTMGL1_003')\n", "vis_params = {\n", " 'min': 0,\n", " 'max': 4000,\n", " 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],\n", "}\n", "Map.addLayer(dem, vis_params, 'SRTM DEM')\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vis_params = {\n", " 'bands': ['elevation'],\n", " 'palette': ['333399', ' 00b2b2', ' 99eb85', ' ccbe7d', ' 997c76', ' ffffff'],\n", " 'min': 0.0,\n", " 'max': 6000.0,\n", " 'opacity': 1.0,\n", " 'gamma': 1.0,\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Multi-band images" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "landsat7 = ee.Image('LANDSAT/LE7_TOA_5YEAR/1999_2003')\n", "vis_params = {\n", " 'min': 20,\n", " 'max': 200,\n", " 'gamma': 2,\n", " 'bands': ['B4', 'B3', 'B2'],\n", "}\n", "Map.addLayer(landsat7, vis_params, 'Landsat 7')\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Visualizing vector data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "Map.addLayer(states, {}, \"US States\")\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vis_params = {\n", " 'color': 'ff0000ff',\n", " 'width': 2,\n", " 'lineType': 'solid',\n", " 'fillColor': '00000000',\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "Map.addLayer(states.style(**vis_params), {}, \"US States\")\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating legends\n", "\n", "### Built-in legends" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "legends = geemap.builtin_legends\n", "for legend in legends:\n", " print(legend)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.add_legend(builtin_legend='NLCD')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "Map.add_basemap('HYBRID')\n", "\n", "nlcd = ee.Image('USGS/NLCD_RELEASES/2019_REL/NLCD/2019')\n", "landcover = nlcd.select('landcover')\n", "\n", "Map.addLayer(landcover, {}, 'NLCD Land Cover 2019')\n", "Map.add_legend(\n", " title=\"NLCD Land Cover Classification\", builtin_legend='NLCD', height='465px'\n", ")\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Custom legends" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(add_google_map=False)\n", "\n", "labels = ['One', 'Two', 'Three', 'Four', 'ect']\n", "\n", "# colors can be defined using either hex code or RGB (0-255, 0-255, 0-255)\n", "colors = ['#8DD3C7', '#FFFFB3', '#BEBADA', '#FB8072', '#80B1D3']\n", "# legend_colors = [(255, 0, 0), (127, 255, 0), (127, 18, 25), (36, 70, 180), (96, 68 123)]\n", "\n", "Map.add_legend(labels=labels, colors=colors, position='bottomright')\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "\n", "legend_dict = {\n", " '11 Open Water': '466b9f',\n", " '12 Perennial Ice/Snow': 'd1def8',\n", " '21 Developed, Open Space': 'dec5c5',\n", " '22 Developed, Low Intensity': 'd99282',\n", " '23 Developed, Medium Intensity': 'eb0000',\n", " '24 Developed High Intensity': 'ab0000',\n", " '31 Barren Land (Rock/Sand/Clay)': 'b3ac9f',\n", " '41 Deciduous Forest': '68ab5f',\n", " '42 Evergreen Forest': '1c5f2c',\n", " '43 Mixed Forest': 'b5c58f',\n", " '51 Dwarf Scrub': 'af963c',\n", " '52 Shrub/Scrub': 'ccb879',\n", " '71 Grassland/Herbaceous': 'dfdfc2',\n", " '72 Sedge/Herbaceous': 'd1d182',\n", " '73 Lichens': 'a3cc51',\n", " '74 Moss': '82ba9e',\n", " '81 Pasture/Hay': 'dcd939',\n", " '82 Cultivated Crops': 'ab6c28',\n", " '90 Woody Wetlands': 'b8d9eb',\n", " '95 Emergent Herbaceous Wetlands': '6c9fb8',\n", "}\n", "\n", "nlcd = ee.Image('USGS/NLCD_RELEASES/2019_REL/NLCD/2019')\n", "landcover = nlcd.select('landcover')\n", "\n", "Map.addLayer(landcover, {}, 'NLCD Land Cover 2019')\n", "Map.add_legend(title=\"NLCD Land Cover Classification\", legend_dict=legend_dict)\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating color bars" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "dem = ee.Image('USGS/SRTMGL1_003')\n", "vis_params = {\n", " 'min': 0,\n", " 'max': 4000,\n", " 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],\n", "}\n", "\n", "Map.addLayer(dem, vis_params, 'SRTM DEM')\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.add_colorbar(vis_params, label=\"Elevation (m)\", layer_name=\"SRTM DEM\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.add_colorbar(\n", " vis_params, label=\"Elevation (m)\", layer_name=\"SRTM DEM\", orientation=\"vertical\"\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.add_colorbar(\n", " vis_params,\n", " label=\"Elevation (m)\",\n", " layer_name=\"SRTM DEM\",\n", " orientation=\"vertical\",\n", " transparent_bg=True,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Displaying labels" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4, add_google_map=False)\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "style = {'color': 'black', 'fillColor': \"00000000\"}\n", "Map.addLayer(states.style(**style), {}, \"US States\")\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.add_labels(\n", " data=states,\n", " column=\"STUSPS\",\n", " font_size=\"12pt\",\n", " font_color=\"blue\",\n", " font_family=\"arial\",\n", " font_weight=\"bold\",\n", " draggable=True,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.remove_labels()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "centroids = geemap.vector_centroids(states)\n", "df = geemap.ee_to_df(centroids)\n", "df" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.add_labels(\n", " data=df,\n", " column=\"STUSPS\",\n", " font_size=\"12pt\",\n", " font_color=\"blue\",\n", " font_family=\"arial\",\n", " font_weight=\"bold\",\n", " x='longitude',\n", " y='latitude',\n", ")\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Split-panel maps" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "Map.split_map(left_layer='HYBRID', right_layer='TERRAIN')\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=(40, -100), zoom=4, height=600)\n", "\n", "nlcd_2001 = ee.Image('USGS/NLCD_RELEASES/2019_REL/NLCD/2001').select('landcover')\n", "nlcd_2019 = ee.Image('USGS/NLCD_RELEASES/2019_REL/NLCD/2019').select('landcover')\n", "\n", "left_layer = geemap.ee_tile_layer(nlcd_2001, {}, 'NLCD 2001')\n", "right_layer = geemap.ee_tile_layer(nlcd_2019, {}, 'NLCD 2019')\n", "\n", "Map.split_map(left_layer, right_layer)\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Linked maps" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = (\n", " ee.ImageCollection('COPERNICUS/S2')\n", " .filterDate('2018-09-01', '2018-09-30')\n", " .map(lambda img: img.divide(10000))\n", " .median()\n", ")\n", "\n", "vis_params = [\n", " {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 0.3, 'gamma': 1.3},\n", " {'bands': ['B8', 'B11', 'B4'], 'min': 0, 'max': 0.3, 'gamma': 1.3},\n", " {'bands': ['B8', 'B4', 'B3'], 'min': 0, 'max': 0.3, 'gamma': 1.3},\n", " {'bands': ['B12', 'B12', 'B4'], 'min': 0, 'max': 0.3, 'gamma': 1.3},\n", "]\n", "\n", "labels = [\n", " 'Natural Color (B4/B3/B2)',\n", " 'Land/Water (B8/B11/B4)',\n", " 'Color Infrared (B8/B4/B3)',\n", " 'Vegetation (B12/B11/B4)',\n", "]\n", "\n", "geemap.linked_maps(\n", " rows=2,\n", " cols=2,\n", " height=\"300px\",\n", " center=[38.4151, 21.2712],\n", " zoom=12,\n", " ee_objects=[image],\n", " vis_params=vis_params,\n", " labels=labels,\n", " label_position=\"topright\",\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Timeseries inspector\n", "\n", "### Visualizing image collections" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "collection = ee.ImageCollection('USGS/NLCD_RELEASES/2019_REL/NLCD').select('landcover')\n", "vis_params = {'bands': ['landcover']}\n", "years = collection.aggregate_array('system:index').getInfo()\n", "years" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.ts_inspector(\n", " left_ts=collection,\n", " right_ts=collection,\n", " left_names=years,\n", " right_names=years,\n", " left_vis=vis_params,\n", " right_vis=vis_params,\n", " width='80px',\n", ")\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Time slider\n", "\n", "### Visualizing vegetation data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "collection = (\n", " ee.ImageCollection('MODIS/MCD43A4_006_NDVI')\n", " .filter(ee.Filter.date('2018-06-01', '2018-07-01'))\n", " .select(\"NDVI\")\n", ")\n", "vis_params = {\n", " 'min': 0.0,\n", " 'max': 1.0,\n", " 'palette': [\n", " 'FFFFFF',\n", " 'CE7E45',\n", " 'DF923D',\n", " 'F1B555',\n", " 'FCD163',\n", " '99B718',\n", " '74A901',\n", " '66A000',\n", " '529400',\n", " '3E8601',\n", " '207401',\n", " '056201',\n", " '004C00',\n", " '023B01',\n", " '012E01',\n", " '011D01',\n", " '011301',\n", " ],\n", "}\n", "\n", "Map.add_time_slider(collection, vis_params, time_interval=2)\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Visualizing weather data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "collection = (\n", " ee.ImageCollection('NOAA/GFS0P25')\n", " .filterDate('2018-12-22', '2018-12-23')\n", " .limit(24)\n", " .select('temperature_2m_above_ground')\n", ")\n", "\n", "vis_params = {\n", " 'min': -40.0,\n", " 'max': 35.0,\n", " 'palette': ['blue', 'purple', 'cyan', 'green', 'yellow', 'red'],\n", "}\n", "\n", "labels = [str(n).zfill(2) + \":00\" for n in range(0, 24)]\n", "Map.add_time_slider(collection, vis_params, labels=labels, time_interval=1, opacity=0.8)\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Visualizing Sentinel-2 imagery" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[37.75, -122.45], zoom=12)\n", "\n", "collection = (\n", " ee.ImageCollection('COPERNICUS/S2_SR')\n", " .filterBounds(ee.Geometry.Point([-122.45, 37.75]))\n", " .filterMetadata('CLOUDY_PIXEL_PERCENTAGE', 'less_than', 10)\n", ")\n", "\n", "vis_params = {\"min\": 0, \"max\": 4000, \"bands\": [\"B8\", \"B4\", \"B3\"]}\n", "\n", "Map.add_time_slider(collection, vis_params)\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Shaded relief maps" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import geemap.colormaps as cm\n", "\n", "Map = geemap.Map()\n", "\n", "dem = ee.Image(\"USGS/SRTMGL1_003\")\n", "hillshade = ee.Terrain.hillshade(dem)\n", "\n", "vis = {'min': 0, 'max': 6000, 'palette': cm.palettes.terrain}\n", "blend = geemap.blend(top_layer=dem, top_vis=vis)\n", "\n", "Map.addLayer(hillshade, {}, 'Hillshade')\n", "Map.addLayer(blend, {}, 'Shaded relief')\n", "\n", "Map.add_colorbar(vis, label='Elevation (m)')\n", "Map.setCenter(91.4206, 27.3225, zoom=9)\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "left_layer = geemap.ee_tile_layer(blend, {}, \"Shaded relief\")\n", "right_layer = geemap.ee_tile_layer(hillshade, {}, \"Hillshade\")\n", "Map.split_map(left_layer, right_layer)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nlcd = ee.Image(\"USGS/NLCD_RELEASES/2019_REL/NLCD/2019\").select('landcover')\n", "nlcd_vis = {'bands': ['landcover']}\n", "blend = geemap.blend(nlcd, dem, top_vis=nlcd_vis, expression='a*b')\n", "Map.addLayer(blend, {}, 'Blend NLCD')\n", "Map.add_legend(builtin_legend='NLCD', title='NLCD Land Cover')\n", "Map.setCenter(-118.1310, 35.6816, 10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Elevation contours" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import geemap.colormaps as cm" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "image = ee.Image(\"USGS/SRTMGL1_003\")\n", "hillshade = ee.Terrain.hillshade(image)\n", "Map.addLayer(hillshade, {}, \"Hillshade\")\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vis_params = {'min': 0, \"max\": 5000, \"palette\": cm.palettes.dem}\n", "Map.addLayer(image, vis_params, \"dem\", True, 0.5)\n", "Map.add_colorbar(vis_params, label='Elevation (m)')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "contours = geemap.create_contours(image, 0, 5000, 100, region=None)\n", "Map.addLayer(contours, {'palette': 'black'}, 'contours')\n", "Map.setCenter(-119.3678, 37.1671, 12)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Image descriptive statistics" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "centroid = ee.Geometry.Point([-122.4439, 37.7538])\n", "image = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR').filterBounds(centroid).first()\n", "vis = {'min': 0, 'max': 3000, 'bands': ['B5', 'B4', 'B3']}\n", "\n", "Map.centerObject(centroid, 8)\n", "Map.addLayer(image, vis, \"Landsat-8\")\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image.propertyNames().getInfo()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image.get('CLOUD_COVER').getInfo() # 0.05" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "props = geemap.image_props(image)\n", "props.getInfo()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "stats = geemap.image_stats(image, scale=30)\n", "stats.getInfo()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Zonal statistics with Earth Engine\n", "\n", "### Zonal statistics" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "\n", "# Add NASA SRTM\n", "dem = ee.Image('USGS/SRTMGL1_003')\n", "dem_vis = {\n", " 'min': 0,\n", " 'max': 4000,\n", " 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],\n", "}\n", "Map.addLayer(dem, dem_vis, 'SRTM DEM')\n", "\n", "# Add 5-year Landsat TOA composite\n", "landsat = ee.Image('LANDSAT/LE7_TOA_5YEAR/1999_2003')\n", "landsat_vis = {'bands': ['B4', 'B3', 'B2'], 'gamma': 1.4}\n", "Map.addLayer(landsat, landsat_vis, \"Landsat\", False)\n", "\n", "# Add US Census States\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "style = {'fillColor': '00000000'}\n", "Map.addLayer(states.style(**style), {}, 'US States')\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "out_dem_stats = 'dem_stats.csv'\n", "geemap.zonal_stats(\n", " dem, states, out_dem_stats, stat_type='MEAN', scale=1000, return_fc=False\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "out_landsat_stats = 'landsat_stats.csv'\n", "geemap.zonal_stats(\n", " landsat,\n", " states,\n", " out_landsat_stats,\n", " stat_type='MEAN',\n", " scale=1000,\n", " return_fc=False,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Zonal statistics by group" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "\n", "# Add NLCD data\n", "dataset = ee.Image('USGS/NLCD_RELEASES/2019_REL/NLCD/2019')\n", "landcover = dataset.select('landcover')\n", "Map.addLayer(landcover, {}, 'NLCD 2019')\n", "\n", "# Add US census states\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "style = {'fillColor': '00000000'}\n", "Map.addLayer(states.style(**style), {}, 'US States')\n", "\n", "# Add NLCD legend\n", "Map.add_legend(title='NLCD Land Cover', builtin_legend='NLCD')\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nlcd_stats = 'nlcd_stats.csv'\n", "\n", "geemap.zonal_stats_by_group(\n", " landcover,\n", " states,\n", " nlcd_stats,\n", " stat_type='SUM',\n", " denominator=1e6,\n", " decimal_places=2,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nlcd_stats = 'nlcd_stats_pct.csv'\n", "\n", "geemap.zonal_stats_by_group(\n", " landcover,\n", " states,\n", " nlcd_stats,\n", " stat_type='PERCENTAGE',\n", " denominator=1e6,\n", " decimal_places=2,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Zonal statistics with two images" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "dem = ee.Image('USGS/3DEP/10m')\n", "vis = {'min': 0, 'max': 4000, 'palette': 'terrain'}\n", "Map.addLayer(dem, vis, 'DEM')\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "landcover = ee.Image(\"USGS/NLCD_RELEASES/2019_REL/NLCD/2019\").select('landcover')\n", "Map.addLayer(landcover, {}, 'NLCD 2019')\n", "Map.add_legend(title='NLCD Land Cover Classification', builtin_legend='NLCD')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "stats = geemap.image_stats_by_zone(dem, landcover, reducer='MEAN')\n", "stats" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "stats.to_csv('mean.csv', index=False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.image_stats_by_zone(dem, landcover, out_csv=\"std.csv\", reducer='STD')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Extracting pixel values\n", "\n", "### Extracting values to points" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "\n", "dem = ee.Image('USGS/SRTMGL1_003')\n", "landsat7 = ee.Image('LANDSAT/LE7_TOA_5YEAR/1999_2003')\n", "\n", "vis_params = {\n", " 'min': 0,\n", " 'max': 4000,\n", " 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],\n", "}\n", "\n", "Map.addLayer(\n", " landsat7,\n", " {'bands': ['B4', 'B3', 'B2'], 'min': 20, 'max': 200, 'gamma': 2},\n", " 'Landsat 7',\n", ")\n", "Map.addLayer(dem, vis_params, 'SRTM DEM', True, 1)\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "in_shp = 'us_cities.shp'\n", "url = 'https://github.com/giswqs/data/raw/main/us/us_cities.zip'\n", "geemap.download_file(url)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "in_fc = geemap.shp_to_ee(in_shp)\n", "Map.addLayer(in_fc, {}, 'Cities')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.extract_values_to_points(in_fc, dem, out_fc=\"dem.shp\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.shp_to_gdf(\"dem.shp\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.extract_values_to_points(in_fc, landsat7, 'landsat.csv')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.csv_to_df('landsat.csv')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Extracting pixel values along a transect" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "Map.add_basemap(\"TERRAIN\")\n", "\n", "image = ee.Image('USGS/SRTMGL1_003')\n", "vis_params = {\n", " 'min': 0,\n", " 'max': 4000,\n", " 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],\n", "}\n", "Map.addLayer(image, vis_params, 'SRTM DEM', True, 0.5)\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "line = Map.user_roi\n", "if line is None:\n", " line = ee.Geometry.LineString(\n", " [[-120.2232, 36.3148], [-118.9269, 36.7121], [-117.2022, 36.7562]]\n", " )\n", " Map.addLayer(line, {}, \"ROI\")\n", "Map.centerObject(line)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "reducer = 'mean'\n", "transect = geemap.extract_transect(\n", " image, line, n_segments=100, reducer=reducer, to_pandas=True\n", ")\n", "transect" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.line_chart(\n", " data=transect,\n", " x='distance',\n", " y='mean',\n", " markers=True,\n", " x_label='Distance (m)',\n", " y_label='Elevation (m)',\n", " height=400,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "transect.to_csv('transect.csv')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Interactive region reduction" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "collection = (\n", " ee.ImageCollection('MODIS/061/MOD13A2')\n", " .filterDate('2015-01-01', '2019-12-31')\n", " .select('NDVI')\n", ")\n", "\n", "image = collection.toBands()\n", "\n", "ndvi_vis = {\n", " 'min': 0.0,\n", " 'max': 9000.0,\n", " 'palette': 'ndvi',\n", "}\n", "\n", "Map.addLayer(image, {}, 'MODIS NDVI Time-series')\n", "Map.addLayer(image.select(0), ndvi_vis, 'First image')\n", "\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dates = geemap.image_dates(collection).getInfo()\n", "dates" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "len(dates)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.set_plot_options(add_marker_cluster=True)\n", "Map.roi_reducer = ee.Reducer.mean()\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.extract_values_to_points('ndvi.csv')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Mapping available image count" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "collection = ee.ImageCollection(\"LANDSAT/LC08/C02/T1_L2\")\n", "image = geemap.image_count(\n", " collection, region=None, start_date='2021-01-01', end_date='2022-01-01', clip=False\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "vis = {'min': 0, 'max': 60, 'palette': 'coolwarm'}\n", "Map.addLayer(image, vis, 'Image Count')\n", "Map.add_colorbar(vis, label='Landsat 8 Image Count')\n", "\n", "countries = ee.FeatureCollection(geemap.examples.get_ee_path('countries'))\n", "style = {\"color\": \"00000088\", \"width\": 1, \"fillColor\": \"00000000\"}\n", "Map.addLayer(countries.style(**style), {}, \"Countries\")\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Cloud-free composites" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "collection = ee.ImageCollection('LANDSAT/LC08/C02/T1').filterDate(\n", " '2021-01-01', '2022-01-01'\n", ")\n", "\n", "composite = ee.Algorithms.Landsat.simpleComposite(collection)\n", "\n", "vis_params = {'bands': ['B5', 'B4', 'B3'], 'max': 128}\n", "\n", "Map.setCenter(-122.3578, 37.7726, 10)\n", "Map.addLayer(composite, vis_params, 'TOA composite')\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "customComposite = ee.Algorithms.Landsat.simpleComposite(\n", " **{'collection': collection, 'percentile': 30, 'cloudScoreRange': 5}\n", ")\n", "\n", "Map.addLayer(customComposite, vis_params, 'Custom TOA composite')\n", "Map.setCenter(-105.4317, 52.5536, 11)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vis_params = [\n", " {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 128},\n", " {'bands': ['B5', 'B4', 'B3'], 'min': 0, 'max': 128},\n", " {'bands': ['B7', 'B6', 'B4'], 'min': 0, 'max': 128},\n", " {'bands': ['B6', 'B5', 'B2'], 'min': 0, 'max': 128},\n", "]\n", "\n", "labels = [\n", " 'Natural Color (4, 3, 2)',\n", " 'Color Infrared (5, 4, 3)',\n", " 'Short-Wave Infrared (7, 6 4)',\n", " 'Agriculture (6, 5, 2)',\n", "]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.linked_maps(\n", " rows=2,\n", " cols=2,\n", " height=\"300px\",\n", " center=[37.7726, -122.1578],\n", " zoom=9,\n", " ee_objects=[composite],\n", " vis_params=vis_params,\n", " labels=labels,\n", " label_position=\"topright\",\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exporting images" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "image = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318').select(\n", " ['B5', 'B4', 'B3']\n", ")\n", "\n", "vis_params = {'min': 0, 'max': 0.5, 'gamma': [0.95, 1.1, 1]}\n", "\n", "Map.centerObject(image)\n", "Map.addLayer(image, vis_params, 'Landsat')\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "region = ee.Geometry.BBox(-122.5955, 37.5339, -122.0982, 37.8252)\n", "fc = ee.FeatureCollection(region)\n", "style = {'color': 'ffff00ff', 'fillColor': '00000000'}\n", "Map.addLayer(fc.style(**style), {}, 'ROI')\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### To local drive" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.ee_export_image(image, filename=\"landsat.tif\", scale=30, region=region)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "projection = image.select(0).projection().getInfo()\n", "projection" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "crs = projection['crs']\n", "crs_transform = projection['transform']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.ee_export_image(\n", " image,\n", " filename=\"landsat_crs.tif\",\n", " crs=crs,\n", " crs_transform=crs_transform,\n", " region=region,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.download_ee_image(image, filename='landsat_full.tif', scale=60)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fishnet = geemap.fishnet(image.geometry(), rows=4, cols=4, delta=0.5)\n", "style = {'color': 'ffff00ff', 'fillColor': '00000000'}\n", "Map.addLayer(fishnet.style(**style), {}, 'Fishnet')\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.download_ee_image_tiles(\n", " image, fishnet, out_dir=\".\", prefix=\"landsat_\", crs=\"EPSG:3857\", scale=30\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### To Google Drive" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.ee_export_image_to_drive(\n", " image, description='landsat', folder='export', region=region, scale=30\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### To Asset" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "assetId = 'landsat_sfo'\n", "geemap.ee_export_image_to_asset(\n", " image, description='landsat', assetId=assetId, region=region, scale=30\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### To NumPy array" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "region = ee.Geometry.BBox(-122.5003, 37.7233, -122.3410, 37.8026)\n", "rgb_img = geemap.ee_to_numpy(image, region=region)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(rgb_img.shape)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "rgb_img_test = (255 * ((rgb_img[:, :, 0:3]) + 0.2)).astype('uint8')\n", "plt.imshow(rgb_img_test)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exporting image collections" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "point = ee.Geometry.Point(-99.2222, 46.7816)\n", "collection = (\n", " ee.ImageCollection('USDA/NAIP/DOQQ')\n", " .filterBounds(point)\n", " .filterDate('2008-01-01', '2018-01-01')\n", " .filter(ee.Filter.listContains(\"system:band_names\", \"N\"))\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "collection.aggregate_array('system:index').getInfo()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### To local drive" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "out_dir = os.path.expanduser('~/Downloads')\n", "geemap.ee_export_image_collection(collection, out_dir=out_dir, scale=10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### To Google Drive" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.ee_export_image_collection_to_drive(collection, folder='export', scale=10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### To Assets" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.ee_export_image_collection_to_asset(collection)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exporting feature collections" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "fc = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017').filter(\n", " ee.Filter.eq('wld_rgn', 'Europe')\n", ")\n", "\n", "Map.addLayer(fc, {}, \"Europe\")\n", "Map.centerObject(fc, 3)\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### To local drive" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.ee_to_shp(fc, filename='europe.shp', selectors=None)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.ee_export_vector(fc, filename='europe2.shp')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.ee_to_geojson(fc, filename='europe.geojson')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.ee_to_csv(fc, filename='europe.csv')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gdf = geemap.ee_to_gdf(fc)\n", "gdf" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = geemap.ee_to_df(fc)\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### To Google Drive" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.ee_export_vector_to_drive(\n", " fc, description=\"europe\", fileFormat='SHP', folder=\"export\"\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### To Asset" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.ee_export_vector_to_asset(fc, description='Exporting Europe', assetId='europe')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exporting maps" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "image = ee.Image('USGS/SRTMGL1_003')\n", "vis_params = {\n", " 'min': 0,\n", " 'max': 4000,\n", " 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],\n", "}\n", "Map.addLayer(image, vis_params, 'SRTM DEM', True)\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.to_html(\n", " filename=\"mymap.html\", title=\"Earth Engine Map\", width='100%', height='800px'\n", ")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }