1
1
import os
2
2
import warnings
3
3
from numbers import Number
4
+ from os import PathLike
4
5
5
6
import geopandas as gpd
6
7
import numpy as np
22
23
def cell_based_association (
23
24
cell_size : int ,
24
25
geodata : List [gpd .GeoDataFrame ],
25
- output_path : str ,
26
+ output_path : Union [ str , PathLike ] ,
26
27
column : Optional [List [str ]] = None ,
27
28
subset_target_attribute_values : Optional [List [Union [None , list , str ]]] = None ,
28
29
add_name : Optional [List [Union [str , None ]]] = None ,
@@ -39,7 +40,8 @@ def cell_based_association(
39
40
cell_size: Size of the cells.
40
41
geodata: GeoDataFrame to create the CBA matrix. Additional
41
42
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.
43
45
column: Name of the column of interest. If no attribute is specified,
44
46
then an artificial attribute is created representing the presence
45
47
or absence of the geometries of this file for each cell of the CBA
@@ -88,10 +90,8 @@ def cell_based_association(
88
90
raise InvalidColumnException ("Targeted column not found in the GeoDataFrame." )
89
91
90
92
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." )
95
95
96
96
# Computation
97
97
for i , data in enumerate (geodata ):
@@ -528,14 +528,14 @@ def _to_csv(cba: gpd.GeoDataFrame, output_path: str) -> None:
528
528
529
529
530
530
@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 :
532
532
"""Intermediate utility.
533
533
534
534
Saves the object as a raster TIFF file.
535
535
536
536
Args:
537
537
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) .
539
539
nan_val: values taken by cells with no values in them (outside the study
540
540
area).
541
541
@@ -579,7 +579,7 @@ def _to_raster(cba: gpd.GeoDataFrame, output_path: str, nan_val: int = -9999) ->
579
579
transform = rasterio .transform .from_bounds (min_x , min_y , max_x , max_y , width = width , height = height )
580
580
581
581
with rasterio .open (
582
- output_path + ".tif" ,
582
+ output_path ,
583
583
mode = "w" ,
584
584
driver = "GTiff" ,
585
585
height = height ,
0 commit comments