Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add param to only get spatial output #445

Merged
merged 2 commits into from
Feb 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 32 additions & 17 deletions hydromt_fiat/fiat.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def setup_output(
output_dir: str = "output",
output_csv_name: str = "output.csv",
output_vector_name: Union[str, List[str]] = "spatial.gpkg",
output_single_file: bool = False
) -> None:
"""Setup Delft-FIAT output folder and files.

Expand All @@ -146,14 +147,19 @@ def setup_output(
The name of the output csv file, by default "output.csv".
output_vector_name : Union[str, List[str]], optional
The name of the output vector file, by default "spatial.gpkg".
output_single_file: bool
If True the exposure data is written in a single spatial file. Default set to False
"""
self.output_single_file = False
self.set_config("output.path", output_dir)
self.set_config("output.csv.name", output_csv_name)
if isinstance(output_vector_name, str):
output_vector_name = [output_vector_name]
for i, name in enumerate(output_vector_name):
self.set_config(f"output.geom.name{str(i+1)}", name)

if output_single_file:
self.output_single_file = True

def setup_region(
self,
region,
Expand Down Expand Up @@ -1667,22 +1673,29 @@ def write_geoms(self):
"""_summary_."""
if self.exposure and "exposure" in self._tables:
fn = "exposure/{name}.gpkg"
for i, (geom, name) in enumerate(
zip(self.exposure.exposure_geoms, self.exposure.geom_names)
):
_fn = os.path.join(self.root, fn.format(name=name))
if not os.path.isdir(os.path.dirname(_fn)):
os.makedirs(os.path.dirname(_fn))

# This whole ordeal is terrible,
# but it needs a refactor that is too much to fix this properly
self.set_config(
f"exposure.geom.file{str(i+1)}",
fn.format(name=name),
)
geom.to_file(_fn)
if self.geoms:
GridModel.write_geoms(self)
if self.output_single_file :
gdf = self.exposure.get_full_gdf(self.exposure.exposure_db)
name = "exposure.gpkg"
_fn = os.path.join(self.root, "exposure" , name)
gdf.to_file(os.path.join(self.root, _fn))
else:
for i, (geom, name) in enumerate(
zip(self.exposure.exposure_geoms, self.exposure.geom_names)
):
_fn = os.path.join(self.root, fn.format(name=name))
if not os.path.isdir(os.path.dirname(_fn)):
os.makedirs(os.path.dirname(_fn))

# This whole ordeal is terrible,
# but it needs a refactor that is too much to fix this properly
self.set_config(
f"exposure.geom.file{str(i+1)}",
fn.format(name=name),
)
geom.to_file(_fn)
if self.geoms:
GridModel.write_geoms(self)


def write_tables(self) -> None:
if len(self._tables) == 0:
Expand All @@ -1708,6 +1721,8 @@ def write_tables(self) -> None:
writer.writerow([metadata])
# Exposure
elif name == "exposure":
if self.output_single_file:
continue
# The default location and save settings of the exposure data
fn = "exposure/exposure.csv"
kwargs = {"index": False}
Expand Down
Loading