{ "cells": [ { "cell_type": "markdown", "id": "391be3a2", "metadata": {}, "source": [ "# Notebook Example" ] }, { "cell_type": "code", "execution_count": 1, "id": "threatened-relative", "metadata": {}, "outputs": [], "source": [ "from gee_zonal import Catalog, ZonalStats\n", "import ee\n", "import geopandas as gpd" ] }, { "cell_type": "markdown", "id": "a0e849fb", "metadata": {}, "source": [ "## Input Features" ] }, { "cell_type": "markdown", "id": "blank-breakdown", "metadata": {}, "source": [ "Input target features can be referenced directly as a GEE asset, or can be supplied\n", "as a ``geopandas.GeoDataFrame``, or a path to a shapefile/GeoJSON (will be automatically\n", "converted to ``ee.FeatureCollection``)." ] }, { "cell_type": "code", "execution_count": 2, "id": "structural-course", "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/plain": [ "{'type': 'FeatureCollection',\n", " 'columns': {'Area': 'Float', 'Id': 'Integer', 'system:index': 'String'},\n", " 'version': 1570633589922603,\n", " 'id': 'users/afche18/Ethiopia_AOI',\n", " 'properties': {'system:asset_size': 11288},\n", " 'features': [{'type': 'Feature',\n", " 'geometry': {'type': 'Polygon',\n", " 'coordinates': [[[42.06158510035763, 9.397823252053993],\n", " [42.06119314725495, 9.20601121183654],\n", " [42.18901118834056, 9.20584119448768],\n", " [42.316813505733144, 9.205625705724506],\n", " [42.3125304107434, 9.301530485292622],\n", " [42.30825425836041, 9.397435608019856],\n", " [42.184926941544006, 9.39765098188209],\n", " [42.06158510035763, 9.397823252053993]]]},\n", " 'id': '00000000000000000000',\n", " 'properties': {'Area': 586.656, 'Id': 0}},\n", " {'type': 'Feature',\n", " 'geometry': {'type': 'Polygon',\n", " 'coordinates': [[[35.164007676333824, 7.315648092803201],\n", " [35.164482803968816, 7.270435726027228],\n", " [35.25634053363444, 7.210965732212082],\n", " [35.348179623989004, 7.151464739986416],\n", " [35.50236858461292, 7.092454680579472],\n", " [35.51569123843248, 7.11196715143379],\n", " [35.461916853024185, 7.22665425180959],\n", " [35.41052043644675, 7.307558148286968],\n", " [35.23872404563077, 7.330402310785946],\n", " [35.164007676333824, 7.315648092803201]]]},\n", " 'id': '00000000000000000001',\n", " 'properties': {'Area': 523.326, 'Id': 1}}]}" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "AOIs = ee.FeatureCollection('users/afche18/Ethiopia_AOI')\n", "AOIs.getInfo()" ] }, { "cell_type": "code", "execution_count": 3, "id": "0dfd1424", "metadata": {}, "outputs": [], "source": [ "gdf = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))" ] }, { "cell_type": "code", "execution_count": 4, "id": "0259823c", "metadata": { "tags": [ "scroll-output" ] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
pop_estcontinentnameiso_a3gdp_md_estgeometry
0889953.0OceaniaFijiFJI5496MULTIPOLYGON (((180.00000 -16.06713, 180.00000...
158005463.0AfricaTanzaniaTZA63177POLYGON ((33.90371 -0.95000, 34.07262 -1.05982...
2603253.0AfricaW. SaharaESH907POLYGON ((-8.66559 27.65643, -8.66512 27.58948...
337589262.0North AmericaCanadaCAN1736425MULTIPOLYGON (((-122.84000 49.00000, -122.9742...
4328239523.0North AmericaUnited States of AmericaUSA21433226MULTIPOLYGON (((-122.84000 49.00000, -120.0000...
\n", "
" ], "text/plain": [ " pop_est continent name iso_a3 gdp_md_est \\\n", "0 889953.0 Oceania Fiji FJI 5496 \n", "1 58005463.0 Africa Tanzania TZA 63177 \n", "2 603253.0 Africa W. Sahara ESH 907 \n", "3 37589262.0 North America Canada CAN 1736425 \n", "4 328239523.0 North America United States of America USA 21433226 \n", "\n", " geometry \n", "0 MULTIPOLYGON (((180.00000 -16.06713, 180.00000... \n", "1 POLYGON ((33.90371 -0.95000, 34.07262 -1.05982... \n", "2 POLYGON ((-8.66559 27.65643, -8.66512 27.58948... \n", "3 MULTIPOLYGON (((-122.84000 49.00000, -122.9742... \n", "4 MULTIPOLYGON (((-122.84000 49.00000, -120.0000... " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gdf = gdf.head().copy()\n", "gdf" ] }, { "cell_type": "markdown", "id": "29445754", "metadata": {}, "source": [ "## Initialize Zonal Stats" ] }, { "cell_type": "markdown", "id": "ecological-oxford", "metadata": {}, "source": [ "### Option A: Retrieve results directly \n", "Output: DataFrame with statistics \n", "*Recommended for small areas / low n*" ] }, { "cell_type": "code", "execution_count": 5, "id": "skilled-angola", "metadata": {}, "outputs": [], "source": [ "zs = ZonalStats(\n", " collection_id='LANDSAT/LC08/C01/T1_8DAY_NDVI', \n", " target_features=AOIs, \n", " statistic_type=\"all\",\n", " frequency=\"annual\",\n", " temporal_stat=\"mean\"\n", ")" ] }, { "cell_type": "code", "execution_count": 6, "id": "proper-woman", "metadata": { "tags": [ "scroll-output" ] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
2013_NDVI_mean_max2013_NDVI_mean_mean2013_NDVI_mean_min2013_NDVI_mean_stdDev2014_NDVI_mean_max2014_NDVI_mean_mean2014_NDVI_mean_min2014_NDVI_mean_stdDev2015_NDVI_mean_max2015_NDVI_mean_mean...2021_NDVI_mean_max2021_NDVI_mean_mean2021_NDVI_mean_min2021_NDVI_mean_stdDev2022_NDVI_mean_max2022_NDVI_mean_mean2022_NDVI_mean_min2022_NDVI_mean_stdDevAreaId
000000000000000000000.4788390.3129250.1254590.0410140.5366930.3282550.1335190.0397590.5478180.325812...0.4358560.2920270.1075150.037247NaNNaNNaNNaN586.6560.0
000000000000000000010.6699670.5611630.0589390.0566260.5498130.4490390.0280170.0448160.6009750.491076...0.6307990.4787780.1073420.057189NaNNaNNaNNaN523.3261.0
\n", "

2 rows × 42 columns

\n", "
" ], "text/plain": [ " 2013_NDVI_mean_max 2013_NDVI_mean_mean \\\n", "00000000000000000000 0.478839 0.312925 \n", "00000000000000000001 0.669967 0.561163 \n", "\n", " 2013_NDVI_mean_min 2013_NDVI_mean_stdDev \\\n", "00000000000000000000 0.125459 0.041014 \n", "00000000000000000001 0.058939 0.056626 \n", "\n", " 2014_NDVI_mean_max 2014_NDVI_mean_mean \\\n", "00000000000000000000 0.536693 0.328255 \n", "00000000000000000001 0.549813 0.449039 \n", "\n", " 2014_NDVI_mean_min 2014_NDVI_mean_stdDev \\\n", "00000000000000000000 0.133519 0.039759 \n", "00000000000000000001 0.028017 0.044816 \n", "\n", " 2015_NDVI_mean_max 2015_NDVI_mean_mean ... \\\n", "00000000000000000000 0.547818 0.325812 ... \n", "00000000000000000001 0.600975 0.491076 ... \n", "\n", " 2021_NDVI_mean_max 2021_NDVI_mean_mean \\\n", "00000000000000000000 0.435856 0.292027 \n", "00000000000000000001 0.630799 0.478778 \n", "\n", " 2021_NDVI_mean_min 2021_NDVI_mean_stdDev \\\n", "00000000000000000000 0.107515 0.037247 \n", "00000000000000000001 0.107342 0.057189 \n", "\n", " 2022_NDVI_mean_max 2022_NDVI_mean_mean \\\n", "00000000000000000000 NaN NaN \n", "00000000000000000001 NaN NaN \n", "\n", " 2022_NDVI_mean_min 2022_NDVI_mean_stdDev Area Id \n", "00000000000000000000 NaN NaN 586.656 0.0 \n", "00000000000000000001 NaN NaN 523.326 1.0 \n", "\n", "[2 rows x 42 columns]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "res = zs.runZonalStats()\n", "res" ] }, { "cell_type": "markdown", "id": "mounted-voice", "metadata": {}, "source": [ "### Option B: Submit a task \n", "Output: csv table on Google Drive \n", "*Recommended for big areas / high n*" ] }, { "cell_type": "code", "execution_count": 12, "id": "simplified-billy", "metadata": {}, "outputs": [], "source": [ "zs = ZonalStats(\n", " collection_id='LANDSAT/LC08/C01/T1_8DAY_NDVI', \n", " target_features=AOIs, \n", " statistic_type=\"mean\",\n", " frequency=\"annual\",\n", " temporal_stat=\"mean\",\n", " scale=1000,\n", " output_dir=\"pretty_folder\",\n", " output_name=\"pretty_file\"\n", ")" ] }, { "cell_type": "code", "execution_count": 13, "id": "fallen-discretion", "metadata": {}, "outputs": [], "source": [ "zs.runZonalStats()" ] }, { "cell_type": "code", "execution_count": 15, "id": "amber-financing", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Completed\n", "Runtime: 0 minutes and 7 seconds\n" ] } ], "source": [ "zs.reportRunTime()" ] } ], "metadata": { "kernelspec": { "display_name": "Package Test", "language": "python", "name": "pkg" }, "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.0" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "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": 5 }