GeoTensor
GeoTensor
¶
This class is a wrapper around a numpy or torch tensor with geospatial information. It can store 2D, 3D or 4D tensors. The last two dimensions are the spatial dimensions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values
|
Tensor
|
numpy or torch tensor (2D, 3D or 4D). |
required |
transform
|
Affine
|
affine geospatial transform |
required |
crs
|
Any
|
coordinate reference system |
required |
fill_value_default
|
Optional[Union[int, float]]
|
Value to fill when reading out of bounds. Could be None. Defaults to 0. |
0
|
Attributes:
Name | Type | Description |
---|---|---|
values |
Tensor
|
numpy or torch tensor |
transform |
Affine
|
affine geospatial transform |
crs |
Any
|
coordinate reference system |
fill_value_default |
Optional[Union[int, float]]
|
Value to fill when reading out of bounds. Could be None. Defaults to 0. |
shape |
Tuple
|
shape of the tensor |
res |
Tuple[float, float]
|
resolution of the tensor |
dtype |
data type of the tensor |
|
height |
int
|
height of the tensor |
width |
int
|
width of the tensor |
count |
int
|
number of bands in the tensor |
bounds |
Tuple[float, float, float, float]
|
bounds of the tensor |
dims |
Tuple[str]
|
names of the dimensions |
attrs |
Dict[str, Any]
|
dictionary with the attributes of the GeoTensor |
Examples:
>>> import numpy as np
>>> transform = rasterio.Affine(1, 0, 0, 0, -1, 0)
>>> crs = "EPSG:4326"
>>> gt = GeoTensor(np.random.rand(3, 100, 100), transform, crs)
Source code in georeader/geotensor.py
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 |
|
__add__(other)
¶
Add two GeoTensors. The georeferencing must match.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
GeoTensor
|
GeoTensor to add. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
if the georeferencing does not match. |
TypeError
|
if other is not a GeoTensor. |
Returns:
Name | Type | Description |
---|---|---|
GeoTensor |
__class__
|
GeoTensor with the result of the addition. |
Source code in georeader/geotensor.py
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 |
|
__init__(values, transform, crs, fill_value_default=0)
¶
This class is a wrapper around a numpy or torch tensor with geospatial information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values
|
Tensor
|
numpy or torch tensor |
required |
transform
|
Affine
|
affine geospatial transform |
required |
crs
|
Any
|
coordinate reference system |
required |
fill_value_default
|
Optional[Union[int, float]]
|
Value to fill when reading out of bounds. Could be None. Defaults to 0. |
0
|
Raises:
Type | Description |
---|---|
ValueError
|
when the shape of the tensor is not 2d, 3d or 4d. |
Source code in georeader/geotensor.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
|
__mul__(other)
¶
Multiply two GeoTensors. The georeferencing must match.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
GeoTensor
|
GeoTensor to add. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
if the georeferencing does not match. |
TypeError
|
if other is not a GeoTensor. |
Returns:
Name | Type | Description |
---|---|---|
GeoTensor |
__class__
|
GeoTensor with the result of the multiplication. |
Source code in georeader/geotensor.py
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 |
|
__setitem__(index, value)
¶
Set the values of the GeoTensor object using an index and a new value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index
|
tuple or ndarray
|
Index or boolean mask to apply to the GeoTensor values. |
required |
value
|
ndarray
|
New value to assign to the GeoTensor values at the specified index. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the index is not a tuple or a boolean numpy array with the same shape as the GeoTensor values. |
Examples:
>>> gt = GeoTensor(np.random.rand(3, 100, 100), transform, crs)
>>> boolmask = gt.values > 0.5
>>> gt[boolmask] = 0.5
Source code in georeader/geotensor.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
|
__sub__(other)
¶
Substract two GeoTensors. The georeferencing must match.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
GeoTensor
|
GeoTensor to add. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
if the georeferencing does not match. |
TypeError
|
if other is not a GeoTensor. |
Returns:
Name | Type | Description |
---|---|---|
GeoTensor |
__class__
|
GeoTensor with the result of the substraction. |
Source code in georeader/geotensor.py
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 |
|
__truediv__(other)
¶
Divide two GeoTensors. The georeferencing must match.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
GeoTensor
|
GeoTensor to add. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
if the georeferencing does not match. |
TypeError
|
if other is not a GeoTensor. |
Returns:
Name | Type | Description |
---|---|---|
GeoTensor |
__class__
|
GeoTensor with the result of the division. |
Source code in georeader/geotensor.py
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 |
|
clip(a_min, a_max)
¶
Clip the GeoTensor values between the GeoTensor min and max values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a_min
|
float
|
Minimum value. |
required |
a_max
|
float
|
Maximum value. |
required |
Returns:
Name | Type | Description |
---|---|---|
GeoTensor |
__class__
|
GeoTensor with the clipped values. |
Source code in georeader/geotensor.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
|
footprint(crs=None)
¶
Returns the footprint of the GeoTensor as a Polygon.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
crs
|
Optional[str]
|
Coordinate reference system. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Polygon |
Polygon
|
footprint of the GeoTensor. |
Examples:
>>> gt = GeoTensor(np.random.rand(3, 100, 100), transform, crs)
>>> gt.footprint(crs="EPSG:4326") # returns a Polygon in WGS84
Source code in georeader/geotensor.py
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
|
isel(sel)
¶
Slicing with dict. It doesn't work with negative indexes!
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sel
|
Dict[str, Union[slice, list, int]]
|
Dict with slice selection; i.e. |
required |
Returns:
Name | Type | Description |
---|---|---|
GeoTensor |
__class__
|
GeoTensor with the sliced values. |
Examples:
>>> gt = GeoTensor(np.random.rand(3, 100, 100), transform, crs)
>>> gt.isel({"x": slice(10, 20), "y": slice(20, 340)})
Source code in georeader/geotensor.py
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 |
|
pad(pad_width, mode='constant', constant_values=None)
¶
Pad the GeoTensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pad_width
|
_type_
|
dictionary with Tuple to pad for each dimension
|
required |
mode
|
str
|
pad mode (see np.pad or torch.nn.functional.pad). Defaults to "constant". |
'constant'
|
constant_values
|
Any
|
description. Defaults to |
None
|
Returns:
Name | Type | Description |
---|---|---|
GeoTensor |
__class__
|
padded GeoTensor. |
Examples:
>>> gt = GeoTensor(np.random.rand(3, 100, 100), transform, crs)
>>> gt.pad({"x": (10, 10), "y": (10, 10)})
>>> assert gt.shape == (3, 120, 120)
Source code in georeader/geotensor.py
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 |
|
read_from_window(window, boundless=True)
¶
returns a new GeoTensor object with the spatial dimensions sliced
Parameters:
Name | Type | Description | Default |
---|---|---|---|
window
|
Window
|
window to slice the current GeoTensor |
required |
boundless
|
bool
|
read from window in boundless mode (i.e. if the window is larger or negative it will pad
the GeoTensor with |
True
|
Raises:
Type | Description |
---|---|
WindowError
|
if |
Returns:
Type | Description |
---|---|
__class__
|
GeoTensor object with the spatial dimensions sliced |
Source code in georeader/geotensor.py
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 |
|
resize(output_shape, anti_aliasing=True, anti_aliasing_sigma=None, interpolation='bilinear', mode_pad='constant')
¶
Resize the geotensor to match a certain size output_shape. This function works with GeoTensors of 2D, 3D and 4D. The geoinformation of the output tensor is changed accordingly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_shape
|
Tuple[int, int]
|
output spatial shape |
required |
anti_aliasing
|
bool
|
Whether to apply a Gaussian filter to smooth the image prior to downsampling |
True
|
anti_aliasing_sigma
|
Optional[Union[float, ndarray]]
|
anti_aliasing_sigma : {float}, optional Standard deviation for Gaussian filtering used when anti-aliasing. By default, this value is chosen as (s - 1) / 2 where s is the downsampling factor, where s > 1 |
None
|
interpolation
|
Optional[str]
|
– algorithm used for resizing: 'nearest' | 'bilinear' | ‘bicubic’ |
'bilinear'
|
mode_pad
|
str
|
mode pad for resize function |
'constant'
|
Returns:
Type | Description |
---|---|
__class__
|
resized GeoTensor |
Examples:
>>> gt = GeoTensor(np.random.rand(3, 100, 100), transform, crs)
>>> resized = gt.resize((50, 50))
>>> assert resized.shape == (3, 50, 50)
>>> assert resized.res == (2*gt.res[0], 2*gt.res[1])
Source code in georeader/geotensor.py
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 |
|
same_extent(other, precision=0.001)
¶
Check if two GeoTensors have the same georeferencing (crs and transform)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
__class__ | GeoData
|
GeoTensor to compare with. Other GeoData object can be passed (it requires crs, transform and shape attributes) |
required |
precision
|
float
|
precision to compare the transform. Defaults to 1e-3. |
0.001
|
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if both GeoTensors have the same georeferencing. |
Source code in georeader/geotensor.py
176 177 178 179 180 181 182 183 184 185 186 187 |
|
squeeze()
¶
Remove single-dimensional entries from the shape of the GeoTensor values. It does not squeeze the spatial dimensions (last two dimensions).
Returns:
Name | Type | Description |
---|---|---|
GeoTensor |
__class__
|
GeoTensor with the squeezed values. |
Source code in georeader/geotensor.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
|
valid_footprint(crs=None, method='all')
¶
vectorizes the valid values of the GeoTensor and returns the footprint as a Polygon.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
crs
|
Optional[str]
|
Coordinate reference system. Defaults to None. |
None
|
method
|
str
|
"all" or "any" to aggregate the channels of the image. Defaults to "all". |
'all'
|
Returns:
Type | Description |
---|---|
Union[MultiPolygon, Polygon]
|
Polygon or MultiPolygon: footprint of the GeoTensor. |
Source code in georeader/geotensor.py
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 |
|
write_from_window(data, window)
¶
Writes array to GeoTensor values object at the given window position. If window surpasses the bounds of this object it crops the data to fit the object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Tensor
|
Tensor to write. Expected: spatial dimensions |
required |
window
|
Window
|
Window object that specifies the spatial location to write the data |
required |
Examples:
>>> gt = GeoTensor(np.random.rand(3, 100, 100), transform, crs)
>>> data = np.random.rand(3, 50, 50)
>>> window = rasterio.windows.Window(col_off=7, row_off=9, width=50, height=50)
>>> gt.write_from_window(data, window)
Source code in georeader/geotensor.py
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 |
|
concatenate(geotensors, axis=0)
¶
Concatenates a list of geotensors along a given axis, assert that all of them has same shape, transform and crs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
geotensors
|
List[GeoTensor]
|
list of geotensors to concat. All with same shape, transform and crs. |
required |
axis
|
int
|
axis to concatenate. Must be less than the number of dimensions of the geotensors minus 2. default is 0. |
0
|
Returns:
Type | Description |
---|---|
GeoTensor
|
geotensor with extra dim at the front: (len(geotensors),) + shape |
Examples:
>>> gt1 = GeoTensor(np.random.rand(3, 100, 100), transform, crs)
>>> gt2 = GeoTensor(np.random.rand(3, 100, 100), transform, crs)
>>> gt3 = GeoTensor(np.random.rand(3, 100, 100), transform, crs)
>>> gt = concatenate([gt1, gt2, gt3], axis=0)
>>> assert gt.shape == (9, 100, 100)
Source code in georeader/geotensor.py
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 |
|
stack(geotensors)
¶
Stacks a list of geotensors, assert that all of them has same shape, transform and crs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
geotensors
|
List[GeoTensor]
|
list of geotensors to concat. All with same shape, transform and crs. |
required |
Returns:
Type | Description |
---|---|
GeoTensor
|
geotensor with extra dim at the front: (len(geotensors),) + shape |
Examples:
>>> gt1 = GeoTensor(np.random.rand(3, 100, 100), transform, crs)
>>> gt2 = GeoTensor(np.random.rand(3, 100, 100), transform, crs)
>>> gt3 = GeoTensor(np.random.rand(3, 100, 100), transform, crs)
>>> gt = stack([gt1, gt2, gt3])
>>> assert gt.shape == (3, 3, 100, 100)
Source code in georeader/geotensor.py
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 |
|