Skip to content

Commit

Permalink
Merge pull request #228 from Deltares/saving_svi_equity_data_in_right…
Browse files Browse the repository at this point in the history
…_folders

Saving svi + equity data in the right folders
  • Loading branch information
frederique-hub authored Dec 13, 2023
2 parents ea6232d + e87a2a4 commit 51c209d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
6 changes: 4 additions & 2 deletions hydromt_fiat/fiat.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,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)
Expand Down Expand Up @@ -742,7 +743,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)
Expand Down
14 changes: 6 additions & 8 deletions hydromt_fiat/workflows/equity_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -161,16 +160,15 @@ 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" / (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"))
if shapefiles:
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"
Expand Down
9 changes: 4 additions & 5 deletions hydromt_fiat/workflows/social_vulnerability_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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" / (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"))
Expand Down

0 comments on commit 51c209d

Please sign in to comment.