Skip to content

Commit

Permalink
Revert "[pre-commit.ci] auto fixes from pre-commit.com hooks"
Browse files Browse the repository at this point in the history
This reverts commit 2eb4db3.
  • Loading branch information
elbeejay committed Feb 29, 2024
1 parent c6ce3ca commit be10311
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,4 @@ dmypy.json
_build/
docs/api/
src/GOSTurban/_version.py
docs/notebooks/
docs/notebooks/
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"sphinx_jupyterbook_latex",
"sphinx.ext.napoleon",
"sphinxcontrib.apidoc",
# "nbsphinx"
#"nbsphinx"
]
external_toc_exclude_missing = True
external_toc_path = "_toc.yml"
Expand Down
30 changes: 13 additions & 17 deletions notebooks/Replications/URB_NovelUrbanization.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

72 changes: 24 additions & 48 deletions notebooks/Tutorials/LEI_Example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)"
]
},
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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()"
Expand Down Expand Up @@ -266,27 +260,19 @@
}
],
"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",
" elif val < exp_val:\n",
" 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)"
]
},
{
Expand Down Expand Up @@ -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()"
Expand Down Expand Up @@ -452,27 +436,19 @@
}
],
"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",
" elif val < exp_val:\n",
" 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)"
]
},
{
Expand Down
8 changes: 4 additions & 4 deletions notebooks/Tutorials/UrbanAreas_tutorials.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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])"
]
},
{
Expand Down Expand Up @@ -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()"
]
},
Expand Down Expand Up @@ -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()"
]
},
Expand Down Expand Up @@ -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')"
]
},
{
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ elevation
geojson
hatch
git


2 changes: 1 addition & 1 deletion src/GOSTurban/LEI.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit be10311

Please sign in to comment.