From 158184a8c8c083f2d9120008cd8bf790361f34d3 Mon Sep 17 00:00:00 2001 From: Frederique Date: Wed, 13 Dec 2023 13:58:31 +0100 Subject: [PATCH 1/2] Implemented that the SVI and equity shapefiles are saved in the right folder --- hydromt_fiat/fiat.py | 6 ++++-- hydromt_fiat/workflows/equity_data.py | 14 ++++++-------- .../workflows/social_vulnerability_index.py | 9 ++++----- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/hydromt_fiat/fiat.py b/hydromt_fiat/fiat.py index 5ed499f2..6f9cc102 100644 --- a/hydromt_fiat/fiat.py +++ b/hydromt_fiat/fiat.py @@ -652,7 +652,8 @@ def setup_social_vulnerability_index( county_numbers = get_us_county_numbers(counties, us_states_counties) # Create SVI object - svi = SocialVulnerabilityIndex(self.data_catalog, self.logger) + save_folder = str(Path(self.root) / "exposure" / "SVI") + svi = SocialVulnerabilityIndex(self.data_catalog, self.logger, save_folder) # Call functionalities of SVI svi.set_up_census_key(census_key) @@ -739,7 +740,8 @@ def setup_equity_data( county_numbers = get_us_county_numbers(counties, us_states_counties) # Create equity object - equity = EquityData(self.data_catalog, self.logger) + save_folder = str(Path(self.root) / "equity") + equity = EquityData(self.data_catalog, self.logger, save_folder) # Call functionalities of equity equity.set_up_census_key(census_key) diff --git a/hydromt_fiat/workflows/equity_data.py b/hydromt_fiat/workflows/equity_data.py index 0439d1d7..5c161197 100644 --- a/hydromt_fiat/workflows/equity_data.py +++ b/hydromt_fiat/workflows/equity_data.py @@ -12,8 +12,9 @@ class EquityData: - def __init__(self, data_catalog: DataCatalog, logger: Logger): + def __init__(self, data_catalog: DataCatalog, logger: Logger, save_folder: str): self.data_catalog = data_catalog + self.save_folder = save_folder self.census_key = Census self.download_codes = {} self.state_fips = [] @@ -26,8 +27,6 @@ def __init__(self, data_catalog: DataCatalog, logger: Logger): self.logger = logger self.svi_data_shp = gpd.GeoDataFrame() self.block_groups = gpd.GeoDataFrame() - self.temp_folder = [] - def set_up_census_key(self, census_key: str): """The Census key can be inputted in the ini file. @@ -55,7 +54,7 @@ def set_up_state_code(self, state_abbreviation: List[str]): states_done = [] for state in state_abbreviation: if state not in states_done: - self.logger.info(f"The state abbreviation specified is: {state}") + self.logger.info(f"The states for which census data will be downloaded is (it's an abbreviation): {state}") state_obj = getattr(states, state) self.state_fips.append(state_obj.fips) states_done.append(state) @@ -135,7 +134,7 @@ def download_and_unzip(self, url, extract_to='.'): zipfile = ZipFile(BytesIO(http_response.read())) zipfile.extractall(path=extract_to) except Exception as e: - print(f"Error during download and unzip: {e}") + self.logger.warning(f"Error during download and unzip: {e}") def download_shp_geom(self, year_data: int, counties: List[str]): """Downloading the shapefiles from the government Tiger website @@ -161,8 +160,7 @@ def download_shp_geom(self, year_data: int, counties: List[str]): return # Save shapefiles - folder = f'Shapefiles/{sf}{county}/{year_data}' - self.temp_folder.append(folder) + folder = Path(self.save_folder) / "shapefiles" / ("state" + sf + "_county" + county) / str(year_data) self.logger.info(f"Downloading the county shapefile for {str(year_data)}") self.download_and_unzip(url, folder) shapefiles = list(Path(folder).glob("*.shp")) @@ -170,7 +168,7 @@ def download_shp_geom(self, year_data: int, counties: List[str]): shp = gpd.read_file(shapefiles[0]) self.logger.info("The shapefile was downloaded") else: - print("No shapefile found in the directory.") + self.logger.warning("No county shapefile found in the directory.") # Dissolve shapefile based on block groups code = "20" diff --git a/hydromt_fiat/workflows/social_vulnerability_index.py b/hydromt_fiat/workflows/social_vulnerability_index.py index 59436152..4e1adc77 100644 --- a/hydromt_fiat/workflows/social_vulnerability_index.py +++ b/hydromt_fiat/workflows/social_vulnerability_index.py @@ -71,8 +71,9 @@ def list_of_states(): return states_inverted class SocialVulnerabilityIndex: - def __init__(self, data_catalog: DataCatalog, logger: Logger): + def __init__(self, data_catalog: DataCatalog, logger: Logger, save_folder: str): self.data_catalog = data_catalog + self.save_folder = save_folder self.census_key = Census self.download_codes = {} self.state_fips = [] @@ -90,7 +91,6 @@ def __init__(self, data_catalog: DataCatalog, logger: Logger): self.logger = logger self.svi_data_shp = gpd.GeoDataFrame() self.block_groups = gpd.GeoDataFrame() - self.temp_folder = [] def read_dataset(self, path: str): """If you have your own dataset (e.g. already downloaded census data), you can use this to load it from a csv @@ -144,7 +144,7 @@ def set_up_state_code(self, state_abbreviation: List[str]): states_done = [] for state in state_abbreviation: if state not in states_done: - self.logger.info(f"The state abbreviation specified is: {state}") + self.logger.info(f"The states for which census data will be downloaded is (it's an abbreviation): {state}") state_obj = getattr(states, state) self.state_fips.append(state_obj.fips) states_done.append(state) @@ -581,8 +581,7 @@ def download_shp_geom(self, year_data: int, counties: List[str]): return # Save shapefiles - folder = f'Shapefiles/{sf}{county}/{year_data}' - self.temp_folder.append(folder) + folder = Path(self.save_folder) / "shapefiles" / ("state" + sf + "_county" + county) / str(year_data) self.logger.info(f"Downloading the county shapefile for {str(year_data)}") self.download_and_unzip(url, folder) shapefiles = list(Path(folder).glob("*.shp")) From e87a2a4a1695a659a3c0649b30c1610f6695e739 Mon Sep 17 00:00:00 2001 From: Frederique Date: Wed, 13 Dec 2023 13:58:39 +0100 Subject: [PATCH 2/2] Change of folder --- hydromt_fiat/workflows/equity_data.py | 2 +- hydromt_fiat/workflows/social_vulnerability_index.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/hydromt_fiat/workflows/equity_data.py b/hydromt_fiat/workflows/equity_data.py index 5c161197..98cb5bcd 100644 --- a/hydromt_fiat/workflows/equity_data.py +++ b/hydromt_fiat/workflows/equity_data.py @@ -160,7 +160,7 @@ def download_shp_geom(self, year_data: int, counties: List[str]): return # Save shapefiles - folder = Path(self.save_folder) / "shapefiles" / ("state" + sf + "_county" + county) / str(year_data) + folder = Path(self.save_folder) / "shapefiles" / (sf + county) / str(year_data) self.logger.info(f"Downloading the county shapefile for {str(year_data)}") self.download_and_unzip(url, folder) shapefiles = list(Path(folder).glob("*.shp")) diff --git a/hydromt_fiat/workflows/social_vulnerability_index.py b/hydromt_fiat/workflows/social_vulnerability_index.py index 4e1adc77..72980132 100644 --- a/hydromt_fiat/workflows/social_vulnerability_index.py +++ b/hydromt_fiat/workflows/social_vulnerability_index.py @@ -581,7 +581,7 @@ def download_shp_geom(self, year_data: int, counties: List[str]): return # Save shapefiles - folder = Path(self.save_folder) / "shapefiles" / ("state" + sf + "_county" + county) / str(year_data) + folder = Path(self.save_folder) / "shapefiles" / (sf + county) / str(year_data) self.logger.info(f"Downloading the county shapefile for {str(year_data)}") self.download_and_unzip(url, folder) shapefiles = list(Path(folder).glob("*.shp"))