From f4ad4279c6ee4c6bfb2f0134420189279e5340d9 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 1 May 2024 13:34:58 +0000 Subject: [PATCH 1/2] Bump docutils from 0.17.1 to 0.21.2 Bumps [docutils](https://docutils.sourceforge.io) from 0.17.1 to 0.21.2. --- updated-dependencies: - dependency-name: docutils dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 644e22e..ffdc4b0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,7 +36,7 @@ dependencies = [ [project.optional-dependencies] dev = ["pytest", "pytest-cov"] docs = [ - "docutils==0.17.1", + "docutils==0.21.2", "jupyter-book>=1,<2", "sphinxcontrib-apidoc>=0.4.0,<1", ] From acc38751111cabf230c4f23c0b185440ff852628 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 1 May 2024 13:35:14 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/_toc.yml | 1 - docs/conf.py | 2 +- .../Replications/URB_NovelUrbanization.py | 30 ++++---- notebooks/Tutorials/LEI_Example.ipynb | 72 ++++++++++++------- .../Tutorials/UrbanAreas_tutorials.ipynb | 8 +-- requirements.txt | 2 - src/GOSTurban/LEI.py | 2 +- 7 files changed, 71 insertions(+), 46 deletions(-) diff --git a/docs/_toc.yml b/docs/_toc.yml index 8000614..209f5d8 100755 --- a/docs/_toc.yml +++ b/docs/_toc.yml @@ -15,4 +15,3 @@ parts: chapters: - file: notebooks/Tutorials/UrbanAreas_tutorials.ipynb - file: notebooks/Tutorials/LEI_Example.ipynb - diff --git a/docs/conf.py b/docs/conf.py index 08d061d..5867762 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -24,7 +24,7 @@ "sphinx_jupyterbook_latex", "sphinx.ext.napoleon", "sphinxcontrib.apidoc", - #"nbsphinx" + # "nbsphinx" ] external_toc_exclude_missing = True external_toc_path = "_toc.yml" diff --git a/notebooks/Replications/URB_NovelUrbanization.py b/notebooks/Replications/URB_NovelUrbanization.py index a31150b..4897bf6 100644 --- a/notebooks/Replications/URB_NovelUrbanization.py +++ b/notebooks/Replications/URB_NovelUrbanization.py @@ -1,39 +1,43 @@ -import sys, os, shutil, requests +import sys +import os +import shutil +import requests import rasterio -import pandas as pd -import geopandas as gpd -import numpy as np import GOSTurban.UrbanRaster as urban + def download_pop_file(url, filename): # Open the url r = requests.get(url) # Set decode_content value to True, otherwise the downloaded image file's size will be zero. r.raw.decode_content = True # Open a local file with wb ( write binary ) permission. - with open(filename,'wb') as f: + with open(filename, "wb") as f: shutil.copyfileobj(r.raw, f) + def main(iso3, out_folder): # download the population data - wp_url = f'https://data.worldpop.org/GIS/Population/Global_2000_2020_1km/2020/{iso3.upper()}/{iso3.lower()}_ppp_2020_1km_Aggregated.tif' - print (wp_url) + wp_url = f"https://data.worldpop.org/GIS/Population/Global_2000_2020_1km/2020/{iso3.upper()}/{iso3.lower()}_ppp_2020_1km_Aggregated.tif" + print(wp_url) if not os.path.exists(out_folder): os.makedirs(out_folder) - out_file = os.path.join(out_folder, f'{iso3}_ppp_2020_1km_Aggregated.tif') + out_file = os.path.join(out_folder, f"{iso3}_ppp_2020_1km_Aggregated.tif") out_urban = os.path.join(out_folder, "urban_extents.geojson") out_hd_urban = os.path.join(out_folder, "hd_urban_extents.geojson") - + try: if not os.path.exists(out_file): download_pop_file(wp_url, out_file) except: print(f"Could not download national population data for {iso3} from {wp_url}") - print("If you can manually download to the defined out_folder, the script will run") + print( + "If you can manually download to the defined out_folder, the script will run" + ) if os.path.exists(out_file): - inR = rasterio.open(out_file) + inR = rasterio.open(out_file) urban_calculator = urban.urbanGriddedPop(inR) urban_extents = urban_calculator.calculateUrban( densVal=300, totalPopThresh=5000, smooth=False, queen=False @@ -43,14 +47,14 @@ def main(iso3, out_folder): densVal=1500, totalPopThresh=50000, smooth=True, - queen=True, # high density extents use queen's case contiguity, and are smoothed + queen=True, # high density extents use queen's case contiguity, and are smoothed ) urban_extents.to_file(out_urban, driver="GeoJSON") hd_urban_extents.to_file(out_hd_urban, driver="GeoJSON") + if __name__ == "__main__": iso3 = sys.argv[1] out_folder = sys.argv[2] main(iso3, out_folder) - diff --git a/notebooks/Tutorials/LEI_Example.ipynb b/notebooks/Tutorials/LEI_Example.ipynb index 2a78b22..b9f0bea 100644 --- a/notebooks/Tutorials/LEI_Example.ipynb +++ b/notebooks/Tutorials/LEI_Example.ipynb @@ -24,20 +24,16 @@ "outputs": [], "source": [ "import os\n", - "import sys\n", - "import importlib\n", "import rasterio\n", "import rasterio.features\n", "\n", "import geopandas as gpd\n", "import pandas as pd\n", - "import numpy as np\n", "\n", "import GOSTrocks.rasterMisc as rMisc\n", "import GOSTrocks.ghslMisc as ghslMisc\n", "import GOSTurban.LEI as lei\n", "import GOSTrocks.mapMisc as mapMisc\n", - "import GOSTrocks.rasterMisc as rMisc\n", "\n", "%load_ext autoreload\n", "%autoreload 2" @@ -71,18 +67,22 @@ " temp_folder = \"C:/Temp\"\n", " # clip from global GHSL file\n", " ghsl_folder = \"J:/Data/GLOBAL/GHSL/built\"\n", - " ghsl_files = [os.path.join(ghsl_folder, x) for x in os.listdir(ghsl_folder) if x.endswith(\".tif\")] \n", + " ghsl_files = [\n", + " os.path.join(ghsl_folder, x)\n", + " for x in os.listdir(ghsl_folder)\n", + " if x.endswith(\".tif\")\n", + " ]\n", " inA = gpd.read_file(aoi_file)\n", - " \n", + "\n", " temp_ghsl_files = []\n", " for ghsl_file in ghsl_files:\n", " temp_file = os.path.join(temp_folder, os.path.basename(ghsl_file))\n", " temp_ghsl_files.append(temp_file)\n", " if not os.path.exists(temp_file):\n", " rMisc.clipRaster(rasterio.open(ghsl_file), inA, temp_file)\n", - " \n", + "\n", " ghsl_res, ghsl_profile = ghslMisc.combine_ghsl_annual(temp_ghsl_files)\n", - " with rasterio.open(input_ghsl, 'w', **ghsl_profile) as outR:\n", + " with rasterio.open(input_ghsl, \"w\", **ghsl_profile) as outR:\n", " outR.write_band(1, ghsl_res)" ] }, @@ -116,7 +116,7 @@ "source": [ "ghsl_r = rasterio.open(input_ghsl)\n", "ghsl_d = ghsl_r.read()\n", - "ghsl_d[ghsl_d == ghsl_r.meta['nodata']] = 0\n", + "ghsl_d[ghsl_d == ghsl_r.meta[\"nodata\"]] = 0\n", "\n", "thresh = list(range(1975, 2031, 5))\n", "with rMisc.create_rasterio_inmemory(ghsl_r.profile, ghsl_d) as temp_ghsl:\n", @@ -218,8 +218,14 @@ ], "source": [ "# This calculates the change from 1990 and 2000\n", - "lei_raw = lei.calculate_LEI(input_ghsl, old_list=list(range(1975,1991,5)), new_list=list(range(1995,2001,5)))\n", - "lei_90_00 = gpd.GeoDataFrame(pd.DataFrame(lei_raw, columns=[\"geometry\", \"old\", \"total\"]), geometry='geometry', crs=ghsl_r.crs)\n", + "lei_raw = lei.calculate_LEI(\n", + " input_ghsl, old_list=list(range(1975, 1991, 5)), new_list=list(range(1995, 2001, 5))\n", + ")\n", + "lei_90_00 = gpd.GeoDataFrame(\n", + " pd.DataFrame(lei_raw, columns=[\"geometry\", \"old\", \"total\"]),\n", + " geometry=\"geometry\",\n", + " crs=ghsl_r.crs,\n", + ")\n", "lei_90_00[\"LEI\"] = lei_90_00[\"old\"] / lei_90_00[\"total\"]\n", "\n", "lei_90_00.head()" @@ -260,10 +266,12 @@ } ], "source": [ - "#Map LEI results\n", - "leap_val=0.30\n", - "exp_val=0.70\n", - "lei_90_00['area'] = lei_90_00['geometry'].apply(lambda x: x.area)\n", + "# Map LEI results\n", + "leap_val = 0.30\n", + "exp_val = 0.70\n", + "lei_90_00[\"area\"] = lei_90_00[\"geometry\"].apply(lambda x: x.area)\n", + "\n", + "\n", "def calculate_LEI(val, leap_val, exp_val):\n", " if val <= leap_val:\n", " return 3\n", @@ -271,8 +279,14 @@ " return 2\n", " else:\n", " return 1\n", - "lei_90_00[\"class\"] = lei_90_00[\"LEI\"].apply(lambda x: calculate_LEI(x, leap_val, exp_val))\n", - "mapMisc.static_map_vector(lei_90_00, \"class\", edgecolor='match', colormap=\"Dark2\")#, basemap=ctx.providers.CartoDB.Voyager)" + "\n", + "\n", + "lei_90_00[\"class\"] = lei_90_00[\"LEI\"].apply(\n", + " lambda x: calculate_LEI(x, leap_val, exp_val)\n", + ")\n", + "mapMisc.static_map_vector(\n", + " lei_90_00, \"class\", edgecolor=\"match\", colormap=\"Dark2\"\n", + ") # , basemap=ctx.providers.CartoDB.Voyager)" ] }, { @@ -395,7 +409,9 @@ ], "source": [ "# This calculates the change from 2000 and 2014\n", - "lei_raw = lei.calculate_LEI(input_ghsl, old_list=list(range(1975,2011,5)), new_list=list(range(2015,2030,5)))\n", + "lei_raw = lei.calculate_LEI(\n", + " input_ghsl, old_list=list(range(1975, 2011, 5)), new_list=list(range(2015, 2030, 5))\n", + ")\n", "lei_00_14 = pd.DataFrame(lei_raw, columns=[\"geometry\", \"old\", \"total\"])\n", "lei_00_14[\"LEI\"] = lei_00_14[\"old\"] / lei_00_14[\"total\"]\n", "lei_00_14.head()" @@ -436,10 +452,12 @@ } ], "source": [ - "#Map LEI results\n", - "leap_val=0.30\n", - "exp_val=0.70\n", - "lei_90_00['area'] = lei_90_00['geometry'].apply(lambda x: x.area)\n", + "# Map LEI results\n", + "leap_val = 0.30\n", + "exp_val = 0.70\n", + "lei_90_00[\"area\"] = lei_90_00[\"geometry\"].apply(lambda x: x.area)\n", + "\n", + "\n", "def calculate_LEI(val, leap_val, exp_val):\n", " if val <= leap_val:\n", " return 3\n", @@ -447,8 +465,14 @@ " return 2\n", " else:\n", " return 1\n", - "lei_90_00[\"class\"] = lei_90_00[\"LEI\"].apply(lambda x: calculate_LEI(x, leap_val, exp_val))\n", - "mapMisc.static_map_vector(lei_90_00, \"class\", edgecolor='match', colormap=\"Dark2\")#, basemap=ctx.providers.CartoDB.Voyager)" + "\n", + "\n", + "lei_90_00[\"class\"] = lei_90_00[\"LEI\"].apply(\n", + " lambda x: calculate_LEI(x, leap_val, exp_val)\n", + ")\n", + "mapMisc.static_map_vector(\n", + " lei_90_00, \"class\", edgecolor=\"match\", colormap=\"Dark2\"\n", + ") # , basemap=ctx.providers.CartoDB.Voyager)" ] }, { diff --git a/notebooks/Tutorials/UrbanAreas_tutorials.ipynb b/notebooks/Tutorials/UrbanAreas_tutorials.ipynb index 1d20c25..8ba47e3 100644 --- a/notebooks/Tutorials/UrbanAreas_tutorials.ipynb +++ b/notebooks/Tutorials/UrbanAreas_tutorials.ipynb @@ -100,7 +100,7 @@ } ], "source": [ - "mapMisc.static_map_raster(inR, thresh=[1,5,50,100,300,1000,3000])" + "mapMisc.static_map_raster(inR, thresh=[1, 5, 50, 100, 300, 1000, 3000])" ] }, { @@ -223,7 +223,7 @@ "urban_extents = urban_calculator.calculateUrban(\n", " densVal=300, totalPopThresh=5000, smooth=False, queen=False, verbose=True\n", ")\n", - "urban_extents['Type'] = 1\n", + "urban_extents[\"Type\"] = 1\n", "urban_extents.head()" ] }, @@ -406,7 +406,7 @@ " queen=True, # high density extents use queen's case contiguity, and\n", " verbose=True,\n", ") # High density extents have hole smoothing applied.\n", - "hd_urban_extents['Type'] = 2\n", + "hd_urban_extents[\"Type\"] = 2\n", "hd_urban_extents.head()" ] }, @@ -491,7 +491,7 @@ ], "source": [ "combo_extents = pd.concat([urban_extents, hd_urban_extents])\n", - "mapMisc.static_map_vector(combo_extents, \"Type\", colormap='magma')" + "mapMisc.static_map_vector(combo_extents, \"Type\", colormap=\"magma\")" ] }, { diff --git a/requirements.txt b/requirements.txt index abbc506..f1a28c7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,5 +10,3 @@ elevation geojson hatch git - - diff --git a/src/GOSTurban/LEI.py b/src/GOSTurban/LEI.py index 5ee1cf0..5293a34 100755 --- a/src/GOSTurban/LEI.py +++ b/src/GOSTurban/LEI.py @@ -195,4 +195,4 @@ def calculate_LEI(val, leap_val, exp_val): res["class"] = res["LEI"].apply(lambda x: calculate_LEI(x, leap_val, exp_val)) xx = res.groupby("class") - return xx['area'].sum() + return xx["area"].sum()