From 79f1fa9625b503fea082ac4429e0cc6d6d07eb1b Mon Sep 17 00:00:00 2001 From: dfguerrerom Date: Mon, 4 Nov 2024 11:49:38 +0100 Subject: [PATCH] filter by countries --- ...obal_SDG_15_4_2_Sub_A_Default_values.ipynb | 201 +++++++++++++++++- ...obal_SDG_15_4_2_Sub_B_Default_values.ipynb | 7 +- component/scripts/gee.py | 6 +- 3 files changed, 199 insertions(+), 15 deletions(-) diff --git a/Sepal_Global_SDG_15_4_2_Sub_A_Default_values.ipynb b/Sepal_Global_SDG_15_4_2_Sub_A_Default_values.ipynb index 19702fe..5317e80 100644 --- a/Sepal_Global_SDG_15_4_2_Sub_A_Default_values.ipynb +++ b/Sepal_Global_SDG_15_4_2_Sub_A_Default_values.ipynb @@ -13,7 +13,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -23,7 +23,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -34,9 +34,173 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/javascript": "/*******************************************************************************\n * remove any links from fontawesome 5 created by jupyter in favor of\n * fontawesome 6. to be removed when Jupyter updates it\n */\n\nfunction remove_fa5() {\n let links = document.querySelectorAll(\n \"link[href^='https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@^5']\"\n );\n\n links.forEach((link) => link.remove());\n}\n\nif (document.readyState != \"loading\") remove_fa5();\nelse document.addEventListener(\"DOMContentLoaded\", remove_fa5);\n", + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ "import os\n", "from datetime import datetime\n", @@ -61,7 +225,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -77,7 +241,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": { "id": "t9C1qT5bGoqt" }, @@ -110,7 +274,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -121,7 +285,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": { "id": "mSvTM29TnndR" }, @@ -143,7 +307,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -165,11 +329,25 @@ "metadata": { "id": "BiuBEJwPue2v" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Length of admin boundaries to process 1\n", + "Length of distinct admin boundaries to process 1\n" + ] + } + ], "source": [ "# admin boundary feature collection\n", "admin_boundaries = ee.FeatureCollection(admin_asset_id)\n", "\n", + "# Uncomment to process all countries\n", + "# codes_to_process = [840]\n", + "# admin_boundaries = admin_boundaries.filter(ee.Filter.inList(\"M49Code\", codes_to_process))\n", + "\n", + "\n", "# list to process\n", "list_of_countries = admin_boundaries.aggregate_array(admin_asset_property_name).getInfo()\n", "\n", @@ -224,7 +402,8 @@ " rsa=False,\n", " dem=DEM_DEFAULT, #default digital elevation model (DEM). Relevant for the real surface area (RSA) implementation.\n", " lc_years= year,\n", - " transition_matrix=False\n", + " transition_matrix=False,\n", + " scale=None # None means native resolution\n", " )\n", " ).set(\"process_id\", year[0][\"year\"])\n", " for year in get_a_years(a_years) # creates GEE images and runs stats on each. Images to run are in the 'a_years\" dictionary (above)\n", diff --git a/Sepal_Global_SDG_15_4_2_Sub_B_Default_values.ipynb b/Sepal_Global_SDG_15_4_2_Sub_B_Default_values.ipynb index a972ee8..21aa823 100644 --- a/Sepal_Global_SDG_15_4_2_Sub_B_Default_values.ipynb +++ b/Sepal_Global_SDG_15_4_2_Sub_B_Default_values.ipynb @@ -171,6 +171,10 @@ "# admin boundary feature collection\n", "admin_boundaries = ee.FeatureCollection(admin_asset_id)\n", "\n", + "# # Uncomment to process all countries\n", + "# codes_to_process = [840]\n", + "# admin_boundaries = admin_boundaries.filter(ee.Filter.inList(\"M49Code\", codes_to_process))\n", + "\n", "# list to process\n", "list_of_countries = admin_boundaries.aggregate_array(admin_asset_property_name).getInfo()\n", "\n", @@ -284,6 +288,7 @@ " dem=DEM_DEFAULT,\n", " lc_years= year,\n", " transition_matrix=default_transition_matrix_path\n", + " scale=None \n", " )\n", " ).set(\"process_id\", \"_\".join([str(y[\"year\"]) for y in year]))\n", " for year in years \n", @@ -438,7 +443,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.15" + "version": "3.10.0" } }, "nbformat": 4, diff --git a/component/scripts/gee.py b/component/scripts/gee.py index 9bdc27e..f87702c 100644 --- a/component/scripts/gee.py +++ b/component/scripts/gee.py @@ -99,7 +99,7 @@ def reduce_regions( dem: str, lc_years: List[Tuple[Dict]], transition_matrix: str, - scale: int = None, + scale: Optional[int] = None, ) -> ee.Dictionary: """Reduce land use/land cover image to bioclimatic belts regions using planimetric or real surface area @@ -131,11 +131,11 @@ def reduce_regions( # When using rsa, we need to use the dem scale, otherwise # we will end with wrong results. image_area = get_real_surface_area(dem, aoi) - scale = ee_lc_start.projection().nominalScale().getInfo() + scale = scale or ee_lc_start.projection().nominalScale().getInfo() else: # Otherwise, we will use the coarse scale to the output. image_area = ee.Image.pixelArea() - scale = ( + scale = scale or ( ee_lc_start.projection() .nominalScale() .max(ee_lc_start.projection().nominalScale())