1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 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 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463
|
# -*- coding: utf-8 -*-
"""
The abstract_space_time_dataset module provides the AbstractSpaceTimeDataset
class that is the base class for all space time datasets.
(C) 2011-2013 by the GRASS Development Team
This program is free software under the GNU General Public
License (>=v2). Read the file COPYING that comes with GRASS
for details.
:authors: Soeren Gebbert
"""
from __future__ import print_function
import sys
import uuid
from .abstract_dataset import *
from .temporal_granularity import *
from .spatio_temporal_relationships import *
###############################################################################
class AbstractSpaceTimeDataset(AbstractDataset):
"""Abstract space time dataset class
Base class for all space time datasets.
This class represents an abstract space time dataset. Convenient functions
to select, update, insert or delete objects of this type in the SQL
temporal database exists as well as functions to register or unregister
raster maps.
Parts of the temporal logic are implemented in the SQL temporal
database, like the computation of the temporal and spatial extent as
well as the collecting of metadata.
"""
__metaclass__ = ABCMeta
def __init__(self, ident):
AbstractDataset.__init__(self)
self.reset(ident)
self.map_counter = 0
def create_map_register_name(self):
"""Create the name of the map register table of this space time
dataset
A uuid and the map type are used to create the table name
ATTENTION: It must be assured that the base object has selected its
content from the database.
:return: The name of the map register table
"""
uuid_rand = str(uuid.uuid4()).replace("-", "")
table_name = self.get_new_map_instance(None).get_type() + "_map_register_" + uuid_rand
return table_name
@abstractmethod
def get_new_map_instance(self, ident=None):
"""Return a new instance of a map which is associated
with the type of this object
:param ident: The unique identifier of the new object
"""
@abstractmethod
def get_map_register(self):
"""Return the name of the map register table
:return: The map register table name
"""
@abstractmethod
def set_map_register(self, name):
"""Set the name of the map register table
This table stores all map names which are registered
in this space time dataset.
This method only modifies this object and does not commit
the modifications to the temporal database.
:param name: The name of the register table
"""
def print_self(self):
"""Print the content of the internal structure to stdout"""
self.base.print_self()
self.temporal_extent.print_self()
self.spatial_extent.print_self()
self.metadata.print_self()
def print_info(self):
"""Print information about this class in human readable style"""
if self.get_type() == "strds":
# 1 2 3 4 5 6 7
# 0123456789012345678901234567890123456789012345678901234567890123456789012345678
print(" +-------------------- Space Time Raster Dataset -----------------------------+")
if self.get_type() == "str3ds":
# 1 2 3 4 5 6 7
# 0123456789012345678901234567890123456789012345678901234567890123456789012345678
print(" +-------------------- Space Time 3D Raster Dataset --------------------------+")
if self.get_type() == "stvds":
# 1 2 3 4 5 6 7
# 0123456789012345678901234567890123456789012345678901234567890123456789012345678
print(" +-------------------- Space Time Vector Dataset -----------------------------+")
print(" | |")
self.base.print_info()
self.temporal_extent.print_info()
self.spatial_extent.print_info()
self.metadata.print_info()
print(" +----------------------------------------------------------------------------+")
def print_shell_info(self):
"""Print information about this class in shell style"""
self.base.print_shell_info()
self.temporal_extent.print_shell_info()
self.spatial_extent.print_shell_info()
self.metadata.print_shell_info()
def print_history(self):
"""Print history information about this class in human readable
shell style
"""
self.metadata.print_history()
def set_initial_values(self, temporal_type, semantic_type=None,
title=None, description=None):
"""Set the initial values of the space time dataset
In addition the command creation string is generated
an inserted into the metadata object.
This method only modifies this object and does not commit
the modifications to the temporal database.
The insert() function must be called to commit
this content into the temporal database.
:param temporal_type: The temporal type of this space
time dataset (absolute or relative)
:param semantic_type: The semantic type of this dataset
:param title: The title
:param description: The description of this dataset
"""
if temporal_type == "absolute":
self.base.set_ttype("absolute")
elif temporal_type == "relative":
self.base.set_ttype("relative")
else:
self.msgr.fatal(_("Unknown temporal type \"%s\"") % (temporal_type))
self.base.set_semantic_type(semantic_type)
self.metadata.set_title(title)
self.metadata.set_description(description)
self.metadata.set_command(self.create_command_string())
def set_aggregation_type(self, aggregation_type):
"""Set the aggregation type of the space time dataset
:param aggregation_type: The aggregation type of the space time
dataset
"""
self.metadata.set_aggregation_type(aggregation_type)
def update_command_string(self, dbif=None):
"""Append the current command string to any existing command string
in the metadata class and calls metadata update
:param dbif: The database interface to be used
"""
self.metadata.select(dbif=dbif)
command = self.metadata.get_command()
if command is None:
command = ""
command += self.create_command_string()
self.metadata.set_command(command)
self.metadata.update(dbif=dbif)
def create_command_string(self):
"""Create the command string that was used to create this
space time dataset.
The command string should be set with self.metadata.set_command()
:return: The command string
"""
# The grass module
command = "# %s \n"%(str(datetime.today().strftime("%Y-%m-%d %H:%M:%S")))
command += os.path.basename(sys.argv[0])
# We will wrap the command line to fit into 80 character
length = len(command)
for token in sys.argv[1:]:
# We need to remove specific characters
token = token.replace("\'", " ")
token = token.replace("\"", " ")
# Check for sub strings
if token.find("=") > 0:
first = token.split("=")[0]
second = ""
flag = 0
for t in token.split("=")[1:]:
if flag == 0:
second += t
flag = 1
else:
second += "=" + t
token = "%s=\"%s\"" % (first, second)
if length + len(token) >= 76:
command += "\n %s" % (token)
length = len(token) + 4
else:
command += " %s" % (token)
length += len(token) + 1
command += "\n"
return str(command)
def get_semantic_type(self):
"""Return the semantic type of this dataset
:return: The semantic type
"""
return self.base.get_semantic_type()
def get_initial_values(self):
"""Return the initial values: temporal_type,
semantic_type, title, description"""
temporal_type = self.get_temporal_type()
semantic_type = self.base.get_semantic_type()
title = self.metadata.get_title()
description = self.metadata.get_description()
return temporal_type, semantic_type, title, description
def get_granularity(self):
"""Return the granularity of the space time dataset
Granularity can be of absolute time or relative time.
In case of absolute time a string containing an integer
value and the time unit (years, months, days, hours, minuts,
seconds). In case of relative time an integer value is expected.
:return: The granularity
"""
return self.temporal_extent.get_granularity()
def set_granularity(self, granularity):
"""Set the granularity
The granularity is usually computed by the space time dataset at
runtime.
Granularity can be of absolute time or relative time.
In case of absolute time a string containing an integer
value and the time unit (years, months, days, hours, minuts,
seconds). In case of relative time an integer value is expected.
This method only modifies this object and does not commit
the modifications to the temporal database.
:param granularity: The granularity of the dataset
"""
temporal_type = self.get_temporal_type()
check = check_granularity_string(granularity, temporal_type)
if not check:
self.msgr.fatal(_("Wrong granularity: \"%s\"") % str(granularity))
if temporal_type == "absolute":
self.base.set_ttype("absolute")
elif temporal_type == "relative":
self.base.set_ttype("relative")
else:
self.msgr.fatal(_("Unknown temporal type \"%s\"") % (temporal_type))
self.temporal_extent.set_granularity(granularity)
def set_relative_time_unit(self, unit):
"""Set the relative time unit which may be of type:
years, months, days, hours, minutes or seconds
All maps registered in a (relative time)
space time dataset must have the same unit
This method only modifies this object and does not commit
the modifications to the temporal database.
:param unit: The relative time unit
"""
temporal_type = self.get_temporal_type()
if temporal_type == "relative":
if not self.check_relative_time_unit(unit):
self.msgr.fatal(_("Unsupported temporal unit: %s") % (unit))
self.relative_time.set_unit(unit)
def insert(self, dbif=None, execute=True):
"""Insert the space time dataset content into the database from the internal
structure
The map register table will be created, so that maps
can be registered.
:param dbif: The database interface to be used
:param execute: If True the SQL statements will be executed.
If False the prepared SQL statements are
returned and must be executed by the caller.
:return: The SQL insert statement in case execute=False, or an
empty string otherwise
"""
dbif, connected = init_dbif(dbif)
# We need to create the register table if it does not exist
stds_register_table = self.get_map_register()
# Create the map register table
sql_path = get_sql_template_path()
statement = ""
# We need to create the map register table
if stds_register_table is None:
# Create table name
stds_register_table = self.create_map_register_name()
# Assure that the table and index do not exist
#dbif.execute_transaction("DROP INDEX IF EXISTS %s; DROP TABLE IF EXISTS %s;"%(stds_register_table + "_index", stds_register_table))
# Read the SQL template
sql = open(os.path.join(sql_path,
"stds_map_register_table_template.sql"),
'r').read()
# Create a raster, raster3d or vector tables
sql = sql.replace("SPACETIME_REGISTER_TABLE", stds_register_table)
statement += sql
if dbif.get_dbmi().__name__ == "sqlite3":
statement += "CREATE INDEX %s_index ON %s (id);" % \
(stds_register_table, stds_register_table)
# Set the map register table name
self.set_map_register(stds_register_table)
self.msgr.debug(1, _("Created register table <%s> for space "
"time %s dataset <%s>") %
(stds_register_table,
self.get_new_map_instance(None).get_type(),
self.get_id()))
statement += AbstractDataset.insert(self, dbif=dbif, execute=False)
if execute:
dbif.execute_transaction(statement)
statement = ""
if connected:
dbif.close()
return statement
def get_map_time(self):
"""Return the type of the map time, interval, point, mixed or invalid
"""
return self.temporal_extent.get_map_time()
def count_temporal_types(self, maps=None, dbif=None):
"""Return the temporal type of the registered maps as dictionary
The map list must be ordered by start time
The temporal type can be:
- point -> only the start time is present
- interval -> start and end time
- invalid -> No valid time point or interval found
:param maps: A sorted (start_time) list of AbstractDataset objects
:param dbif: The database interface to be used
"""
if maps is None:
maps = get_registered_maps_as_objects(
where=None, order="start_time", dbif=dbif)
time_invalid = 0
time_point = 0
time_interval = 0
tcount = {}
for i in range(len(maps)):
# Check for point and interval data
if maps[i].is_time_absolute():
start, end = maps[i].get_absolute_time()
if maps[i].is_time_relative():
start, end, unit = maps[i].get_relative_time()
if start is not None and end is not None:
time_interval += 1
elif start is not None and end is None:
time_point += 1
else:
time_invalid += 1
tcount["point"] = time_point
tcount["interval"] = time_interval
tcount["invalid"] = time_invalid
return tcount
def count_gaps(self, maps=None, dbif=None):
"""Count the number of gaps between temporal neighbors
:param maps: A sorted (start_time) list of AbstractDataset objects
:param dbif: The database interface to be used
:return: The numbers of gaps between temporal neighbors
"""
if maps is None:
maps = self.get_registered_maps_as_objects(
where=None, order="start_time", dbif=dbif)
gaps = 0
# Check for gaps
for i in range(len(maps)):
if i < len(maps) - 1:
relation = maps[i + 1].temporal_relation(maps[i])
if relation == "after":
gaps += 1
return gaps
def print_spatio_temporal_relationships(self, maps=None, spatial=None,
dbif=None):
"""Print the spatio-temporal relationships for each map of the space
time dataset or for each map of the optional list of maps
:param maps: a ordered by start_time list of map objects, if None
the registered maps of the space time dataset are used
:param spatial: This indicates if the spatial topology is created as
well: spatial can be None (no spatial topology),
"2D" using west, east, south, north or "3D" using
west, east, south, north, bottom, top
:param dbif: The database interface to be used
"""
if maps is None:
maps = self.get_registered_maps_as_objects(
where=None, order="start_time", dbif=dbif)
print_spatio_temporal_topology_relationships(maps1=maps, maps2=maps,
spatial=spatial,
dbif=dbif)
def count_temporal_relations(self, maps=None, dbif=None):
"""Count the temporal relations between the registered maps.
The map list must be ordered by start time.
Temporal relations are counted by analysing the sparse upper right
side temporal relationships matrix.
:param maps: A sorted (start_time) list of AbstractDataset objects
:param dbif: The database interface to be used
:return: A dictionary with counted temporal relationships
"""
if maps is None:
maps = self.get_registered_maps_as_objects(
where=None, order="start_time", dbif=dbif)
return count_temporal_topology_relationships(maps1=maps, dbif=dbif)
def check_temporal_topology(self, maps=None, dbif=None):
"""Check the temporal topology of all maps of the current space time
dataset or of an optional list of maps
Correct topology means, that time intervals are not overlap or
that intervals does not contain other intervals.
Equal time intervals are not allowed.
The optional map list must be ordered by start time
Allowed and not allowed temporal relationships for correct topology:
- after -> allowed
- precedes -> allowed
- follows -> allowed
- precedes -> allowed
- equal -> not allowed
- during -> not allowed
- contains -> not allowed
- overlaps -> not allowed
- overlapped -> not allowed
- starts -> not allowed
- finishes -> not allowed
- started -> not allowed
- finished -> not allowed
:param maps: An optional list of AbstractDataset objects, in case of
None all maps of the space time dataset are checked
:param dbif: The database interface to be used
:return: True if topology is correct
"""
if maps is None:
maps = self.get_registered_maps_as_objects(
where=None, order="start_time", dbif=dbif)
relations = count_temporal_topology_relationships(maps1=maps,
dbif=dbif)
if relations is None:
return False
map_time = self.get_map_time()
if map_time == "interval" or map_time == "mixed":
if "equal" in relations and relations["equal"] > 0:
return False
if "during" in relations and relations["during"] > 0:
return False
if "contains" in relations and relations["contains"] > 0:
return False
if "overlaps" in relations and relations["overlaps"] > 0:
return False
if "overlapped" in relations and relations["overlapped"] > 0:
return False
if "starts" in relations and relations["starts"] > 0:
return False
if "finishes" in relations and relations["finishes"] > 0:
return False
if "started" in relations and relations["started"] > 0:
return False
if "finished" in relations and relations["finished"] > 0:
return False
elif map_time == "point":
if "equal" in relations and relations["equal"] > 0:
return False
else:
return False
return True
def sample_by_dataset(self, stds, method=None, spatial=False, dbif=None):
"""Sample this space time dataset with the temporal topology
of a second space time dataset
In case spatial is True, the spatial overlap between
temporal related maps is performed. Only
temporal related and spatial overlapping maps are returned.
Return all registered maps as ordered (by start_time) object list.
Each list entry is a list of map
objects which are potentially located in temporal relation to the
actual granule of the second space time dataset.
Each entry in the object list is a dict. The actual sampler
map and its temporal extent (the actual granule) and
the list of samples are stored:
.. code-block:: python
list = self.sample_by_dataset(stds=sampler, method=[
"during","overlap","contains","equal"])
for entry in list:
granule = entry["granule"]
maplist = entry["samples"]
for map in maplist:
map.select()
map.print_info()
A valid temporal topology (no overlapping or inclusion allowed)
is needed to get correct results in case of gaps in the sample
dataset.
Gaps between maps are identified as unregistered maps with id==None.
The objects are initialized with their id's' and the spatio-temporal
extent (temporal type, start time, end time, west, east, south,
north, bottom and top).
In case more map information are needed, use the select()
method for each listed object.
:param stds: The space time dataset to be used for temporal sampling
:param method: This option specifies what sample method should be
used. In case the registered maps are of temporal
point type, only the start time is used for sampling.
In case of mixed of interval data the user can chose
between:
- Example ["start", "during", "equals"]
- start: Select maps of which the start time is
located in the selection granule::
map : s
granule: s-----------------e
map : s--------------------e
granule: s-----------------e
map : s--------e
granule: s-----------------e
- contains: Select maps which are temporal
during the selection granule::
map : s-----------e
granule: s-----------------e
- overlap: Select maps which temporal overlap
the selection granule, this includes overlaps and
overlapped::
map : s-----------e
granule: s-----------------e
map : s-----------e
granule: s----------e
- during: Select maps which temporally contains
the selection granule::
map : s-----------------e
granule: s-----------e
- equals: Select maps which temporally equal
to the selection granule::
map : s-----------e
granule: s-----------e
- follows: Select maps which temporally follow
the selection granule::
map : s-----------e
granule: s-----------e
- precedes: Select maps which temporally precedes
the selection granule::
map : s-----------e
granule: s-----------e
All these methods can be combined. Method must be of
type tuple including the identification strings.
:param spatial: If set True additional the 2d spatial overlapping
is used for selection -> spatio-temporal relation.
The returned map objects will have temporal and
spatial extents
:param dbif: The database interface to be used
:return: A list of lists of map objects or None in case nothing was
found None
"""
if self.get_temporal_type() != stds.get_temporal_type():
self.msgr.error(_("The space time datasets must be of "
"the same temporal type"))
return None
if stds.get_map_time() != "interval":
self.msgr.error(_("The temporal map type of the sample "
"dataset must be interval"))
return None
dbif, connected = init_dbif(dbif)
relations = copy.deepcopy(method)
# Tune the temporal relations
if "start" in relations:
if "overlapped" not in relations:
relations.append("overlapped")
if "starts" not in relations:
relations.append("starts")
if "started" not in relations:
relations.append("started")
if "finishes" not in relations:
relations.append("finishes")
if "contains" not in relations:
relations.append("contains")
if "equals" not in relations:
relations.append("equals")
if "overlap" in relations or "over" in relations:
if "overlapped" not in relations:
relations.append("overlapped")
if "overlaps" not in relations:
relations.append("overlaps")
if "contain" in relations:
if "contains" not in relations:
relations.append("contains")
# Remove start, equal, contain and overlap
relations = [relation.upper().strip() for relation in relations
if relation not in ["start", "overlap", "contain"]]
# print(relations)
tb = SpatioTemporalTopologyBuilder()
if spatial:
spatial = "2D"
else:
spatial = None
mapsA = self.get_registered_maps_as_objects(dbif=dbif)
mapsB = stds.get_registered_maps_as_objects_with_gaps(dbif=dbif)
tb.build(mapsB, mapsA, spatial)
obj_list = []
for map in mapsB:
result = {}
maplist = []
# Get map relations
map_relations = map.get_temporal_relations()
#print(map.get_temporal_extent_as_tuple())
#for key in map_relations.keys():
# if key not in ["NEXT", "PREV"]:
# print(key, map_relations[key][0].get_temporal_extent_as_tuple())
result["granule"] = map
# Append the maps that fulfill the relations
for relation in relations:
if relation in map_relations.keys():
for sample_map in map_relations[relation]:
if sample_map not in maplist:
maplist.append(sample_map)
# Add an empty map if no map was found
if not maplist:
empty_map = self.get_new_map_instance(None)
empty_map.set_spatial_extent(map.get_spatial_extent())
empty_map.set_temporal_extent(map.get_temporal_extent())
maplist.append(empty_map)
result["samples"] = maplist
obj_list.append(result)
if connected:
dbif.close()
return obj_list
def sample_by_dataset_sql(self, stds, method=None, spatial=False,
dbif=None):
"""Sample this space time dataset with the temporal topology
of a second space time dataset using SQL queries.
This function is very slow for huge large space time datasets
but can run several times in the same process without problems.
The sample dataset must have "interval" as temporal map type,
so all sample maps have valid interval time.
In case spatial is True, the spatial overlap between
temporal related maps is performed. Only
temporal related and spatial overlapping maps are returned.
Return all registered maps as ordered (by start_time) object list
with "gap" map objects (id==None). Each list entry is a list of map
objects which are potentially located in temporal relation to the
actual granule of the second space time dataset.
Each entry in the object list is a dict. The actual sampler
map and its temporal extent (the actual granule) and
the list of samples are stored:
.. code-block:: python
list = self.sample_by_dataset(stds=sampler, method=[
"during","overlap","contain","equal"])
for entry in list:
granule = entry["granule"]
maplist = entry["samples"]
for map in maplist:
map.select()
map.print_info()
A valid temporal topology (no overlapping or inclusion allowed)
is needed to get correct results in case of gaps in the sample
dataset.
Gaps between maps are identified as unregistered maps with id==None.
The objects are initialized with their id's' and the spatio-temporal
extent (temporal type, start time, end time, west, east, south,
north, bottom and top).
In case more map information are needed, use the select()
method for each listed object.
:param stds: The space time dataset to be used for temporal sampling
:param method: This option specifies what sample method should be
used. In case the registered maps are of temporal
point type, only the start time is used for sampling.
In case of mixed of interval data the user can chose
between:
- Example ["start", "during", "equals"]
- start: Select maps of which the start time is
located in the selection granule::
map : s
granule: s-----------------e
map : s--------------------e
granule: s-----------------e
map : s--------e
granule: s-----------------e
- contains: Select maps which are temporal
during the selection granule::
map : s-----------e
granule: s-----------------e
- overlap: Select maps which temporal overlap
the selection granule, this includes overlaps and
overlapped::
map : s-----------e
granule: s-----------------e
map : s-----------e
granule: s----------e
- during: Select maps which temporally contains
the selection granule::
map : s-----------------e
granule: s-----------e
- equals: Select maps which temporally equal
to the selection granule::
map : s-----------e
granule: s-----------e
- follows: Select maps which temporally follow
the selection granule::
map : s-----------e
granule: s-----------e
- precedes: Select maps which temporally precedes
the selection granule::
map : s-----------e
granule: s-----------e
All these methods can be combined. Method must be of
type tuple including the identification strings.
:param spatial: If set True additional the 2d spatial overlapping
is used for selection -> spatio-temporal relation.
The returned map objects will have temporal and
spatial extents
:param dbif: The database interface to be used
:return: A list of lists of map objects or None in case nothing was
found None
"""
use_start = False
use_during = False
use_overlap = False
use_contain = False
use_equal = False
use_follows = False
use_precedes = False
# Initialize the methods
if method is not None:
for name in method:
if name == "start":
use_start = True
if name == "during":
use_during = True
if name == "overlap":
use_overlap = True
if name == "contain" or name == "contains":
use_contain = True
if name == "equal" or name == "equals":
use_equal = True
if name == "follows":
use_follows = True
if name == "precedes":
use_precedes = True
else:
use_during = True
use_overlap = True
use_contain = True
use_equal = True
if self.get_temporal_type() != stds.get_temporal_type():
self.msgr.error(_("The space time datasets must be of "
"the same temporal type"))
return None
if stds.get_map_time() != "interval":
self.msgr.error(_("The temporal map type of the sample "
"dataset must be interval"))
return None
# In case points of time are available, disable the interval specific
# methods
if self.get_map_time() == "point":
use_start = True
use_during = False
use_overlap = False
use_contain = False
use_equal = False
use_follows = False
use_precedes = False
dbif, connected = init_dbif(dbif)
obj_list = []
sample_maps = stds.get_registered_maps_as_objects_with_gaps(
where=None, dbif=dbif)
for granule in sample_maps:
# Read the spatial extent
if spatial:
granule.spatial_extent.select(dbif)
start, end = granule.get_temporal_extent_as_tuple()
where = create_temporal_relation_sql_where_statement(
start, end, use_start, use_during, use_overlap,
use_contain, use_equal, use_follows, use_precedes)
maps = self.get_registered_maps_as_objects(
where, "start_time", dbif)
result = {}
result["granule"] = granule
num_samples = 0
maplist = []
if maps is not None:
for map in maps:
# Read the spatial extent
if spatial:
map.spatial_extent.select(dbif)
# Ignore spatial disjoint maps
if not granule.spatial_overlapping(map):
continue
num_samples += 1
maplist.append(copy.copy(map))
# Fill with empty map in case no spatio-temporal relations found
if maps is None or num_samples == 0:
map = self.get_new_map_instance(None)
if self.is_time_absolute():
map.set_absolute_time(start, end)
elif self.is_time_relative():
map.set_relative_time(start, end,
self.get_relative_time_unit())
maplist.append(copy.copy(map))
result["samples"] = maplist
obj_list.append(copy.copy(result))
if connected:
dbif.close()
return obj_list
def get_registered_maps_as_objects_by_granularity(self, gran=None,
dbif=None):
"""Return all registered maps as ordered (by start_time) object list
with "gap" map objects (id==None) for spatio-temporal topological
operations that require the temporal extent only.
Each list entry is a list of AbstractMapDatasets objects
which are potentially equal the actual granule, contain the
actual granule or are located in the actual granule.
Hence for each granule a list of AbstractMapDatasets can be
expected.
Maps that overlap the granule are ignored.
The granularity of the space time dataset is used as increment in
case the granule is not user defined.
A valid temporal topology (no overlapping or inclusion allowed)
is needed to get correct results.
Space time datasets with interval time, time instances and mixed
time are supported.
Gaps between maps are identified as unregistered maps with id==None.
The objects are initialized with their id's' and the spatio-temporal
extent (temporal type, start time, end time, west, east, south,
north, bottom and top).
In case more map information are needed, use the select()
method for each listed object.
:param gran: The granularity string to be used, if None the
granularity of the space time dataset is used.
Absolute time has the format "number unit", relative
time has the format "number".
The unit in case of absolute time can be one of "second,
seconds, minute, minutes, hour, hours, day, days, week,
weeks, month, months, year, years". The unit of the
relative time granule is always the space time dataset
unit and can not be changed.
:param dbif: The database interface to be used
:return: ordered list of map lists. Each list represents a single
granule, or None in case nothing found
"""
dbif, connected = init_dbif(dbif)
if gran is None:
gran = self.get_granularity()
check = check_granularity_string(gran, self.get_temporal_type())
if not check:
self.msgr.fatal(_("Wrong granularity: \"%s\"") % str(gran))
start, end = self.get_temporal_extent_as_tuple()
if start is None or end is None:
return None
maps = self.get_registered_maps_as_objects(dbif=dbif,
order="start_time")
if not maps:
return None
# We need to adjust the end time in case the the dataset has no
# interval time, so we can catch time instances at the end
if self.get_map_time() != "interval":
if self.is_time_absolute():
end = increment_datetime_by_string(end, gran)
else:
end = end + gran
l = AbstractSpaceTimeDataset.resample_maplist_by_granularity(maps,
start,
end,
gran)
if connected:
dbif.close()
return l
@staticmethod
def resample_maplist_by_granularity(maps, start, end, gran):
"""Resample a list of AbstractMapDatasets by a given granularity
The provided map list must be sorted by start time.
A valid temporal topology (no overlapping or inclusion allowed)
is needed to receive correct results.
Maps with interval time, time instances and mixed
time are supported.
The temporal topology search order is as follows:
1. Maps that are equal to the actual granule are used
2. If no euqal found then maps that contain the actual granule
are used
3. If no maps are found that contain the actual granule then maps
are used that overlaps the actual granule
4. If no overlaps maps found then overlapped maps are used
5. If no overlapped maps are found then maps are used that are
durin the actual granule
Each entry in the resulting list is a list of
AbstractMapDatasets objects.
Hence for each granule a list of AbstractMapDatasets can be
expected.
Gaps between maps are identified as unregistered maps with id==None.
:param maps: An ordered list (by start time) of AbstractMapDatasets
objects. All maps must have the same temporal type
and the same unit in case of relative time.
:param start: The start time of the provided map list
:param end: The end time of the provided map list
:param gran: The granularity string to be used, if None the
granularity of the space time dataset is used.
Absolute time has the format "number unit", relative
time has the format "number".
The unit in case of absolute time can be one of "second,
seconds, minute, minutes, hour, hours, day, days, week,
weeks, month, months, year, years". The unit of the
relative time granule is always the space time dataset
unit and can not be changed.
:return: ordered list of map lists. Each list represents a single
granule, or None in case nothing found
Usage:
.. code-block:: python
>>> import grass.temporal as tgis
>>> maps = []
>>> for i in xrange(3):
... map = tgis.RasterDataset("map%i@PERMANENT"%i)
... check = map.set_relative_time(i + 2, i + 3, "days")
... maps.append(map)
>>> grans = tgis.AbstractSpaceTimeDataset.resample_maplist_by_granularity(maps,0,8,1)
>>> for map_list in grans:
... print(map_list[0].get_id(), map_list[0].get_temporal_extent_as_tuple())
None (0, 1)
None (1, 2)
map0@PERMANENT (2, 3)
map1@PERMANENT (3, 4)
map2@PERMANENT (4, 5)
None (5, 6)
None (6, 7)
None (7, 8)
>>> maps = []
>>> map1 = tgis.RasterDataset("map1@PERMANENT")
>>> check = map1.set_relative_time(2, 6, "days")
>>> maps.append(map1)
>>> map2 = tgis.RasterDataset("map2@PERMANENT")
>>> check = map2.set_relative_time(7, 13, "days")
>>> maps.append(map2)
>>> grans = tgis.AbstractSpaceTimeDataset.resample_maplist_by_granularity(maps,0,16,2)
>>> for map_list in grans:
... print(map_list[0].get_id(), map_list[0].get_temporal_extent_as_tuple())
None (0, 2)
map1@PERMANENT (2, 4)
map1@PERMANENT (4, 6)
map2@PERMANENT (6, 8)
map2@PERMANENT (8, 10)
map2@PERMANENT (10, 12)
map2@PERMANENT (12, 14)
None (14, 16)
>>> maps = []
>>> map1 = tgis.RasterDataset("map1@PERMANENT")
>>> check = map1.set_relative_time(2, None, "days")
>>> maps.append(map1)
>>> map2 = tgis.RasterDataset("map2@PERMANENT")
>>> check = map2.set_relative_time(7, None, "days")
>>> maps.append(map2)
>>> grans = tgis.AbstractSpaceTimeDataset.resample_maplist_by_granularity(maps,0,16,2)
>>> for map_list in grans:
... print(map_list[0].get_id(), map_list[0].get_temporal_extent_as_tuple())
None (0, 2)
map1@PERMANENT (2, 4)
None (4, 6)
map2@PERMANENT (6, 8)
None (8, 10)
None (10, 12)
None (12, 14)
None (14, 16)
>>> maps = []
>>> map1 = tgis.RasterDataset("map1@PERMANENT")
>>> check = map1.set_absolute_time(datetime(2000, 4,1), datetime(2000, 6, 1))
>>> maps.append(map1)
>>> map2 = tgis.RasterDataset("map2@PERMANENT")
>>> check = map2.set_absolute_time(datetime(2000, 8,1), datetime(2000, 12, 1))
>>> maps.append(map2)
>>> grans = tgis.AbstractSpaceTimeDataset.resample_maplist_by_granularity(maps,datetime(2000,1,1),datetime(2001,4,1),"1 month")
>>> for map_list in grans:
... print(map_list[0].get_id(), map_list[0].get_temporal_extent_as_tuple())
None (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2000, 2, 1, 0, 0))
None (datetime.datetime(2000, 2, 1, 0, 0), datetime.datetime(2000, 3, 1, 0, 0))
None (datetime.datetime(2000, 3, 1, 0, 0), datetime.datetime(2000, 4, 1, 0, 0))
map1@PERMANENT (datetime.datetime(2000, 4, 1, 0, 0), datetime.datetime(2000, 5, 1, 0, 0))
map1@PERMANENT (datetime.datetime(2000, 5, 1, 0, 0), datetime.datetime(2000, 6, 1, 0, 0))
None (datetime.datetime(2000, 6, 1, 0, 0), datetime.datetime(2000, 7, 1, 0, 0))
None (datetime.datetime(2000, 7, 1, 0, 0), datetime.datetime(2000, 8, 1, 0, 0))
map2@PERMANENT (datetime.datetime(2000, 8, 1, 0, 0), datetime.datetime(2000, 9, 1, 0, 0))
map2@PERMANENT (datetime.datetime(2000, 9, 1, 0, 0), datetime.datetime(2000, 10, 1, 0, 0))
map2@PERMANENT (datetime.datetime(2000, 10, 1, 0, 0), datetime.datetime(2000, 11, 1, 0, 0))
map2@PERMANENT (datetime.datetime(2000, 11, 1, 0, 0), datetime.datetime(2000, 12, 1, 0, 0))
None (datetime.datetime(2000, 12, 1, 0, 0), datetime.datetime(2001, 1, 1, 0, 0))
None (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2001, 2, 1, 0, 0))
None (datetime.datetime(2001, 2, 1, 0, 0), datetime.datetime(2001, 3, 1, 0, 0))
None (datetime.datetime(2001, 3, 1, 0, 0), datetime.datetime(2001, 4, 1, 0, 0))
"""
if not maps:
return None
first = maps[0]
# Build the gaplist
gap_list = []
while start < end:
if first.is_time_absolute():
next = increment_datetime_by_string(start, gran)
else:
next = start + gran
map = first.get_new_instance(None)
map.set_spatial_extent_from_values(0, 0, 0, 0, 0, 0)
if first.is_time_absolute():
map.set_absolute_time(start, next)
else:
map.set_relative_time(start, next,
first.get_relative_time_unit())
gap_list.append(copy.copy(map))
start = next
tb = SpatioTemporalTopologyBuilder()
tb.build(gap_list, maps)
relations_order = ["EQUAL", "DURING", "OVERLAPS", "OVERLAPPED",
"CONTAINS"]
gran_list = []
for gap in gap_list:
# If not temporal relations then gap
if not gap.get_temporal_relations():
gran_list.append([gap, ])
else:
relations = gap.get_temporal_relations()
map_list = []
for relation in relations_order:
if relation in relations:
map_list += relations[relation]
break
if map_list:
new_maps = []
for map in map_list:
new_map = map.get_new_instance(map.get_id())
new_map.set_temporal_extent(gap.get_temporal_extent())
new_map.set_spatial_extent(map.get_spatial_extent())
new_maps.append(new_map)
gran_list.append(new_maps)
else:
gran_list.append([gap, ])
if gran_list:
return gran_list
return None
def get_registered_maps_as_objects_with_gaps(self, where=None, dbif=None):
"""Return all or a subset of the registered maps as
ordered (by start_time) object list with
"gap" map objects (id==None) for spatio-temporal topological
operations that require the spatio-temporal extent only.
Gaps between maps are identified as maps with id==None
The objects are initialized with their id's' and the spatio-temporal
extent (temporal type, start time, end time, west, east, south,
north, bottom and top).
In case more map information are needed, use the select()
method for each listed object.
:param where: The SQL where statement to select a
subset of the registered maps without "WHERE"
:param dbif: The database interface to be used
:return: ordered object list, in case nothing found None is returned
"""
dbif, connected = init_dbif(dbif)
obj_list = []
maps = self.get_registered_maps_as_objects(where, "start_time", dbif)
if maps is not None and len(maps) > 0:
for i in range(len(maps)):
obj_list.append(maps[i])
# Detect and insert gaps
if i < len(maps) - 1:
relation = maps[i + 1].temporal_relation(maps[i])
if relation == "after":
start1, end1 = maps[i].get_temporal_extent_as_tuple()
start2, end2 = maps[i + 1].get_temporal_extent_as_tuple()
end = start2
if end1 is not None:
start = end1
else:
start = start1
map = self.get_new_map_instance(None)
if self.is_time_absolute():
map.set_absolute_time(start, end)
elif self.is_time_relative():
map.set_relative_time(start, end,
self.get_relative_time_unit())
map.set_spatial_extent_from_values(0, 0, 0, 0, 0, 0)
obj_list.append(copy.copy(map))
if connected:
dbif.close()
return obj_list
def get_registered_maps_as_objects_with_temporal_topology(self, where=None,
order="start_time",
dbif=None):
"""Return all or a subset of the registered maps as ordered object
list with spatio-temporal topological relationship information.
The objects are initialized with their id's' and the spatio-temporal
extent (temporal type, start time, end time, west, east, south,
north, bottom and top).
In case more map information are needed, use the select()
method for each listed object.
:param where: The SQL where statement to select a subset of
the registered maps without "WHERE"
:param order: The SQL order statement to be used to order the
objects in the list without "ORDER BY"
:param dbif: The database interface to be used
:return: The ordered map object list,
In case nothing found None is returned
"""
dbif, connected = init_dbif(dbif)
obj_list = self.get_registered_maps_as_objects(where, order, dbif)
tb = SpatioTemporalTopologyBuilder()
tb.build(obj_list)
if connected:
dbif.close()
return obj_list
def get_registered_maps_as_objects(self, where=None, order="start_time",
dbif=None):
"""Return all or a subset of the registered maps as ordered object
list for spatio-temporal topological operations that require the
spatio-temporal extent only
The objects are initialized with their id's' and the spatio-temporal
extent (temporal type, start time, end time, west, east, south,
north, bottom and top).
In case more map information are needed, use the select()
method for each listed object.
:param where: The SQL where statement to select a subset of
the registered maps without "WHERE"
:param order: The SQL order statement to be used to order the
objects in the list without "ORDER BY"
:param dbif: The database interface to be used
:return: The ordered map object list,
In case nothing found None is returned
"""
dbif, connected = init_dbif(dbif)
obj_list = []
# Older temporal databases have no bottom and top columns
# in their views so we need a work around to set the full
# spatial extent as well
rows = get_tgis_metadata(dbif)
db_version = 0
if rows:
for row in rows:
if row["key"] == "tgis_db_version":
db_version = int(float(row["value"]))
if db_version >= 1:
has_bt_columns = True
columns = "id,start_time,end_time, west,east,south,north,bottom,top"
else:
has_bt_columns = False
columns = "id,start_time,end_time, west,east,south,north"
rows = self.get_registered_maps(columns, where, order, dbif)
if rows is not None:
for row in rows:
map = self.get_new_map_instance(row["id"])
if self.is_time_absolute():
map.set_absolute_time(row["start_time"], row["end_time"])
elif self.is_time_relative():
map.set_relative_time(row["start_time"], row["end_time"],
self.get_relative_time_unit())
# The fast way
if has_bt_columns:
map.set_spatial_extent_from_values(west=row["west"],
east=row["east"],
south=row["south"],
top=row["top"],
north=row["north"],
bottom=row["bottom"])
# The slow work around
else:
map.spatial_extent.select(dbif)
obj_list.append(copy.copy(map))
if connected:
dbif.close()
return obj_list
def get_registered_maps(self, columns=None, where=None, order=None,
dbif=None):
"""Return SQL rows of all registered maps.
In case columns are not specified, each row includes all columns
specified in the datatype specific view.
:param columns: Columns to be selected as SQL compliant string
:param where: The SQL where statement to select a subset
of the registered maps without "WHERE"
:param order: The SQL order statement to be used to order the
objects in the list without "ORDER BY"
:param dbif: The database interface to be used
:return: SQL rows of all registered maps,
In case nothing found None is returned
"""
dbif, connected = init_dbif(dbif)
rows = None
if self.get_map_register() is not None:
# Use the correct temporal table
if self.get_temporal_type() == "absolute":
map_view = self.get_new_map_instance(
None).get_type() + "_view_abs_time"
else:
map_view = self.get_new_map_instance(
None).get_type() + "_view_rel_time"
if columns is not None and columns != "":
sql = "SELECT %s FROM %s WHERE %s.id IN (SELECT id FROM %s)" %\
(columns, map_view, map_view, self.get_map_register())
else:
sql = "SELECT * FROM %s WHERE %s.id IN (SELECT id FROM %s)" % \
(map_view, map_view, self.get_map_register())
if where is not None and where != "":
sql += " AND (%s)" % (where.split(";")[0])
if order is not None and order != "":
sql += " ORDER BY %s" % (order.split(";")[0])
try:
dbif.execute(sql, mapset=self.base.mapset)
rows = dbif.fetchall(mapset=self.base.mapset)
except:
if connected:
dbif.close()
self.msgr.error(_("Unable to get map ids from register table "
"<%s>") % (self.get_map_register()))
raise
if connected:
dbif.close()
return rows
@staticmethod
def shift_map_list(maps, gran):
"""Temporally shift each map in the list with the provided granularity
This method does not perform any temporal database operations.
:param maps: A list of maps with initialized temporal extent
:param gran: The granularity to be used for shifting
:return: The modified map list, None if nothing to shift or wrong
granularity
.. code-block:: python
>>> import grass.temporal as tgis
>>> maps = []
>>> for i in range(5):
... map = tgis.RasterDataset(None)
... if i%2 == 0:
... check = map.set_relative_time(i, i + 1, 'years')
... else:
... check = map.set_relative_time(i, None, 'years')
... maps.append(map)
>>> for map in maps:
... map.temporal_extent.print_info()
+-------------------- Relative time -----------------------------------------+
| Start time:................. 0
| End time:................... 1
| Relative time unit:......... years
+-------------------- Relative time -----------------------------------------+
| Start time:................. 1
| End time:................... None
| Relative time unit:......... years
+-------------------- Relative time -----------------------------------------+
| Start time:................. 2
| End time:................... 3
| Relative time unit:......... years
+-------------------- Relative time -----------------------------------------+
| Start time:................. 3
| End time:................... None
| Relative time unit:......... years
+-------------------- Relative time -----------------------------------------+
| Start time:................. 4
| End time:................... 5
| Relative time unit:......... years
>>> maps = tgis.AbstractSpaceTimeDataset.shift_map_list(maps, 5)
>>> for map in maps:
... map.temporal_extent.print_info()
+-------------------- Relative time -----------------------------------------+
| Start time:................. 5
| End time:................... 6
| Relative time unit:......... years
+-------------------- Relative time -----------------------------------------+
| Start time:................. 6
| End time:................... None
| Relative time unit:......... years
+-------------------- Relative time -----------------------------------------+
| Start time:................. 7
| End time:................... 8
| Relative time unit:......... years
+-------------------- Relative time -----------------------------------------+
| Start time:................. 8
| End time:................... None
| Relative time unit:......... years
+-------------------- Relative time -----------------------------------------+
| Start time:................. 9
| End time:................... 10
| Relative time unit:......... years
"""
if maps is None:
return None
if not check_granularity_string(gran, maps[-1].get_temporal_type()):
self.msgr.error(_("Wrong granularity format: %s" % (gran)))
return None
for map in maps:
start, end = map.get_temporal_extent_as_tuple()
if map.is_time_absolute():
start = increment_datetime_by_string(start, gran)
if end is not None:
end = increment_datetime_by_string(end, gran)
map.set_absolute_time(start, end)
elif map.is_time_relative():
start = start + int(gran)
if end is not None:
end = end + int(gran)
map.set_relative_time(start, end, map.get_relative_time_unit())
return maps
def shift(self, gran, dbif=None):
"""Temporally shift each registered map with the provided granularity
:param gran: The granularity to be used for shifting
:param dbif: The database interface to be used
:return: True something to shift, False if nothing to shift or wrong
granularity
"""
if get_enable_mapset_check() is True and \
self.get_mapset() != get_current_mapset():
self.msgr.fatal(_("Unable to shift dataset <%(ds)s> of type "
"%(type)s in the temporal database. The mapset "
"of the dataset does not match the current "
"mapset") % ({"ds": self.get_id()},
{"type": self.get_type()}))
if not check_granularity_string(gran, self.get_temporal_type()):
self.msgr.error(_("Wrong granularity format: %s" % (gran)))
return False
dbif, connected = init_dbif(dbif)
maps = self.get_registered_maps_as_objects(dbif=dbif)
if maps is None:
return False
date_list = []
# We need to make a dry run to avoid a break
# in the middle of the update process when the increment
# results in wrong number of days in a month
for map in maps:
start, end = map.get_temporal_extent_as_tuple()
if self.is_time_absolute():
start = increment_datetime_by_string(start, gran)
if end is not None:
end = increment_datetime_by_string(end, gran)
elif self.is_time_relative():
start = start + int(gran)
if end is not None:
end = end + int(gran)
date_list.append((start, end))
self. _update_map_timestamps(maps, date_list, dbif)
if connected:
dbif.close()
@staticmethod
def snap_map_list(maps):
"""For each map in the list snap the end time to the start time of its
temporal nearest neighbor in the future.
Maps with equal time stamps are not snapped.
The granularity of the map list will be used to create the end time
of the last map in case it has a time instance as timestamp.
This method does not perform any temporal database operations.
:param maps: A list of maps with initialized temporal extent
:return: The modified map list, None nothing to shift or wrong
granularity
Usage:
.. code-block:: python
>>> import grass.temporal as tgis
>>> maps = []
>>> for i in range(5):
... map = tgis.RasterDataset(None)
... if i%2 == 0:
... check = map.set_relative_time(i, i + 1, 'years')
... else:
... check = map.set_relative_time(i, None, 'years')
... maps.append(map)
>>> for map in maps:
... map.temporal_extent.print_info()
+-------------------- Relative time -----------------------------------------+
| Start time:................. 0
| End time:................... 1
| Relative time unit:......... years
+-------------------- Relative time -----------------------------------------+
| Start time:................. 1
| End time:................... None
| Relative time unit:......... years
+-------------------- Relative time -----------------------------------------+
| Start time:................. 2
| End time:................... 3
| Relative time unit:......... years
+-------------------- Relative time -----------------------------------------+
| Start time:................. 3
| End time:................... None
| Relative time unit:......... years
+-------------------- Relative time -----------------------------------------+
| Start time:................. 4
| End time:................... 5
| Relative time unit:......... years
>>> maps = tgis.AbstractSpaceTimeDataset.snap_map_list(maps)
>>> for map in maps:
... map.temporal_extent.print_info()
+-------------------- Relative time -----------------------------------------+
| Start time:................. 0
| End time:................... 1
| Relative time unit:......... years
+-------------------- Relative time -----------------------------------------+
| Start time:................. 1
| End time:................... 2
| Relative time unit:......... years
+-------------------- Relative time -----------------------------------------+
| Start time:................. 2
| End time:................... 3
| Relative time unit:......... years
+-------------------- Relative time -----------------------------------------+
| Start time:................. 3
| End time:................... 4
| Relative time unit:......... years
+-------------------- Relative time -----------------------------------------+
| Start time:................. 4
| End time:................... 5
| Relative time unit:......... years
"""
if maps is None or len(maps) == 0:
return None
# We need to sort the maps temporally by start time
maps = sorted(maps, key=AbstractDatasetComparisonKeyStartTime)
for i in range(len(maps) - 1):
start, end = maps[i].get_temporal_extent_as_tuple()
start_next, end = maps[i + 1].get_temporal_extent_as_tuple()
# Maps with equal time stamps can not be snapped
if start != start_next:
if maps[i].is_time_absolute():
maps[i].set_absolute_time(start, start_next)
elif maps[i].is_time_relative():
maps[i].set_relative_time(start, start_next,
maps[i].get_relative_time_unit())
else:
if maps[i].is_time_absolute():
maps[i].set_absolute_time(start, end)
elif maps[i].is_time_relative():
maps[i].set_relative_time(start, end,
maps[i].get_relative_time_unit())
# Last map
start, end = maps[-1].get_temporal_extent_as_tuple()
# We increment the start time with the dataset
# granularity if the end time is None
if end is None:
if maps[-1].is_time_absolute():
gran = compute_absolute_time_granularity(maps)
end = increment_datetime_by_string(start, gran)
maps[-1].set_absolute_time(start, end)
elif maps[-1].is_time_relative():
gran = compute_relative_time_granularity(maps)
end = start + gran
maps[-1].set_relative_time(start, end,
maps[-1].get_relative_time_unit())
return maps
def snap(self, dbif=None):
"""For each registered map snap the end time to the start time of
its temporal nearest neighbor in the future
Maps with equal time stamps are not snapped
:param dbif: The database interface to be used
"""
if get_enable_mapset_check() is True and \
self.get_mapset() != get_current_mapset():
self.msgr.fatal(_("Unable to snap dataset <%(ds)s> of type "
"%(type)s in the temporal database. The mapset "
"of the dataset does not match the current "
"mapset") % ({"ds": self.get_id()},
{"type": self.get_type()}))
dbif, connected = init_dbif(dbif)
maps = self.get_registered_maps_as_objects(dbif=dbif)
if maps is None:
return
date_list = []
for i in range(len(maps) - 1):
start, end = maps[i].get_temporal_extent_as_tuple()
start_next, end = maps[i + 1].get_temporal_extent_as_tuple()
# Maps with equal time stamps can not be snapped
if start != start_next:
date_list.append((start, start_next))
else:
# Keep the original time stamps
date_list.append((start, end))
# Last map
start, end = maps[-1].get_temporal_extent_as_tuple()
# We increment the start time with the dataset
# granularity if the end time is None
if end is None:
if self.is_time_absolute():
end = increment_datetime_by_string(start,
self.get_granularity())
elif self.is_time_relative():
end = start + self.get_granularity()
date_list.append((start, end))
self._update_map_timestamps(maps, date_list, dbif)
if connected:
dbif.close()
def _update_map_timestamps(self, maps, date_list, dbif):
"""Update the timestamps of maps with the start and end time
stored in the date_list.
The number of dates in the list must be equal to the number
of maps.
:param maps: A list of map objects
:param date_list: A list with date tuples (start_time, end_time)
:param dbif: The database interface to be used
"""
datatsets_to_modify = {}
# Now update the maps
count = 0
for map in maps:
start = date_list[count][0]
end = date_list[count][1]
map.select(dbif)
count += 1
if self.is_time_absolute():
map.update_absolute_time(start_time=start, end_time=end,
dbif=dbif)
elif self.is_time_relative():
map.update_relative_time(start_time=start, end_time=end,
unit=self.get_relative_time_unit(),
dbif=dbif)
# Save the datasets that must be updated
datasets = map.get_registered_stds(dbif)
if datasets:
for dataset in datasets:
datatsets_to_modify[dataset] = dataset
self.update_from_registered_maps(dbif)
# Update affected datasets
if datatsets_to_modify:
for dataset in datatsets_to_modify:
if dataset != self.get_id():
ds = self.get_new_instance(ident=dataset)
ds.select(dbif)
ds.update_from_registered_maps(dbif)
def rename(self, ident, dbif=None):
"""Rename the space time dataset
This method renames the space time dataset, the map register table
and updates the entries in registered maps stds register.
Renaming does not work with Postgresql yet.
:param ident: The new identifier "name@mapset"
:param dbif: The database interface to be used
"""
if get_enable_mapset_check() is True and self.get_mapset() != get_current_mapset():
self.msgr.fatal(_("Unable to rename dataset <%(ds)s> of type "
"%(type)s in the temporal database. The mapset "
"of the dataset does not match the current "
"mapset") % ({"ds": self.get_id()},
{"type": self.get_type()}))
dbif, connected = init_dbif(dbif)
if dbif.get_dbmi().__name__ != "sqlite3":
self.msgr.fatal(_("Renaming of space time datasets is not "
"supported for PostgreSQL."))
# SELECT all needed information from the database
self.select(dbif)
# We need to select the registered maps here
maps = self.get_registered_maps_as_objects(None, "start_time", dbif)
# Safe old identifier
old_ident = self.get_id()
# We need to rename the old table
old_map_register_table = self.get_map_register()
# Set new identifier
self.set_id(ident)
# Create map register table name from new identifier
new_map_register_table = self.create_map_register_name()
# Set new map register table name
self.set_map_register(new_map_register_table)
# Get the update statement, we update the table entry of the old
# identifier
statement = self.update(dbif, execute=False, ident=old_ident)
# We need to rename the raster register table
statement += "ALTER TABLE %s RENAME TO \"%s\";\n" % \
(old_map_register_table, new_map_register_table)
# We need to take care of the stds index in the sqlite3 database
if dbif.get_dbmi().__name__ == "sqlite3":
statement += "DROP INDEX %s_index;\n" % (old_map_register_table)
statement += "CREATE INDEX %s_index ON %s (id);" % \
(new_map_register_table, new_map_register_table)
# We need to rename the space time dataset in the maps register table
if maps:
for map in maps:
map.remove_stds_from_register(stds_id=old_ident, dbif=dbif)
map.add_stds_to_register(stds_id=ident, dbif=dbif)
# Execute the accumulated statements
dbif.execute_transaction(statement)
if connected:
dbif.close()
def delete(self, dbif=None, execute=True):
"""Delete a space time dataset from the temporal database
This method removes the space time dataset from the temporal
database and drops its map register table
:param dbif: The database interface to be used
:param execute: If True the SQL DELETE and DROP table
statements will be executed.
If False the prepared SQL statements are returned
and must be executed by the caller.
:return: The SQL statements if execute == False, else an empty
string
"""
# First we need to check if maps are registered in this dataset and
# unregister them
self.msgr.verbose(_("Delete space time %s dataset <%s> from temporal "
"database") % (self.get_new_map_instance(ident=None).get_type(),
self.get_id()))
if get_enable_mapset_check() is True and \
self.get_mapset() != get_current_mapset():
self.msgr.fatal(_("Unable to delete dataset <%(ds)s> of type "
"%(type)s from the temporal database. The mapset"
" of the dataset does not match the current "
"mapset") % {"ds": self.get_id(),
"type": self.get_type()})
statement = ""
dbif, connected = init_dbif(dbif)
# SELECT all needed information from the database
self.metadata.select(dbif)
if self.get_map_register() is not None:
self.msgr.debug(1, _("Drop map register table: %s") % (
self.get_map_register()))
rows = self.get_registered_maps("id", None, None, dbif)
# Unregister each registered map in the table
if rows is not None:
for row in rows:
# Unregister map
map = self.get_new_map_instance(row["id"])
statement += self.unregister_map(
map=map, dbif=dbif, execute=False)
# Safe the DROP table statement
statement += "DROP TABLE IF EXISTS " + self.get_map_register() + ";\n"
# Remove the primary key, the foreign keys will be removed by trigger
statement += self.base.get_delete_statement()
if execute:
dbif.execute_transaction(statement)
self.reset(None)
if connected:
dbif.close()
if execute:
return ""
return statement
def is_map_registered(self, map_id, dbif=None):
"""Check if a map is registered in the space time dataset
:param map_id: The map id
:param dbif: The database interface to be used
:return: True if success, False otherwise
"""
stds_register_table = self.get_map_register()
dbif, connected = init_dbif(dbif)
is_registered = False
# Check if map is already registered
if stds_register_table is not None:
if dbif.get_dbmi().paramstyle == "qmark":
sql = "SELECT id FROM " + \
stds_register_table + " WHERE id = (?)"
else:
sql = "SELECT id FROM " + \
stds_register_table + " WHERE id = (%s)"
try:
dbif.execute(sql, (map_id,), mapset=self.base.mapset)
row = dbif.fetchone(mapset=self.base.mapset)
except:
self.msgr.warning(_("Error in register table request"))
raise
if row is not None and row[0] == map_id:
is_registered = True
if connected is True:
dbif.close()
return is_registered
def register_map(self, map, dbif=None):
"""Register a map in the space time dataset.
This method takes care of the registration of a map
in a space time dataset.
In case the map is already registered this function
will break with a warning and return False.
This method raises a FatalError exception in case of a fatal error
:param map: The AbstractMapDataset object that should be registered
:param dbif: The database interface to be used
:return: True if success, False otherwise
"""
if get_enable_mapset_check() is True and \
self.get_mapset() != get_current_mapset():
self.msgr.fatal(_("Unable to register map in dataset <%(ds)s> of "
"type %(type)s. The mapset of the dataset does "
"not match the current mapset") %
{"ds": self.get_id(), "type": self.get_type()})
dbif, connected = init_dbif(dbif)
if map.is_in_db(dbif) is False:
dbif.close()
self.msgr.fatal(_("Only a map that was inserted in the temporal "
"database can be registered in a space time "
"dataset"))
if map.get_layer():
self.msgr.debug(1, "Register %s map <%s> with layer %s in space "
"time %s dataset <%s>" % (map.get_type(),
map.get_map_id(),
map.get_layer(),
map.get_type(),
self.get_id()))
else:
self.msgr.debug(1, "Register %s map <%s> in space time %s "
"dataset <%s>" % (map.get_type(),
map.get_map_id(),
map.get_type(),
self.get_id()))
# First select all data from the database
map.select(dbif)
if not map.check_for_correct_time():
if map.get_layer():
self.msgr.fatal(_("Map <%(id)s> with layer %(l)s has invalid "
"time") % {'id': map.get_map_id(),
'l': map.get_layer()})
else:
self.msgr.fatal(_("Map <%s> has invalid time") % (map.get_map_id()))
# Get basic info
map_id = map.base.get_id()
map_mapset = map.base.get_mapset()
map_rel_time_unit = map.get_relative_time_unit()
map_ttype = map.get_temporal_type()
stds_mapset = self.base.get_mapset()
stds_register_table = self.get_map_register()
stds_ttype = self.get_temporal_type()
# The gathered SQL statemets are stroed here
statement = ""
# Check temporal types
if stds_ttype != map_ttype:
if map.get_layer():
self.msgr.fatal(_("Temporal type of space time dataset "
"<%(id)s> and map <%(map)s> with layer %(l)s"
" are different") % {'id': self.get_id(),
'map': map.get_map_id(),
'l': map.get_layer()})
else:
self.msgr.fatal(_("Temporal type of space time dataset "
"<%(id)s> and map <%(map)s> are different")
% {'id': self.get_id(),
'map': map.get_map_id()})
# In case no map has been registered yet, set the
# relative time unit from the first map
if (self.metadata.get_number_of_maps() is None or
self.metadata.get_number_of_maps() == 0) and \
self.map_counter == 0 and self.is_time_relative():
self.set_relative_time_unit(map_rel_time_unit)
statement += self.relative_time.get_update_all_statement_mogrified(
dbif)
self.msgr.debug(1, _("Set temporal unit for space time %s dataset "
"<%s> to %s") % (map.get_type(),
self.get_id(),
map_rel_time_unit))
stds_rel_time_unit = self.get_relative_time_unit()
# Check the relative time unit
if self.is_time_relative() and (stds_rel_time_unit != map_rel_time_unit):
if map.get_layer():
self.msgr.fatal(_("Relative time units of space time dataset "
"<%(id)s> and map <%(map)s> with layer %(l)s"
" are different") % {'id': self.get_id(),
'map': map.get_map_id(),
'l': map.get_layer()})
else:
self.msgr.fatal(_("Relative time units of space time dataset "
"<%(id)s> and map <%(map)s> are different") %
{'id': self.get_id(), 'map': map.get_map_id()})
if get_enable_mapset_check() is True and stds_mapset != map_mapset:
dbif.close()
self.msgr.fatal(_("Only maps from the same mapset can be registered"))
# Check if map is already registered
if self.is_map_registered(map_id, dbif=dbif):
if map.get_layer() is not None:
self.msgr.warning(_("Map <%(map)s> with layer %(l)s is already"
" registered.") % {'map': map.get_map_id(),
'l': map.get_layer()})
else:
self.msgr.warning(_("Map <%s> is already registered.") %
(map.get_map_id()))
return False
# Register the stds in the map stds register table column
statement += map.add_stds_to_register(stds_id=self.base.get_id(),
dbif=dbif, execute=False)
# Now put the raster name in the stds map register table
if dbif.get_dbmi().paramstyle == "qmark":
sql = "INSERT INTO " + stds_register_table + \
" (id) " + "VALUES (?);\n"
else:
sql = "INSERT INTO " + stds_register_table + \
" (id) " + "VALUES (%s);\n"
statement += dbif.mogrify_sql_statement((sql, (map_id,)))
# Now execute the insert transaction
dbif.execute_transaction(statement)
if connected:
dbif.close()
# increase the counter
self.map_counter += 1
return True
def unregister_map(self, map, dbif=None, execute=True):
"""Unregister a map from the space time dataset.
This method takes care of the un-registration of a map
from a space time dataset.
:param map: The map object to unregister
:param dbif: The database interface to be used
:param execute: If True the SQL DELETE and DROP table
statements will be executed.
If False the prepared SQL statements are
returned and must be executed by the caller.
:return: The SQL statements if execute == False, else an empty
string, None in case of a failure
"""
if get_enable_mapset_check() is True and \
self.get_mapset() != get_current_mapset():
self.msgr.fatal(_("Unable to unregister map from dataset <%(ds)s>"
" of type %(type)s in the temporal database."
" The mapset of the dataset does not match the"
" current mapset") % {"ds": self.get_id(),
"type": self.get_type()})
statement = ""
dbif, connected = init_dbif(dbif)
# Check if the map is registered in the space time raster dataset
if self.is_map_registered(map.get_id(), dbif) is False:
if map.get_layer() is not None:
self.msgr.warning(_("Map <%(map)s> with layer %(l)s is not "
"registered in space time dataset "
"<%(base)s>") % {'map': map.get_map_id(),
'l': map.get_layer(),
'base': self.base.get_id()})
else:
self.msgr.warning(_("Map <%(map)s> is not registered in space "
"time dataset <%(base)s>") %
{'map': map.get_map_id(),
'base': self.base.get_id()})
if connected is True:
dbif.close()
return ""
# Remove the space time dataset from the dataset register
# We need to execute the statement here, otherwise the space time
# dataset will not be removed correctly
map.remove_stds_from_register(self.base.get_id(),
dbif=dbif, execute=True)
# Remove the map from the space time dataset register
stds_register_table = self.get_map_register()
if stds_register_table is not None:
if dbif.get_dbmi().paramstyle == "qmark":
sql = "DELETE FROM " + stds_register_table + " WHERE id = ?;\n"
else:
sql = "DELETE FROM " + \
stds_register_table + " WHERE id = %s;\n"
statement += dbif.mogrify_sql_statement((sql, (map.get_id(), )))
if execute:
dbif.execute_transaction(statement)
statement = ""
if connected:
dbif.close()
# decrease the counter
self.map_counter -= 1
return statement
def update_from_registered_maps(self, dbif=None):
"""This methods updates the modification time, the spatial and
temporal extent as well as type specific metadata. It should always
been called after maps are registered or unregistered/deleted from
the space time dataset.
The update of the temporal extent checks if the end time is set
correctly.
In case the registered maps have no valid end time (None) the
maximum start time
will be used. If the end time is earlier than the maximum start
time, it will be replaced by the maximum start time.
:param dbif: The database interface to be used
"""
if get_enable_mapset_check() is True and \
self.get_mapset() != get_current_mapset():
self.msgr.fatal(_("Unable to update dataset <%(ds)s> of type "
"%(type)s in the temporal database. The mapset"
" of the dataset does not match the current "
"mapset") % {"ds": self.get_id(),
"type": self.get_type()})
self.msgr.verbose(_("Update metadata, spatial and temporal extent from"
" all registered maps of <%s>") % (self.get_id()))
# Nothing to do if the map register is not present
if not self.get_map_register():
return
dbif, connected = init_dbif(dbif)
map_time = None
use_start_time = False
# Get basic info
stds_name = self.base.get_name()
stds_mapset = self.base.get_mapset()
sql_path = get_sql_template_path()
stds_register_table = self.get_map_register()
# We create a transaction
sql_script = ""
# Update the spatial and temporal extent from registered maps
# Read the SQL template
sql = open(os.path.join(sql_path,
"update_stds_spatial_temporal_extent_template.sql"),
'r').read()
sql = sql.replace(
"GRASS_MAP", self.get_new_map_instance(None).get_type())
sql = sql.replace("SPACETIME_REGISTER_TABLE", stds_register_table)
sql = sql.replace("SPACETIME_ID", self.base.get_id())
sql = sql.replace("STDS", self.get_type())
sql_script += sql
sql_script += "\n"
# Update type specific metadata
sql = open(os.path.join(sql_path, "update_" +
self.get_type() +
"_metadata_template.sql"),
'r').read()
sql = sql.replace("SPACETIME_REGISTER_TABLE", stds_register_table)
sql = sql.replace("SPACETIME_ID", self.base.get_id())
sql_script += sql
sql_script += "\n"
dbif.execute_transaction(sql_script)
# Read and validate the selected end time
self.select(dbif)
if self.is_time_absolute():
start_time, end_time = self.get_absolute_time()
else:
start_time, end_time, unit = self.get_relative_time()
# In case no end time is set, use the maximum start time of
# all registered maps as end time
if end_time is None:
use_start_time = True
else:
# Check if the end time is smaller than the maximum start time
if self.is_time_absolute():
sql = """SELECT max(start_time) FROM GRASS_MAP_absolute_time
WHERE GRASS_MAP_absolute_time.id IN
(SELECT id FROM SPACETIME_REGISTER_TABLE);"""
sql = sql.replace("GRASS_MAP", self.get_new_map_instance(
None).get_type())
sql = sql.replace("SPACETIME_REGISTER_TABLE",
stds_register_table)
else:
sql = """SELECT max(start_time) FROM GRASS_MAP_relative_time
WHERE GRASS_MAP_relative_time.id IN
(SELECT id FROM SPACETIME_REGISTER_TABLE);"""
sql = sql.replace("GRASS_MAP", self.get_new_map_instance(
None).get_type())
sql = sql.replace("SPACETIME_REGISTER_TABLE",
stds_register_table)
dbif.execute(sql, mapset=self.base.mapset)
row = dbif.fetchone(mapset=self.base.mapset)
if row is not None:
# This seems to be a bug in sqlite3 Python driver
if dbif.get_dbmi().__name__ == "sqlite3":
tstring = row[0]
# Convert the unicode string into the datetime format
if self.is_time_absolute():
if tstring.find(":") > 0:
time_format = "%Y-%m-%d %H:%M:%S"
else:
time_format = "%Y-%m-%d"
max_start_time = datetime.strptime(
tstring, time_format)
else:
max_start_time = row[0]
else:
max_start_time = row[0]
if end_time < max_start_time:
use_start_time = True
# Set the maximum start time as end time
if use_start_time:
if self.is_time_absolute():
sql = """UPDATE STDS_absolute_time SET end_time =
(SELECT max(start_time) FROM GRASS_MAP_absolute_time WHERE
GRASS_MAP_absolute_time.id IN
(SELECT id FROM SPACETIME_REGISTER_TABLE)
) WHERE id = 'SPACETIME_ID';"""
sql = sql.replace("GRASS_MAP", self.get_new_map_instance(
None).get_type())
sql = sql.replace("SPACETIME_REGISTER_TABLE",
stds_register_table)
sql = sql.replace("SPACETIME_ID", self.base.get_id())
sql = sql.replace("STDS", self.get_type())
elif self.is_time_relative():
sql = """UPDATE STDS_relative_time SET end_time =
(SELECT max(start_time) FROM GRASS_MAP_relative_time WHERE
GRASS_MAP_relative_time.id IN
(SELECT id FROM SPACETIME_REGISTER_TABLE)
) WHERE id = 'SPACETIME_ID';"""
sql = sql.replace("GRASS_MAP", self.get_new_map_instance(
None).get_type())
sql = sql.replace("SPACETIME_REGISTER_TABLE",
stds_register_table)
sql = sql.replace("SPACETIME_ID", self.base.get_id())
sql = sql.replace("STDS", self.get_type())
dbif.execute_transaction(sql)
# Count the temporal map types
maps = self.get_registered_maps_as_objects(dbif=dbif)
tlist = self.count_temporal_types(maps)
if tlist["interval"] > 0 and tlist["point"] == 0 and \
tlist["invalid"] == 0:
map_time = "interval"
elif tlist["interval"] == 0 and tlist["point"] > 0 and \
tlist["invalid"] == 0:
map_time = "point"
elif tlist["interval"] > 0 and tlist["point"] > 0 and \
tlist["invalid"] == 0:
map_time = "mixed"
else:
map_time = "invalid"
# Compute the granularity
if map_time != "invalid":
# Smallest supported temporal resolution
if self.is_time_absolute():
gran = compute_absolute_time_granularity(maps)
elif self.is_time_relative():
gran = compute_relative_time_granularity(maps)
else:
gran = None
# Set the map time type and update the time objects
self.temporal_extent.select(dbif)
self.metadata.select(dbif)
if self.metadata.get_number_of_maps() > 0:
self.temporal_extent.set_map_time(map_time)
self.temporal_extent.set_granularity(gran)
else:
self.temporal_extent.set_map_time(None)
self.temporal_extent.set_granularity(None)
self.temporal_extent.update_all(dbif)
# Set the modification time
self.base.set_mtime(datetime.now())
self.base.update(dbif)
if connected:
dbif.close()
###############################################################################
if __name__ == "__main__":
import doctest
doctest.testmod()
|