{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\"Open\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 geemap" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import ee\n", "import geemap\n", "import os" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.show_youtube('cORcGGH03gg')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Analyzing National Land Cover Database (NLCD)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add NLCD data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dataset = ee.Image('USGS/NLCD/NLCD2016')\n", "landcover = ee.Image(dataset.select('landcover'))\n", "Map.addLayer(landcover, {}, 'NLCD 2016')\n", "\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "Map.addLayer(states, {}, 'US States')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.add_legend(builtin_legend='NLCD')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Calculate land cover compostion of each US state" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')\n", "nlcd_stats = os.path.join(out_dir, 'nlcd_stats.csv') \n", "\n", "if not os.path.exists(out_dir):\n", " os.makedirs(out_dir)\n", " \n", "# statistics_type can be either 'SUM' or 'PERCENTAGE'\n", "# denominator can be used to convert square meters to other areal units, such as square kilimeters\n", "geemap.zonal_statistics_by_group(landcover, states, nlcd_stats, statistics_type='SUM', denominator=1000000, decimal_places=2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.create_download_link(nlcd_stats)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Analyzing Global Land Cover" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add MODIS global land cover data\n", "\n", "MODIS MCD12Q1.051 Land Cover Type Yearly Global 500m \n", "\n", "https://developers.google.com/earth-engine/datasets/catalog/MODIS_051_MCD12Q1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "landcover = ee.Image('MODIS/051/MCD12Q1/2013_01_01') \\\n", " .select('Land_Cover_Type_1')\n", "\n", "Map.setCenter(6.746, 46.529, 2)\n", "Map.addLayer(landcover, {}, 'MODIS Land Cover')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.add_legend(builtin_legend='MODIS/051/MCD12Q1')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "countries_shp = '../data/countries.shp'\n", "countries = geemap.shp_to_ee(countries_shp)\n", "Map.addLayer(countries, {}, 'Countries')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')\n", "global_stats = os.path.join(out_dir, 'global_stats.csv') \n", "\n", "# statistics_type can be either 'SUM' or 'PERCENTAGE'\n", "# denominator can be used to convert square meters to other areal units, such as square kilimeters\n", "geemap.zonal_statistics_by_group(landcover, countries, global_stats, statistics_type='PERCENTAGE', denominator=1000000, decimal_places=2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.create_download_link(global_stats)" ] } ], "metadata": { "hide_input": false, "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.5" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": true, "title_cell": "Table of Contents", "title_sidebar": "Table of Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": true }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }