elapid.geo¶
Geospatial data operations like reading/writing/indexing raster and vector data.
annotate(points, raster_paths, labels=None, drop_na=True, quiet=False)
¶
Read raster values for each point in a vector and append as new columns.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
points |
Union[str, GeoSeries, GeoDataFrame]
|
path to a point-format vector, OR GeoDataFrame with point locations, OR GeoSeries (e.g., gdf['geometry']) with point locations |
required |
raster_paths |
Union[str, list]
|
raster paths to extract pixel values from. |
required |
labels |
list
|
band name labels. number of labels should match the total number of bands across all raster_paths. |
None
|
drop_na |
bool
|
drop all records with no-data values. |
True
|
quiet |
bool
|
silence progress bar output. |
False
|
Returns:
Type | Description |
---|---|
GeoDataFrame
|
GeoDataFrame annotated with the pixel values from each raster |
Source code in elapid/geo.py
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 338 339 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 |
|
annotate_geoseries(points, raster_paths, labels=None, drop_na=True, dtype=None, quiet=False)
¶
Reads and stores pixel values from rasters using point locations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
points |
GeoSeries
|
GeoSeries with point locations. |
required |
raster_paths |
list
|
rasters to extract pixel values from. |
required |
labels |
list
|
band labels. must match the total number of bands for all raster_paths. |
None
|
drop_na |
bool
|
drop records with no-data values. |
True
|
dtype |
str
|
output column data type. uses the first raster's dtype by default. |
None
|
quiet |
bool
|
silence progress bar output. |
False
|
Returns:
Name | Type | Description |
---|---|---|
gdf |
(GeoDataFrame, ndarray)
|
GeoDataFrame annotated with the pixel values from each raster |
Source code in elapid/geo.py
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 |
|
annotate_vector(vector_path, raster_paths, labels=None, drop_na=True, quiet=False)
¶
Reads and stores pixel values from rasters using a point-format vector file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vector_path |
str
|
path to a vector file (shp, geojson, etc) |
required |
raster_paths |
list
|
raster paths to extract pixel values from |
required |
labels |
list
|
band name labels. should match the total number of bands across all raster_paths |
None
|
drop_na |
bool
|
drop all records with no-data values |
True
|
quiet |
bool
|
silence progress bar output. |
False
|
Returns:
Name | Type | Description |
---|---|---|
gdf |
GeoDataFrame
|
GeoDataFrame annotated with the pixel values from each raster |
Source code in elapid/geo.py
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
|
apply_model_to_array(model, array, nodata, nodata_idx, count=1, dtype='float32', predict_proba=False, **kwargs)
¶
Applies a model to an array of covariates.
Covariate array should be of shape (nbands, nrows, ncols).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
BaseEstimator
|
object with a |
required |
array |
ndarray
|
array of shape (nbands, nrows, ncols) with pixel values |
required |
nodata |
float
|
numeric nodata value to apply to the output array |
required |
nodata_idx |
int
|
array of bools with shape (nbands, nrows, ncols) containing nodata locations |
required |
count |
int
|
number of bands in the prediction output |
1
|
dtype |
str
|
prediction array dtype |
'float32'
|
predict_proba |
bool
|
use model.predict_proba() instead of model.predict() |
False
|
**kwargs |
additonal keywords to pass to model.predict() |
{}
|
Returns:
Name | Type | Description |
---|---|---|
ypred_window |
ndarray
|
Array of shape (nrows, ncols) with model predictions |
Source code in elapid/geo.py
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 |
|
apply_model_to_rasters(model, raster_paths, output_path, resampling=rio.enums.Resampling.average, count=1, dtype='float32', nodata=-9999, driver='GTiff', compress='deflate', bigtiff=True, template_idx=0, windowed=True, predict_proba=False, ignore_sklearn=True, quiet=False, **kwargs)
¶
Applies a trained model to a list of raster datasets.
The list and band order of the rasters must match the order of the covariates used to train the model. It reads each dataset block-by-block, applies the model, and writes gridded predictions. If the raster datasets are not consistent (different extents, resolutions, etc.), it wll re-project the data on the fly, with the grid size, extent and projection based on a 'template' raster.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
BaseEstimator
|
object with a model.predict() function |
required |
raster_paths |
list
|
raster paths of covariates to apply the model to |
required |
output_path |
str
|
path to the output file to create |
required |
resampling |
Enum
|
resampling algorithm to apply to on-the-fly reprojection from rasterio.enums.Resampling |
average
|
count |
int
|
number of bands in the prediction output |
1
|
dtype |
str
|
the output raster data type |
'float32'
|
nodata |
float
|
output nodata value |
-9999
|
driver |
str
|
output raster format from rasterio.drivers.raster_driver_extensions() |
'GTiff'
|
compress |
str
|
compression to apply to the output file |
'deflate'
|
bigtiff |
bool
|
specify the output file as a bigtiff (for rasters > 2GB) |
True
|
template_idx |
int
|
index of the raster file to use as a template. template_idx=0 sets the first raster as template |
0
|
windowed |
bool
|
apply the model using windowed read/write slower, but more memory efficient |
True
|
predict_proba |
bool
|
use model.predict_proba() instead of model.predict() |
False
|
ignore_sklearn |
bool
|
silence sklearn warning messages |
True
|
quiet |
bool
|
silence progress bar output |
False
|
**kwargs |
additonal keywords to pass to model.predict() |
{}
|
Returns:
Name | Type | Description |
---|---|---|
None |
None
|
saves model predictions to disk. |
Source code in elapid/geo.py
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 |
|
crs_match(crs1, crs2)
¶
Evaluates whether two coordinate reference systems are the same.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
crs1 |
CRSType
|
the first CRS, from a rasterio dataset, a GeoDataFrame, or a string with projection parameters. |
required |
crs2 |
CRSType
|
the second CRS, from the same sources above. |
required |
Returns:
Name | Type | Description |
---|---|---|
matches |
bool
|
Boolean for whether the CRS match. |
Source code in elapid/geo.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
|
distance_weights(points, n_neighbors=-1, center='median', cpu_count=-1)
¶
Compute sample weights based on the distance between points.
Assigns higher scores to isolated points, lower scores to clustered points.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
points |
Vector
|
point-format GeoSeries or GeoDataFrame |
required |
n_neighbors |
int
|
compute weights based on average distance to the nearest n_neighbors set to -1 to compute the distance to all neighbors. |
-1
|
center |
str
|
rescale the weights to center the mean or median of the array on 1 accepts either 'mean' or 'median' as input. pass None to ignore. |
'median'
|
cpu_count |
int
|
number of cpus to use for estimation. -1 uses all cores |
-1
|
Returns:
Type | Description |
---|---|
ndarray
|
array of shape (len(points),) with scaled sample weights. Scaling is performed by dividing by the maximum value, preserving the relative scale of differences between the min and max distance. |
Source code in elapid/geo.py
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 |
|
nearest_point_distance(points1, points2=None, n_neighbors=1, cpu_count=-1)
¶
Compute the average euclidean distance to the nearest point in a series.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
points1 |
Vector
|
return the closest distance from these points |
required |
points2 |
Vector
|
return the closest distance to these points if None, compute the distance to the nearest points in the points1 series |
None
|
n_neighbors |
int
|
compute the average distance to the nearest n_neighbors. set to -1 to compute the distance to all neighbors. |
1
|
cpu_count |
int
|
number of cpus to use for estimation. -1 uses all cores |
-1
|
Returns:
Type | Description |
---|---|
ndarray
|
array of shape (len(points),) with the distance to each point's nearest neighbor |
Source code in elapid/geo.py
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 |
|
parse_crs_string(string)
¶
Parses a string to determine the CRS/spatial projection format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string |
str
|
a string with CRS/projection data. |
required |
Returns:
Name | Type | Description |
---|---|---|
crs_type |
str
|
Str in ["wkt", "proj4", "epsg", "string"]. |
Source code in elapid/geo.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
|
read_raster_from_polygon(src, poly)
¶
Read valid pixel values from all locations inside a polygon Uses the polygon as a mask in addition to the existing raster mask
Parameters:
Name | Type | Description | Default |
---|---|---|---|
src |
DatasetReader
|
an open rasterio dataset to read from |
required |
poly |
Union[Polygon, MultiPolygon]
|
a shapely Polygon or MultiPolygon |
required |
Returns:
Type | Description |
---|---|
MaskedArray
|
masked array of shape (nbands, nrows, ncols) |
Source code in elapid/geo.py
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 |
|
sample_bias_file(raster_path, count, ignore_mask=False)
¶
Creates a semi-random geographic sampling of points weighted towards biased areas.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raster_path |
str
|
raster bias file path to sample from. pixel values can be in arbitrary range, but must be odered low -> high probability |
required |
count |
int
|
total number of samples to generate |
required |
ignore_mask |
bool
|
sample from the full extent of the raster instead of unmasked areas only |
False
|
Returns:
Name | Type | Description |
---|---|---|
points |
GeoSeries
|
Point geometry geoseries |
Source code in elapid/geo.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
|
sample_geoseries(geoseries, count, overestimate=2)
¶
Creates random geographic point samples inside a polygon/multipolygon
Parameters:
Name | Type | Description | Default |
---|---|---|---|
geoseries |
GeoSeries
|
geometry dataset (e.g. |
required |
count |
int
|
number of samples to generate |
required |
overestimate |
float
|
scaler to generate extra samples to toss points outside of the polygon/inside it's bounds |
2
|
Returns:
Name | Type | Description |
---|---|---|
points |
GeoSeries
|
Point geometry geoseries |
Source code in elapid/geo.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
|
sample_raster(raster_path, count, nodata=None, ignore_mask=False)
¶
Create a random geographic sample of points based on a raster's extent.
Selects from unmasked locations if the rasters nodata value is set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raster_path |
str
|
raster file path to sample locations from |
required |
count |
int
|
number of samples to generate |
required |
nodata |
float
|
add pixels with this value to the nodata mask |
None
|
ignore_mask |
bool
|
sample from the full extent of the raster instead of unmasked areas only |
False
|
Returns:
Name | Type | Description |
---|---|---|
points |
GeoSeries
|
Point geometry geoseries |
Source code in elapid/geo.py
111 112 113 114 115 116 117 118 119 120 121 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 |
|
sample_vector(vector_path, count, overestimate=2)
¶
Creates a random geographic sampling of points inside of a polygon/multipolygon type vector file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vector_path |
str
|
path to a vector file (shp, geojson, etc) |
required |
count |
int
|
number of samples to generate |
required |
overestimate |
float
|
scaler to generate extra samples to toss points outside of the polygon/inside it's bounds |
2
|
Returns:
Name | Type | Description |
---|---|---|
points |
GeoSeries
|
Point geometry geoseries |
Source code in elapid/geo.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
|
stack_geodataframes(presence, background, add_class_label=False, target_crs='presence')
¶
Concatenate geometries from two GeoSeries/GeoDataFrames.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
presence |
Vector
|
presence geometry (y=1) locations |
required |
background |
Vector
|
background geometry (y=0) locations |
required |
add_class_label |
bool
|
add a column labeling the y value for each point |
False
|
target_crs |
str
|
if reprojection is necessary, use this variable's crs. valid options are "presence" and "background" |
'presence'
|
Returns:
Type | Description |
---|---|
GeoDataFrame
|
merged GeoDataFrame with all geometries projected to the same crs. |
Source code in elapid/geo.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
|
string_to_crs(string)
¶
Converts a crs/projection string to a pyproj-readable CRS object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string |
str
|
a crs/projection string. |
required |
Returns:
Name | Type | Description |
---|---|---|
crs |
CRS
|
the coordinate reference system |
Source code in elapid/geo.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
|
validate_gpd(geo)
¶
Validates whether an input is a GeoDataFrame or a GeoSeries.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
geo |
Vector
|
an input variable that should be in GeoPandas format |
required |
Raises:
Type | Description |
---|---|
TypeError
|
geo is not a GeoPandas dataframe or series |
Source code in elapid/geo.py
665 666 667 668 669 670 671 672 673 674 675 |
|
validate_polygons(geometry)
¶
Iterate over a geoseries to find rows with invalid geometry types.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
geometry |
Vector
|
a GeoSeries or GeoDataFrame with polygon geometries |
required |
Returns:
Type | Description |
---|---|
Index
|
an index of rows with valid polygon types |
Source code in elapid/geo.py
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 |
|
xy_to_geoseries(x, y, crs='epsg:4326')
¶
Converts x/y data into a geopandas geoseries.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Union[float, list, ndarray]
|
1-D array-like of x location values |
required |
y |
Union[float, list, ndarray]
|
1-D array-like of y location values |
required |
crs |
CRSType
|
coordinate reference system. accepts pyproj.CRS / rio.crs.CRS objects or anything allowed by pyproj.CRS.from_user_input() |
'epsg:4326'
|
Returns:
Name | Type | Description |
---|---|---|
gs |
GeoSeries
|
Point geometry geoseries |
Source code in elapid/geo.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
zonal_stats(polygons, raster_paths, labels=None, all_touched=True, mean=True, stdv=True, min=False, max=False, count=False, sum=False, skew=False, kurtosis=False, mode=False, all=False, percentiles=[], quiet=False)
¶
Compute raster summary stats for each polygon in a GeoSeries or GeoDataFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polygons |
Vector
|
GeoSeries or GeoDataFrame with polygon geometries. |
required |
raster_paths |
list
|
list of paths to rasters to summarize |
required |
labels |
list
|
band labels. must match the total number of bands for all raster_paths. |
None
|
all_touched |
bool
|
include all pixels that touch a polygon. set to False to only include pixels whose centers intersect the polygon |
True
|
mean, |
(min, max, count, sum, stdv, skew, kurtosis, mode)
|
set to True to compute these stats |
required |
all |
bool
|
compute all of the above stats |
False
|
percentiles |
list
|
list of 0-100 percentile ranges to compute |
[]
|
quiet |
bool
|
silence progress bar output |
False
|
Returns:
Type | Description |
---|---|
GeoDataFrame
|
GeoDataFrame with zonal stats for each raster band in new columns.
If |
Source code in elapid/geo.py
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 |
|