From be10311ed8f767de87f0de731c4cdf672dde6d19 Mon Sep 17 00:00:00 2001 From: elbeejay Date: Wed, 28 Feb 2024 20:20:55 -0500 Subject: [PATCH] Revert "[pre-commit.ci] auto fixes from pre-commit.com hooks" This reverts commit 2eb4db345222d11b6c2d695efe5abc911d28aa1c. --- .gitignore | 2 +- 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, 46 insertions(+), 72 deletions(-) diff --git a/.gitignore b/.gitignore index 73615b4..4006b46 100644 --- a/.gitignore +++ b/.gitignore @@ -132,4 +132,4 @@ dmypy.json _build/ docs/api/ src/GOSTurban/_version.py -docs/notebooks/ +docs/notebooks/ \ No newline at end of file diff --git a/docs/conf.py b/docs/conf.py index 5867762..08d061d 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 4897bf6..a31150b 100644 --- a/notebooks/Replications/URB_NovelUrbanization.py +++ b/notebooks/Replications/URB_NovelUrbanization.py @@ -1,43 +1,39 @@ -import sys -import os -import shutil -import requests +import sys, os, shutil, 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 @@ -47,14 +43,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 b9f0bea..2a78b22 100644 --- a/notebooks/Tutorials/LEI_Example.ipynb +++ b/notebooks/Tutorials/LEI_Example.ipynb @@ -24,16 +24,20 @@ "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" @@ -67,22 +71,18 @@ " temp_folder = \"C:/Temp\"\n", " # clip from global GHSL file\n", " ghsl_folder = \"J:/Data/GLOBAL/GHSL/built\"\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", + " ghsl_files = [os.path.join(ghsl_folder, x) for x in os.listdir(ghsl_folder) if x.endswith(\".tif\")] \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,14 +218,8 @@ ], "source": [ "# This calculates the change from 1990 and 2000\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_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_90_00[\"LEI\"] = lei_90_00[\"old\"] / lei_90_00[\"total\"]\n", "\n", "lei_90_00.head()" @@ -266,12 +260,10 @@ } ], "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", - "\n", - "\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", "def calculate_LEI(val, leap_val, exp_val):\n", " if val <= leap_val:\n", " return 3\n", @@ -279,14 +271,8 @@ " return 2\n", " else:\n", " return 1\n", - "\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)" + "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)" ] }, { @@ -409,9 +395,7 @@ ], "source": [ "# This calculates the change from 2000 and 2014\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_raw = lei.calculate_LEI(input_ghsl, old_list=list(range(1975,2011,5)), new_list=list(range(2015,2030,5)))\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()" @@ -452,12 +436,10 @@ } ], "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", - "\n", - "\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", "def calculate_LEI(val, leap_val, exp_val):\n", " if val <= leap_val:\n", " return 3\n", @@ -465,14 +447,8 @@ " return 2\n", " else:\n", " return 1\n", - "\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)" + "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)" ] }, { diff --git a/notebooks/Tutorials/UrbanAreas_tutorials.ipynb b/notebooks/Tutorials/UrbanAreas_tutorials.ipynb index 8ba47e3..1d20c25 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 f1a28c7..abbc506 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,3 +10,5 @@ elevation geojson hatch git + + diff --git a/src/GOSTurban/LEI.py b/src/GOSTurban/LEI.py index 5293a34..5ee1cf0 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()