Spatial Operations#
Crop, align, reproject, resample, CRS handling, and coordinate conversion.
pyramids.dataset.ops.spatial.Spatial
#
Source code in src/pyramids/dataset/ops/spatial.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 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 109 110 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 156 157 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 191 192 193 194 195 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 227 228 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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 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 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 375 376 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 405 406 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 491 492 493 494 495 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 534 535 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 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 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 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 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 840 841 842 843 844 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 887 888 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 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 | |
set_crs(crs=None, epsg=None)
#
Set the Coordinate Reference System (CRS).
Set the Coordinate Reference System (CRS) of a
Parameters:
Source code in src/pyramids/dataset/ops/spatial.py
to_crs(to_epsg, method='nearest neighbor', maintain_alignment=False)
#
Reproject the dataset to any projection.
(default the WGS84 web mercator projection, without resampling)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_epsg
|
int
|
reference number to the new projection (https://epsg.io/). Default 3857 is the reference number of WGS84 web mercator. |
required |
method
|
str
|
resampling method. Default is "nearest neighbor". See https://gisgeography.com/raster-resampling/. Allowed values: "nearest neighbor", "cubic", "bilinear". |
'nearest neighbor'
|
maintain_alignment
|
bool
|
True to maintain the number of rows and columns of the raster the same after reprojection. Default is False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Dataset |
Dataset
|
A new reprojected Dataset. |
Examples:
- Create a dataset and reproject it:
>>> import numpy as np
>>> arr = np.random.rand(4, 5, 5)
>>> top_left_corner = (0, 0)
>>> cell_size = 0.05
>>> dataset = Dataset.create_from_array(arr, top_left_corner=top_left_corner, cell_size=cell_size, epsg=4326)
>>> print(dataset)
<BLANKLINE>
Cell size: 0.05
Dimension: 5 * 5
EPSG: 4326
Number of Bands: 4
Band names: ['Band_1', 'Band_2', 'Band_3', 'Band_4']
Mask: -9999.0
Data type: float64
File:...
<BLANKLINE>
>>> print(dataset.crs)
GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]
>>> print(dataset.epsg)
4326
>>> reprojected_dataset = dataset.to_crs(to_epsg=3857)
>>> print(reprojected_dataset)
<BLANKLINE>
Cell size: 5565.983370404396
Dimension: 5 * 5
EPSG: 3857
Number of Bands: 4
Band names: ['Band_1', 'Band_2', 'Band_3', 'Band_4']
Mask: -9999.0
Data type: float64
File:...
<BLANKLINE>
>>> print(reprojected_dataset.crs)
PROJCS["WGS 84 / Pseudo-Mercator",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Mercator_1SP"],PARAMETER["central_meridian",0],PARAMETER["scale_factor",1],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],EXTENSION["PROJ4","+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs"],AUTHORITY["EPSG","3857"]]
>>> print(reprojected_dataset.epsg)
3857
Source code in src/pyramids/dataset/ops/spatial.py
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 109 110 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 156 157 158 159 160 | |
convert_longitude()
#
Convert Longitude.
- convert the longitude from 0-360 to -180 - 180.
- currently the function works correctly if the raster covers the whole world, it means that the columns in the rasters covers from longitude 0 to 360.
Returns:
| Name | Type | Description |
|---|---|---|
Dataset |
Dataset
|
A new Dataset with longitude converted to -180/180. |
Source code in src/pyramids/dataset/ops/spatial.py
resample(cell_size, method='nearest neighbor')
#
resample.
resample method reprojects a raster to any projection (default the WGS84 web mercator projection, without resampling). The function returns a GDAL in-memory file object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cell_size
|
int
|
New cell size to resample the raster. If None, raster will not be resampled. |
required |
method
|
str
|
Resampling method: "nearest neighbor", "cubic", or "bilinear". Default is "nearest neighbor". |
'nearest neighbor'
|
Returns:
| Name | Type | Description |
|---|---|---|
Dataset |
Dataset
|
A new resampled Dataset. |
Examples:
- Create a Dataset with 4 bands, 10 rows, 10 columns, at lon/lat (0, 0):
>>> import numpy as np
>>> arr = np.random.rand(4, 10, 10)
>>> top_left_corner = (0, 0)
>>> cell_size = 0.05
>>> dataset = Dataset.create_from_array(arr, top_left_corner=top_left_corner, cell_size=cell_size, epsg=4326)
>>> print(dataset)
<BLANKLINE>
Cell size: 0.05
Dimension: 10 * 10
EPSG: 4326
Number of Bands: 4
Band names: ['Band_1', 'Band_2', 'Band_3', 'Band_4']
Mask: -9999.0
Data type: float64
File: ...
<BLANKLINE>
>>> dataset.plot(band=0)
(<Figure size 800x800 with 2 Axes>, <Axes: >)
- Resample the raster to a new cell size of 0.1:
>>> new_dataset = dataset.resample(cell_size=0.1)
>>> print(new_dataset)
<BLANKLINE>
Cell size: 0.1
Dimension: 5 * 5
EPSG: 4326
Number of Bands: 4
Band names: ['Band_1', 'Band_2', 'Band_3', 'Band_4']
Mask: -9999.0
Data type: float64
File:...
<BLANKLINE>
>>> new_dataset.plot(band=0)
(<Figure size 800x800 with 2 Axes>, <Axes: >)
- Resampling the dataset from cell_size 0.05 to 0.1 degrees reduced the number of cells to 5 in each dimension instead of 10.
Source code in src/pyramids/dataset/ops/spatial.py
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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | |
fill_gaps(mask, src_array)
#
Fill gaps in src_array using nearest neighbors where mask indicates valid cells.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
Dataset | ndarray
|
Mask dataset or array used to determine valid cells. |
required |
src_array
|
ndarray
|
Source array whose gaps will be filled. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: The source array with gaps filled where applicable. |
Source code in src/pyramids/dataset/ops/spatial.py
align(alignment_src)
#
Align the current dataset (rows and columns) to match a given dataset.
Copies spatial properties from alignment_src to the current raster
- The coordinate system
- The number of rows and columns
- Cell size
Then resamples values from the current dataset using the nearest neighbor interpolation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alignment_src
|
Dataset
|
Spatial information source raster to get the spatial information (coordinate system, number of rows and columns). The data values of the current dataset are resampled to this alignment. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Dataset |
Dataset
|
A new aligned Dataset. |
Examples:
- The source dataset has a
top_left_cornerat (0, 0) with a 5*5 alignment, and a 0.05 degree cell size.
>>> import numpy as np
>>> arr = np.random.rand(5, 5)
>>> top_left_corner = (0, 0)
>>> cell_size = 0.05
>>> dataset = Dataset.create_from_array(arr, top_left_corner=top_left_corner, cell_size=cell_size, epsg=4326)
>>> print(dataset)
<BLANKLINE>
Cell size: 0.05
Dimension: 5 * 5
EPSG: 4326
Number of Bands: 1
Band names: ['Band_1']
Mask: -9999.0
Data type: float64
File:...
<BLANKLINE>
- The dataset to be aligned has a top_left_corner at (-0.1, 0.1) (i.e., it has two more rows on top of the dataset, and two columns on the left of the dataset).
>>> arr = np.random.rand(10, 10)
>>> top_left_corner = (-0.1, 0.1)
>>> cell_size = 0.07
>>> dataset_target = Dataset.create_from_array(arr, top_left_corner=top_left_corner, cell_size=cell_size,
... epsg=4326)
>>> print(dataset_target)
<BLANKLINE>
Cell size: 0.07
Dimension: 10 * 10
EPSG: 4326
Number of Bands: 1
Band names: ['Band_1']
Mask: -9999.0
Data type: float64
File:...
<BLANKLINE>

- Now call the
alignmethod and use the dataset as the alignment source.
>>> aligned_dataset = dataset_target.align(dataset)
>>> print(aligned_dataset)
<BLANKLINE>
Cell size: 0.05
Dimension: 5 * 5
EPSG: 4326
Number of Bands: 1
Band names: ['Band_1']
Mask: -9999.0
Data type: float64
File:...
<BLANKLINE>

Source code in src/pyramids/dataset/ops/spatial.py
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 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 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 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 | |
crop(mask, touch=True)
#
Crop dataset using dataset/feature collection.
Crop/Clip the Dataset object using a polygon/raster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
GeoDataFrame | Dataset
|
GeoDataFrame with a polygon geometry, or a Dataset object. |
required |
touch
|
bool
|
Include the cells that touch the polygon, not only those that lie entirely inside the polygon mask. Default is True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
Dataset |
Dataset
|
A new cropped Dataset. |
Hint
- If the mask is a dataset with multi-bands, the
cropmethod will use the first band as the mask.
Examples:
-
Crop the raster using a polygon mask.
-
The polygon covers 4 cells in the 3rd and 4th rows and 3rd and 4th column
arr[2:4, 2:4], so the result dataset will have the same number of bands4, 2 rows and 2 columns. - First, create the dataset to have 4 bands, 10 rows and 10 columns; the dataset has a cell size of 0.05 degree, the top left corner of the dataset is (0, 0).
>>> import numpy as np
>>> import geopandas as gpd
>>> from shapely.geometry import Polygon
>>> arr = np.random.rand(4, 10, 10)
>>> cell_size = 0.05
>>> top_left_corner = (0, 0)
>>> dataset = Dataset.create_from_array(
... arr, top_left_corner=top_left_corner, cell_size=cell_size, epsg=4326
... )
```python
>>> mask = gpd.GeoDataFrame(geometry=[Polygon([(0.1, -0.1), (0.1, -0.2), (0.2, -0.2), (0.2, -0.1)])], crs=4326)
```
- Pass the
geodataframeto the crop method using themaskparameter.
>>> print(cropped_dataset.shape)
(4, 2, 2)
>>> print(cropped_dataset.geotransform)
(0.1, 0.05, 0.0, -0.1, 0.0, -0.05)
>>> print(cropped_dataset.read_array(band=0))# doctest: +SKIP
[[0.00921161 0.90841171]
[0.355636 0.18650262]]
>>> print(arr[0, 2:4, 2:4])# doctest: +SKIP
[[0.00921161 0.90841171]
[0.355636 0.18650262]]
- Create a mask dataset with the same extent of the polygon we used in the previous example.
>>> geotransform = (0.1, 0.05, 0.0, -0.1, 0.0, -0.05)
>>> mask_dataset = Dataset.create_from_array(np.random.rand(2, 2), geo=geotransform, epsg=4326)
>>> cropped_dataset_2 = dataset.crop(mask=mask_dataset)
>>> print(cropped_dataset_2.shape)
(4, 2, 2)
>>> print(cropped_dataset_2.geotransform)
(0.1, 0.05, 0.0, -0.1, 0.0, -0.05)
>>> print(cropped_dataset_2.read_array(band=0))# doctest: +SKIP
[[0.00921161 0.90841171]
[0.355636 0.18650262]]
>>> print(arr[0, 2:4, 2:4])# doctest: +SKIP
[[0.00921161 0.90841171]
[0.355636 0.18650262]]
Source code in src/pyramids/dataset/ops/spatial.py
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 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 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 | |