GeoTensor¶
The GeoTensor class is a subclass of np.ndarray that stores the spatial affine transform, the coordinate reference system (crs) and no data value (called fill_value_default). When a GeoTensor is sliced, its transform attribute is shifted accordingly. Additionally its transform is also shifted if the GeoTensor is padded. GeoTensors are restricted to be 2D, 3D or 4D arrays and their two last dimensions are assumed to be the y and x spatial axis.
GeoTensor implements the GeoData protocol. This makes it fully compatible with all read_* methods in the library, allowing for operations like reprojection, subseting from spatial coordinates, mosaicking or vectorization.
For a detailed guide on working with the NumPy API in GeoTensors, see the GeoTensor NumPy API tutorial which covers slicing, operations, masking, and more practical examples.
As a subclass of np.ndarray, operations with GeoTensor objects work similar than operations with np.ndarrays. However, there are some restrictions that we have implemented to keep consistency with the GeoTensor concept. If you need to use the numpy implementation you can access the bare numpy object with the .values attribute. Below there's a list with restrictions on numpy operations:
-
Slicing a
GeoTensoris more restrictive than anumpyarray. It only allows to slice withlists, numbers orslices. In particular the spatial dimensions can only be sliced withslices. Slicing for inplace modification is not restricted (i.e. you can slice with boolean arrays to modify certain values of the object). See isel and getitem methods. -
Binary operations (such as add add, multiply mul, ==, | etc) check, for
GeoTensorinputs, if they have the same_extent; that is, sametransformcrsand spatial dimensions (widthandheight). -
squeeze, expand_dims and transpose make sure spatial dimensions (last two axes) are not modified and kept at the end of the array.
-
concatenate and stack make sure all operated
GeoTensors havesame_extentandshape.concatenatedoes not allow to concatenate on the spatial dims. -
Reductions (such as
np.meanornp.all) returnGeoTensorobject if the spatial dimensions are preserved andnp.ndarrayor scalars otherwise. This is handled by the array_ufunc method.
Additional Features¶
- Masking utilities: Methods like validmask() and invalidmask() create boolean masks based on the
fill_value_default. - Window-based access: read_from_window() and write_from_window() provide window-based operations using rasterio's window system.
- File I/O: Class methods load_file() and load_bytes() for easily loading GeoTensors from files or memory.
- Footprint extraction: Methods to extract the valid data footprint as vector geometries (footprint() and valid_footprint()).
- Coordinate transformation: The meshgrid() method creates coordinate arrays for the spatial dimensions.
- Resizing: The resize() method changes the spatial resolution while maintaining geospatial information correct (i.e. changing the spatial resolution of the
transform). - Metadata storage: GeoTensor includes an
attrsdictionary for storing additional metadata like tags and band descriptions.
API Reference¶
GeoTensor
¶
Bases: ndarray
This class is a wrapper around a numpy 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. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
values |
NDArray
|
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
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 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 | |
values
property
¶
Return a view of the array (memory shared with original)
__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 |
Self
|
GeoTensor with the result of the addition. |
Source code in georeader/geotensor.py
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 | |
__and__(other)
¶
Perform bitwise AND operation between two GeoTensors. The georeferencing must match.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Union[Number, NDArray, GeoTensor]
|
GeoTensor or array-like to AND with. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the georeferencing does not match when other is a GeoTensor. |
Returns:
| Name | Type | Description |
|---|---|---|
GeoTensor |
Self
|
GeoTensor with the result of the bitwise AND operation. |
Source code in georeader/geotensor.py
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 | |
__array__(dtype=None)
¶
Convert the GeoTensor to a standard NumPy array.
This method is called by np.asarray() and most NumPy functions to get the underlying NumPy array representation of this object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dtype
|
Optional[dtype]
|
The desired data type for the returned array. If None, the array's current dtype is preserved. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A NumPy array view of this GeoTensor. |
Source code in georeader/geotensor.py
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | |
__array_finalize__(obj)
¶
Initialize attributes when a new GeoTensor is created from an existing array.
This method is called whenever a new array object is created from an existing array (e.g., through slicing, view casting, or copy operations).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Optional[ndarray]
|
The array object from which the new array is created. Can be None if the array is being created from scratch. |
required |
Source code in georeader/geotensor.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
__array_ufunc__(ufunc, method, *inputs, **kwargs)
¶
Handle NumPy universal functions applied to this GeoTensor.
This method is called when a NumPy universal function (ufunc) is applied to the GeoTensor. It converts GeoTensor inputs to NumPy arrays, applies the ufunc, and converts array results back to GeoTensor objects with the same geospatial metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ufunc
|
ufunc
|
The NumPy universal function being applied |
required |
method
|
str
|
The method of the ufunc ('call', 'reduce', etc.) |
required |
*inputs
|
The input arrays to the ufunc |
()
|
|
**kwargs
|
Additional keyword arguments to the ufunc |
{}
|
Returns:
| Type | Description |
|---|---|
|
Union[GeoTensor, NDArray]: If the result is an array, returns a new GeoTensor with the same geospatial attributes. Otherwise, returns the original result. |
Source code in georeader/geotensor.py
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 | |
__eq__(other)
¶
Element-wise equality comparison between GeoTensors or with a scalar/array. The georeferencing must match if comparing with another GeoTensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Union[Number, NDArray, GeoTensor]
|
Value to compare with. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If comparing with a GeoTensor and the georeferencing doesn't match. |
Returns:
| Name | Type | Description |
|---|---|---|
GeoTensor |
Self
|
GeoTensor with boolean values indicating equality. |
Source code in georeader/geotensor.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 | |
__ge__(other)
¶
Element-wise greater than or equal comparison between GeoTensors or with a scalar/array. The georeferencing must match if comparing with another GeoTensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Union[Number, NDArray, GeoTensor]
|
Value to compare with. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If comparing with a GeoTensor and the georeferencing doesn't match. |
Returns:
| Name | Type | Description |
|---|---|---|
GeoTensor |
Self
|
GeoTensor with boolean values indicating greater than or equal relationship. |
Source code in georeader/geotensor.py
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 | |
__getitem__(key)
¶
Get the values of the GeoTensor using the given key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Key to index the GeoTensor. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
GeoTensor |
GeoTensor with the selected values. |
Source code in georeader/geotensor.py
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 | |
__gt__(other)
¶
Element-wise greater than comparison between GeoTensors or with a scalar/array. The georeferencing must match if comparing with another GeoTensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Union[Number, NDArray, GeoTensor]
|
Value to compare with. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If comparing with a GeoTensor and the georeferencing doesn't match. |
Returns:
| Name | Type | Description |
|---|---|---|
GeoTensor |
Self
|
GeoTensor with boolean values indicating greater than relationship. |
Source code in georeader/geotensor.py
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 | |
__le__(other)
¶
Element-wise less than or equal comparison between GeoTensors or with a scalar/array. The georeferencing must match if comparing with another GeoTensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Union[Number, NDArray, GeoTensor]
|
Value to compare with. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If comparing with a GeoTensor and the georeferencing doesn't match. |
Returns:
| Name | Type | Description |
|---|---|---|
GeoTensor |
Self
|
GeoTensor with boolean values indicating less than or equal relationship. |
Source code in georeader/geotensor.py
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 | |
__lt__(other)
¶
Element-wise less than comparison between GeoTensors or with a scalar/array. The georeferencing must match if comparing with another GeoTensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Union[Number, NDArray, GeoTensor]
|
Value to compare with. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If comparing with a GeoTensor and the georeferencing doesn't match. |
Returns:
| Name | Type | Description |
|---|---|---|
GeoTensor |
Self
|
GeoTensor with boolean values indicating less than relationship. |
Source code in georeader/geotensor.py
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 | |
__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 |
Self
|
GeoTensor with the result of the multiplication. |
Source code in georeader/geotensor.py
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 | |
__ne__(other)
¶
Element-wise inequality comparison between GeoTensors or with a scalar/array. The georeferencing must match if comparing with another GeoTensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Union[Number, NDArray, GeoTensor]
|
Value to compare with. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If comparing with a GeoTensor and the georeferencing doesn't match. |
Returns:
| Name | Type | Description |
|---|---|---|
GeoTensor |
Self
|
GeoTensor with boolean values indicating inequality. |
Source code in georeader/geotensor.py
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 | |
__new__(values, transform, crs, fill_value_default=0, attrs=None)
¶
This class is a wrapper around a numpy or torch tensor with geospatial information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
NDArray
|
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
|
attrs
|
Optional[Dict[str, Any]]
|
dictionary with the attributes of the GeoTensor Defaults to None. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
when the shape of the tensor is not 2d, 3d or 4d. |
Source code in georeader/geotensor.py
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 | |
__or__(other)
¶
Perform bitwise OR operation between two GeoTensors. The georeferencing must match.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Union[Number, NDArray, GeoTensor]
|
GeoTensor or array-like to OR with. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the georeferencing does not match when other is a GeoTensor. |
Returns:
| Name | Type | Description |
|---|---|---|
GeoTensor |
Self
|
GeoTensor with the result of the bitwise OR operation. |
Source code in georeader/geotensor.py
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 | |
__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 |
Self
|
GeoTensor with the result of the substraction. |
Source code in georeader/geotensor.py
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 | |
__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 |
Self
|
GeoTensor with the result of the division. |
Source code in georeader/geotensor.py
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 | |
array_as_geotensor(result, fill_value_default=None)
¶
Convert a NumPy array result back to a GeoTensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
Union[ndarray, Self]
|
Any NumPy array or GeoTensor. |
required |
fill_value_default
|
Optional[Number]
|
fill value for the returned GeoTensor. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
A new GeoTensor with the same geospatial attributes as the original. |
Source code in georeader/geotensor.py
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 | |
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 |
Self
|
GeoTensor with the clipped values. |
Source code in georeader/geotensor.py
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 | |
concatenate(geotensors, axis=0)
classmethod
¶
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[Self]
|
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 |
|---|---|
Self
|
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
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 | |
expand_dims(axis)
¶
Expand the dimensions of the GeoTensor values while preserving the spatial dimensions.
This method ensures that no dimensions are added after or in between the spatial dimensions (which are always the last two dimensions).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axis
|
Union[int, tuple]
|
Position or positions where new axes should be inserted. Must be less than the number of dimensions minus 2 (to preserve spatial dims). Positions are counted from the first dimension. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
GeoTensor |
Self
|
GeoTensor with the expanded values. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If trying to add dimensions at or after the spatial dimensions. |
Examples:
>>> gt = GeoTensor(np.random.rand(3, 100, 100), transform, crs)
>>> # Add a new dimension at axis 0
>>> gt_expanded = gt.expand_dims(0)
>>> assert gt_expanded.shape == (1, 3, 100, 100)
Source code in georeader/geotensor.py
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 | |
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
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 | |
invalidmask()
¶
Returns a mask of the invalid values of the GeoTensor. The mask is a boolean array
with the same shape as the GeoTensor values, where True indicates invalid values and
False indicates valid values.
The mask is created by comparing the values of the GeoTensor with the self.fill_value_default.
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
GeoTensor with the invalid boolean mask. |
Source code in georeader/geotensor.py
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 | |
isel(sel)
¶
Slicing with dict. Spatial dimensions can only be sliced with slices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sel
|
Dict[str, Union[slice, list, int]]
|
Dict with slice selection; i.e. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
GeoTensor |
Self
|
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
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 | |
load_bytes(bytes_read, load_tags=False, rio_env_options=None)
classmethod
¶
Load a GeoTensor from a byte stream. It uses rasterio to read the data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bytes_read
|
Union[bytes, bytearray, memoryview]
|
Byte stream to read. |
required |
load_tags
|
bool
|
if True, load the tags of the image. Defaults to False. |
False
|
rio_env_options
|
Optional[Dict[str, str]]
|
Rasterio environment options. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
__class__ |
Self
|
GeoTensor object with the loaded data. |
Note
The descriptions attribute cannote be loaded from a byte stream. This is a limitation of rasterio.
The issue is related to how rasterio.io.MemoryFile handles band descriptions
compared to direct file access. This is a known limitation when working
with in-memory file representations in GDAL (which rasterio uses under
the hood). If you need to load descriptions, you should use georeader.rasterio_reader
class.
Source code in georeader/geotensor.py
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 | |
load_file(path, fs=None, load_tags=False, load_descriptions=False, rio_env_options=None)
classmethod
¶
Load a GeoTensor from a file. It uses rasterio to read the data. This function
loads all the data in memory. For a lazy loading reader use georeader.rasterio_reader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the file. |
required |
fs
|
Optional[Any]
|
fsspec.Filesystem object. Defaults to None. |
None
|
load_descriptions
|
bool
|
If True, load the description of the image. Defaults to False. |
False
|
load_tags
|
bool
|
If True, load the tags of the image. Defaults to False. |
False
|
rio_env_options
|
Optional[Dict[str, str]]
|
Rasterio environment options. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
GeoTensor |
Self
|
GeoTensor object with the loaded data. |
Source code in georeader/geotensor.py
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 | |
meshgrid(dst_crs=None)
¶
Create a meshgrid of spatial dimensions of the GeoTensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dst_crs
|
Optional[Any]
|
output coordinate reference system. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[NDArray, NDArray]
|
Tuple[NDArray, NDArray]: 2D arrays of xs and ys coordinates. |
Source code in georeader/geotensor.py
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 | |
pad(pad_width, mode='constant', **kwargs)
¶
Pad the GeoTensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pad_width
|
Union[Dict[str, Tuple[int, int]], List[Tuple[int, int]]]
|
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'
|
kwargs
|
additional arguments for the pad function. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
GeoTensor |
padded GeoTensor. |
Source code in georeader/geotensor.py
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 | |
pad_array(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 |
Self
|
padded GeoTensor. |
Examples:
>>> gt = GeoTensor(np.random.rand(3, 100, 100), transform, crs)
>>> gt.pad_array({"x": (10, 10), "y": (10, 10)})
>>> assert gt.shape == (3, 120, 120)
Source code in georeader/geotensor.py
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 | |
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 |
|---|---|
Self
|
GeoTensor object with the spatial dimensions sliced |
Source code in georeader/geotensor.py
1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 | |
resize(output_shape=None, resolution_dst=None, 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
|
Optional[Tuple[int, int]]
|
output spatial shape if None resolution_dst must be provided. If not provided, the output shape is computed from the resolution_dst rounding to the closest integer. |
None
|
resolution_dst
|
Optional[Tuple[float, float]]
|
output resolution if None output_shape must be provided. |
None
|
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 |
|---|---|
Self
|
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
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 | |
same_extent(other, precision=0.001)
¶
Check if two GeoTensors have the same georeferencing (crs, transform and spatial dimensions).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
GeoTensor | 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
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | |
squeeze(axis=None)
¶
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 |
Self
|
GeoTensor with the squeezed values. |
Source code in georeader/geotensor.py
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 | |
stack(geotensors)
classmethod
¶
Stacks a list of geotensors, assert that all of them has same shape, transform and crs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
geotensors
|
List[Self]
|
list of geotensors to concat. All with same shape, transform and crs. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
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
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 | |
transpose(axes=None)
¶
Permute the dimensions of the GeoTensor while keeping the spatial dimensions at the end.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axes
|
tuple
|
If specified, it must be a tuple or list of axes. The last two values must be the original spatial dimensions indices (ndim-2, ndim-1). If None, the non-spatial dimensions are reversed while spatial dimensions remain at the end. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
GeoTensor |
Self
|
A view of the array with dimensions transposed. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the spatial dimensions are moved from their last positions. |
Examples:
>>> gt = GeoTensor(np.random.rand(3, 4, 100, 200), transform, crs)
>>> # Shape is (3, 4, 100, 200)
>>> gt_t = gt.transpose()
>>> # Shape is now (4, 3, 100, 200)
>>>
>>> # You can also specify axes explicitly:
>>> gt_t = gt.transpose((1, 0, 2, 3)) # Valid: spatial dims remain at end
>>> # But this would raise an error:
>>> # gt.transpose((0, 2, 1, 3)) # Invalid: spatial dims must stay at end
Source code in georeader/geotensor.py
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 | |
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
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 | |
validmask()
¶
Returns a mask of the valid values of the GeoTensor. The mask is a boolean array
with the same shape as the GeoTensor values, where True indicates valid values and
False indicates invalid values.
The mask is created by comparing the values of the GeoTensor with the self.fill_value_default.
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
GeoTensor with the valid boolean mask. |
Source code in georeader/geotensor.py
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 | |
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
|
ndarray
|
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
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 | |