vector
This module contains functions that process or create vector data.
create_grid_coordinates #
create_grid_coordinates(
bounding_box: list | tuple | ndarray, grid_size: float, logger: Logger = LOGGER
) -> tuple[ndarray, ndarray]
Create grid coordinates based on input bounding box and grid size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bounding_box
|
list | tuple | ndarray
|
The bounding box of the grid as (min_lon, min_lat, max_lon, max_lat). Unit needs to be based on projection used (meters, degrees, etc.). |
required |
grid_size
|
float
|
Cell size for grid. Unit needs to be based on projection used (meters, degrees, etc.). |
required |
logger
|
Logger
|
Logger instance. |
LOGGER
|
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray]
|
A tuple containing two numpy arrays for longitude and latitude coordinates. |
Source code in src/geospatial_tools/vector.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
generate_flattened_grid_coords #
generate_flattened_grid_coords(
lon_coords: ndarray, lat_coords: ndarray, logger: Logger = LOGGER
) -> tuple[ndarray, ndarray]
Takes in previously created grid coordinates and flattens them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lon_coords
|
ndarray
|
Longitude grid coordinates |
required |
lat_coords
|
ndarray
|
Latitude grid coordinates |
required |
logger
|
Logger
|
Logger instance. |
LOGGER
|
Returns:
Source code in src/geospatial_tools/vector.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
create_vector_grid #
create_vector_grid(
bounding_box: list | tuple,
grid_size: float,
crs: str = "4326",
logger: Logger = LOGGER,
) -> GeoDataFrame
Create a grid of polygons within the specified bounds and cell size. This function uses NumPy vectorized arrays for optimized performance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bounding_box
|
list | tuple
|
The bounding box of the grid as (min_lon, min_lat, max_lon, max_lat). |
required |
grid_size
|
float
|
The size of each grid cell in degrees. |
required |
crs
|
str
|
CRS code for projection. ex. 'EPSG:4326' |
'4326'
|
logger
|
Logger
|
Logger instance. |
LOGGER
|
Returns:
Source code in src/geospatial_tools/vector.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
create_vector_grid_parallel #
create_vector_grid_parallel(
bounding_box: list | tuple | ndarray,
grid_size: float,
crs: str | int | None = None,
num_of_workers: int | None = None,
logger: Logger = LOGGER,
) -> GeoDataFrame
Create a grid of polygons within the specified bounds and cell size. This function uses NumPy for optimized performance and ProcessPoolExecutor for parallel execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bounding_box
|
list | tuple | ndarray
|
The bounding box of the grid as (min_lon, min_lat, max_lon, max_lat). |
required |
grid_size
|
float
|
The size of each grid cell in degrees. |
required |
crs
|
str | int | None
|
Coordinate reference system for the resulting GeoDataFrame. |
None
|
num_of_workers
|
int | None
|
The number of processes to use for parallel execution. Defaults to the min of number of CPU cores or number of cells in the grid |
None
|
logger
|
Logger
|
Logger instance. |
LOGGER
|
Returns:
Source code in src/geospatial_tools/vector.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | |
dask_spatial_join #
dask_spatial_join(
select_features_from: GeoDataFrame,
intersected_with: GeoDataFrame,
join_type: str = "inner",
predicate: str = "intersects",
num_of_workers=4,
logger: Logger = LOGGER,
) -> GeoDataFrame
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
select_features_from
|
GeoDataFrame
|
|
required |
intersected_with
|
GeoDataFrame
|
|
required |
join_type
|
str
|
|
'inner'
|
predicate
|
str
|
|
'intersects'
|
num_of_workers
|
|
4
|
|
logger
|
Logger
|
|
LOGGER
|
Returns:
Source code in src/geospatial_tools/vector.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |
select_polygons_by_location #
select_polygons_by_location(
select_features_from: GeoDataFrame,
intersected_with: GeoDataFrame,
num_of_workers: int | None = None,
join_type: str = "inner",
predicate="intersects",
join_function=dask_spatial_join,
logger: Logger = LOGGER,
) -> GeoDataFrame
This function executes a select by location operation on a GeoDataFrame. It is essentially a wrapper around
gpd.sjoin to allow parallel execution. While it does use sjoin, only the columns from select_features_from are
kept.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
select_features_from
|
GeoDataFrame
|
GeoDataFrame containing the polygons from which to select features from. |
required |
intersected_with
|
GeoDataFrame
|
Geodataframe containing the polygons that will be used to select features with via an intersect operation. |
required |
num_of_workers
|
int | None
|
Number of parallel processes to use for execution. If using on a compute cluster, please set a specific amount (ex. 1 per CPU core requested). Defaults to the min of number of CPU cores or number (cpu_count()) |
None
|
join_type
|
str
|
|
'inner'
|
predicate
|
The predicate to use for selecting features from. Available predicates are: ['intersects', 'contains', 'within', 'touches', 'crosses', 'overlaps']. Defaults to 'intersects' |
'intersects'
|
|
join_function
|
Function that will execute the join operation. Available functions are: 'multiprocessor_spatial_join'; 'dask_spatial_join'; or custom functions. (Default value = multiprocessor_spatial_join) |
dask_spatial_join
|
|
logger
|
Logger
|
Logger instance. |
LOGGER
|
Returns:
Source code in src/geospatial_tools/vector.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | |
to_geopackage #
to_geopackage(gdf: GeoDataFrame, filename: str | Path, logger=LOGGER) -> str | Path
Save GeoDataFrame to a Geopackage file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gdf
|
GeoDataFrame
|
The GeoDataFrame to save. |
required |
filename
|
str | Path
|
The filename to save to. |
required |
logger
|
Logger instance (Default value = LOGGER) |
LOGGER
|
Returns:
Source code in src/geospatial_tools/vector.py
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | |
to_geopackage_chunked #
to_geopackage_chunked(
gdf: GeoDataFrame, filename: str, chunk_size: int = 1000000, logger: Logger = LOGGER
) -> str
Save GeoDataFrame to a Geopackage file using chunks to help with potential memory consumption. This function can
potentially be slower than to_geopackage, especially if chunk_size is not adequately defined. Therefore, this
function should only be required if to_geopackage fails because of memory issues.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gdf
|
GeoDataFrame
|
The GeoDataFrame to save. |
required |
filename
|
str
|
The filename to save to. |
required |
chunk_size
|
int
|
The number of rows per chunk. |
1000000
|
logger
|
Logger
|
Logger instance. |
LOGGER
|
Returns:
Source code in src/geospatial_tools/vector.py
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | |
spatial_join_within #
spatial_join_within(
polygon_features: GeoDataFrame,
polygon_column: str,
vector_features: GeoDataFrame,
vector_column_name: str,
join_type: str = "left",
predicate: str = "within",
logger=LOGGER,
) -> GeoDataFrame
This function does a spatial join based on a within operation between features to associate which vector_features
are within which polygon_features, groups the results by vector feature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polygon_features
|
GeoDataFrame
|
Dataframes containing polygons. Will be used to find which features of |
required |
polygon_column
|
str
|
The name of the column in |
required |
vector_features
|
GeoDataFrame
|
The dataframe containing the features that will be grouped by polygon. |
required |
vector_column_name
|
str
|
The name of the column in |
required |
join_type
|
str
|
The type of join to perform. Defaults to 'left'. |
'left'
|
predicate
|
str
|
The predicate to use for the spatial join operation. Defaults to |
'within'
|
logger
|
Logger instance |
LOGGER
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
A new GeoDataFrame with the joined features. |
Source code in src/geospatial_tools/vector.py
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | |