rslearn.utils.raster_format¶
raster_format ¶
Abstract RasterFormat class.
RasterFormat ¶
An abstract class for writing raster data.
Implementations of RasterFormat should support reading and writing raster data in a UPath. Raster data is represented as a RasterArray (C, T, H, W).
Source code in rslearn/utils/raster_format.py
encode_raster ¶
encode_raster(path: UPath, projection: Projection, bounds: PixelBounds, raster: RasterArray) -> None
Encodes raster data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
UPath
|
the directory to write to |
required |
projection
|
Projection
|
the projection of the raster data |
required |
bounds
|
PixelBounds
|
the bounds of the raster data in the projection |
required |
raster
|
RasterArray
|
the raster data |
required |
Source code in rslearn/utils/raster_format.py
decode_raster ¶
decode_raster(path: UPath, projection: Projection, bounds: PixelBounds, resampling: Resampling = bilinear) -> RasterArray
Decodes raster data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
UPath
|
the directory to read from |
required |
projection
|
Projection
|
the projection to read the raster in. |
required |
bounds
|
PixelBounds
|
the bounds to read in the given projection. |
required |
resampling
|
Resampling
|
resampling method to use in case resampling is needed. |
bilinear
|
Returns:
| Type | Description |
|---|---|
RasterArray
|
the raster data |
Source code in rslearn/utils/raster_format.py
ImageTileRasterMetadata ¶
Bases: BaseModel
Metadata sidecar for ImageTileRasterFormat.
Source code in rslearn/utils/raster_format.py
ImageTileRasterFormat ¶
Bases: RasterFormat
A RasterFormat that stores data in image tiles corresponding to grid cells.
A tile size defines the grid size in pixels. One file is created for each grid cell that the raster intersects. The image format is configurable. The images are named by their (possibly negative) column and row along the grid.
Source code in rslearn/utils/raster_format.py
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 | |
encode_tile ¶
encode_tile(f: BinaryIO, projection: Projection, bounds: PixelBounds, array: NDArray[Any], nodata_value: int | float | None = None) -> None
Encodes a single tile to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
BinaryIO
|
the file object to write to |
required |
projection
|
Projection
|
the projection (used for GeoTIFF metadata) |
required |
bounds
|
PixelBounds
|
the bounds in the projection (used for GeoTIFF metadata) |
required |
array
|
NDArray[Any]
|
the raster data at this tile |
required |
nodata_value
|
int | float | None
|
optional scalar nodata value |
None
|
Source code in rslearn/utils/raster_format.py
decode_tile ¶
Decodes a single tile from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
BinaryIO
|
the file object to read from |
required |
Source code in rslearn/utils/raster_format.py
encode_raster ¶
encode_raster(path: UPath, projection: Projection, bounds: PixelBounds, raster: RasterArray) -> None
Encodes raster data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
UPath
|
the directory to write to |
required |
projection
|
Projection
|
the projection of the raster data |
required |
bounds
|
PixelBounds
|
the bounds of the raster data in the projection |
required |
raster
|
RasterArray
|
the raster data (CTHW RasterArray, T must be 1) |
required |
Source code in rslearn/utils/raster_format.py
decode_raster ¶
decode_raster(path: UPath, projection: Projection, bounds: PixelBounds, resampling: Resampling = bilinear) -> RasterArray
Decodes raster data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
UPath
|
the directory to read from |
required |
projection
|
Projection
|
the projection to read the raster in. |
required |
bounds
|
PixelBounds
|
the bounds to read in the given projection. |
required |
resampling
|
Resampling
|
resampling method to use in case resampling is needed. |
bilinear
|
Returns:
| Type | Description |
|---|---|
RasterArray
|
the raster data |
Source code in rslearn/utils/raster_format.py
get_extension ¶
Returns the extension to use based on the configured image format.
Source code in rslearn/utils/raster_format.py
GeotiffRasterMetadata ¶
Bases: BaseModel
Metadata sidecar for GeotiffRasterFormat.
All fields are optional for backward compatibility with legacy metadata files.
Source code in rslearn/utils/raster_format.py
GeotiffRasterFormat ¶
Bases: RasterFormat
A raster format that uses one big, tiled GeoTIFF with small block size.
Source code in rslearn/utils/raster_format.py
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 | |
encode_metadata
staticmethod
¶
encode_metadata(path: UPath, metadata: GeotiffRasterMetadata) -> None
Write a GeotiffRasterMetadata sidecar to path / metadata.json.
Source code in rslearn/utils/raster_format.py
decode_metadata
staticmethod
¶
decode_metadata(path: UPath) -> GeotiffRasterMetadata | None
Read the GeotiffRasterMetadata sidecar, or return None if absent.
Source code in rslearn/utils/raster_format.py
encode_raster ¶
encode_raster(path: UPath, projection: Projection, bounds: PixelBounds, raster: RasterArray, fname: str | None = None) -> None
Encodes raster data.
Supports multi-timestep data (T > 1) by flattening (C, T, H, W) to
(C*T, H, W) in the GeoTIFF and writing a metadata.json sidecar with
num_channels, num_timesteps, and timestamps.
For T == 1, a metadata.json is only written when timestamps are present.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
UPath
|
the directory to write to |
required |
projection
|
Projection
|
the projection of the raster data |
required |
bounds
|
PixelBounds
|
the bounds of the raster data in the projection |
required |
raster
|
RasterArray
|
the raster data (CTHW RasterArray) |
required |
fname
|
str | None
|
override the filename to save as |
None
|
Source code in rslearn/utils/raster_format.py
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 | |
decode_raster ¶
decode_raster(path: UPath, projection: Projection, bounds: PixelBounds, resampling: Resampling = bilinear, fname: str | None = None, nodata_val: int | float | None = None) -> RasterArray
Decodes raster data.
If a metadata.json sidecar exists with num_timesteps > 1, the GeoTIFF
is treated as (C*T, H, W) and reshaped back to (C, T, H, W).
If the GeoTIFF has a nodata value set, it is read back and populated on
the returned RasterArray.metadata.nodata_value. When nodata_val
is provided it overrides the source nodata for the WarpedVRT read, so
out-of-bounds pixels are filled with that value instead;
metadata.nodata_value on the returned array is then set to the
override value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
UPath
|
the directory to read from |
required |
projection
|
Projection
|
the projection to read the raster in. |
required |
bounds
|
PixelBounds
|
the bounds to read in the given projection. |
required |
resampling
|
Resampling
|
resampling method to use in case resampling is needed. |
bilinear
|
fname
|
str | None
|
override the filename to read from |
None
|
nodata_val
|
int | float | None
|
override the nodata value in the raster when reading. Pixels in bounds that are not present in the source raster will be initialized to this value. Note that, if the raster specifies a nodata value, and some source pixels have that value, they will still be read under their original value; overriding the nodata value is primarily useful if the user wants out of bounds pixels to have a different value from the source pixels, e.g. if the source data has background and foreground classes (with background being nodata) but we want to read it in a different projection and have out of bounds pixels be a third "invalid" value. |
None
|
Returns:
| Type | Description |
|---|---|
RasterArray
|
the raster data as a RasterArray (CTHW) |
Source code in rslearn/utils/raster_format.py
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 | |
get_raster_bounds ¶
Returns the bounds of the stored raster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
UPath
|
the directory where the raster data was written |
required |
Returns:
| Type | Description |
|---|---|
PixelBounds
|
the PixelBounds of the raster |
Source code in rslearn/utils/raster_format.py
SingleImageRasterMetadata ¶
Bases: BaseModel
Metadata sidecar for SingleImageRasterFormat.
Source code in rslearn/utils/raster_format.py
SingleImageRasterFormat ¶
Bases: RasterFormat
A raster format that produces a single image called image.png/jpg.
Primarily for ease-of-use with external tools that don't support georeferenced images and would rather have everything in pixel coordinate system.
Source code in rslearn/utils/raster_format.py
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 | |
get_extension ¶
Get the filename extension to use when storing the image.
Returns:
| Type | Description |
|---|---|
str
|
the string filename extension, e.g. png or jpg |
Source code in rslearn/utils/raster_format.py
encode_raster ¶
encode_raster(path: UPath, projection: Projection, bounds: PixelBounds, raster: RasterArray) -> None
Encodes raster data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
UPath
|
the directory to write to |
required |
projection
|
Projection
|
the projection of the raster data |
required |
bounds
|
PixelBounds
|
the bounds of the raster data in the projection |
required |
raster
|
RasterArray
|
the raster data (CTHW RasterArray, T must be 1) |
required |
Source code in rslearn/utils/raster_format.py
decode_raster ¶
decode_raster(path: UPath, projection: Projection, bounds: PixelBounds, resampling: Resampling = bilinear) -> RasterArray
Decodes raster data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
UPath
|
the directory to read from |
required |
projection
|
Projection
|
the projection to read the raster in. |
required |
bounds
|
PixelBounds
|
the bounds to read in the given projection. |
required |
resampling
|
Resampling
|
resampling method to use in case resampling is needed. |
bilinear
|
Returns:
| Type | Description |
|---|---|
RasterArray
|
the raster data |
Source code in rslearn/utils/raster_format.py
NumpyRasterMetadata ¶
Bases: BaseModel
Metadata sidecar for NumpyRasterFormat.
Source code in rslearn/utils/raster_format.py
NumpyRasterFormat ¶
Bases: RasterFormat
A raster format that stores data as a NumPy .npy file.
This avoids GeoTIFF overhead for small spatial arrays (e.g. 1x1 pixels) and/or arrays with many bands (e.g. C*T > 1000).
The directory contains two files:
- data.npy: the raw (C, T, H, W) array.
- metadata.json: projection, bounds, dtype, channel/timestep counts,
and optional timestamps.
decode_raster does not support re-projection, only cropping/padding; if the
requested Projection does not match the Projection under which the image data was
stored, an exception will be raised.
Source code in rslearn/utils/raster_format.py
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 | |
encode_raster ¶
encode_raster(path: UPath, projection: Projection, bounds: PixelBounds, raster: RasterArray) -> None
Encode a RasterArray to data.npy + metadata.json.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
UPath
|
directory to write into. |
required |
projection
|
Projection
|
the projection of the raster data. |
required |
bounds
|
PixelBounds
|
the bounds of the raster data in the projection. |
required |
raster
|
RasterArray
|
the (C, T, H, W) RasterArray to store. |
required |
Source code in rslearn/utils/raster_format.py
decode_raster ¶
decode_raster(path: UPath, projection: Projection, bounds: PixelBounds, resampling: Resampling = bilinear) -> RasterArray
Decode a previously stored data.npy + metadata.json.
Projection must match the stored projection. If the requested bounds differ from the stored bounds the array is cropped/padded accordingly (out-of-bounds pixels are filled with the nodata value, or 0).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
UPath
|
directory to read from. |
required |
projection
|
Projection
|
the projection to read the raster in. |
required |
bounds
|
PixelBounds
|
the bounds to read in the given projection. |
required |
resampling
|
Resampling
|
ignored (kept for interface conformance). |
bilinear
|
Returns:
| Type | Description |
|---|---|
RasterArray
|
the (C, T, H, W) RasterArray. |
Source code in rslearn/utils/raster_format.py
get_bandset_dirname ¶
Get the directory name that should be used to store the given group of bands.
Source code in rslearn/utils/raster_format.py
get_raster_projection_and_bounds_from_transform ¶
get_raster_projection_and_bounds_from_transform(crs: CRS, transform: Affine, width: int, height: int) -> tuple[Projection, PixelBounds]
Determine Projection and bounds from the specified CRS and transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
crs
|
CRS
|
the coordinate reference system. |
required |
transform
|
Affine
|
corresponding affine transform matrix. |
required |
width
|
int
|
the array width |
required |
height
|
int
|
the array height |
required |
Returns:
| Type | Description |
|---|---|
tuple[Projection, PixelBounds]
|
a tuple (projection, bounds). |
Source code in rslearn/utils/raster_format.py
get_raster_projection_and_bounds ¶
get_raster_projection_and_bounds(raster: DatasetReader) -> tuple[Projection, PixelBounds]
Determine the Projection and bounds of the specified raster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raster
|
DatasetReader
|
the raster dataset opened with rasterio. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Projection, PixelBounds]
|
a tuple (projection, bounds). |
Source code in rslearn/utils/raster_format.py
get_transform_from_projection_and_bounds ¶
get_transform_from_projection_and_bounds(projection: Projection, bounds: PixelBounds) -> Affine
Get the affine transform that corresponds to the given projection and bounds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
projection
|
Projection
|
the projection. Only the resolutions are used. |
required |
bounds
|
PixelBounds
|
the bounding box. Only the top-left corner is used. |
required |
Source code in rslearn/utils/raster_format.py
adjust_projection_and_bounds_for_array ¶
adjust_projection_and_bounds_for_array(projection: Projection, bounds: PixelBounds, array: NDArray) -> tuple[Projection, PixelBounds]
Adjust the projection and bounds to correspond to the resolution of the array.
The returned projection and bounds cover the same spatial extent as the inputs, but are updated so that the width and height match that of the array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
projection
|
Projection
|
the original projection. |
required |
bounds
|
PixelBounds
|
the original bounds. |
required |
array
|
NDArray
|
the CHW array for which to compute an updated projection and bounds. The returned bounds will have the same width and height as this array. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Projection, PixelBounds]
|
a tuple of adjusted (projection, bounds) |