From 61fe477910344013483376b218933e273ba471c4 Mon Sep 17 00:00:00 2001 From: elbeejay Date: Fri, 10 May 2024 16:59:09 -0400 Subject: [PATCH] adding tests and an integrative test --- src/GOSTurban/UrbanRaster.py | 5 ++-- tests/test_UrbanRaster.py | 15 ++++++++-- tests/test_UrbanRaster_integrated.py | 41 ++++++++++++++++++++++++++++ tests/test_urban_helper.py | 19 +++++++++++++ 4 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 tests/test_UrbanRaster_integrated.py diff --git a/src/GOSTurban/UrbanRaster.py b/src/GOSTurban/UrbanRaster.py index 051cd07..d801f69 100644 --- a/src/GOSTurban/UrbanRaster.py +++ b/src/GOSTurban/UrbanRaster.py @@ -221,8 +221,7 @@ def calculateDegurba( tPrint("%s: Creating Shape %s" % (print_message, idx)) idx = idx + 1 if value > 0: - # RRemove holes from urban shape - # origShape = cShape + # Remove holes from urban shape xx = shape(cShape) xx = Polygon(xx.exterior) cShape = xx.__geo_interface__ @@ -252,7 +251,7 @@ def calculateDegurba( urban_raster = _create_urban_raster(data, urbDens, 22) final_raster = data[0, :, :] * 0 + 11 - # Analyze the high density shapes + # Analyze the urban density shapes if verbose: tPrint(f"{print_message}: extracting URBAN clusters") diff --git a/tests/test_UrbanRaster.py b/tests/test_UrbanRaster.py index 288716f..6072288 100644 --- a/tests/test_UrbanRaster.py +++ b/tests/test_UrbanRaster.py @@ -21,10 +21,21 @@ class TestGeocodeCities: """Tests for the geocode_cities() function.""" # read some of the tutorial data to test - gdf = gpd.read_file("data/tutorial_data/AOI.geojson") + geopath = "data/tutorial_data/AOI.geojson" def test_geocode_cities(self): - gdf = self.gdf + gdf = gpd.read_file(self.geopath) + # run the function - adding city/state/country info + result = UrbanRaster.geocode_cities(gdf) + # assert things about the result + # e.g., should have city/state/country columns + assert "City" in result.columns + assert "State" in result.columns + assert "Country" in result.columns + + def test_geocode_cities_reversed(self): + gdf = gpd.read_file(self.geopath) + gdf["geometry"][0] = gdf["geometry"][0].reverse() # run the function - adding city/state/country info result = UrbanRaster.geocode_cities(gdf) # assert things about the result diff --git a/tests/test_UrbanRaster_integrated.py b/tests/test_UrbanRaster_integrated.py new file mode 100644 index 0000000..d730452 --- /dev/null +++ b/tests/test_UrbanRaster_integrated.py @@ -0,0 +1,41 @@ +"""Integrative tests for UrbanRaster.py based on the tutorial notebook.""" +import os +import rasterio +import geopandas as gpd +import GOSTurban.UrbanRaster as urban + + +def test_urbanraster_integrated(): + # Define input population raster + tutorial_folder = "./data/tutorial_data" + pop_file = os.path.join(tutorial_folder, "wp_2020_1k_AOI.tif") + + inR = rasterio.open(pop_file) + # Initiate the urban calculator + urban_calculator = urban.urbanGriddedPop(inR) + + # Extract the urban extents + # (minimum density 300/km2, minimum total population 5000) + urban_extents = urban_calculator.calculateUrban( + densVal=300, totalPopThresh=5000, smooth=False, queen=False, verbose=True + ) + urban_extents["Type"] = 1 + urban_extents.head() + + # assert that urban_extents is a geopandas.GeoDataFrame + assert isinstance(urban_extents, gpd.GeoDataFrame) + + # Extract the high density urban extents + # (minimum density 1500/km2, minimum total population 50000) + hd_urban_extents = urban_calculator.calculateUrban( + densVal=1500, + totalPopThresh=50000, + smooth=True, + queen=True, # high density extents use queen's case contiguity, and + verbose=True, + ) # High density extents have hole smoothing applied. + hd_urban_extents["Type"] = 2 + hd_urban_extents.head() + + # assert that hd_urban_extents is a geopandas.GeoDataFrame + assert isinstance(hd_urban_extents, gpd.GeoDataFrame) diff --git a/tests/test_urban_helper.py b/tests/test_urban_helper.py index 7092d7e..4095bce 100644 --- a/tests/test_urban_helper.py +++ b/tests/test_urban_helper.py @@ -1,6 +1,7 @@ """Unit tests for the urban_helper.py module""" import pytest # noqa: F401 from GOSTurban import urban_helper +import os import numpy as np from unittest import mock import geopandas as gpd @@ -96,3 +97,21 @@ def test_init(self, tmp_path): # process the dem uc.process_dem() # doesn't return anything or change any attributes so nothing to assert + + def test_init_02(self, tmp_path): + outf = os.path.join(tmp_path, "fin_folder") + os.makedirs(outf) + os.makedirs(os.path.join(outf, "FINAL_STANDARD")) + admin_pth = os.path.join(outf, "FINAL_STANDARD", "usa_adm.shp") + with open(admin_pth, "w") as f: + f.write("fake shapefile") + + uc = urban_helper.urban_country( + iso3="USA", + output_folder=outf, + country_bounds=[], + pop_files=[], + ) + # minor assertions about class attributes + assert uc.admin_shp == admin_pth + assert isinstance(uc.pop_files, list)