DataCube Class#
pyramids.datacube.Datacube
#
DataCube.
Source code in pyramids/datacube.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 |
|
data
instance-attribute
#
files
list of geotiff files' names
base
property
#
base.
Base Dataset
files
property
#
Files.
time_length
property
#
Length of the dataset.
rows
property
#
Number of rows.
shape
property
#
Number of rows.
columns
property
#
Number of columns.
values
deletable
property
writable
#
Values.
- The attribute where the dataset array is stored.
- the 3D numpy array, [dataset length, rows, cols], [dataset length, lons, lats]
create_cube(src, dataset_length)
classmethod
#
read_multiple_files(path, with_order=False, regex_string='\\d{4}.\\d{2}.\\d{2}', date=True, file_name_data_fmt=None, start=None, end=None, fmt='%Y-%m-%d', extension='.tif')
classmethod
#
read_multiple_files.
- Read rasters from a folder (or list of files) and create a 3D array with the same 2D dimensions as the
first raster and length equal to the number of files.
- All rasters should have the same dimensions.
- If you want to read the rasters with a certain order, the raster file names should contain a date
that follows a consistent format (YYYY.MM.DD / YYYY-MM-DD or YYYY_MM_DD), e.g. "MSWEP_1979.01.01.tif".
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | List[str]
|
Path of the folder that contains all the rasters, or a list containing the paths of the rasters to read. |
required |
with_order
|
bool
|
False
|
|
regex_string
|
str
|
A regex string used to locate the date in the file names. Default is r"\d{4}.\d{2}.\d{2}". For example:
|
'\\d{4}.\\d{2}.\\d{2}'
|
date
|
bool
|
True if the number in the file name is a date. Default is True. |
True
|
file_name_data_fmt
|
str
|
None
|
|
start
|
str
|
Start date if you want to read the input raster for a specific period only and not all rasters. If not given, all rasters in the given path will be read. |
None
|
end
|
str
|
End date if you want to read the input rasters for a specific period only. If not given, all rasters in the given path will be read. |
None
|
fmt
|
str
|
Format of the given date in the start/end parameter. |
'%Y-%m-%d'
|
extension
|
str
|
The extension of the files you want to read from the given path. Default is ".tif". |
'.tif'
|
Returns:
Name | Type | Description |
---|---|---|
Datacube |
Datacube
|
Instance of the Datacube class. |
Examples:
- Read all rasters in a folder:
>>> from pyramids.datacube import Datacube
>>> raster_folder = "examples/GIS/data/raster-folder"
>>> prec = Datacube.read_multiple_files(raster_folder)
- Read from a pre-collected list without ordering:
>>> import glob
>>> search_criteria = "*.tif"
>>> file_list = glob.glob(os.path.join(raster_folder, search_criteria))
>>> prec = Datacube.read_multiple_files(file_list, with_order=False)
Source code in pyramids/datacube.py
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 |
|
open_datacube(band=0)
#
Open the datacube.
Read values from the given band as arrays for all files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
band
|
int
|
Index of the band you want to read. Default is 0. |
0
|
Returns:
Name | Type | Description |
---|---|---|
None |
None
|
Loads values into the internal 3D array [time, rows, cols] in-place. |
Source code in pyramids/datacube.py
iloc(i)
#
iloc.
- Access dataset array using index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
i
|
int
|
Index of the dataset to access. |
required |
Returns:
Name | Type | Description |
---|---|---|
Dataset |
Dataset
|
Dataset object. |
Source code in pyramids/datacube.py
plot(band=0, exclude_value=None, **kwargs)
#
Read Array.
- read the values stored in a given band.
Parameters:
Name | Type | Description | Default | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
band
|
int
|
The band you want to get its data. Default is 0. |
0
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
exclude_value
|
Any
|
Value to exclude from the plot. Default is None. |
None
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
**kwargs
|
Any
|
|
{}
|
Returns:
Name | Type | Description |
---|---|---|
ArrayGlyph |
ArrayGlyph
|
A plotting/animation handle (from cleopatra.ArrayGlyph). |
Source code in pyramids/datacube.py
to_file(path, driver='geotiff', band=0)
#
Save to geotiff format.
saveRaster saves a raster to a path
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
Union[str, List[str]]
|
a path includng the name of the raster and extention. |
required |
driver
|
str
|
driver = "geotiff". |
'geotiff'
|
band
|
int
|
band index, needed only in case of ascii drivers. Default is 1. |
0
|
Examples:
- Save to a file:
>>> raster_obj = Dataset.read_file("path/to/file/***.tif")
>>> output_path = "examples/GIS/data/save_raster_test.tif"
>>> raster_obj.to_file(output_path)
Source code in pyramids/datacube.py
to_crs(to_epsg=3857, method='nearest neighbor', maintain_alignment=False)
#
to_epsg.
- to_epsg reprojects a raster to any projection (default the WGS84 web mercator projection,
without resampling) The function returns a GDAL in-memory file object, where you can ReadAsArray etc.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
to_epsg
|
int
|
Reference number to the new projection (https://epsg.io/) (default 3857 the reference no of WGS84 web mercator). |
3857
|
method
|
str
|
Resampling technique. Default is "Nearest". See https://gisgeography.com/raster-resampling/. "Nearest" for nearest neighbor, "cubic" for cubic convolution, "bilinear" for 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 |
---|---|---|
None |
None
|
Updates the datacube values and base in place after reprojection. |
Examples:
- Reproject dataset to EPSG:3857:
>>> from pyramids.dataset import Dataset
>>> src = Dataset.read_file("path/raster_name.tif")
>>> projected_raster = src.to_crs(to_epsg=3857)
Source code in pyramids/datacube.py
crop(mask, inplace=False, touch=True)
#
crop.
crop matches the location of nodata value from src raster to dst raster. Mask is where the NoDatavalue will
be taken and the location of this value. src_dir is path to the folder where rasters exist where we need to
put the NoDataValue of the mask in RasterB at the same locations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mask
|
Dataset
|
Dataset object of the mask raster to crop the rasters (to get the NoData value and its location in the array). Mask should include the name of the raster and the extension like "data/dem.tif", or you can read the mask raster using gdal and use it as the first parameter to the function. |
required |
inplace
|
bool
|
True to make the changes in place. |
False
|
touch
|
bool
|
Include the cells that touch the polygon, not only those that lie entirely inside the polygon mask. Default is True. |
True
|
Returns:
Type | Description |
---|---|
Union[None, Dataset]
|
Union[None, "Datacube"]: New rasters have the values from rasters in B_input_path with the NoDataValue in |
Union[None, Dataset]
|
the same locations as raster A. |
Examples:
- Crop aligned rasters using a DEM mask:
>>> dem_path = "examples/GIS/data/acc4000.tif"
>>> src_path = "examples/GIS/data/aligned_rasters/"
>>> out_path = "examples/GIS/data/crop_aligned_folder/"
>>> Datacube.crop(dem_path, src_path, out_path)
Source code in pyramids/datacube.py
align(alignment_src)
#
matchDataAlignment.
This function matches the coordinate system and the number of rows and columns between two rasters. Raster A is the source of the coordinate system, number of rows, number of columns, and cell size. The result will be a raster with the same structure as Raster A but with values from Raster B using nearest neighbor interpolation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alignment_src
|
Dataset
|
Dataset to use as the spatial template (CRS, rows, columns). |
required |
Returns:
Name | Type | Description |
---|---|---|
None |
None
|
Updates the datacube values in place to match the alignment of alignment_src. |
Examples:
- Align all rasters in the datacube to a DEM raster:
>>> dem_path = "01GIS/inputs/4000/acc4000.tif"
>>> prec_in_path = "02Precipitation/CHIRPS/Daily/"
>>> prec_out_path = "02Precipitation/4km/"
>>> Dataset.align(dem_path, prec_in_path, prec_out_path)
Source code in pyramids/datacube.py
merge(src, dst, no_data_value='0', init='nan', n='nan')
staticmethod
#
merge.
Merges a group of rasters into one raster.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
src
|
List[str]
|
List of paths to all input rasters. |
required |
dst
|
str
|
Path to the output raster. |
required |
no_data_value
|
float | int | str
|
Assign a specified nodata value to output bands. |
'0'
|
init
|
float | int | str
|
Pre-initialize the output image bands with these values. However, it is not marked as the nodata value in the output file. If only one value is given, the same value is used in all the bands. |
'nan'
|
n
|
float | int | str
|
Ignore pixels from files being merged in with this pixel value. |
'nan'
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in pyramids/datacube.py
apply(ufunc)
#
apply.
Apply a function on each raster in the datacube.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ufunc
|
Callable
|
Callable universal function (builtin or user defined). See https://numpy.org/doc/stable/reference/ufuncs.html To create a ufunc from a normal function: https://numpy.org/doc/stable/reference/generated/numpy.frompyfunc.html |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Examples:
- Apply a simple modulo operation to each value:
Source code in pyramids/datacube.py
overlay(classes_map, exclude_value=None)
#
Overlay.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
classes_map
|
Dataset
|
Dataset object for the raster that has classes to overlay with. |
required |
exclude_value
|
float | int
|
Values to exclude from extracted values. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Dict[List[float], List[float]]
|
Dict[List[float], List[float]]: Dictionary with a list of values in the basemap as keys and for each key a list of all the intersected values in the maps from the path. |