/usr/lib/python3/dist-packages/gpxpy/gpx.py is in python3-gpxpy 1.1.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 | # -*- coding: utf-8 -*-
# Copyright 2011 Tomo Krajina
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
GPX related stuff
"""
import logging as mod_logging
import math as mod_math
import collections as mod_collections
import copy as mod_copy
import datetime as mod_datetime
from . import utils as mod_utils
from . import geo as mod_geo
from . import gpxfield as mod_gpxfield
# GPX date format to be used when writing the GPX output:
DATE_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
# GPX date format(s) used for parsing. The T between date and time and Z after
# time are allowed, too:
DATE_FORMATS = [
'%Y-%m-%d %H:%M:%S',
'%Y-%m-%d %H:%M:%S.%f',
#'%Y-%m-%d %H:%M:%S%z',
#'%Y-%m-%d %H:%M:%S.%f%z',
]
# Used in smoothing, sum must be 1:
SMOOTHING_RATIO = (0.4, 0.2, 0.4)
# When computing stopped time -- this is the minimum speed between two points,
# if speed is less than this value -- we'll assume it is zero
DEFAULT_STOPPED_SPEED_THRESHOLD = 1
# Fields used for all point elements (route point, track point, waypoint):
GPX_10_POINT_FIELDS = [
mod_gpxfield.GPXField('latitude', attribute='lat', type=mod_gpxfield.FLOAT_TYPE, mandatory=True),
mod_gpxfield.GPXField('longitude', attribute='lon', type=mod_gpxfield.FLOAT_TYPE, mandatory=True),
mod_gpxfield.GPXField('elevation', 'ele', type=mod_gpxfield.FLOAT_TYPE),
mod_gpxfield.GPXField('time', type=mod_gpxfield.TIME_TYPE),
mod_gpxfield.GPXField('magnetic_variation', 'magvar', type=mod_gpxfield.FLOAT_TYPE),
mod_gpxfield.GPXField('geoid_height', 'geoidheight', type=mod_gpxfield.FLOAT_TYPE),
mod_gpxfield.GPXField('name'),
mod_gpxfield.GPXField('comment', 'cmt'),
mod_gpxfield.GPXField('description', 'desc'),
mod_gpxfield.GPXField('source', 'src'),
mod_gpxfield.GPXField('link', 'url'),
mod_gpxfield.GPXField('link_text', 'urlname'),
mod_gpxfield.GPXField('symbol', 'sym'),
mod_gpxfield.GPXField('type'),
mod_gpxfield.GPXField('type_of_gpx_fix', 'fix', possible=('none', '2d', '3d', 'dgps', 'pps',)),
mod_gpxfield.GPXField('satellites', 'sat', type=mod_gpxfield.INT_TYPE),
mod_gpxfield.GPXField('horizontal_dilution', 'hdop', type=mod_gpxfield.FLOAT_TYPE),
mod_gpxfield.GPXField('vertical_dilution', 'vdop', type=mod_gpxfield.FLOAT_TYPE),
mod_gpxfield.GPXField('position_dilution', 'pdop', type=mod_gpxfield.FLOAT_TYPE),
mod_gpxfield.GPXField('age_of_dgps_data', 'ageofdgpsdata', type=mod_gpxfield.FLOAT_TYPE),
mod_gpxfield.GPXField('dgps_id', 'dgpsid'),
]
GPX_11_POINT_FIELDS = [
mod_gpxfield.GPXField('latitude', attribute='lat', type=mod_gpxfield.FLOAT_TYPE, mandatory=True),
mod_gpxfield.GPXField('longitude', attribute='lon', type=mod_gpxfield.FLOAT_TYPE, mandatory=True),
mod_gpxfield.GPXField('elevation', 'ele', type=mod_gpxfield.FLOAT_TYPE),
mod_gpxfield.GPXField('time', type=mod_gpxfield.TIME_TYPE),
mod_gpxfield.GPXField('magnetic_variation', 'magvar', type=mod_gpxfield.FLOAT_TYPE),
mod_gpxfield.GPXField('geoid_height', 'geoidheight', type=mod_gpxfield.FLOAT_TYPE),
mod_gpxfield.GPXField('name'),
mod_gpxfield.GPXField('comment', 'cmt'),
mod_gpxfield.GPXField('description', 'desc'),
mod_gpxfield.GPXField('source', 'src'),
'link',
mod_gpxfield.GPXField('link', attribute='href'),
mod_gpxfield.GPXField('link_text', tag='text'),
mod_gpxfield.GPXField('link_type', tag='type'),
'/link',
mod_gpxfield.GPXField('symbol', 'sym'),
mod_gpxfield.GPXField('type'),
mod_gpxfield.GPXField('type_of_gpx_fix', 'fix', possible=('none', '2d', '3d', 'dgps', 'pps',)),
mod_gpxfield.GPXField('satellites', 'sat', type=mod_gpxfield.INT_TYPE),
mod_gpxfield.GPXField('horizontal_dilution', 'hdop', type=mod_gpxfield.FLOAT_TYPE),
mod_gpxfield.GPXField('vertical_dilution', 'vdop', type=mod_gpxfield.FLOAT_TYPE),
mod_gpxfield.GPXField('position_dilution', 'pdop', type=mod_gpxfield.FLOAT_TYPE),
mod_gpxfield.GPXField('age_of_dgps_data', 'ageofdgpsdata', type=mod_gpxfield.FLOAT_TYPE),
mod_gpxfield.GPXField('dgps_id', 'dgpsid'),
mod_gpxfield.GPXExtensionsField('extensions'),
]
# GPX1.0 track points have two more fields after time
# Note that this is not true for GPX1.1
GPX_TRACK_POINT_FIELDS = GPX_10_POINT_FIELDS[:4] \
+ [ \
mod_gpxfield.GPXField('course', type=mod_gpxfield.FLOAT_TYPE), \
mod_gpxfield.GPXField('speed', type=mod_gpxfield.FLOAT_TYPE) \
] \
+ GPX_10_POINT_FIELDS[4:]
# When possible, the result of various methods are named tuples defined here:
TimeBounds = mod_collections.namedtuple(
'TimeBounds',
('start_time', 'end_time'))
MovingData = mod_collections.namedtuple(
'MovingData',
('moving_time', 'stopped_time', 'moving_distance', 'stopped_distance', 'max_speed'))
UphillDownhill = mod_collections.namedtuple(
'UphillDownhill',
('uphill', 'downhill'))
MinimumMaximum = mod_collections.namedtuple(
'MinimumMaximum',
('minimum', 'maximum'))
NearestLocationData = mod_collections.namedtuple(
'NearestLocationData',
('location', 'track_no', 'segment_no', 'point_no'))
PointData = mod_collections.namedtuple(
'PointData',
('point', 'distance_from_start', 'track_no', 'segment_no', 'point_no'))
class GPXException(Exception):
"""
Exception used for invalid GPX files. Is is used when the XML file is
valid but something is wrong with the GPX data.
"""
pass
class GPXBounds:
gpx_10_fields = gpx_11_fields = [
mod_gpxfield.GPXField('min_latitude', attribute='minlat', type=mod_gpxfield.FLOAT_TYPE),
mod_gpxfield.GPXField('max_latitude', attribute='maxlat', type=mod_gpxfield.FLOAT_TYPE),
mod_gpxfield.GPXField('min_longitude', attribute='minlon', type=mod_gpxfield.FLOAT_TYPE),
mod_gpxfield.GPXField('max_longitude', attribute='maxlon', type=mod_gpxfield.FLOAT_TYPE),
]
__slots__ = ('min_latitude', 'max_latitude', 'min_longitude', 'max_longitude')
def __init__(self, min_latitude=None, max_latitude=None, min_longitude=None, max_longitude=None):
self.min_latitude = min_latitude
self.max_latitude = max_latitude
self.min_longitude = min_longitude
self.max_longitude = max_longitude
def __iter__(self):
return (self.min_latitude, self.max_latitude, self.min_longitude, self.max_longitude,).__iter__()
def __hash__(self):
return mod_utils.hash_object(self, self.__slots__)
class GPXXMLSyntaxException(GPXException):
"""
Exception used when the the XML syntax is invalid.
The __cause__ can be a minidom or lxml exception (See http://www.python.org/dev/peps/pep-3134/).
"""
def __init__(self, message, original_exception):
GPXException.__init__(self, message)
self.__cause__ = original_exception
class GPXWaypoint(mod_geo.Location):
gpx_10_fields = GPX_10_POINT_FIELDS
gpx_11_fields = GPX_11_POINT_FIELDS
__slots__ = ('latitude', 'longitude', 'elevation', 'time',
'magnetic_variation', 'geoid_height', 'name', 'comment',
'description', 'source', 'link', 'link_text', 'symbol',
'type', 'type_of_gpx_fix', 'satellites',
'horizontal_dilution', 'vertical_dilution',
'position_dilution', 'age_of_dgps_data', 'dgps_id',
'link_type', 'extensions')
def __init__(self, latitude=None, longitude=None, elevation=None, time=None,
name=None, description=None, symbol=None, type=None,
comment=None, horizontal_dilution=None, vertical_dilution=None,
position_dilution=None):
mod_geo.Location.__init__(self, latitude, longitude, elevation)
self.latitude = latitude
self.longitude = longitude
self.elevation = elevation
self.time = time
self.magnetic_variation = None
self.geoid_height = None
self.name = name
self.comment = comment
self.description = description
self.source = None
self.link = None
self.link_text = None
self.link_type = None
self.symbol = symbol
self.type = type
self.type_of_gpx_fix = None
self.satellites = None
self.horizontal_dilution = horizontal_dilution
self.vertical_dilution = vertical_dilution
self.position_dilution = position_dilution
self.age_of_dgps_data = None
self.dgps_id = None
self.extensions = None
def __str__(self):
return '[wpt{%s}:%s,%s@%s]' % (self.name, self.latitude, self.longitude, self.elevation)
def __repr__(self):
representation = '%s, %s' % (self.latitude, self.longitude)
for attribute in 'elevation', 'time', 'name', 'description', 'symbol', 'type', 'comment', \
'horizontal_dilution', 'vertical_dilution', 'position_dilution':
value = getattr(self, attribute)
if value is not None:
representation += ', %s=%s' % (attribute, repr(value))
return 'GPXWaypoint(%s)' % representation
def get_max_dilution_of_precision(self):
"""
Only care about the max dop for filtering, no need to go into too much detail
"""
return max(self.horizontal_dilution, self.vertical_dilution, self.position_dilution)
def __hash__(self):
return mod_utils.hash_object(self, self.__slots__)
class GPXRoutePoint(mod_geo.Location):
gpx_10_fields = GPX_10_POINT_FIELDS
gpx_11_fields = GPX_11_POINT_FIELDS
__slots__ = ('latitude', 'longitude', 'elevation', 'time',
'magnetic_variation', 'geoid_height', 'name', 'comment',
'description', 'source', 'link', 'link_text', 'symbol',
'type', 'type_of_gpx_fix', 'satellites',
'horizontal_dilution', 'vertical_dilution',
'position_dilution', 'age_of_dgps_data', 'dgps_id',
'link_type', 'extensions')
def __init__(self, latitude=None, longitude=None, elevation=None, time=None, name=None,
description=None, symbol=None, type=None, comment=None,
horizontal_dilution=None, vertical_dilution=None,
position_dilution=None):
mod_geo.Location.__init__(self, latitude, longitude, elevation)
self.latitude = latitude
self.longitude = longitude
self.elevation = elevation
self.time = time
self.magnetic_variation = None
self.geoid_height = None
self.name = name
self.comment = comment
self.description = description
self.source = None
self.link = None
self.link_text = None
self.symbol = symbol
self.type = type
self.type_of_gpx_fix = None
self.satellites = None
self.horizontal_dilution = horizontal_dilution
self.vertical_dilution = vertical_dilution
self.position_dilution = position_dilution
self.age_of_dgps_data = None
self.dgps_id = None
self.link_type = None
self.extensions = None
def __str__(self):
return '[rtept{%s}:%s,%s@%s]' % (self.name, self.latitude, self.longitude, self.elevation)
def __repr__(self):
representation = '%s, %s' % (self.latitude, self.longitude)
for attribute in 'elevation', 'time', 'name', 'description', 'symbol', 'type', 'comment', \
'horizontal_dilution', 'vertical_dilution', 'position_dilution':
value = getattr(self, attribute)
if value is not None:
representation += ', %s=%s' % (attribute, repr(value))
return 'GPXRoutePoint(%s)' % representation
def __hash__(self):
return mod_utils.hash_object(self, self.__slots__)
class GPXRoute:
gpx_10_fields = [
mod_gpxfield.GPXField('name'),
mod_gpxfield.GPXField('comment', 'cmt'),
mod_gpxfield.GPXField('description', 'desc'),
mod_gpxfield.GPXField('source', 'src'),
mod_gpxfield.GPXField('link', 'url'),
mod_gpxfield.GPXField('link_text', 'urlname'),
mod_gpxfield.GPXField('number', type=mod_gpxfield.INT_TYPE),
mod_gpxfield.GPXComplexField('points', tag='rtept', classs=GPXRoutePoint, is_list=True),
]
gpx_11_fields = [
mod_gpxfield.GPXField('name'),
mod_gpxfield.GPXField('comment', 'cmt'),
mod_gpxfield.GPXField('description', 'desc'),
mod_gpxfield.GPXField('source', 'src'),
'link',
mod_gpxfield.GPXField('link', attribute='href'),
mod_gpxfield.GPXField('link_text', tag='text'),
mod_gpxfield.GPXField('link_type', tag='type'),
'/link',
mod_gpxfield.GPXField('number', type=mod_gpxfield.INT_TYPE),
mod_gpxfield.GPXField('type'),
mod_gpxfield.GPXExtensionsField('extensions'),
mod_gpxfield.GPXComplexField('points', tag='rtept', classs=GPXRoutePoint, is_list=True),
]
__slots__ = ('name', 'comment', 'description', 'source', 'link',
'link_text', 'number', 'points', 'link_type', 'type',
'extensions')
def __init__(self, name=None, description=None, number=None):
self.name = name
self.comment = None
self.description = description
self.source = None
self.link = None
self.link_text = None
self.number = number
self.points = []
self.link_type = None
self.type = None
self.extensions = None
def remove_elevation(self):
""" Removes elevation data from route """
for point in self.points:
point.remove_elevation()
def length(self):
"""
Computes length (2-dimensional) of route.
Returns:
-----------
length: float
Length returned in meters
"""
return mod_geo.length_2d(self.points)
def get_center(self):
"""
Get the center of the route.
Returns
-------
center: Location
latitude: latitude of center in degrees
longitude: longitude of center in degrees
elevation: not calculated here
"""
if not self.points:
return None
if not self.points:
return None
sum_lat = 0.
sum_lon = 0.
n = 0.
for point in self.points:
n += 1.
sum_lat += point.latitude
sum_lon += point.longitude
if not n:
return mod_geo.Location(float(0), float(0))
return mod_geo.Location(latitude=sum_lat / n, longitude=sum_lon / n)
def walk(self, only_points=False):
"""
Generator for iterating over route points
Parameters
----------
only_points: boolean
Only yield points (no index yielded)
Yields
------
point: GPXRoutePoint
A point in the GPXRoute
point_no: int
Not included in yield if only_points is true
"""
for point_no, point in enumerate(self.points):
if only_points:
yield point
else:
yield point, point_no
def get_points_no(self):
"""
Get the number of points in route.
Returns
----------
num_points : integer
Number of points in route
"""
return len(self.points)
def move(self, location_delta):
"""
Moves each point in the route.
Parameters
----------
location_delta: LocationDelta
LocationDelta to move each point
"""
for route_point in self.points:
route_point.move(location_delta)
def __hash__(self):
return mod_utils.hash_object(self, self.__slots__)
def __repr__(self):
representation = ''
for attribute in 'name', 'description', 'number':
value = getattr(self, attribute)
if value is not None:
representation += '%s%s=%s' % (', ' if representation else '', attribute, repr(value))
representation += '%spoints=[%s])' % (', ' if representation else '', '...' if self.points else '')
return 'GPXRoute(%s)' % representation
class GPXTrackPoint(mod_geo.Location):
gpx_10_fields = GPX_TRACK_POINT_FIELDS
gpx_11_fields = GPX_11_POINT_FIELDS
__slots__ = ('latitude', 'longitude', 'elevation', 'time', 'course',
'speed', 'magnetic_variation', 'geoid_height', 'name',
'comment', 'description', 'source', 'link', 'link_text',
'symbol', 'type', 'type_of_gpx_fix', 'satellites',
'horizontal_dilution', 'vertical_dilution',
'position_dilution', 'age_of_dgps_data', 'dgps_id',
'link_type', 'extensions')
def __init__(self, latitude=None, longitude=None, elevation=None, time=None, symbol=None, comment=None,
horizontal_dilution=None, vertical_dilution=None, position_dilution=None, speed=None,
name=None):
mod_geo.Location.__init__(self, latitude, longitude, elevation)
self.latitude = latitude
self.longitude = longitude
self.elevation = elevation
self.time = time
self.course = None
self.speed = speed
self.magnetic_variation = None
self.geoid_height = None
self.name = name
self.comment = comment
self.description = None
self.source = None
self.link = None
self.link_text = None
self.link_type = None
self.symbol = symbol
self.type = None
self.type_of_gpx_fix = None
self.satellites = None
self.horizontal_dilution = horizontal_dilution
self.vertical_dilution = vertical_dilution
self.position_dilution = position_dilution
self.age_of_dgps_data = None
self.dgps_id = None
self.extensions = None
def __repr__(self):
representation = '%s, %s' % (self.latitude, self.longitude)
for attribute in 'elevation', 'time', 'symbol', 'comment', 'horizontal_dilution', \
'vertical_dilution', 'position_dilution', 'speed', 'name':
value = getattr(self, attribute)
if value is not None:
representation += ', %s=%s' % (attribute, repr(value))
return 'GPXTrackPoint(%s)' % representation
def adjust_time(self, delta):
"""
Adjusts the time of the point by the specified delta
Parameters
----------
delta : datetime.timedelta
Positive time delta will adjust time into the future
Negative time delta will adjust time into the past
"""
if self.time:
self.time += delta
def remove_time(self):
""" Will remove time metadata. """
self.time = None
def time_difference(self, track_point):
"""
Get time difference between specified point and this point.
Parameters
----------
track_point : GPXTrackPoint
Returns
----------
time_difference : float
Time difference returned in seconds
"""
if not self.time or not track_point or not track_point.time:
return None
time_1 = self.time
time_2 = track_point.time
if time_1 == time_2:
return 0
if time_1 > time_2:
delta = time_1 - time_2
else:
delta = time_2 - time_1
return mod_utils.total_seconds(delta)
def speed_between(self, track_point):
"""
Compute the speed between specified point and this point.
NOTE: This is a computed speed, not the GPXTrackPoint speed that comes
the GPX file.
Parameters
----------
track_point : GPXTrackPoint
Returns
----------
speed : float
Speed returned in meters/second
"""
if not track_point:
return None
seconds = self.time_difference(track_point)
length = self.distance_3d(track_point)
if not length:
length = self.distance_2d(track_point)
if not seconds or length is None:
return None
return length / float(seconds)
def __str__(self):
return '[trkpt:%s,%s@%s@%s]' % (self.latitude, self.longitude, self.elevation, self.time)
def __hash__(self):
return mod_utils.hash_object(self, self.__slots__)
class GPXTrackSegment:
gpx_10_fields = [
mod_gpxfield.GPXComplexField('points', tag='trkpt', classs=GPXTrackPoint, is_list=True),
]
gpx_11_fields = [
mod_gpxfield.GPXComplexField('points', tag='trkpt', classs=GPXTrackPoint, is_list=True),
mod_gpxfield.GPXExtensionsField('extensions'),
]
__slots__ = ('points', 'extensions', )
def __init__(self, points=None):
self.points = points if points else []
self.extensions = None
def simplify(self, max_distance=None):
"""
Simplify using the Ramer-Douglas-Peucker algorithm: http://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm
"""
if not max_distance:
max_distance = 10
self.points = mod_geo.simplify_polyline(self.points, max_distance)
def reduce_points(self, min_distance):
"""
Reduces the number of points in the track segment. Segment points will
be updated in place.
Parameters
----------
min_distance : float
The minimum separation in meters between points
"""
reduced_points = []
for point in self.points:
if reduced_points:
distance = reduced_points[-1].distance_3d(point)
if distance >= min_distance:
reduced_points.append(point)
else:
# Leave first point:
reduced_points.append(point)
self.points = reduced_points
def _find_next_simplified_point(self, pos, max_distance):
for candidate in range(pos + 1, len(self.points) - 1):
for i in range(pos + 1, candidate):
d = mod_geo.distance_from_line(self.points[i],
self.points[pos],
self.points[candidate])
if d > max_distance:
return candidate - 1
return None
def adjust_time(self, delta):
"""
Adjusts the time of all points in the segment by the specified delta
Parameters
----------
delta : datetime.timedelta
Positive time delta will adjust point times into the future
Negative time delta will adjust point times into the past
"""
for track_point in self.points:
track_point.adjust_time(delta)
def remove_time(self):
""" Removes time data for all points in the segment. """
for track_point in self.points:
track_point.remove_time()
def remove_elevation(self):
""" Removes elevation data for all points in the segment. """
for track_point in self.points:
track_point.remove_elevation()
def length_2d(self):
"""
Computes 2-dimensional length (meters) of segment (only latitude and
longitude, no elevation).
Returns
----------
length : float
Length returned in meters
"""
return mod_geo.length_2d(self.points)
def length_3d(self):
"""
Computes 3-dimensional length of segment (latitude, longitude, and
elevation).
Returns
----------
length : float
Length returned in meters
"""
return mod_geo.length_3d(self.points)
def move(self, location_delta):
"""
Moves each point in the segment.
Parameters
----------
location_delta: LocationDelta object
Delta (distance/angle or lat/lon offset to apply each point in the
segment
"""
for track_point in self.points:
track_point.move(location_delta)
def walk(self, only_points=False):
"""
Generator for iterating over segment points
Parameters
----------
only_points: boolean
Only yield points (no index yielded)
Yields
------
point: GPXTrackPoint
A point in the sement
point_no: int
Not included in yield if only_points is true
"""
for point_no, point in enumerate(self.points):
if only_points:
yield point
else:
yield point, point_no
def get_points_no(self):
"""
Gets the number of points in segment.
Returns
----------
num_points : integer
Number of points in segment
"""
if not self.points:
return 0
return len(self.points)
def split(self, point_no):
"""
Splits the segment into two parts. If one of the split segments is
empty it will not be added in the result. The segments will be split
in place.
Parameters
----------
point_no : integer
The index of the track point in the segment to split
"""
part_1 = self.points[:point_no + 1]
part_2 = self.points[point_no + 1:]
return GPXTrackSegment(part_1), GPXTrackSegment(part_2)
def join(self, track_segment):
""" Joins with another segment """
self.points += track_segment.points
def remove_point(self, point_no):
""" Removes a point specificed by index from the segment """
if point_no < 0 or point_no >= len(self.points):
return
part_1 = self.points[:point_no]
part_2 = self.points[point_no + 1:]
self.points = part_1 + part_2
def get_moving_data(self, stopped_speed_threshold=None):
"""
Return a tuple of (moving_time, stopped_time, moving_distance,
stopped_distance, max_speed) that may be used for detecting the time
stopped, and max speed. Not that those values are not absolutely true,
because the "stopped" or "moving" information aren't saved in the segment.
Because of errors in the GPS recording, it may be good to calculate
them on a reduced and smoothed version of the track.
Parameters
----------
stopped_speed_threshold : float
speeds (km/h) below this threshold are treated as if having no
movement. Default is 1 km/h.
Returns
----------
moving_data : MovingData : named tuple
moving_time : float
time (seconds) of segment in which movement was occuring
stopped_time : float
time (seconds) of segment in which no movement was occuring
stopped_distance : float
distance (meters) travelled during stopped times
moving_distance : float
distance (meters) travelled during moving times
max_speed : float
Maximum speed (m/s) during the segment.
"""
if not stopped_speed_threshold:
stopped_speed_threshold = DEFAULT_STOPPED_SPEED_THRESHOLD
moving_time = 0.
stopped_time = 0.
moving_distance = 0.
stopped_distance = 0.
speeds_and_distances = []
for i in range(1, len(self.points)):
previous = self.points[i - 1]
point = self.points[i]
# Won't compute max_speed for first and last because of common GPS
# recording errors, and because smoothing don't work well for those
# points:
if point.time and previous.time:
timedelta = point.time - previous.time
if point.elevation and previous.elevation:
distance = point.distance_3d(previous)
else:
distance = point.distance_2d(previous)
seconds = mod_utils.total_seconds(timedelta)
speed_kmh = 0
if seconds > 0:
# TODO: compute treshold in m/s instead this to kmh every time:
speed_kmh = (distance / 1000.) / (mod_utils.total_seconds(timedelta) / 60. ** 2)
#print speed, stopped_speed_threshold
if speed_kmh <= stopped_speed_threshold:
stopped_time += mod_utils.total_seconds(timedelta)
stopped_distance += distance
else:
moving_time += mod_utils.total_seconds(timedelta)
moving_distance += distance
if distance and moving_time:
speeds_and_distances.append((distance / mod_utils.total_seconds(timedelta), distance, ))
max_speed = None
if speeds_and_distances:
max_speed = mod_geo.calculate_max_speed(speeds_and_distances)
return MovingData(moving_time, stopped_time, moving_distance, stopped_distance, max_speed)
def get_time_bounds(self):
"""
Gets the time bound (start and end) of the segment.
returns
----------
time_bounds : TimeBounds named tuple
start_time : datetime
Start time of the first segment in track
end time : datetime
End time of the last segment in track
"""
start_time = None
end_time = None
for point in self.points:
if point.time:
if not start_time:
start_time = point.time
if point.time:
end_time = point.time
return TimeBounds(start_time, end_time)
def get_bounds(self):
"""
Gets the latitude and longitude bounds of the segment.
Returns
----------
bounds : Bounds named tuple
min_latitude : float
Minimum latitude of segment in decimal degrees [-90, 90]
max_latitude : float
Maxium latitude of segment in decimal degrees [-90, 90]
min_longitude : float
Minium longitude of segment in decimal degrees [-180, 180]
max_longitude : float
Maxium longitude of segment in decimal degrees [-180, 180]
"""
min_lat = None
max_lat = None
min_lon = None
max_lon = None
for point in self.points:
if min_lat is None or point.latitude < min_lat:
min_lat = point.latitude
if max_lat is None or point.latitude > max_lat:
max_lat = point.latitude
if min_lon is None or point.longitude < min_lon:
min_lon = point.longitude
if max_lon is None or point.longitude > max_lon:
max_lon = point.longitude
return GPXBounds(min_lat, max_lat, min_lon, max_lon)
def get_speed(self, point_no):
"""
Computes the speed at the specified point index.
Parameters
----------
point_no : integer
index of the point used to compute speed
Returns
----------
speed : float
Speed returned in m/s
"""
point = self.points[point_no]
previous_point = None
next_point = None
if 0 < point_no < len(self.points):
previous_point = self.points[point_no - 1]
if 0 < point_no < len(self.points) - 1:
next_point = self.points[point_no + 1]
#mod_logging.debug('previous: %s' % previous_point)
#mod_logging.debug('next: %s' % next_point)
speed_1 = point.speed_between(previous_point)
speed_2 = point.speed_between(next_point)
if speed_1:
speed_1 = abs(speed_1)
if speed_2:
speed_2 = abs(speed_2)
if speed_1 and speed_2:
return (speed_1 + speed_2) / 2.
if speed_1:
return speed_1
return speed_2
def add_elevation(self, delta):
"""
Adjusts elevation data for segment.
Parameters
----------
delta : float
Elevation delta in meters to apply to track
"""
mod_logging.debug('delta = %s' % delta)
if not delta:
return
for track_point in self.points:
if track_point.elevation is not None:
track_point.elevation += delta
def add_missing_data(self, get_data_function, add_missing_function):
"""
Calculate missing data.
Parameters
----------
get_data_function : object
Returns the data from point
add_missing_function : void
Function with the following arguments: array with points with missing data, the point before them (with data),
the point after them (with data), and distance ratios between points in the interval (the sum of distances ratios
will be 1)
"""
if not get_data_function:
raise GPXException('Invalid get_data_function: %s' % get_data_function)
if not add_missing_function:
raise GPXException('Invalid add_missing_function: %s' % add_missing_function)
# Points (*without* data) between two points (*with* data):
interval = []
# Point (*with* data) before and after the interval:
start_point = None
previous_point = None
for track_point in self.points:
data = get_data_function(track_point)
if data is None and previous_point:
if not start_point:
start_point = previous_point
interval.append(track_point)
else:
if interval:
distances_ratios = self._get_interval_distances_ratios(interval,
start_point, track_point)
add_missing_function(interval, start_point, track_point,
distances_ratios)
start_point = None
interval = []
previous_point = track_point
def _get_interval_distances_ratios(self, interval, start, end):
assert start, start
assert end, end
assert interval, interval
assert len(interval) > 0, interval
distances = []
distance_from_start = 0
previous_point = start
for point in interval:
distance_from_start += float(point.distance_3d(previous_point))
distances.append(distance_from_start)
previous_point = point
from_start_to_end = distances[-1] + interval[-1].distance_3d(end)
assert len(interval) == len(distances)
return list(map(
lambda distance: (distance / from_start_to_end) if from_start_to_end else 0,
distances))
def get_duration(self):
"""
Calculates duration or track segment
Returns
-------
duration: float
Duration in seconds
"""
if not self.points or len(self.points) < 2:
return 0
# Search for start:
first = self.points[0]
if not first.time:
first = self.points[1]
last = self.points[-1]
if not last.time:
last = self.points[-2]
if not last.time or not first.time:
mod_logging.debug('Can\'t find time')
return None
if last.time < first.time:
mod_logging.debug('Not enough time data')
return None
return mod_utils.total_seconds(last.time - first.time)
def get_uphill_downhill(self):
"""
Calculates the uphill and downhill elevation climbs for the track
segment. If elevation for some points is not found those are simply
ignored.
Returns
-------
uphill_downhill: UphillDownhill named tuple
uphill: float
Uphill elevation climbs in meters
downhill: float
Downhill elevation descent in meters
"""
if not self.points:
return UphillDownhill(0, 0)
elevations = list(map(lambda point: point.elevation, self.points))
uphill, downhill = mod_geo.calculate_uphill_downhill(elevations)
return UphillDownhill(uphill, downhill)
def get_elevation_extremes(self):
"""
Calculate elevation extremes of track segment
Returns
-------
min_max_elevation: MinimumMaximum named tuple
minimum: float
Minimum elevation in meters
maximum: float
Maximum elevation in meters
"""
if not self.points:
return MinimumMaximum(None, None)
elevations = map(lambda location: location.elevation, self.points)
elevations = filter(lambda elevation: elevation is not None, elevations)
elevations = list(elevations)
if len(elevations) == 0:
return MinimumMaximum(None, None)
return MinimumMaximum(min(elevations), max(elevations))
def get_location_at(self, time):
"""
Gets approx. location at given time. Note that, at the moment this
method returns an instance of GPXTrackPoint in the future -- this may
be a mod_geo.Location instance with approximated latitude, longitude
and elevation!
"""
if not self.points:
return None
if not time:
return None
first_time = self.points[0].time
last_time = self.points[-1].time
if not first_time and not last_time:
mod_logging.debug('No times for track segment')
return None
if not first_time <= time <= last_time:
mod_logging.debug('Not in track (search for:%s, start:%s, end:%s)' % (time, first_time, last_time))
return None
for point in self.points:
if point.time and time <= point.time:
# TODO: If between two points -- approx position!
# return mod_geo.Location(point.latitude, point.longitude)
return point
def get_nearest_location(self, location):
""" Return the (location, track_point_no) on this track segment """
if not self.points:
return None, None
result = None
current_distance = None
result_track_point_no = None
for i in range(len(self.points)):
track_point = self.points[i]
if not result:
result = track_point
else:
distance = track_point.distance_2d(location)
#print current_distance, distance
if not current_distance or distance < current_distance:
current_distance = distance
result = track_point
result_track_point_no = i
return result, result_track_point_no
def smooth(self, vertical=True, horizontal=False, remove_extremes=False):
""" "Smooths" the elevation graph. Can be called multiple times. """
if len(self.points) <= 3:
return
elevations = []
latitudes = []
longitudes = []
for point in self.points:
elevations.append(point.elevation)
latitudes.append(point.latitude)
longitudes.append(point.longitude)
avg_distance = 0
avg_elevation_delta = 1
if remove_extremes:
# compute the average distance between two points:
distances = []
elevations_delta = []
for i in range(len(self.points))[1:]:
distances.append(self.points[i].distance_2d(self.points[i - 1]))
elevation_1 = self.points[i].elevation
elevation_2 = self.points[i - 1].elevation
if elevation_1 is not None and elevation_2 is not None:
elevations_delta.append(abs(elevation_1 - elevation_2))
if distances:
avg_distance = 1.0 * sum(distances) / len(distances)
if elevations_delta:
avg_elevation_delta = 1.0 * sum(elevations_delta) / len(elevations_delta)
# If The point moved more than this number * the average distance between two
# points -- then is a candidate for deletion:
# TODO: Make this a method parameter
remove_2d_extremes_threshold = 1.75 * avg_distance
remove_elevation_extremes_threshold = avg_elevation_delta * 5 # TODO: Param
new_track_points = [self.points[0]]
for i in range(len(self.points))[1:-1]:
new_point = None
point_removed = False
if vertical and elevations[i - 1] and elevations[i] and elevations[i + 1]:
old_elevation = self.points[i].elevation
new_elevation = SMOOTHING_RATIO[0] * elevations[i - 1] + \
SMOOTHING_RATIO[1] * elevations[i] + \
SMOOTHING_RATIO[2] * elevations[i + 1]
if not remove_extremes:
self.points[i].elevation = new_elevation
if remove_extremes:
# The point must be enough distant to *both* neighbours:
d1 = abs(old_elevation - elevations[i - 1])
d2 = abs(old_elevation - elevations[i + 1])
#print d1, d2, remove_2d_extremes_threshold
# TODO: Remove extremes threshold is meant only for 2D, elevation must be
# computed in different way!
if min(d1, d2) < remove_elevation_extremes_threshold and abs(old_elevation - new_elevation) < remove_2d_extremes_threshold:
new_point = self.points[i]
else:
#print 'removed elevation'
point_removed = True
else:
new_point = self.points[i]
else:
new_point = self.points[i]
if horizontal:
old_latitude = self.points[i].latitude
new_latitude = SMOOTHING_RATIO[0] * latitudes[i - 1] + \
SMOOTHING_RATIO[1] * latitudes[i] + \
SMOOTHING_RATIO[2] * latitudes[i + 1]
old_longitude = self.points[i].longitude
new_longitude = SMOOTHING_RATIO[0] * longitudes[i - 1] + \
SMOOTHING_RATIO[1] * longitudes[i] + \
SMOOTHING_RATIO[2] * longitudes[i + 1]
if not remove_extremes:
self.points[i].latitude = new_latitude
self.points[i].longitude = new_longitude
# TODO: This is not ideal.. Because if there are points A, B and C on the same
# line but B is very close to C... This would remove B (and possibly) A even though
# it is not an extreme. This is the reason for this algorithm:
d1 = mod_geo.distance(latitudes[i - 1], longitudes[i - 1], None, latitudes[i], longitudes[i], None)
d2 = mod_geo.distance(latitudes[i + 1], longitudes[i + 1], None, latitudes[i], longitudes[i], None)
d = mod_geo.distance(latitudes[i - 1], longitudes[i - 1], None, latitudes[i + 1], longitudes[i + 1], None)
#print d1, d2, d, remove_extremes
if d1 + d2 > d * 1.5 and remove_extremes:
d = mod_geo.distance(old_latitude, old_longitude, None, new_latitude, new_longitude, None)
#print "d, threshold = ", d, remove_2d_extremes_threshold
if d < remove_2d_extremes_threshold:
new_point = self.points[i]
else:
#print 'removed 2d'
point_removed = True
else:
new_point = self.points[i]
if new_point and not point_removed:
new_track_points.append(new_point)
new_track_points.append(self.points[- 1])
#print 'len=', len(new_track_points)
self.points = new_track_points
def has_times(self):
"""
Returns if points in this segment contains timestamps.
The first point, the last point, and 75% of the points must have times
for this method to return true.
"""
if not self.points:
return True
# ... or otherwise one empty track segment would change the entire
# track's "has_times" status!
found = 0
for track_point in self.points:
if track_point.time:
found += 1
return len(self.points) > 2 and float(found) / float(len(self.points)) > .75
def has_elevations(self):
"""
Returns if points in this segment contains elevation.
The first point, the last point, and at least 75% of the points must
have elevation for this method to return true.
"""
if not self.points:
return True
# ... or otherwise one empty track segment would change the entire
# track's "has_times" status!
found = 0
for track_point in self.points:
if track_point.elevation:
found += 1
return len(self.points) > 2 and float(found) / float(len(self.points)) > .75
def __hash__(self):
return mod_utils.hash_object(self, self.__slots__)
def __repr__(self):
return 'GPXTrackSegment(points=[%s])' % ('...' if self.points else '')
def clone(self):
return mod_copy.deepcopy(self)
class GPXTrack:
gpx_10_fields = [
mod_gpxfield.GPXField('name'),
mod_gpxfield.GPXField('comment', 'cmt'),
mod_gpxfield.GPXField('description', 'desc'),
mod_gpxfield.GPXField('source', 'src'),
mod_gpxfield.GPXField('link', 'url'),
mod_gpxfield.GPXField('link_text', 'urlname'),
mod_gpxfield.GPXField('number', type=mod_gpxfield.INT_TYPE),
mod_gpxfield.GPXComplexField('segments', tag='trkseg', classs=GPXTrackSegment, is_list=True),
]
gpx_11_fields = [
mod_gpxfield.GPXField('name'),
mod_gpxfield.GPXField('comment', 'cmt'),
mod_gpxfield.GPXField('description', 'desc'),
mod_gpxfield.GPXField('source', 'src'),
'link',
mod_gpxfield.GPXField('link', attribute='href'),
mod_gpxfield.GPXField('link_text', tag='text'),
mod_gpxfield.GPXField('link_type', tag='type'),
'/link',
mod_gpxfield.GPXField('number', type=mod_gpxfield.INT_TYPE),
mod_gpxfield.GPXField('type'),
mod_gpxfield.GPXExtensionsField('extensions'),
mod_gpxfield.GPXComplexField('segments', tag='trkseg', classs=GPXTrackSegment, is_list=True),
]
__slots__ = ('name', 'comment', 'description', 'source', 'link',
'link_text', 'number', 'segments', 'link_type', 'type',
'extensions')
def __init__(self, name=None, description=None, number=None):
self.name = name
self.comment = None
self.description = description
self.source = None
self.link = None
self.link_text = None
self.number = number
self.segments = []
self.link_type = None
self.type = None
self.extensions = None
def simplify(self, max_distance=None):
"""
Simplify using the Ramer-Douglas-Peucker algorithm: http://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm
"""
for segment in self.segments:
segment.simplify(max_distance=max_distance)
def reduce_points(self, min_distance):
"""
Reduces the number of points in the track. Segment points will be
updated in place.
Parameters
----------
min_distance : float
The minimum separation in meters between points
"""
for segment in self.segments:
segment.reduce_points(min_distance)
def adjust_time(self, delta):
"""
Adjusts the time of all segments in the track by the specified delta
Parameters
----------
delta : datetime.timedelta
Positive time delta will adjust time into the future
Negative time delta will adjust time into the past
"""
for segment in self.segments:
segment.adjust_time(delta)
def remove_time(self):
""" Removes time data for all points in all segments of track. """
for segment in self.segments:
segment.remove_time()
def remove_elevation(self):
""" Removes elevation data for all points in all segments of track. """
for segment in self.segments:
segment.remove_elevation()
def remove_empty(self):
""" Removes empty segments in track """
result = []
for segment in self.segments:
if len(segment.points) > 0:
result.append(segment)
self.segments = result
def length_2d(self):
"""
Computes 2-dimensional length (meters) of track (only latitude and
longitude, no elevation). This is the sum of the 2D length of all
segments.
Returns
----------
length : float
Length returned in meters
"""
length = 0
for track_segment in self.segments:
d = track_segment.length_2d()
if d:
length += d
return length
def get_time_bounds(self):
"""
Gets the time bound (start and end) of the track.
Returns
----------
time_bounds : TimeBounds named tuple
start_time : datetime
Start time of the first segment in track
end time : datetime
End time of the last segment in track
"""
start_time = None
end_time = None
for track_segment in self.segments:
point_start_time, point_end_time = track_segment.get_time_bounds()
if not start_time and point_start_time:
start_time = point_start_time
if point_end_time:
end_time = point_end_time
return TimeBounds(start_time, end_time)
def get_bounds(self):
"""
Gets the latitude and longitude bounds of the track.
Returns
----------
bounds : Bounds named tuple
min_latitude : float
Minimum latitude of track in decimal degrees [-90, 90]
max_latitude : float
Maxium latitude of track in decimal degrees [-90, 90]
min_longitude : float
Minium longitude of track in decimal degrees [-180, 180]
max_longitude : float
Maxium longitude of track in decimal degrees [-180, 180]
"""
min_lat = None
max_lat = None
min_lon = None
max_lon = None
for track_segment in self.segments:
bounds = track_segment.get_bounds()
if not mod_utils.is_numeric(min_lat) or (bounds.min_latitude and bounds.min_latitude < min_lat):
min_lat = bounds.min_latitude
if not mod_utils.is_numeric(max_lat) or (bounds.max_latitude and bounds.max_latitude > max_lat):
max_lat = bounds.max_latitude
if not mod_utils.is_numeric(min_lon) or (bounds.min_longitude and bounds.min_longitude < min_lon):
min_lon = bounds.min_longitude
if not mod_utils.is_numeric(max_lon) or (bounds.max_longitude and bounds.max_longitude > max_lon):
max_lon = bounds.max_longitude
return GPXBounds(min_lat, max_lat, min_lon, max_lon)
def walk(self, only_points=False):
"""
Generator used to iterates through track
Parameters
----------
only_point s: boolean
Only yield points while walking
Yields
----------
point : GPXTrackPoint
Point in the track
segment_no : integer
Index of segment containint point. This is suppressed if only_points
is True.
point_no : integer
Index of point. This is suppressed if only_points is True.
"""
for segment_no, segment in enumerate(self.segments):
for point_no, point in enumerate(segment.points):
if only_points:
yield point
else:
yield point, segment_no, point_no
def get_points_no(self):
"""
Get the number of points in all segments in the track.
Returns
----------
num_points : integer
Number of points in track
"""
result = 0
for track_segment in self.segments:
result += track_segment.get_points_no()
return result
def length_3d(self):
"""
Computes 3-dimensional length of track (latitude, longitude, and
elevation). This is the sum of the 3D length of all segments.
Returns
----------
length : float
Length returned in meters
"""
length = 0
for track_segment in self.segments:
d = track_segment.length_3d()
if d:
length += d
return length
def split(self, track_segment_no, track_point_no):
"""
Splits one of the segments in the track in two parts. If one of the
split segments is empty it will not be added in the result. The
segments will be split in place.
Parameters
----------
track_segment_no : integer
The index of the segment to split
track_point_no : integer
The index of the track point in the segment to split
"""
new_segments = []
for i in range(len(self.segments)):
segment = self.segments[i]
if i == track_segment_no:
segment_1, segment_2 = segment.split(track_point_no)
if segment_1:
new_segments.append(segment_1)
if segment_2:
new_segments.append(segment_2)
else:
new_segments.append(segment)
self.segments = new_segments
def join(self, track_segment_no, track_segment_no_2=None):
"""
Joins two segments of this track. The segments will be split in place.
Parameters
----------
track_segment_no : integer
The index of the first segment to join
track_segment_no_2 : integer
The index of second segment to join. If track_segment_no_2 is not
provided,the join will be with the next segment after
track_segment_no.
"""
if not track_segment_no_2:
track_segment_no_2 = track_segment_no + 1
if track_segment_no_2 >= len(self.segments):
return
new_segments = []
for i in range(len(self.segments)):
segment = self.segments[i]
if i == track_segment_no:
second_segment = self.segments[track_segment_no_2]
segment.join(second_segment)
new_segments.append(segment)
elif i == track_segment_no_2:
# Nothing, it is already joined
pass
else:
new_segments.append(segment)
self.segments = new_segments
def get_moving_data(self, stopped_speed_threshold=None):
"""
Return a tuple of (moving_time, stopped_time, moving_distance,
stopped_distance, max_speed) that may be used for detecting the time
stopped, and max speed. Not that those values are not absolutely true,
because the "stopped" or "moving" information aren't saved in the track.
Because of errors in the GPS recording, it may be good to calculate
them on a reduced and smoothed version of the track.
Parameters
----------
stopped_speed_threshold : float
speeds (km/h) below this threshold are treated as if having no
movement. Default is 1 km/h.
Returns
----------
moving_data : MovingData : named tuple
moving_time : float
time (seconds) of track in which movement was occuring
stopped_time : float
time (seconds) of track in which no movement was occuring
stopped_distance : float
distance (meters) travelled during stopped times
moving_distance : float
distance (meters) travelled during moving times
max_speed : float
Maximum speed (m/s) during the track.
"""
moving_time = 0.
stopped_time = 0.
moving_distance = 0.
stopped_distance = 0.
max_speed = 0.
for segment in self.segments:
track_moving_time, track_stopped_time, track_moving_distance, track_stopped_distance, track_max_speed = segment.get_moving_data(stopped_speed_threshold)
moving_time += track_moving_time
stopped_time += track_stopped_time
moving_distance += track_moving_distance
stopped_distance += track_stopped_distance
if track_max_speed is not None and track_max_speed > max_speed:
max_speed = track_max_speed
return MovingData(moving_time, stopped_time, moving_distance, stopped_distance, max_speed)
def add_elevation(self, delta):
"""
Adjusts elevation data for track.
Parameters
----------
delta : float
Elevation delta in meters to apply to track
"""
for track_segment in self.segments:
track_segment.add_elevation(delta)
def add_missing_data(self, get_data_function, add_missing_function):
for track_segment in self.segments:
track_segment.add_missing_data(get_data_function, add_missing_function)
def move(self, location_delta):
"""
Moves each point in the track.
Parameters
----------
location_delta: LocationDelta object
Delta (distance/angle or lat/lon offset to apply each point in each
segment of the track
"""
for track_segment in self.segments:
track_segment.move(location_delta)
def get_duration(self):
"""
Calculates duration or track
Returns
-------
duration: float
Duration in seconds or None if any time data is missing
"""
if not self.segments:
return 0
result = 0
for track_segment in self.segments:
duration = track_segment.get_duration()
if duration or duration == 0:
result += duration
elif duration is None:
return None
return result
def get_uphill_downhill(self):
"""
Calculates the uphill and downhill elevation climbs for the track.
If elevation for some points is not found those are simply ignored.
Returns
-------
uphill_downhill: UphillDownhill named tuple
uphill: float
Uphill elevation climbs in meters
downhill: float
Downhill elevation descent in meters
"""
if not self.segments:
return UphillDownhill(0, 0)
uphill = 0
downhill = 0
for track_segment in self.segments:
current_uphill, current_downhill = track_segment.get_uphill_downhill()
uphill += current_uphill
downhill += current_downhill
return UphillDownhill(uphill, downhill)
def get_location_at(self, time):
"""
Gets approx. location at given time. Note that, at the moment this
method returns an instance of GPXTrackPoint in the future -- this may
be a mod_geo.Location instance with approximated latitude, longitude
and elevation!
"""
result = []
for track_segment in self.segments:
location = track_segment.get_location_at(time)
if location:
result.append(location)
return result
def get_elevation_extremes(self):
"""
Calculate elevation extremes of track
Returns
-------
min_max_elevation: MinimumMaximum named tuple
minimum: float
Minimum elevation in meters
maximum: float
Maximum elevation in meters
"""
if not self.segments:
return MinimumMaximum(None, None)
elevations = []
for track_segment in self.segments:
(_min, _max) = track_segment.get_elevation_extremes()
if _min is not None:
elevations.append(_min)
if _max is not None:
elevations.append(_max)
if len(elevations) == 0:
return MinimumMaximum(None, None)
return MinimumMaximum(min(elevations), max(elevations))
def get_center(self):
"""
Get the center of the route.
Returns
-------
center: Location
latitude: latitude of center in degrees
longitude: longitude of center in degrees
elevation: not calculated here
"""
if not self.segments:
return None
sum_lat = 0
sum_lon = 0
n = 0
for track_segment in self.segments:
for point in track_segment.points:
n += 1.
sum_lat += point.latitude
sum_lon += point.longitude
if not n:
return mod_geo.Location(float(0), float(0))
return mod_geo.Location(latitude=sum_lat / n, longitude=sum_lon / n)
def smooth(self, vertical=True, horizontal=False, remove_extremes=False):
""" See: GPXTrackSegment.smooth() """
for track_segment in self.segments:
track_segment.smooth(vertical, horizontal, remove_extremes)
def has_times(self):
""" See GPXTrackSegment.has_times() """
if not self.segments:
return None
result = True
for track_segment in self.segments:
result = result and track_segment.has_times()
return result
def has_elevations(self):
""" Returns true if track data has elevation for all segments """
if not self.segments:
return None
result = True
for track_segment in self.segments:
result = result and track_segment.has_elevations()
return result
def get_nearest_location(self, location):
""" Returns (location, track_segment_no, track_point_no) for nearest location on track """
if not self.segments:
return None
result = None
distance = None
result_track_segment_no = None
result_track_point_no = None
for i in range(len(self.segments)):
track_segment = self.segments[i]
nearest_location, track_point_no = track_segment.get_nearest_location(location)
nearest_location_distance = None
if nearest_location:
nearest_location_distance = nearest_location.distance_2d(location)
if not distance or nearest_location_distance < distance:
if nearest_location:
distance = nearest_location_distance
result = nearest_location
result_track_segment_no = i
result_track_point_no = track_point_no
return result, result_track_segment_no, result_track_point_no
def clone(self):
return mod_copy.deepcopy(self)
def __hash__(self):
return mod_utils.hash_object(self, self.__slots__)
def __repr__(self):
representation = ''
for attribute in 'name', 'description', 'number':
value = getattr(self, attribute)
if value is not None:
representation += '%s%s=%s' % (', ' if representation else '', attribute, repr(value))
representation += '%ssegments=%s' % (', ' if representation else '', repr(self.segments))
return 'GPXTrack(%s)' % representation
class GPX:
gpx_10_fields = [
mod_gpxfield.GPXField('version', attribute=True),
mod_gpxfield.GPXField('creator', attribute=True),
mod_gpxfield.GPXField('name'),
mod_gpxfield.GPXField('description', 'desc'),
mod_gpxfield.GPXField('author_name', 'author'),
mod_gpxfield.GPXField('author_email', 'email'),
mod_gpxfield.GPXField('link', 'url'),
mod_gpxfield.GPXField('link_text', 'urlname'),
mod_gpxfield.GPXField('time', type=mod_gpxfield.TIME_TYPE),
mod_gpxfield.GPXField('keywords'),
mod_gpxfield.GPXComplexField('bounds', classs=GPXBounds),
mod_gpxfield.GPXComplexField('waypoints', classs=GPXWaypoint, tag='wpt', is_list=True),
mod_gpxfield.GPXComplexField('routes', classs=GPXRoute, tag='rte', is_list=True),
mod_gpxfield.GPXComplexField('tracks', classs=GPXTrack, tag='trk', is_list=True),
]
gpx_11_fields = [
mod_gpxfield.GPXField('version', attribute=True),
mod_gpxfield.GPXField('creator', attribute=True),
'metadata',
mod_gpxfield.GPXField('name', 'name'),
mod_gpxfield.GPXField('description', 'desc'),
'author',
mod_gpxfield.GPXField('author_name', 'name'),
mod_gpxfield.GPXEmailField('author_email', 'email'),
'link',
mod_gpxfield.GPXField('author_link', attribute='href'),
mod_gpxfield.GPXField('author_link_text', tag='text'),
mod_gpxfield.GPXField('author_link_type', tag='type'),
'/link',
'/author',
'copyright',
mod_gpxfield.GPXField('copyright_author', attribute='author'),
mod_gpxfield.GPXField('copyright_year', tag='year'),
mod_gpxfield.GPXField('copyright_license', tag='license'),
'/copyright',
'link',
mod_gpxfield.GPXField('link', attribute='href'),
mod_gpxfield.GPXField('link_text', tag='text'),
mod_gpxfield.GPXField('link_type', tag='type'),
'/link',
mod_gpxfield.GPXField('time', type=mod_gpxfield.TIME_TYPE),
mod_gpxfield.GPXField('keywords'),
mod_gpxfield.GPXComplexField('bounds', classs=GPXBounds),
mod_gpxfield.GPXExtensionsField('metadata_extensions', tag='extensions'),
'/metadata',
mod_gpxfield.GPXComplexField('waypoints', classs=GPXWaypoint, tag='wpt', is_list=True),
mod_gpxfield.GPXComplexField('routes', classs=GPXRoute, tag='rte', is_list=True),
mod_gpxfield.GPXComplexField('tracks', classs=GPXTrack, tag='trk', is_list=True),
mod_gpxfield.GPXExtensionsField('extensions'),
]
__slots__ = ('version', 'creator', 'name', 'description', 'author_name',
'author_email', 'link', 'link_text', 'time', 'keywords',
'bounds', 'waypoints', 'routes', 'tracks', 'author_link',
'author_link_text', 'author_link_type', 'copyright_author',
'copyright_year', 'copyright_license', 'link_type',
'metadata_extensions', 'extensions')
def __init__(self):
self.version = None
self.creator = None
self.name = None
self.description = None
self.link = None
self.link_text = None
self.link_type = None
self.time = None
self.keywords = None
self.bounds = None
self.author_name = None
self.author_email = None
self.author_link = None
self.author_link_text = None
self.author_link_type = None
self.copyright_author = None
self.copyright_year = None
self.copyright_license = None
self.metadata_extensions = None
self.extensions = None
self.waypoints = []
self.routes = []
self.tracks = []
def simplify(self, max_distance=None):
"""
Simplify using the Ramer-Douglas-Peucker algorithm: http://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm
"""
for track in self.tracks:
track.simplify(max_distance=max_distance)
def reduce_points(self, max_points_no=None, min_distance=None):
"""
Reduces the number of points. Points will be updated in place.
Parameters
----------
max_points : int
The maximum number of points to include in the GPX
min_distance : float
The minimum separation in meters between points
"""
if max_points_no is None and min_distance is None:
raise ValueError("Either max_point_no or min_distance must be supplied")
if max_points_no is not None and max_points_no < 2:
raise ValueError("max_points_no must be greater than or equal to 2")
points_no = len(list(self.walk()))
if max_points_no is not None and points_no <= max_points_no:
# No need to reduce points only if no min_distance is specified:
if not min_distance:
return
length = self.length_3d()
min_distance = min_distance or 0
max_points_no = max_points_no or 1000000000
min_distance = max(min_distance, mod_math.ceil(length / float(max_points_no)))
for track in self.tracks:
track.reduce_points(min_distance)
# TODO
mod_logging.debug('Track reduced to %s points' % self.get_track_points_no())
def adjust_time(self, delta):
"""
Adjusts the time of all points in all of the segments of all tracks by
the specified delta.
Parameters
----------
delta : datetime.timedelta
Positive time delta will adjust times into the future
Negative time delta will adjust times into the past
"""
if self.time:
self.time += delta
for track in self.tracks:
track.adjust_time(delta)
def remove_time(self):
""" Removes time data. """
for track in self.tracks:
track.remove_time()
def remove_elevation(self, tracks=True, routes=False, waypoints=False):
""" Removes elevation data. """
if tracks:
for track in self.tracks:
track.remove_elevation()
if routes:
for route in self.routes:
route.remove_elevation()
if waypoints:
for waypoint in self.waypoints:
waypoint.remove_elevation()
def get_time_bounds(self):
"""
Gets the time bounds (start and end) of the GPX file.
Returns
----------
time_bounds : TimeBounds named tuple
start_time : datetime
Start time of the first segment in track
end time : datetime
End time of the last segment in track
"""
start_time = None
end_time = None
for track in self.tracks:
track_start_time, track_end_time = track.get_time_bounds()
if not start_time:
start_time = track_start_time
if track_end_time:
end_time = track_end_time
return TimeBounds(start_time, end_time)
def get_bounds(self):
"""
Gets the latitude and longitude bounds of the GPX file.
Returns
----------
bounds : Bounds named tuple
min_latitude : float
Minimum latitude of track in decimal degrees [-90, 90]
max_latitude : float
Maxium latitude of track in decimal degrees [-90, 90]
min_longitude : float
Minium longitude of track in decimal degrees [-180, 180]
max_longitude : float
Maxium longitude of track in decimal degrees [-180, 180]
"""
min_lat = None
max_lat = None
min_lon = None
max_lon = None
for track in self.tracks:
bounds = track.get_bounds()
if not mod_utils.is_numeric(min_lat) or bounds.min_latitude < min_lat:
min_lat = bounds.min_latitude
if not mod_utils.is_numeric(max_lat) or bounds.max_latitude > max_lat:
max_lat = bounds.max_latitude
if not mod_utils.is_numeric(min_lon) or bounds.min_longitude < min_lon:
min_lon = bounds.min_longitude
if not mod_utils.is_numeric(max_lon) or bounds.max_longitude > max_lon:
max_lon = bounds.max_longitude
return GPXBounds(min_lat, max_lat, min_lon, max_lon)
def get_points_no(self):
"""
Get the number of points in all segments of all track.
Returns
----------
num_points : integer
Number of points in GPX
"""
result = 0
for track in self.tracks:
result += track.get_points_no()
return result
def refresh_bounds(self):
"""
Compute bounds and reload min_latitude, max_latitude, min_longitude
and max_longitude properties of this object
"""
bounds = self.get_bounds()
self.bounds = bounds
def smooth(self, vertical=True, horizontal=False, remove_extremes=False):
""" See GPXTrackSegment.smooth(...) """
for track in self.tracks:
track.smooth(vertical=vertical, horizontal=horizontal, remove_extremes=remove_extremes)
def remove_empty(self):
""" Removes segments, routes """
routes = []
for route in self.routes:
if len(route.points) > 0:
routes.append(route)
self.routes = routes
for track in self.tracks:
track.remove_empty()
def get_moving_data(self, stopped_speed_threshold=None):
"""
Return a tuple of (moving_time, stopped_time, moving_distance, stopped_distance, max_speed)
that may be used for detecting the time stopped, and max speed. Not that those values are not
absolutely true, because the "stopped" or "moving" information aren't saved in the track.
Because of errors in the GPS recording, it may be good to calculate them on a reduced and
smoothed version of the track. Something like this:
cloned_gpx = gpx.clone()
cloned_gpx.reduce_points(2000, min_distance=10)
cloned_gpx.smooth(vertical=True, horizontal=True)
cloned_gpx.smooth(vertical=True, horizontal=False)
moving_time, stopped_time, moving_distance, stopped_distance, max_speed_ms = cloned_gpx.get_moving_data
max_speed_kmh = max_speed_ms * 60. ** 2 / 1000.
Experiment with your own variations to get the values you expect.
Max speed is in m/s.
"""
moving_time = 0.
stopped_time = 0.
moving_distance = 0.
stopped_distance = 0.
max_speed = 0.
for track in self.tracks:
track_moving_time, track_stopped_time, track_moving_distance, track_stopped_distance, track_max_speed = track.get_moving_data(stopped_speed_threshold)
moving_time += track_moving_time
stopped_time += track_stopped_time
moving_distance += track_moving_distance
stopped_distance += track_stopped_distance
if track_max_speed > max_speed:
max_speed = track_max_speed
return MovingData(moving_time, stopped_time, moving_distance, stopped_distance, max_speed)
def split(self, track_no, track_segment_no, track_point_no):
"""
Splits one of the segments of a track in two parts. If one of the
split segments is empty it will not be added in the result. The
segments will be split in place.
Parameters
----------
track_no : integer
The index of the track to split
track_segment_no : integer
The index of the segment to split
track_point_no : integer
The index of the track point in the segment to split
"""
track = self.tracks[track_no]
track.split(track_segment_no=track_segment_no, track_point_no=track_point_no)
def length_2d(self):
"""
Computes 2-dimensional length of the GPX file (only latitude and
longitude, no elevation). This is the sum of 3D length of all segments
in all tracks.
Returns
----------
length : float
Length returned in meters
"""
result = 0
for track in self.tracks:
length = track.length_2d()
if length or length == 0:
result += length
return result
def length_3d(self):
"""
Computes 3-dimensional length of the GPX file (latitude, longitude, and
elevation). This is the sum of 3D length of all segments in all tracks.
Returns
----------
length : float
Length returned in meters
"""
result = 0
for track in self.tracks:
length = track.length_3d()
if length or length == 0:
result += length
return result
def walk(self, only_points=False):
"""
Generator used to iterates through points in GPX file
Parameters
----------
only_point s: boolean
Only yield points while walking
Yields
----------
point : GPXTrackPoint
Point in the track
track_no : integer
Index of track containint point. This is suppressed if only_points
is True.
segment_no : integer
Index of segment containint point. This is suppressed if only_points
is True.
point_no : integer
Index of point. This is suppressed if only_points is True.
"""
for track_no, track in enumerate(self.tracks):
for segment_no, segment in enumerate(track.segments):
for point_no, point in enumerate(segment.points):
if only_points:
yield point
else:
yield point, track_no, segment_no, point_no
def get_track_points_no(self):
""" Number of track points, *without* route and waypoints """
result = 0
for track in self.tracks:
for segment in track.segments:
result += len(segment.points)
return result
def get_duration(self):
"""
Calculates duration of GPX file
Returns
-------
duration: float
Duration in seconds or None if time data is not fully populated.
"""
if not self.tracks:
return 0
result = 0
for track in self.tracks:
duration = track.get_duration()
if duration or duration == 0:
result += duration
elif duration is None:
return None
return result
def get_uphill_downhill(self):
"""
Calculates the uphill and downhill elevation climbs for the gpx file.
If elevation for some points is not found those are simply ignored.
Returns
-------
uphill_downhill: UphillDownhill named tuple
uphill: float
Uphill elevation climbs in meters
downhill: float
Downhill elevation descent in meters
"""
if not self.tracks:
return UphillDownhill(0, 0)
uphill = 0
downhill = 0
for track in self.tracks:
current_uphill, current_downhill = track.get_uphill_downhill()
uphill += current_uphill
downhill += current_downhill
return UphillDownhill(uphill, downhill)
def get_location_at(self, time):
"""
Gets approx. location at given time. Note that, at the moment this
method returns an instance of GPXTrackPoint in the future -- this may
be a mod_geo.Location instance with approximated latitude, longitude
and elevation!
"""
result = []
for track in self.tracks:
locations = track.get_location_at(time)
for location in locations:
result.append(location)
return result
def get_elevation_extremes(self):
"""
Calculate elevation extremes of GPX file
Returns
-------
min_max_elevation: MinimumMaximum named tuple
minimum: float
Minimum elevation in meters
maximum: float
Maximum elevation in meters
"""
if not self.tracks:
return MinimumMaximum(None, None)
elevations = []
for track in self.tracks:
(_min, _max) = track.get_elevation_extremes()
if _min is not None:
elevations.append(_min)
if _max is not None:
elevations.append(_max)
if len(elevations) == 0:
return MinimumMaximum(None, None)
return MinimumMaximum(min(elevations), max(elevations))
def get_points_data(self, distance_2d=False):
"""
Returns a list of tuples containing the actual point, its distance from the start,
track_no, segment_no, and segment_point_no
"""
distance_from_start = 0
previous_point = None
# (point, distance_from_start) pairs:
points = []
for track_no in range(len(self.tracks)):
track = self.tracks[track_no]
for segment_no in range(len(track.segments)):
segment = track.segments[segment_no]
for point_no in range(len(segment.points)):
point = segment.points[point_no]
if previous_point and point_no > 0:
if distance_2d:
distance = point.distance_2d(previous_point)
else:
distance = point.distance_3d(previous_point)
distance_from_start += distance
points.append(PointData(point, distance_from_start, track_no, segment_no, point_no))
previous_point = point
return points
def get_nearest_locations(self, location, threshold_distance=0.01):
"""
Returns a list of locations of elements like
consisting of points where the location may be on the track
threshold_distance is the the minimum distance from the track
so that the point *may* be counted as to be "on the track".
For example 0.01 means 1% of the track distance.
"""
assert location
assert threshold_distance
result = []
points = self.get_points_data()
if not points:
return ()
distance = points[- 1][1]
threshold = distance * threshold_distance
min_distance_candidate = None
distance_from_start_candidate = None
track_no_candidate = None
segment_no_candidate = None
point_no_candidate = None
for point, distance_from_start, track_no, segment_no, point_no in points:
distance = location.distance_3d(point)
if distance < threshold:
if min_distance_candidate is None or distance < min_distance_candidate:
min_distance_candidate = distance
distance_from_start_candidate = distance_from_start
track_no_candidate = track_no
segment_no_candidate = segment_no
point_no_candidate = point_no
else:
if distance_from_start_candidate is not None:
result.append((distance_from_start_candidate, track_no_candidate, segment_no_candidate, point_no_candidate))
min_distance_candidate = None
distance_from_start_candidate = None
track_no_candidate = None
segment_no_candidate = None
point_no_candidate = None
if distance_from_start_candidate is not None:
result.append(NearestLocationData(distance_from_start_candidate, track_no_candidate, segment_no_candidate, point_no_candidate))
return result
def get_nearest_location(self, location):
""" Returns (location, track_no, track_segment_no, track_point_no) for the
nearest location on map """
if not self.tracks:
return None
result = None
distance = None
result_track_no = None
result_segment_no = None
result_point_no = None
for i in range(len(self.tracks)):
track = self.tracks[i]
nearest_location, track_segment_no, track_point_no = track.get_nearest_location(location)
nearest_location_distance = None
if nearest_location:
nearest_location_distance = nearest_location.distance_2d(location)
if not distance or nearest_location_distance < distance:
result = nearest_location
distance = nearest_location_distance
result_track_no = i
result_segment_no = track_segment_no
result_point_no = track_point_no
return NearestLocationData(result, result_track_no, result_segment_no, result_point_no)
def add_elevation(self, delta):
"""
Adjusts elevation data of GPX data.
Parameters
----------
delta : float
Elevation delta in meters to apply to GPX data
"""
for track in self.tracks:
track.add_elevation(delta)
def add_missing_data(self, get_data_function, add_missing_function):
for track in self.tracks:
track.add_missing_data(get_data_function, add_missing_function)
def add_missing_elevations(self):
def _add(interval, start, end, distances_ratios):
assert start
assert end
assert start.elevation is not None
assert end.elevation is not None
assert interval
assert len(interval) == len(distances_ratios)
for i in range(len(interval)):
interval[i].elevation = start.elevation + distances_ratios[i] * (end.elevation - start.elevation)
self.add_missing_data(get_data_function=lambda point: point.elevation,
add_missing_function=_add)
def add_missing_times(self):
def _add(interval, start, end, distances_ratios):
if (not start) or (not end) or (not start.time) or (not end.time):
return
assert interval
assert len(interval) == len(distances_ratios)
seconds_between = float(mod_utils.total_seconds(end.time - start.time))
for i in range(len(interval)):
point = interval[i]
ratio = distances_ratios[i]
point.time = start.time + mod_datetime.timedelta(
seconds=ratio * seconds_between)
self.add_missing_data(get_data_function=lambda point: point.time,
add_missing_function=_add)
def add_missing_speeds(self):
"""
The missing speeds are added to a segment.
The weighted harmonic mean is used to approximate the speed at
a :obj:'~.GPXTrackPoint'.
For this to work the speed of the first and last track point in a
segment needs to be known.
"""
def _add(interval, start, end, distances_ratios):
if (not start) or (not end) or (not start.time) or (not end.time):
return
assert interval
assert len(interval) == len(distances_ratios)
time_dist_before = (interval[0].time_difference(start),
interval[0].distance_3d(start))
time_dist_after = (interval[-1].time_difference(end),
interval[-1].distance_3d(end))
# Assemble list of times and distance to neighboring points
times_dists = [(interval[i].time_difference(interval[i+1]),
interval[i].distance_3d(interval[i+1]))
for i in range(len(interval) - 1)]
times_dists.insert(0, time_dist_before)
times_dists.append(time_dist_after)
for i, point in enumerate(interval):
time_left, dist_left = times_dists[i]
time_right, dist_right = times_dists[i+1]
point.speed = float(dist_left + dist_right) / (time_left + time_right)
self.add_missing_data(get_data_function=lambda point: point.speed,
add_missing_function=_add)
def move(self, location_delta):
"""
Moves each point in the gpx file (routes, waypoints, tracks).
Parameters
----------
location_delta: LocationDelta
LocationDelta to move each point
"""
for route in self.routes:
route.move(location_delta)
for waypoint in self.waypoints:
waypoint.move(location_delta)
for track in self.tracks:
track.move(location_delta)
def to_xml(self, version=None):
"""
FIXME: Note, this method will change self.version
"""
if not version:
if self.version:
version = self.version
else:
version = '1.0'
if version != '1.0' and version != '1.1':
raise GPXException('Invalid version %s' % version)
self.version = version
if not self.creator:
self.creator = 'gpx.py -- https://github.com/tkrajina/gpxpy'
v = version.replace('.', '/')
xml_attributes = {
'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns': 'http://www.topografix.com/GPX/%s' % v,
'xsi:schemaLocation': 'http://www.topografix.com/GPX/%s http://www.topografix.com/GPX/%s/gpx.xsd' % (v, v)
}
content = mod_gpxfield.gpx_fields_to_xml(self, 'gpx', version, custom_attributes=xml_attributes)
return '<?xml version="1.0" encoding="UTF-8"?>\n' + content.strip()
def has_times(self):
""" See GPXTrackSegment.has_times() """
if not self.tracks:
return None
result = True
for track in self.tracks:
result = result and track.has_times()
return result
def has_elevations(self):
""" See GPXTrackSegment.has_elevations()) """
if not self.tracks:
return None
result = True
for track in self.tracks:
result = result and track.has_elevations()
return result
def __hash__(self):
return mod_utils.hash_object(self, self.__slots__)
def __repr__(self):
representation = ''
for attribute in 'waypoints', 'routes', 'tracks':
value = getattr(self, attribute)
if value:
representation += '%s%s=%s' % (', ' if representation else '', attribute, repr(value))
return 'GPX(%s)' % representation
def clone(self):
return mod_copy.deepcopy(self)
# Add attributes and fill default values (lists or None) for all GPX elements:
for var_name in dir():
var_value = vars()[var_name]
if hasattr(var_value, 'gpx_10_fields') or hasattr(var_value, 'gpx_11_fields'):
#print('Check/fill %s' % var_value)
mod_gpxfield.gpx_check_slots_and_default_values(var_value)
|