Skip to content

Commit 686777b

Browse files
committed
Added CLI function for CBA, minor adjustment to path handling in CBA tool
1 parent e7354ca commit 686777b

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

eis_toolkit/cli.py

+33-1
Original file line numberDiff line numberDiff line change
@@ -2023,7 +2023,39 @@ def distance_computation_cli(
20232023

20242024

20252025
# CBA
2026-
# TODO
2026+
@app.command()
2027+
def cell_based_association_cli(
2028+
input_vector: INPUT_FILE_OPTION,
2029+
output_raster: OUTPUT_FILE_OPTION,
2030+
cell_size: int = typer.Option(),
2031+
column: Optional[str] = None,
2032+
subset_target_attribute_values: Optional[List[str]] = None,
2033+
add_name: Optional[str] = None,
2034+
add_buffer: Optional[float] = None,
2035+
):
2036+
"""Create a CBA matrix."""
2037+
from eis_toolkit.vector_processing.cell_based_association import cell_based_association
2038+
2039+
typer.echo("Progress: 10%")
2040+
2041+
geodataframe = gpd.read_file(input_vector)
2042+
typer.echo("Progress: 25%")
2043+
2044+
cell_based_association(
2045+
cell_size=cell_size,
2046+
geodata=[geodataframe],
2047+
output_path=output_raster,
2048+
column=column if column is None else [column],
2049+
subset_target_attribute_values=subset_target_attribute_values
2050+
if subset_target_attribute_values is None
2051+
else [subset_target_attribute_values],
2052+
add_name=add_name if add_name is None else [add_name],
2053+
add_buffer=add_buffer if add_buffer is None else [add_buffer],
2054+
)
2055+
2056+
typer.echo("Progress: 100%")
2057+
2058+
typer.echo(f"Cell based association completed, writing raster to {output_raster}.")
20272059

20282060

20292061
# --- PREDICTION ---

eis_toolkit/vector_processing/cell_based_association.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import warnings
33
from numbers import Number
4+
from os import PathLike
45

56
import geopandas as gpd
67
import numpy as np
@@ -22,7 +23,7 @@
2223
def cell_based_association(
2324
cell_size: int,
2425
geodata: List[gpd.GeoDataFrame],
25-
output_path: str,
26+
output_path: Union[str, PathLike],
2627
column: Optional[List[str]] = None,
2728
subset_target_attribute_values: Optional[List[Union[None, list, str]]] = None,
2829
add_name: Optional[List[Union[str, None]]] = None,
@@ -39,7 +40,8 @@ def cell_based_association(
3940
cell_size: Size of the cells.
4041
geodata: GeoDataFrame to create the CBA matrix. Additional
4142
GeoDataFrame(s) can be imputed to add to the CBA matrix.
42-
output_path: Name of the saved .tif file.
43+
output_path: Name of the saved .tif file. Include file extension (.tif)
44+
in the path.
4345
column: Name of the column of interest. If no attribute is specified,
4446
then an artificial attribute is created representing the presence
4547
or absence of the geometries of this file for each cell of the CBA
@@ -528,14 +530,14 @@ def _to_csv(cba: gpd.GeoDataFrame, output_path: str) -> None:
528530

529531

530532
@beartype
531-
def _to_raster(cba: gpd.GeoDataFrame, output_path: str, nan_val: int = -9999) -> None:
533+
def _to_raster(cba: gpd.GeoDataFrame, output_path: Union[str, PathLike], nan_val: int = -9999) -> None:
532534
"""Intermediate utility.
533535
534536
Saves the object as a raster TIFF file.
535537
536538
Args:
537539
cba: CBA matrix to save.
538-
output_path: Name of the saved file.
540+
output_path: Name of the saved file, include file extension (.tif).
539541
nan_val: values taken by cells with no values in them (outside the study
540542
area).
541543
@@ -579,7 +581,7 @@ def _to_raster(cba: gpd.GeoDataFrame, output_path: str, nan_val: int = -9999) ->
579581
transform = rasterio.transform.from_bounds(min_x, min_y, max_x, max_y, width=width, height=height)
580582

581583
with rasterio.open(
582-
output_path + ".tif",
584+
output_path,
583585
mode="w",
584586
driver="GTiff",
585587
height=height,

0 commit comments

Comments
 (0)