Skip to content

Commit 57983c9

Browse files
authored
Merge pull request #391 from GispoCoding/388-add-cba-cli-function
Add CLI function for CBA
2 parents a86dc21 + be25bfa commit 57983c9

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

cba_out.tif

2.32 KB
Binary file not shown.

eis_toolkit/cli.py

+36-1
Original file line numberDiff line numberDiff line change
@@ -2023,7 +2023,42 @@ 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+
if subset_target_attribute_values is not None:
2045+
subset_target_attribute_values = [value.strip() for value in subset_target_attribute_values]
2046+
2047+
cell_based_association(
2048+
cell_size=cell_size,
2049+
geodata=[geodataframe],
2050+
output_path=output_raster,
2051+
column=column if column is None else [column],
2052+
subset_target_attribute_values=subset_target_attribute_values
2053+
if subset_target_attribute_values is None
2054+
else [subset_target_attribute_values],
2055+
add_name=add_name if add_name is None else [add_name],
2056+
add_buffer=add_buffer if add_buffer is None else [add_buffer],
2057+
)
2058+
2059+
typer.echo("Progress: 100%")
2060+
2061+
typer.echo(f"Cell based association completed, writing raster to {output_raster}.")
20272062

20282063

20292064
# --- PREDICTION ---

eis_toolkit/vector_processing/cell_based_association.py

+9-9
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
@@ -88,10 +90,8 @@ def cell_based_association(
8890
raise InvalidColumnException("Targeted column not found in the GeoDataFrame.")
8991

9092
for i, subset in enumerate(subset_target_attribute_values):
91-
if subset is not None:
92-
for value in subset:
93-
if value not in geodata[i][column[i]].unique():
94-
raise InvalidParameterValueException("Subset of value(s) not found in the targeted column.")
93+
if subset is not None and not all(value in geodata[i][column[i]].tolist() for value in subset):
94+
raise InvalidParameterValueException("Subset of value(s) not found in the targeted column.")
9595

9696
# Computation
9797
for i, data in enumerate(geodata):
@@ -528,14 +528,14 @@ def _to_csv(cba: gpd.GeoDataFrame, output_path: str) -> None:
528528

529529

530530
@beartype
531-
def _to_raster(cba: gpd.GeoDataFrame, output_path: str, nan_val: int = -9999) -> None:
531+
def _to_raster(cba: gpd.GeoDataFrame, output_path: Union[str, PathLike], nan_val: int = -9999) -> None:
532532
"""Intermediate utility.
533533
534534
Saves the object as a raster TIFF file.
535535
536536
Args:
537537
cba: CBA matrix to save.
538-
output_path: Name of the saved file.
538+
output_path: Name of the saved file, include file extension (.tif).
539539
nan_val: values taken by cells with no values in them (outside the study
540540
area).
541541
@@ -579,7 +579,7 @@ def _to_raster(cba: gpd.GeoDataFrame, output_path: str, nan_val: int = -9999) ->
579579
transform = rasterio.transform.from_bounds(min_x, min_y, max_x, max_y, width=width, height=height)
580580

581581
with rasterio.open(
582-
output_path + ".tif",
582+
output_path,
583583
mode="w",
584584
driver="GTiff",
585585
height=height,

tests/vector_processing/cell_based_association_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def test_cba():
121121
add_buffer=[False, 5000, 5000, False, 5000],
122122
)
123123
with rasterio.open(raster_path, "r") as one:
124-
with rasterio.open(str(output_file) + ".tif", "r") as two:
124+
with rasterio.open(output_file, "r") as two:
125125
one_array = one.read()
126126
two_array = two.read()
127127
numpy.testing.assert_equal(one_array, two_array)

0 commit comments

Comments
 (0)