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 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694
|
from warnings import warn
import numpy as np
import pandas as pd
from pandas import DataFrame, Series
import shapely
from shapely.geometry import MultiPoint, box
from shapely.geometry.base import BaseGeometry
from . import _compat as compat
from .array import GeometryArray, GeometryDtype, points_from_xy
def is_geometry_type(data):
"""Check if the data is of geometry dtype.
Does not include object array of shapely scalars.
"""
if isinstance(getattr(data, "dtype", None), GeometryDtype):
# GeometryArray, GeoSeries and Series[GeometryArray]
return True
else:
return False
def _delegate_binary_method(op, this, other, align, *args, **kwargs):
# type: (str, GeoSeries, GeoSeries) -> GeoSeries/Series
if align is None:
align = True
maybe_warn = True
else:
maybe_warn = False
this = this.geometry
# Use same alignment logic, regardless of if `other` is Series or GeoSeries.
if (
not isinstance(other, GeoPandasBase)
and isinstance(other, pd.Series)
and isinstance(other.dtype, GeometryDtype)
):
# Avoid circular imports by importing here.
import geopandas.geoseries
other = geopandas.geoseries.GeoSeries(other)
if isinstance(other, GeoPandasBase):
if align and not this.index.equals(other.index):
if maybe_warn:
warn(
"The indices of the left and right GeoSeries' are not equal, and "
"therefore they will be aligned (reordering and/or introducing "
"missing values) before executing the operation. If this alignment "
"is the desired behaviour, you can silence this warning by passing "
"'align=True'. If you don't want alignment and protect yourself of "
"accidentally aligning, you can pass 'align=False'.",
stacklevel=4,
)
this, other = this.align(other.geometry)
else:
other = other.geometry
a_this = GeometryArray(this.values)
other = GeometryArray(other.values)
elif isinstance(other, BaseGeometry):
a_this = GeometryArray(this.values)
else:
raise TypeError(type(this), type(other))
data = getattr(a_this, op)(other, *args, **kwargs)
return data, this.index
def _binary_geo(op, this, other, align, *args, **kwargs):
# type: (str, GeoSeries, GeoSeries) -> GeoSeries
"""Binary operation on GeoSeries objects that returns a GeoSeries."""
from .geoseries import GeoSeries
geoms, index = _delegate_binary_method(op, this, other, align, *args, **kwargs)
return GeoSeries(geoms, index=index, crs=this.crs)
def _binary_op(op, this, other, align, *args, **kwargs):
# type: (str, GeoSeries, GeoSeries, args/kwargs) -> Series[bool/float]
"""Binary operation on GeoSeries objects that returns a Series."""
data, index = _delegate_binary_method(op, this, other, align, *args, **kwargs)
return Series(data, index=index)
def _delegate_property(op, this):
# type: (str, GeoSeries) -> GeoSeries/Series
a_this = GeometryArray(this.geometry.values)
data = getattr(a_this, op)
if isinstance(data, GeometryArray):
from .geoseries import GeoSeries
return GeoSeries(data, index=this.index, crs=this.crs)
else:
return Series(data, index=this.index)
def _delegate_geo_method(op, this, **kwargs):
# type: (str, GeoSeries) -> GeoSeries
"""Unary operation that returns a GeoSeries."""
from .geodataframe import GeoDataFrame
from .geoseries import GeoSeries
if isinstance(this, GeoSeries):
klass, var_name = "GeoSeries", "gs"
elif isinstance(this, GeoDataFrame):
klass, var_name = "GeoDataFrame", "gdf"
else:
klass, var_name = this.__class__.__name__, "this"
for key, val in kwargs.items():
if isinstance(val, pd.Series):
if not val.index.equals(this.index):
raise ValueError(
f"Index of the Series passed as '{key}' does not match index of "
f"the {klass}. If you want both Series to be aligned, align them "
f"before passing them to this method as "
f"`{var_name}, {key} = {var_name}.align({key})`. If "
f"you want to ignore the index, pass the underlying array as "
f"'{key}' using `{key}.values`."
)
kwargs[key] = np.asarray(val)
a_this = GeometryArray(this.geometry.values)
data = getattr(a_this, op)(**kwargs)
return GeoSeries(data, index=this.index, crs=this.crs)
class GeoPandasBase:
@property
def area(self):
"""Return a ``Series`` containing the area of each geometry in the
``GeoSeries`` expressed in the units of the CRS.
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... Polygon([(10, 0), (10, 5), (0, 0)]),
... Polygon([(0, 0), (2, 2), (2, 0)]),
... LineString([(0, 0), (1, 1), (0, 1)]),
... Point(0, 1)
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 POLYGON ((10 0, 10 5, 0 0, 10 0))
2 POLYGON ((0 0, 2 2, 2 0, 0 0))
3 LINESTRING (0 0, 1 1, 0 1)
4 POINT (0 1)
dtype: geometry
>>> s.area
0 0.5
1 25.0
2 2.0
3 0.0
4 0.0
dtype: float64
See Also
--------
GeoSeries.length : measure length
Notes
-----
Area may be invalid for a geographic CRS using degrees as units;
use :meth:`GeoSeries.to_crs` to project geometries to a planar
CRS before using this function.
Every operation in GeoPandas is planar, i.e. the potential third
dimension is not taken into account.
"""
return _delegate_property("area", self)
@property
def crs(self):
"""The Coordinate Reference System (CRS) as a ``pyproj.CRS`` object.
Returns None if the CRS is not set, and to set the value it
:getter: Returns a ``pyproj.CRS`` or None. When setting, the value
can be anything accepted by
:meth:`pyproj.CRS.from_user_input() <pyproj.crs.CRS.from_user_input>`,
such as an authority string (eg "EPSG:4326") or a WKT string.
Examples
--------
>>> s.crs # doctest: +SKIP
<Geographic 2D CRS: EPSG:4326>
Name: WGS 84
Axis Info [ellipsoidal]:
- Lat[north]: Geodetic latitude (degree)
- Lon[east]: Geodetic longitude (degree)
Area of Use:
- name: World
- bounds: (-180.0, -90.0, 180.0, 90.0)
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich
See Also
--------
GeoSeries.set_crs : assign CRS
GeoSeries.to_crs : re-project to another CRS
"""
return self.geometry.values.crs
@crs.setter
def crs(self, value):
"""Set the value of the crs."""
self.geometry.values.crs = value
@property
def geom_type(self):
"""
Returns a ``Series`` of strings specifying the `Geometry Type` of each
object.
Examples
--------
>>> from shapely.geometry import Point, Polygon, LineString
>>> d = {'geometry': [Point(2, 1), Polygon([(0, 0), (1, 1), (1, 0)]),
... LineString([(0, 0), (1, 1)])]}
>>> gdf = geopandas.GeoDataFrame(d, crs="EPSG:4326")
>>> gdf.geom_type
0 Point
1 Polygon
2 LineString
dtype: object
"""
return _delegate_property("geom_type", self)
@property
def type(self):
"""Return the geometry type of each geometry in the GeoSeries."""
return self.geom_type
@property
def length(self):
"""Return a ``Series`` containing the length of each geometry
expressed in the units of the CRS.
In the case of a (Multi)Polygon it measures the length
of its exterior (i.e. perimeter).
Examples
--------
>>> from shapely.geometry import Polygon, LineString, MultiLineString, Point, \
GeometryCollection
>>> s = geopandas.GeoSeries(
... [
... LineString([(0, 0), (1, 1), (0, 1)]),
... LineString([(10, 0), (10, 5), (0, 0)]),
... MultiLineString([((0, 0), (1, 0)), ((-1, 0), (1, 0))]),
... Polygon([(0, 0), (1, 1), (0, 1)]),
... Point(0, 1),
... GeometryCollection([Point(1, 0), LineString([(10, 0), (10, 5), (0,\
0)])])
... ]
... )
>>> s
0 LINESTRING (0 0, 1 1, 0 1)
1 LINESTRING (10 0, 10 5, 0 0)
2 MULTILINESTRING ((0 0, 1 0), (-1 0, 1 0))
3 POLYGON ((0 0, 1 1, 0 1, 0 0))
4 POINT (0 1)
5 GEOMETRYCOLLECTION (POINT (1 0), LINESTRING (1...
dtype: geometry
>>> s.length
0 2.414214
1 16.180340
2 3.000000
3 3.414214
4 0.000000
5 16.180340
dtype: float64
See Also
--------
GeoSeries.area : measure area of a polygon
Notes
-----
Length may be invalid for a geographic CRS using degrees as units;
use :meth:`GeoSeries.to_crs` to project geometries to a planar
CRS before using this function.
Every operation in GeoPandas is planar, i.e. the potential third
dimension is not taken into account.
"""
return _delegate_property("length", self)
@property
def is_valid(self):
"""Return a ``Series`` of ``dtype('bool')`` with value ``True`` for
geometries that are valid.
Examples
--------
An example with one invalid polygon (a bowtie geometry crossing itself)
and one missing geometry:
>>> from shapely.geometry import Polygon
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... Polygon([(0,0), (1, 1), (1, 0), (0, 1)]), # bowtie geometry
... Polygon([(0, 0), (2, 2), (2, 0)]),
... None
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 POLYGON ((0 0, 1 1, 1 0, 0 1, 0 0))
2 POLYGON ((0 0, 2 2, 2 0, 0 0))
3 None
dtype: geometry
>>> s.is_valid
0 True
1 False
2 True
3 False
dtype: bool
See Also
--------
GeoSeries.is_valid_reason : reason for invalidity
"""
return _delegate_property("is_valid", self)
def is_valid_reason(self):
"""Return a ``Series`` of strings with the reason for invalidity of
each geometry.
Examples
--------
An example with one invalid polygon (a bowtie geometry crossing itself)
and one missing geometry:
>>> from shapely.geometry import Polygon
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... Polygon([(0,0), (1, 1), (1, 0), (0, 1)]), # bowtie geometry
... Polygon([(0, 0), (2, 2), (2, 0)]),
... None
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 POLYGON ((0 0, 1 1, 1 0, 0 1, 0 0))
2 POLYGON ((0 0, 2 2, 2 0, 0 0))
3 None
dtype: geometry
>>> s.is_valid_reason()
0 Valid Geometry
1 Self-intersection[0.5 0.5]
2 Valid Geometry
3 None
dtype: object
See Also
--------
GeoSeries.is_valid : detect invalid geometries
GeoSeries.make_valid : fix invalid geometries
"""
return Series(self.geometry.values.is_valid_reason(), index=self.index)
def is_valid_coverage(self, *, gap_width=0.0):
"""Return a ``bool`` indicating whether a ``GeoSeries`` forms a valid coverage.
A ``GeoSeries`` of valid polygons is considered a coverage if the polygons are:
* **Non-overlapping** - polygons do not overlap (their interiors do not
intersect)
* **Edge-Matched** - vertices along shared edges are identical
A valid coverage may contain holes (regions of no coverage). However, sometimes
it might be desirable to detect narrow gaps as invalidities in the coverage. The
``gap_width`` parameter allows to specify the maximum width of gaps to detect.
When gaps are detected, this method will return ``False`` and the
:meth:`coverage_invalid_edges` method can be used to find the edges of those
gaps.
Geometries that are not Polygon or MultiPolygon are ignored and an empty
LineString is returned.
Requires Shapely >= 2.1.
.. versionadded:: 1.1.0
Parameters
----------
gap_width : float, optional
The maximum width of gaps to detect, by default 0.0
Returns
-------
bool
Examples
--------
>>> from shapely import Polygon
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (1, 0), (0, 0)]),
... Polygon([(0, 0), (1, 1), (0, 1), (0, 0)]),
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 1 0, 0 0))
1 POLYGON ((0 0, 1 1, 0 1, 0 0))
dtype: geometry
>>> s.is_valid_coverage()
True
Violation of edge-matching:
>>> s2 = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (1, 0), (0, 0)]),
... Polygon([(0, 0), (0.5, 0.5), (1, 1), (0, 1), (0, 0)])
... ]
... )
>>> s2
0 POLYGON ((0 0, 1 1, 1 0, 0 0))
1 POLYGON ((0 0, 0.5 0.5, 1 1, 0 1, 0 0))
dtype: geometry
>>> s2.is_valid_coverage()
False
See Also
--------
GeoSeries.invalid_coverage_edges
GeoSeries.simplify_coverage
"""
return self.geometry.values.is_valid_coverage(gap_width=gap_width)
def invalid_coverage_edges(self, *, gap_width=0.0):
"""Return a ``GeoSeries`` containing edges causing invalid polygonal coverage.
This method returns (Multi)LineStrings showing the location of edges violating
polygonal coverage (if any) in each polygon in the input ``GeoSeries``.
A ``GeoSeries`` of valid polygons is considered a coverage if the polygons are:
* **Non-overlapping** - polygons do not overlap (their interiors do not
intersect)
* **Edge-Matched** - vertices along shared edges are identical
A valid coverage may contain holes (regions of no coverage). However, sometimes
it might be desirable to detect narrow gaps as invalidities in the coverage. The
``gap_width`` parameter allows to specify the maximum width of gaps to detect.
When gaps are detected, the :meth:`is_valid_coverage` method will return
``False`` and this method can be used to find the edges of those gaps.
Geometries that are not Polygon or MultiPolygon are ignored.
Requires Shapely >= 2.1.
.. versionadded:: 1.1.0
Parameters
----------
gap_width : float, optional
The maximum width of gaps to detect, by default 0.0
Returns
-------
GeoSeries
Examples
--------
Violation of edge-matching:
>>> from shapely import Polygon
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (1, 0), (0, 0)]),
... Polygon([(0, 0), (0.5, 0.5), (1, 1), (0, 1), (0, 0)])
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 1 0, 0 0))
1 POLYGON ((0 0, 0.5 0.5, 1 1, 0 1, 0 0))
dtype: geometry
>>> s.invalid_coverage_edges()
0 LINESTRING (0 0, 1 1)
1 LINESTRING (0 0, 0.5 0.5, 1 1)
dtype: geometry
See Also
--------
GeoSeries.is_valid_coverage
GeoSeries.simplify_coverage
"""
return _delegate_geo_method("invalid_coverage_edges", self, gap_width=gap_width)
@property
def is_empty(self):
"""
Returns a ``Series`` of ``dtype('bool')`` with value ``True`` for
empty geometries.
Examples
--------
An example of a GeoDataFrame with one empty point, one point and one missing
value:
>>> from shapely.geometry import Point
>>> d = {'geometry': [Point(), Point(2, 1), None]}
>>> gdf = geopandas.GeoDataFrame(d, crs="EPSG:4326")
>>> gdf
geometry
0 POINT EMPTY
1 POINT (2 1)
2 None
>>> gdf.is_empty
0 True
1 False
2 False
dtype: bool
See Also
--------
GeoSeries.isna : detect missing values
"""
return _delegate_property("is_empty", self)
def count_coordinates(self):
"""Return a ``Series`` containing the count of the number of coordinate pairs
in each geometry.
Examples
--------
An example of a GeoDataFrame with two line strings, one point and one None
value:
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... LineString([(0, 0), (1, 1), (1, -1), (0, 1)]),
... LineString([(0, 0), (1, 1), (1, -1)]),
... Point(0, 0),
... Polygon([(10, 10), (10, 20), (20, 20), (20, 10), (10, 10)]),
... None
... ]
... )
>>> s
0 LINESTRING (0 0, 1 1, 1 -1, 0 1)
1 LINESTRING (0 0, 1 1, 1 -1)
2 POINT (0 0)
3 POLYGON ((10 10, 10 20, 20 20, 20 10, 10 10))
4 None
dtype: geometry
>>> s.count_coordinates()
0 4
1 3
2 1
3 5
4 0
dtype: int32
See Also
--------
GeoSeries.get_coordinates : extract coordinates as a :class:`~pandas.DataFrame`
GoSeries.count_geometries : count the number of geometries in a collection
"""
return Series(self.geometry.values.count_coordinates(), index=self.index)
def count_geometries(self):
"""Return a ``Series`` containing the count of geometries in each multi-part
geometry.
For single-part geometry objects, this is always 1. For multi-part geometries,
like ``MultiPoint`` or ``MultiLineString``, it is the number of parts in the
geometry. For ``GeometryCollection``, it is the number of geometries direct
parts of the collection (the method does not recurse into collections within
collections).
Examples
--------
>>> from shapely.geometry import Point, MultiPoint, LineString, MultiLineString
>>> s = geopandas.GeoSeries(
... [
... MultiPoint([(0, 0), (1, 1), (1, -1), (0, 1)]),
... MultiLineString([((0, 0), (1, 1)), ((-1, 0), (1, 0))]),
... LineString([(0, 0), (1, 1), (1, -1)]),
... Point(0, 0),
... ]
... )
>>> s
0 MULTIPOINT ((0 0), (1 1), (1 -1), (0 1))
1 MULTILINESTRING ((0 0, 1 1), (-1 0, 1 0))
2 LINESTRING (0 0, 1 1, 1 -1)
3 POINT (0 0)
dtype: geometry
>>> s.count_geometries()
0 4
1 2
2 1
3 1
dtype: int32
See Also
--------
GeoSeries.count_coordinates : count the number of coordinates in a geometry
GeoSeries.count_interior_rings : count the number of interior rings
"""
return Series(self.geometry.values.count_geometries(), index=self.index)
def count_interior_rings(self):
"""Return a ``Series`` containing the count of the number of interior rings
in a polygonal geometry.
For non-polygonal geometries, this is always 0.
Examples
--------
>>> from shapely.geometry import Polygon, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon(
... [(0, 0), (0, 5), (5, 5), (5, 0)],
... [[(1, 1), (1, 4), (4, 4), (4, 1)]],
... ),
... Polygon(
... [(0, 0), (0, 5), (5, 5), (5, 0)],
... [
... [(1, 1), (1, 2), (2, 2), (2, 1)],
... [(3, 2), (3, 3), (4, 3), (4, 2)],
... ],
... ),
... Point(0, 1),
... ]
... )
>>> s
0 POLYGON ((0 0, 0 5, 5 5, 5 0, 0 0), (1 1, 1 4,...
1 POLYGON ((0 0, 0 5, 5 5, 5 0, 0 0), (1 1, 1 2,...
2 POINT (0 1)
dtype: geometry
>>> s.count_interior_rings()
0 1
1 2
2 0
dtype: int32
See Also
--------
GeoSeries.count_coordinates : count the number of coordinates in a geometry
GeoSeries.count_geometries : count the number of geometries in a collection
"""
return Series(self.geometry.values.count_interior_rings(), index=self.index)
@property
def is_simple(self):
"""Return a ``Series`` of ``dtype('bool')`` with value ``True`` for
geometries that do not cross themselves.
This is meaningful only for `LineStrings` and `LinearRings`.
Examples
--------
>>> from shapely.geometry import LineString
>>> s = geopandas.GeoSeries(
... [
... LineString([(0, 0), (1, 1), (1, -1), (0, 1)]),
... LineString([(0, 0), (1, 1), (1, -1)]),
... ]
... )
>>> s
0 LINESTRING (0 0, 1 1, 1 -1, 0 1)
1 LINESTRING (0 0, 1 1, 1 -1)
dtype: geometry
>>> s.is_simple
0 False
1 True
dtype: bool
"""
return _delegate_property("is_simple", self)
@property
def is_ring(self):
"""Return a ``Series`` of ``dtype('bool')`` with value ``True`` for
features that are closed.
When constructing a LinearRing, the sequence of coordinates may be
explicitly closed by passing identical values in the first and last indices.
Otherwise, the sequence will be implicitly closed by copying the first tuple
to the last index.
Examples
--------
>>> from shapely.geometry import LineString, LinearRing
>>> s = geopandas.GeoSeries(
... [
... LineString([(0, 0), (1, 1), (1, -1)]),
... LineString([(0, 0), (1, 1), (1, -1), (0, 0)]),
... LinearRing([(0, 0), (1, 1), (1, -1)]),
... ]
... )
>>> s
0 LINESTRING (0 0, 1 1, 1 -1)
1 LINESTRING (0 0, 1 1, 1 -1, 0 0)
2 LINEARRING (0 0, 1 1, 1 -1, 0 0)
dtype: geometry
>>> s.is_ring
0 False
1 True
2 True
dtype: bool
"""
return _delegate_property("is_ring", self)
@property
def is_ccw(self):
"""Return a ``Series`` of ``dtype('bool')`` with value ``True``
if a LineString or LinearRing is counterclockwise.
Note that there are no checks on whether lines are actually
closed and not self-intersecting, while this is a requirement
for ``is_ccw``. The recommended usage of this property for
LineStrings is ``GeoSeries.is_ccw & GeoSeries.is_simple`` and for
LinearRings ``GeoSeries.is_ccw & GeoSeries.is_valid``.
This property will return False for non-linear geometries and for
lines with fewer than 4 points (including the closing point).
Examples
--------
>>> from shapely.geometry import LineString, LinearRing, Point
>>> s = geopandas.GeoSeries(
... [
... LinearRing([(0, 0), (0, 1), (1, 1), (0, 0)]),
... LinearRing([(0, 0), (1, 1), (0, 1), (0, 0)]),
... LineString([(0, 0), (1, 1), (0, 1)]),
... Point(3, 3)
... ]
... )
>>> s
0 LINEARRING (0 0, 0 1, 1 1, 0 0)
1 LINEARRING (0 0, 1 1, 0 1, 0 0)
2 LINESTRING (0 0, 1 1, 0 1)
3 POINT (3 3)
dtype: geometry
>>> s.is_ccw
0 False
1 True
2 False
3 False
dtype: bool
"""
return _delegate_property("is_ccw", self)
@property
def is_closed(self):
"""Return a ``Series`` of ``dtype('bool')`` with value ``True``
if a LineString's or LinearRing's first and last points are equal.
Returns False for any other geometry type.
Examples
--------
>>> from shapely.geometry import LineString, Point, Polygon
>>> s = geopandas.GeoSeries(
... [
... LineString([(0, 0), (1, 1), (0, 1), (0, 0)]),
... LineString([(0, 0), (1, 1), (0, 1)]),
... Polygon([(0, 0), (0, 1), (1, 1), (0, 0)]),
... Point(3, 3)
... ]
... )
>>> s
0 LINESTRING (0 0, 1 1, 0 1, 0 0)
1 LINESTRING (0 0, 1 1, 0 1)
2 POLYGON ((0 0, 0 1, 1 1, 0 0))
3 POINT (3 3)
dtype: geometry
>>> s.is_closed
0 True
1 False
2 False
3 False
dtype: bool
"""
return _delegate_property("is_closed", self)
@property
def has_z(self):
"""Return a ``Series`` of ``dtype('bool')`` with value ``True`` for
features that have a z-component.
Notes
-----
Every operation in GeoPandas is planar, i.e. the potential third
dimension is not taken into account.
Examples
--------
>>> from shapely.geometry import Point
>>> s = geopandas.GeoSeries(
... [
... Point(0, 1),
... Point(0, 1, 2),
... ]
... )
>>> s
0 POINT (0 1)
1 POINT Z (0 1 2)
dtype: geometry
>>> s.has_z
0 False
1 True
dtype: bool
"""
return _delegate_property("has_z", self)
@property
def has_m(self):
"""Return a ``Series`` of ``dtype('bool')`` with value ``True`` for
features that have a m-component.
Requires Shapely >= 2.1.
.. versionadded:: 1.1.0
Examples
--------
>>> from shapely.geometry import Point
>>> s = geopandas.GeoSeries.from_wkt(
... [
... "POINT M (2 3 5)",
... "POINT Z (1 2 3)",
... "POINT (0 0)",
... ]
... )
>>> s
0 POINT M (2 3 5)
1 POINT Z (1 2 3)
2 POINT (0 0)
dtype: geometry
>>> s.has_m
0 True
1 False
2 False
dtype: bool
"""
return _delegate_property("has_m", self)
def get_precision(self):
"""Return a ``Series`` of the precision of each geometry.
If a precision has not been previously set, it will be 0, indicating regular
double precision coordinates are in use. Otherwise, it will return the precision
grid size that was set on a geometry.
Returns NaN for not-a-geometry values.
Examples
--------
>>> from shapely.geometry import Point
>>> s = geopandas.GeoSeries(
... [
... Point(0, 1),
... Point(0, 1, 2),
... Point(0, 1.5, 2),
... ]
... )
>>> s
0 POINT (0 1)
1 POINT Z (0 1 2)
2 POINT Z (0 1.5 2)
dtype: geometry
>>> s.get_precision()
0 0.0
1 0.0
2 0.0
dtype: float64
>>> s1 = s.set_precision(1)
>>> s1
0 POINT (0 1)
1 POINT Z (0 1 2)
2 POINT Z (0 2 2)
dtype: geometry
>>> s1.get_precision()
0 1.0
1 1.0
2 1.0
dtype: float64
See Also
--------
GeoSeries.set_precision : set precision grid size
"""
return Series(self.geometry.values.get_precision(), index=self.index)
def get_geometry(self, index):
"""Return the n-th geometry from a collection of geometries.
Parameters
----------
index : int or array_like
Position of a geometry to be retrieved within its collection
Returns
-------
GeoSeries
Notes
-----
Simple geometries act as collections of length 1. Any out-of-range index value
returns None.
Examples
--------
>>> from shapely.geometry import Point, MultiPoint, GeometryCollection
>>> s = geopandas.GeoSeries(
... [
... Point(0, 0),
... MultiPoint([(0, 0), (1, 1), (0, 1), (1, 0)]),
... GeometryCollection(
... [MultiPoint([(0, 0), (1, 1), (0, 1), (1, 0)]), Point(0, 1)]
... ),
... ]
... )
>>> s
0 POINT (0 0)
1 MULTIPOINT ((0 0), (1 1), (0 1), (1 0))
2 GEOMETRYCOLLECTION (MULTIPOINT ((0 0), (1 1), ...
dtype: geometry
>>> s.get_geometry(0)
0 POINT (0 0)
1 POINT (0 0)
2 MULTIPOINT ((0 0), (1 1), (0 1), (1 0))
dtype: geometry
>>> s.get_geometry(1)
0 None
1 POINT (1 1)
2 POINT (0 1)
dtype: geometry
>>> s.get_geometry(-1)
0 POINT (0 0)
1 POINT (1 0)
2 POINT (0 1)
dtype: geometry
"""
return _delegate_geo_method("get_geometry", self, index=index)
#
# Unary operations that return a GeoSeries
#
@property
def boundary(self):
"""Return a ``GeoSeries`` of lower dimensional objects representing
each geometry's set-theoretic `boundary`.
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(0, 0), (1, 1), (1, 0)]),
... Point(0, 0),
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 LINESTRING (0 0, 1 1, 1 0)
2 POINT (0 0)
dtype: geometry
>>> s.boundary
0 LINESTRING (0 0, 1 1, 0 1, 0 0)
1 MULTIPOINT ((0 0), (1 0))
2 GEOMETRYCOLLECTION EMPTY
dtype: geometry
See Also
--------
GeoSeries.exterior : outer boundary (without interior rings)
"""
return _delegate_property("boundary", self)
@property
def centroid(self):
"""Return a ``GeoSeries`` of points representing the centroid of each
geometry.
Note that centroid does not have to be on or within original geometry.
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(0, 0), (1, 1), (1, 0)]),
... Point(0, 0),
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 LINESTRING (0 0, 1 1, 1 0)
2 POINT (0 0)
dtype: geometry
>>> s.centroid
0 POINT (0.33333 0.66667)
1 POINT (0.70711 0.5)
2 POINT (0 0)
dtype: geometry
See Also
--------
GeoSeries.representative_point : point guaranteed to be within each geometry
"""
return _delegate_property("centroid", self)
def concave_hull(self, ratio=0.0, allow_holes=False):
"""Return a ``GeoSeries`` of geometries representing the concave hull
of vertices of each geometry.
The concave hull of a geometry is the smallest concave `Polygon`
containing all the points in each geometry, unless the number of points
in the geometric object is less than three. For two points, the concave
hull collapses to a `LineString`; for 1, a `Point`.
The hull is constructed by removing border triangles of the Delaunay
Triangulation of the points as long as their "size" is larger than the
maximum edge length ratio and optionally allowing holes. The edge length factor
is a fraction of the length difference between the longest and shortest edges
in the Delaunay Triangulation of the input points. For further information
on the algorithm used, see
https://libgeos.org/doxygen/classgeos_1_1algorithm_1_1hull_1_1ConcaveHull.html
Parameters
----------
ratio : float, (optional, default 0.0)
Number in the range [0, 1]. Higher numbers will include fewer vertices
in the hull.
allow_holes : bool, (optional, default False)
If set to True, the concave hull may have holes.
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point, MultiPoint
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(0, 0), (1, 1), (1, 0)]),
... MultiPoint([(0, 0), (1, 1), (0, 1), (1, 0), (0.5, 0.5)]),
... MultiPoint([(0, 0), (1, 1)]),
... Point(0, 0),
... ],
... crs=3857
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 LINESTRING (0 0, 1 1, 1 0)
2 MULTIPOINT ((0 0), (1 1), (0 1), (1 0), (0.5 0...
3 MULTIPOINT ((0 0), (1 1))
4 POINT (0 0)
dtype: geometry
>>> s.concave_hull()
0 POLYGON ((0 1, 1 1, 0 0, 0 1))
1 POLYGON ((0 0, 1 1, 1 0, 0 0))
2 POLYGON ((0.5 0.5, 0 1, 1 1, 1 0, 0 0, 0.5 0.5))
3 LINESTRING (0 0, 1 1)
4 POINT (0 0)
dtype: geometry
See Also
--------
GeoSeries.convex_hull : convex hull geometry
Notes
-----
The algorithms considers only vertices of each geometry. As a result the
hull may not fully enclose input geometry. If that happens, increasing ``ratio``
should resolve the issue.
"""
return _delegate_geo_method(
"concave_hull", self, ratio=ratio, allow_holes=allow_holes
)
def constrained_delaunay_triangles(self):
"""Return a :class:`~geopandas.GeoSeries` with the constrained
Delaunay triangulation of polygons.
A constrained Delaunay triangulation requires the edges of the input
polygon(s) to be in the set of resulting triangle edges. An
unconstrained delaunay triangulation only triangulates based on the
vertices, hence triangle edges could cross polygon boundaries.
Requires Shapely >= 2.1.
.. versionadded:: 1.1.0
Examples
--------
>>> from shapely.geometry import Polygon
>>> s = geopandas.GeoSeries([Polygon([(0, 0), (1, 1), (0, 1)])])
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
dtype: geometry
>>> s.constrained_delaunay_triangles()
0 GEOMETRYCOLLECTION (POLYGON ((0 0, 0 1, 1 1, 0...
dtype: geometry
See Also
--------
GeoSeries.delaunay_triangles : Delaunay triangulation
"""
return _delegate_geo_method("constrained_delaunay_triangles", self)
@property
def convex_hull(self):
"""Return a ``GeoSeries`` of geometries representing the convex hull
of each geometry.
The convex hull of a geometry is the smallest convex `Polygon`
containing all the points in each geometry, unless the number of points
in the geometric object is less than three. For two points, the convex
hull collapses to a `LineString`; for 1, a `Point`.
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point, MultiPoint
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(0, 0), (1, 1), (1, 0)]),
... MultiPoint([(0, 0), (1, 1), (0, 1), (1, 0), (0.5, 0.5)]),
... MultiPoint([(0, 0), (1, 1)]),
... Point(0, 0),
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 LINESTRING (0 0, 1 1, 1 0)
2 MULTIPOINT ((0 0), (1 1), (0 1), (1 0), (0.5 0...
3 MULTIPOINT ((0 0), (1 1))
4 POINT (0 0)
dtype: geometry
>>> s.convex_hull
0 POLYGON ((0 0, 0 1, 1 1, 0 0))
1 POLYGON ((0 0, 1 1, 1 0, 0 0))
2 POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))
3 LINESTRING (0 0, 1 1)
4 POINT (0 0)
dtype: geometry
See Also
--------
GeoSeries.concave_hull : concave hull geometry
GeoSeries.envelope : bounding rectangle geometry
"""
return _delegate_property("convex_hull", self)
def delaunay_triangles(self, tolerance=0.0, only_edges=False):
"""Return a ``GeoSeries`` consisting of objects representing
the computed Delaunay triangulation between the vertices of
an input geometry.
All geometries within the GeoSeries are considered together within a single
Delaunay triangulation. The resulting geometries therefore do not map 1:1
to input geometries. Note that each vertex of a geometry is considered a site
for the triangulation, so the triangles will be constructed between the vertices
of each geometry.
Notes
-----
If you want to generate Delaunay triangles for each geometry separately, use
:func:`shapely.delaunay_triangles` instead.
Parameters
----------
tolerance : float, default 0.0
Snap input vertices together if their distance is less than this value.
only_edges : bool (optional, default False)
If set to True, the triangulation will return linestrings instead of
polygons.
Examples
--------
>>> from shapely import LineString, MultiPoint, Point, Polygon
>>> s = geopandas.GeoSeries(
... [
... Point(1, 1),
... Point(2, 2),
... Point(1, 3),
... Point(0, 2),
... ]
... )
>>> s
0 POINT (1 1)
1 POINT (2 2)
2 POINT (1 3)
3 POINT (0 2)
dtype: geometry
>>> s.delaunay_triangles()
0 POLYGON ((0 2, 1 1, 1 3, 0 2))
1 POLYGON ((1 3, 1 1, 2 2, 1 3))
dtype: geometry
>>> s.delaunay_triangles(only_edges=True)
0 LINESTRING (1 3, 2 2)
1 LINESTRING (0 2, 1 3)
2 LINESTRING (0 2, 1 1)
3 LINESTRING (1 1, 2 2)
4 LINESTRING (1 1, 1 3)
dtype: geometry
The method supports any geometry type but keep in mind that the underlying
algorithm is based on the vertices of the input geometries only and does not
consider edge segments between vertices.
>>> s2 = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(1, 0), (2, 1), (1, 2)]),
... MultiPoint([(2, 3), (2, 0), (3, 1)]),
... ]
... )
>>> s2
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 LINESTRING (1 0, 2 1, 1 2)
2 MULTIPOINT ((2 3), (2 0), (3 1))
dtype: geometry
>>> s2.delaunay_triangles()
0 POLYGON ((0 1, 0 0, 1 0, 0 1))
1 POLYGON ((0 1, 1 0, 1 1, 0 1))
2 POLYGON ((0 1, 1 1, 1 2, 0 1))
3 POLYGON ((1 2, 1 1, 2 1, 1 2))
4 POLYGON ((1 2, 2 1, 2 3, 1 2))
5 POLYGON ((2 3, 2 1, 3 1, 2 3))
6 POLYGON ((3 1, 2 1, 2 0, 3 1))
7 POLYGON ((2 0, 2 1, 1 1, 2 0))
8 POLYGON ((2 0, 1 1, 1 0, 2 0))
dtype: geometry
See Also
--------
GeoSeries.voronoi_polygons : Voronoi diagram around vertices
GeoSeries.constrained_delaunay_triangles : constrained Delaunay triangulation
"""
from .geoseries import GeoSeries
geometry_input = shapely.geometrycollections(self.geometry.values._data)
delaunay = shapely.delaunay_triangles(
geometry_input,
tolerance=tolerance,
only_edges=only_edges,
)
return GeoSeries(delaunay, crs=self.crs).explode(ignore_index=True)
def voronoi_polygons(self, tolerance=0.0, extend_to=None, only_edges=False):
"""Return a ``GeoSeries`` consisting of objects representing
the computed Voronoi diagram around the vertices of an input geometry.
All geometries within the GeoSeries are considered together within a single
Voronoi diagram. The resulting geometries therefore do not necessarily map 1:1
to input geometries. Note that each vertex of a geometry is considered a site
for the Voronoi diagram, so the diagram will be constructed around the vertices
of each geometry.
Notes
-----
The order of polygons in the output currently does not correspond to the order
of input vertices.
If you want to generate a Voronoi diagram for each geometry separately, use
:func:`shapely.voronoi_polygons` instead.
Parameters
----------
tolerance : float, default 0.0
Snap input vertices together if their distance is less than this value.
extend_to : shapely.Geometry, default None
If set, the Voronoi diagram will be extended to cover the
envelope of this geometry (unless this envelope is smaller than the input
geometry).
only_edges : bool (optional, default False)
If set to True, the diagram will return LineStrings instead
of Polygons.
Examples
--------
The most common use case is to generate polygons representing the Voronoi
diagram around a set of points:
>>> from shapely import LineString, MultiPoint, Point, Polygon
>>> s = geopandas.GeoSeries(
... [
... Point(1, 1),
... Point(2, 2),
... Point(1, 3),
... Point(0, 2),
... ]
... )
>>> s
0 POINT (1 1)
1 POINT (2 2)
2 POINT (1 3)
3 POINT (0 2)
dtype: geometry
By default, you get back a GeoSeries of polygons:
>>> s.voronoi_polygons()
0 POLYGON ((-2 5, 1 2, -2 -1, -2 5))
1 POLYGON ((4 5, 1 2, -2 5, 4 5))
2 POLYGON ((-2 -1, 1 2, 4 -1, -2 -1))
3 POLYGON ((4 -1, 4 -1, 1 2, 4 5, 4 -1))
dtype: geometry
If you set only_edges to True, you get back LineStrings representing the
edges of the Voronoi diagram:
>>> s.voronoi_polygons(only_edges=True)
0 LINESTRING (-2 5, 1 2)
1 LINESTRING (1 2, -2 -1)
2 LINESTRING (4 5, 1 2)
3 LINESTRING (1 2, 4 -1)
dtype: geometry
You can also extend each diagram to a given geometry:
>>> limit = Polygon([(-10, -10), (0, 15), (15, 15), (15, 0)])
>>> s.voronoi_polygons(extend_to=limit)
0 POLYGON ((-10 13, 1 2, -10 -9, -10 13))
1 POLYGON ((15 15, 15 -10, 13 -10, 1 2, 14 15, 1...
2 POLYGON ((-10 -10, -10 -9, 1 2, 13 -10, -10 -10))
3 POLYGON ((-10 15, 14 15, 1 2, -10 13, -10 15))
dtype: geometry
The method supports any geometry type but keep in mind that the underlying
algorithm is based on the vertices of the input geometries only and does not
consider edge segments between vertices.
>>> s2 = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(1, 0), (2, 1), (1, 2)]),
... MultiPoint([(2, 3), (2, 0), (3, 1)]),
... ]
... )
>>> s2
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 LINESTRING (1 0, 2 1, 1 2)
2 MULTIPOINT ((2 3), (2 0), (3 1))
dtype: geometry
>>> s2.voronoi_polygons()
0 POLYGON ((1.5 1.5, 1.5 0.5, 0.5 0.5, 0.5 1.5, ...
1 POLYGON ((1.5 0.5, 1.5 1.5, 2 2, 2.5 2, 2.5 0....
2 POLYGON ((-3 -3, -3 0.5, 0.5 0.5, 0.5 -3, -3 -3))
3 POLYGON ((0.5 -3, 0.5 0.5, 1.5 0.5, 1.5 -3, 0....
4 POLYGON ((-3 5, 0.5 1.5, 0.5 0.5, -3 0.5, -3 5))
5 POLYGON ((-3 6, -2 6, 2 2, 1.5 1.5, 0.5 1.5, -...
6 POLYGON ((1.5 -3, 1.5 0.5, 2.5 0.5, 6 -3, 1.5 ...
7 POLYGON ((6 6, 6 3.75, 2.5 2, 2 2, -2 6, 6 6))
8 POLYGON ((6 -3, 2.5 0.5, 2.5 2, 6 3.75, 6 -3))
dtype: geometry
See Also
--------
GeoSeries.delaunay_triangles : Delaunay triangulation around vertices
"""
from .geoseries import GeoSeries
geometry_input = shapely.geometrycollections(self.geometry.values._data)
voronoi = shapely.voronoi_polygons(
geometry_input,
tolerance=tolerance,
extend_to=extend_to,
only_edges=only_edges,
)
return GeoSeries(voronoi, crs=self.crs).explode(ignore_index=True)
@property
def envelope(self):
"""Return a ``GeoSeries`` of geometries representing the envelope of
each geometry.
The envelope of a geometry is the bounding rectangle. That is, the
point or smallest rectangular polygon (with sides parallel to the
coordinate axes) that contains the geometry.
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point, MultiPoint
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(0, 0), (1, 1), (1, 0)]),
... MultiPoint([(0, 0), (1, 1)]),
... Point(0, 0),
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 LINESTRING (0 0, 1 1, 1 0)
2 MULTIPOINT ((0 0), (1 1))
3 POINT (0 0)
dtype: geometry
>>> s.envelope
0 POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))
1 POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))
2 POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))
3 POINT (0 0)
dtype: geometry
See Also
--------
GeoSeries.convex_hull : convex hull geometry
"""
return _delegate_property("envelope", self)
def minimum_rotated_rectangle(self):
"""Return a ``GeoSeries`` of the general minimum bounding rectangle
that contains the object.
Unlike envelope this rectangle is not constrained to be parallel
to the coordinate axes. If the convex hull of the object is a
degenerate (line or point) this degenerate is returned.
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point, MultiPoint
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(0, 0), (1, 1), (1, 0)]),
... MultiPoint([(0, 0), (1, 1)]),
... Point(0, 0),
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 LINESTRING (0 0, 1 1, 1 0)
2 MULTIPOINT ((0 0), (1 1))
3 POINT (0 0)
dtype: geometry
>>> s.minimum_rotated_rectangle()
0 POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))
1 POLYGON ((1 1, 1 0, 0 0, 0 1, 1 1))
2 LINESTRING (0 0, 1 1)
3 POINT (0 0)
dtype: geometry
See Also
--------
GeoSeries.envelope : bounding rectangle
"""
return _delegate_geo_method("minimum_rotated_rectangle", self)
@property
def exterior(self):
"""Return a ``GeoSeries`` of LinearRings representing the outer
boundary of each polygon in the GeoSeries.
Applies to GeoSeries containing only Polygons. Returns ``None``` for
other geometry types.
Examples
--------
>>> from shapely.geometry import Polygon, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... Polygon([(1, 0), (2, 1), (0, 0)]),
... Point(0, 1)
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 POLYGON ((1 0, 2 1, 0 0, 1 0))
2 POINT (0 1)
dtype: geometry
>>> s.exterior
0 LINEARRING (0 0, 1 1, 0 1, 0 0)
1 LINEARRING (1 0, 2 1, 0 0, 1 0)
2 None
dtype: geometry
See Also
--------
GeoSeries.boundary : complete set-theoretic boundary
GeoSeries.interiors : list of inner rings of each polygon
"""
# TODO: return empty geometry for non-polygons
return _delegate_property("exterior", self)
def extract_unique_points(self):
"""Return a ``GeoSeries`` of MultiPoints representing all
distinct vertices of an input geometry.
Examples
--------
>>> from shapely import LineString, Polygon
>>> s = geopandas.GeoSeries(
... [
... LineString([(0, 0), (0, 0), (1, 1), (1, 1)]),
... Polygon([(0, 0), (0, 0), (1, 1), (1, 1)])
... ],
... )
>>> s
0 LINESTRING (0 0, 0 0, 1 1, 1 1)
1 POLYGON ((0 0, 0 0, 1 1, 1 1, 0 0))
dtype: geometry
>>> s.extract_unique_points()
0 MULTIPOINT ((0 0), (1 1))
1 MULTIPOINT ((0 0), (1 1))
dtype: geometry
See Also
--------
GeoSeries.get_coordinates : extract coordinates as a :class:`~pandas.DataFrame`
"""
return _delegate_geo_method("extract_unique_points", self)
def offset_curve(self, distance, quad_segs=8, join_style="round", mitre_limit=5.0):
"""Return a ``LineString`` or ``MultiLineString`` geometry at a
distance from the object on its right or its left side.
Parameters
----------
distance : float | array-like
Specifies the offset distance from the input geometry. Negative
for right side offset, positive for left side offset.
quad_segs : int (optional, default 8)
Specifies the number of linear segments in a quarter circle in the
approximation of circular arcs.
join_style : {'round', 'bevel', 'mitre'}, (optional, default 'round')
Specifies the shape of outside corners. 'round' results in
rounded shapes. 'bevel' results in a beveled edge that touches the
original vertex. 'mitre' results in a single vertex that is beveled
depending on the ``mitre_limit`` parameter.
mitre_limit : float (optional, default 5.0)
Crops of 'mitre'-style joins if the point is displaced from the
buffered vertex by more than this limit.
See http://shapely.readthedocs.io/en/latest/manual.html#object.offset_curve
for details.
Examples
--------
>>> from shapely.geometry import LineString
>>> s = geopandas.GeoSeries(
... [
... LineString([(0, 0), (0, 1), (1, 1)]),
... ],
... crs=3857
... )
>>> s
0 LINESTRING (0 0, 0 1, 1 1)
dtype: geometry
>>> s.offset_curve(1)
0 LINESTRING (-1 0, -1 1, -0.981 1.195, -0.924 1...
dtype: geometry
"""
return _delegate_geo_method(
"offset_curve",
self,
distance=distance,
quad_segs=quad_segs,
join_style=join_style,
mitre_limit=mitre_limit,
)
@property
def interiors(self):
"""Return a ``Series`` of List representing the
inner rings of each polygon in the GeoSeries.
Applies to GeoSeries containing only Polygons.
Returns
-------
inner_rings: Series of List
Inner rings of each polygon in the GeoSeries.
Examples
--------
>>> from shapely.geometry import Polygon
>>> s = geopandas.GeoSeries(
... [
... Polygon(
... [(0, 0), (0, 5), (5, 5), (5, 0)],
... [[(1, 1), (2, 1), (1, 2)], [(1, 4), (2, 4), (2, 3)]],
... ),
... Polygon([(1, 0), (2, 1), (0, 0)]),
... ]
... )
>>> s
0 POLYGON ((0 0, 0 5, 5 5, 5 0, 0 0), (1 1, 2 1,...
1 POLYGON ((1 0, 2 1, 0 0, 1 0))
dtype: geometry
>>> s.interiors
0 [LINEARRING (1 1, 2 1, 1 2, 1 1), LINEARRING (...
1 []
dtype: object
See Also
--------
GeoSeries.exterior : outer boundary
"""
return _delegate_property("interiors", self)
def remove_repeated_points(self, tolerance=0.0):
"""Return a ``GeoSeries`` containing a copy of the input geometry
with repeated points removed.
From the start of the coordinate sequence, each next point within the
tolerance is removed.
Removing repeated points with a non-zero tolerance may result in an invalid
geometry being returned.
Parameters
----------
tolerance : float, default 0.0
Remove all points within this distance of each other. Use 0.0
to remove only exactly repeated points (the default).
Examples
--------
>>> from shapely import LineString, Polygon
>>> s = geopandas.GeoSeries(
... [
... LineString([(0, 0), (0, 0), (1, 0)]),
... Polygon([(0, 0), (0, 0.5), (0, 1), (0.5, 1), (0,0)]),
... ],
... )
>>> s
0 LINESTRING (0 0, 0 0, 1 0)
1 POLYGON ((0 0, 0 0.5, 0 1, 0.5 1, 0 0))
dtype: geometry
>>> s.remove_repeated_points(tolerance=0.0)
0 LINESTRING (0 0, 1 0)
1 POLYGON ((0 0, 0 0.5, 0 1, 0.5 1, 0 0))
dtype: geometry
"""
return _delegate_geo_method("remove_repeated_points", self, tolerance=tolerance)
def set_precision(self, grid_size, mode="valid_output"):
"""Return a ``GeoSeries`` with the precision set to a precision grid size.
By default, geometries use double precision coordinates (``grid_size=0``).
Coordinates will be rounded if a precision grid is less precise than the input
geometry. Duplicated vertices will be dropped from lines and polygons for grid
sizes greater than 0. Line and polygon geometries may collapse to empty
geometries if all vertices are closer together than ``grid_size``. Spikes or
sections in Polygons narrower than ``grid_size`` after rounding the vertices
will be removed, which can lead to MultiPolygons or empty geometries. Z values,
if present, will not be modified.
Parameters
----------
grid_size : float
Precision grid size. If 0, will use double precision (will not modify
geometry if precision grid size was not previously set). If this value is
more precise than input geometry, the input geometry will not be modified.
mode : {'valid_output', 'pointwise', 'keep_collapsed'}, default 'valid_output'
This parameter determines the way a precision reduction is applied on the
geometry. There are three modes:
* ``'valid_output'`` (default): The output is always valid. Collapsed
geometry elements (including both polygons and lines) are removed.
Duplicate vertices are removed.
* ``'pointwise'``: Precision reduction is performed pointwise. Output
geometry may be invalid due to collapse or self-intersection. Duplicate
vertices are not removed.
* ``'keep_collapsed'``: Like the default mode, except that collapsed linear
geometry elements are preserved. Collapsed polygonal input elements are
removed. Duplicate vertices are removed.
Examples
--------
>>> from shapely import LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Point(0.9, 0.9),
... Point(0.9, 0.9, 0.9),
... LineString([(0, 0), (0, 0.1), (0, 1), (1, 1)]),
... LineString([(0, 0), (0, 0.1), (0.1, 0.1)])
... ],
... )
>>> s
0 POINT (0.9 0.9)
1 POINT Z (0.9 0.9 0.9)
2 LINESTRING (0 0, 0 0.1, 0 1, 1 1)
3 LINESTRING (0 0, 0 0.1, 0.1 0.1)
dtype: geometry
>>> s.set_precision(1)
0 POINT (1 1)
1 POINT Z (1 1 0.9)
2 LINESTRING (0 0, 0 1, 1 1)
3 LINESTRING EMPTY
dtype: geometry
>>> s.set_precision(1, mode="pointwise")
0 POINT (1 1)
1 POINT Z (1 1 0.9)
2 LINESTRING (0 0, 0 0, 0 1, 1 1)
3 LINESTRING (0 0, 0 0, 0 0)
dtype: geometry
>>> s.set_precision(1, mode="keep_collapsed")
0 POINT (1 1)
1 POINT Z (1 1 0.9)
2 LINESTRING (0 0, 0 1, 1 1)
3 LINESTRING (0 0, 0 0)
dtype: geometry
Notes
-----
Subsequent operations will always be performed in the precision of the geometry
with higher precision (smaller ``grid_size``). That same precision will be
attached to the operation outputs.
Input geometries should be geometrically valid; unexpected results may occur if
input geometries are not. You can check the validity with
:meth:`~GeoSeries.is_valid` and fix invalid geometries with
:meth:`~GeoSeries.make_valid` methods.
"""
return _delegate_geo_method(
"set_precision", self, grid_size=grid_size, mode=mode
)
def representative_point(self):
"""Return a ``GeoSeries`` of (cheaply computed) points that are
guaranteed to be within each geometry.
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(0, 0), (1, 1), (1, 0)]),
... Point(0, 0),
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 LINESTRING (0 0, 1 1, 1 0)
2 POINT (0 0)
dtype: geometry
>>> s.representative_point()
0 POINT (0.25 0.5)
1 POINT (1 1)
2 POINT (0 0)
dtype: geometry
See Also
--------
GeoSeries.centroid : geometric centroid
"""
return _delegate_geo_method("representative_point", self)
def minimum_bounding_circle(self):
"""Return a ``GeoSeries`` of geometries representing the minimum bounding
circle that encloses each geometry.
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1), (0, 0)]),
... LineString([(0, 0), (1, 1), (1, 0)]),
... Point(0, 0),
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 LINESTRING (0 0, 1 1, 1 0)
2 POINT (0 0)
dtype: geometry
>>> s.minimum_bounding_circle()
0 POLYGON ((1.20711 0.5, 1.19352 0.36205, 1.1532...
1 POLYGON ((1.20711 0.5, 1.19352 0.36205, 1.1532...
2 POINT (0 0)
dtype: geometry
See Also
--------
GeoSeries.convex_hull : convex hull geometry
GeoSeries.maximum_inscribed_circle : the largest circle within the geometry
"""
return _delegate_geo_method("minimum_bounding_circle", self)
def maximum_inscribed_circle(self, *, tolerance=None):
"""Return a ``GeoSeries`` of geometries representing the largest circle that
is fully contained within the input geometry.
Constructs the “maximum inscribed circle” (MIC) for a polygonal geometry, up to
a specified tolerance. The MIC is determined by a point in the interior of the
area which has the farthest distance from the area boundary, along with a
boundary point at that distance. In the context of geography the center of the
MIC is known as the “pole of inaccessibility”. A cartographic use case is to
determine a suitable point to place a map label within a polygon. The radius
length of the MIC is a measure of how “narrow” a polygon is. It is the distance
at which the negative buffer becomes empty.
The method supports polygons with holes and multipolygons but will raise an
error for any other geometry type.
Returns a GeoSeries with two-point linestrings rows, with the first point at the
center of the inscribed circle and the second on the boundary of the inscribed
circle.
Requires Shapely >= 2.1.
.. versionadded:: 1.1.0
Parameters
----------
tolerance : float, np.array, pd.Series
Stop the algorithm when the search area is smaller than this tolerance.
When not specified, uses ``max(width, height) / 1000`` per geometry as the
default. If np.array or pd.Series are used then it must have same length as
the GeoSeries.
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1), (0, 0)]),
... Polygon([(0, 0), (10, 10), (0, 10), (0, 0)]),
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 POLYGON ((0 0, 10 10, 0 10, 0 0))
dtype: geometry
>>> s.maximum_inscribed_circle()
0 LINESTRING (0.29297 0.70703, 0.5 0.5)
1 LINESTRING (2.92969 7.07031, 5 5)
dtype: geometry
>>> s.maximum_inscribed_circle(tolerance=2)
0 LINESTRING (0.25 0.5, 0.375 0.375)
1 LINESTRING (2.5 7.5, 2.5 10)
dtype: geometry
See Also
--------
minimum_bounding_circle
"""
return _delegate_geo_method(
"maximum_inscribed_circle", self, tolerance=tolerance
)
def minimum_bounding_radius(self):
"""Return a `Series` of the radii of the minimum bounding circles
that enclose each geometry.
Examples
--------
>>> from shapely.geometry import Point, LineString, Polygon
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1), (0, 0)]),
... LineString([(0, 0), (1, 1), (1, 0)]),
... Point(0,0),
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 LINESTRING (0 0, 1 1, 1 0)
2 POINT (0 0)
dtype: geometry
>>> s.minimum_bounding_radius()
0 0.707107
1 0.707107
2 0.000000
dtype: float64
See Also
--------
GeoSeries.minumum_bounding_circle : minimum bounding circle (geometry)
"""
return Series(self.geometry.values.minimum_bounding_radius(), index=self.index)
def minimum_clearance(self):
"""Return a ``Series`` containing the minimum clearance distance,
which is the smallest distance by which a vertex of the geometry
could be moved to produce an invalid geometry.
If no minimum clearance exists for a geometry (for example,
a single point, or an empty geometry), infinity is returned.
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1), (0, 0)]),
... LineString([(0, 0), (1, 1), (3, 2)]),
... Point(0, 0),
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 LINESTRING (0 0, 1 1, 3 2)
2 POINT (0 0)
dtype: geometry
>>> s.minimum_clearance()
0 0.707107
1 1.414214
2 inf
dtype: float64
See Also
--------
minimum_clearance_line
"""
return Series(self.geometry.values.minimum_clearance(), index=self.index)
def minimum_clearance_line(self):
"""Return a ``GeoSeries`` of linestrings whose endpoints define the
minimum clearance.
A geometry's “minimum clearance” is the smallest distance by which a vertex
of the geometry could be moved to produce an invalid geometry.
If the geometry has no minimum clearance, an empty LineString will be returned.
Requires Shapely >= 2.1.
.. versionadded:: 1.1.0
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1), (0, 0)]),
... LineString([(0, 0), (1, 1), (3, 2)]),
... Point(0, 0),
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 LINESTRING (0 0, 1 1, 3 2)
2 POINT (0 0)
dtype: geometry
>>> s.minimum_clearance_line()
0 LINESTRING (0 1, 0.5 0.5)
1 LINESTRING (0 0, 1 1)
2 LINESTRING EMPTY
dtype: geometry
See Also
--------
minimum_clearance
"""
return _delegate_geo_method("minimum_clearance_line", self)
def normalize(self):
"""Return a ``GeoSeries`` of normalized
geometries to normal form (or canonical form).
This method orders the coordinates, rings of a polygon and parts of
multi geometries consistently. Typically useful for testing purposes
(for example in combination with `equals_exact`).
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(0, 0), (1, 1), (1, 0)]),
... Point(0, 0),
... ],
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 LINESTRING (0 0, 1 1, 1 0)
2 POINT (0 0)
dtype: geometry
>>> s.normalize()
0 POLYGON ((0 0, 0 1, 1 1, 0 0))
1 LINESTRING (0 0, 1 1, 1 0)
2 POINT (0 0)
dtype: geometry
"""
return _delegate_geo_method("normalize", self)
def orient_polygons(self, *, exterior_cw=False):
"""Return a ``GeoSeries`` of geometries with enforced ring orientation.
Enforce a ring orientation on all polygonal elements in the ``GeoSeries``.
Forces (Multi)Polygons to use a counter-clockwise orientation for their exterior
ring, and a clockwise orientation for their interior rings (or the oppposite if
``exterior_cw=True``).
Also processes geometries inside a GeometryCollection in the same way. Other
geometries are returned unchanged.
Requires Shapely >= 2.1.
.. versionadded:: 1.1.0
Parameters
----------
exterior_cw : bool
If ``True``, exterior rings will be clockwise and interior rings will be
counter-clockwise.
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon(
... [(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)],
... holes=[[(2, 2), (2, 4), (4, 4), (4, 2), (2, 2)]],
... ),
... LineString([(0, 0), (1, 1), (1, 0)]),
... Point(0, 0),
... ],
... )
>>> s
0 POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0), (2 2, ...
1 LINESTRING (0 0, 1 1, 1 0)
2 POINT (0 0)
dtype: geometry
>>> s.orient_polygons()
0 POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0), (2 2, ...
1 LINESTRING (0 0, 1 1, 1 0)
2 POINT (0 0)
dtype: geometry
>>> s.orient_polygons(exterior_cw=True)
0 POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0), (2 2, ...
1 LINESTRING (0 0, 1 1, 1 0)
2 POINT (0 0)
dtype: geometry
"""
return _delegate_geo_method("orient_polygons", self, exterior_cw=exterior_cw)
def make_valid(self, *, method="linework", keep_collapsed=True):
"""Repairs invalid geometries.
Returns a ``GeoSeries`` with valid geometries.
If the input geometry is already valid, then it will be preserved.
In many cases, in order to create a valid geometry, the input
geometry must be split into multiple parts or multiple geometries.
If the geometry must be split into multiple parts of the same type
to be made valid, then a multi-part geometry will be returned
(e.g. a MultiPolygon).
If the geometry must be split into multiple parts of different types
to be made valid, then a GeometryCollection will be returned.
Two ``methods`` are available:
* the 'linework' algorithm tries to preserve every edge and vertex in
the input. It combines all rings into a set of noded lines and then
extracts valid polygons from that linework. An alternating even-odd
strategy is used to assign areas as interior or exterior. A
disadvantage is that for some relatively simple invalid geometries
this produces rather complex results.
* the 'structure' algorithm tries to reason from the structure of the
input to find the 'correct' repair: exterior rings bound area,
interior holes exclude area. It first makes all rings valid, then
shells are merged and holes are subtracted from the shells to
generate valid result. It assumes that holes and shells are correctly
categorized in the input geometry.
Parameters
----------
method : {'linework', 'structure'}, default 'linework'
Algorithm to use when repairing geometry. 'structure'
requires GEOS >= 3.10 and shapely >= 2.1.
.. versionadded:: 1.1.0
keep_collapsed : bool, default True
For the 'structure' method, True will keep components that have
collapsed into a lower dimensionality. For example, a ring
collapsing to a line, or a line collapsing to a point. Must be True
for the 'linework' method.
.. versionadded:: 1.1.0
Examples
--------
>>> from shapely.geometry import MultiPolygon, Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (0, 2), (1, 1), (2, 2), (2, 0), (1, 1), (0, 0)]),
... Polygon([(0, 2), (0, 1), (2, 0), (0, 0), (0, 2)]),
... LineString([(0, 0), (1, 1), (1, 0)]),
... ],
... )
>>> s
0 POLYGON ((0 0, 0 2, 1 1, 2 2, 2 0, 1 1, 0 0))
1 POLYGON ((0 2, 0 1, 2 0, 0 0, 0 2))
2 LINESTRING (0 0, 1 1, 1 0)
dtype: geometry
>>> s.make_valid()
0 MULTIPOLYGON (((1 1, 0 0, 0 2, 1 1)), ((2 0, 1...
1 GEOMETRYCOLLECTION (POLYGON ((2 0, 0 0, 0 1, 2...
2 LINESTRING (0 0, 1 1, 1 0)
dtype: geometry
"""
return _delegate_geo_method(
"make_valid", self, method=method, keep_collapsed=keep_collapsed
)
def reverse(self):
"""Return a ``GeoSeries`` with the order of coordinates reversed.
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(0, 0), (1, 1), (1, 0)]),
... Point(0, 0),
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 LINESTRING (0 0, 1 1, 1 0)
2 POINT (0 0)
dtype: geometry
>>> s.reverse()
0 POLYGON ((0 0, 0 1, 1 1, 0 0))
1 LINESTRING (1 0, 1 1, 0 0)
2 POINT (0 0)
dtype: geometry
See Also
--------
GeoSeries.normalize : normalize order of coordinates
"""
return _delegate_geo_method("reverse", self)
def segmentize(self, max_segment_length):
"""Return a ``GeoSeries`` with vertices added to line segments based on
maximum segment length.
Additional vertices will be added to every line segment in an input geometry so
that segments are no longer than the provided maximum segment length. New
vertices will evenly subdivide each segment. Only linear components of input
geometries are densified; other geometries are returned unmodified.
Parameters
----------
max_segment_length : float | array-like
Additional vertices will be added so that all line segments are no longer
than this value. Must be greater than 0.
Returns
-------
GeoSeries
Examples
--------
>>> from shapely.geometry import Polygon, LineString
>>> s = geopandas.GeoSeries(
... [
... LineString([(0, 0), (0, 10)]),
... Polygon([(0, 0), (10, 0), (10, 10), (0, 10), (0, 0)]),
... ],
... )
>>> s
0 LINESTRING (0 0, 0 10)
1 POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))
dtype: geometry
>>> s.segmentize(max_segment_length=5)
0 LINESTRING (0 0, 0 5, 0 10)
1 POLYGON ((0 0, 5 0, 10 0, 10 5, 10 10, 5 10, 0...
dtype: geometry
"""
return _delegate_geo_method(
"segmentize", self, max_segment_length=max_segment_length
)
def transform(self, transformation, include_z=False):
"""Return a ``GeoSeries`` with the transformation function
applied to the geometry coordinates.
Parameters
----------
transformation : Callable
A function that transforms a (N, 2) or (N, 3) ndarray of float64
to another (N,2) or (N, 3) ndarray of float64
include_z : bool, default False
If True include the third dimension in the coordinates array that
is passed to the ``transformation`` function. If a geometry has no third
dimension, the z-coordinates passed to the function will be NaN.
Returns
-------
GeoSeries
Examples
--------
>>> from shapely import Point, Polygon
>>> s = geopandas.GeoSeries([Point(0, 0)])
>>> s.transform(lambda x: x + 1)
0 POINT (1 1)
dtype: geometry
>>> s = geopandas.GeoSeries([Polygon([(0, 0), (1, 1), (0, 1)])])
>>> s.transform(lambda x: x * [2, 3])
0 POLYGON ((0 0, 2 3, 0 3, 0 0))
dtype: geometry
By default the third dimension is ignored and you need explicitly include it:
>>> s = geopandas.GeoSeries([Point(0, 0, 0)])
>>> s.transform(lambda x: x + 1, include_z=True)
0 POINT Z (1 1 1)
dtype: geometry
"""
return _delegate_geo_method(
"transform", self, transformation=transformation, include_z=include_z
)
def force_2d(self):
"""Force the dimensionality of a geometry to 2D.
Removes the additional Z coordinate dimension from all geometries.
Returns
-------
GeoSeries
Examples
--------
>>> from shapely import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Point(0.5, 2.5, 0),
... LineString([(1, 1, 1), (0, 1, 3), (1, 0, 2)]),
... Polygon([(0, 0, 0), (0, 10, 0), (10, 10, 0)]),
... ],
... )
>>> s
0 POINT Z (0.5 2.5 0)
1 LINESTRING Z (1 1 1, 0 1 3, 1 0 2)
2 POLYGON Z ((0 0 0, 0 10 0, 10 10 0, 0 0 0))
dtype: geometry
>>> s.force_2d()
0 POINT (0.5 2.5)
1 LINESTRING (1 1, 0 1, 1 0)
2 POLYGON ((0 0, 0 10, 10 10, 0 0))
dtype: geometry
"""
return _delegate_geo_method("force_2d", self)
def force_3d(self, z=0):
"""Force the dimensionality of a geometry to 3D.
2D geometries will get the provided Z coordinate; 3D geometries
are unchanged (unless their Z coordinate is ``np.nan``).
Note that for empty geometries, 3D is only supported since GEOS 3.9 and then
still only for simple geometries (non-collections).
Parameters
----------
z : float | array_like (default 0)
Z coordinate to be assigned
Returns
-------
GeoSeries
Examples
--------
>>> from shapely import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Point(1, 2),
... Point(0.5, 2.5, 2),
... LineString([(1, 1), (0, 1), (1, 0)]),
... Polygon([(0, 0), (0, 10), (10, 10)]),
... ],
... )
>>> s
0 POINT (1 2)
1 POINT Z (0.5 2.5 2)
2 LINESTRING (1 1, 0 1, 1 0)
3 POLYGON ((0 0, 0 10, 10 10, 0 0))
dtype: geometry
>>> s.force_3d()
0 POINT Z (1 2 0)
1 POINT Z (0.5 2.5 2)
2 LINESTRING Z (1 1 0, 0 1 0, 1 0 0)
3 POLYGON Z ((0 0 0, 0 10 0, 10 10 0, 0 0 0))
dtype: geometry
Z coordinate can be specified as scalar:
>>> s.force_3d(4)
0 POINT Z (1 2 4)
1 POINT Z (0.5 2.5 2)
2 LINESTRING Z (1 1 4, 0 1 4, 1 0 4)
3 POLYGON Z ((0 0 4, 0 10 4, 10 10 4, 0 0 4))
dtype: geometry
Or as an array-like (one value per geometry):
>>> s.force_3d(range(4))
0 POINT Z (1 2 0)
1 POINT Z (0.5 2.5 2)
2 LINESTRING Z (1 1 2, 0 1 2, 1 0 2)
3 POLYGON Z ((0 0 3, 0 10 3, 10 10 3, 0 0 3))
dtype: geometry
"""
return _delegate_geo_method("force_3d", self, z=z)
def line_merge(self, directed=False):
"""Return (Multi)LineStrings formed by combining the lines in a
MultiLineString.
Lines are joined together at their endpoints in case two lines are intersecting.
Lines are not joined when 3 or more lines are intersecting at the endpoints.
Line elements that cannot be joined are kept as is in the resulting
MultiLineString.
The direction of each merged LineString will be that of the majority of the
LineStrings from which it was derived. Except if ``directed=True`` is specified,
then the operation will not change the order of points within lines and so only
lines which can be joined with no change in direction are merged.
Non-linear geometeries result in an empty GeometryCollection.
Parameters
----------
directed : bool, default False
Only combine lines if possible without changing point order.
Requires GEOS >= 3.11.0
Returns
-------
GeoSeries
Examples
--------
>>> from shapely.geometry import MultiLineString, Point
>>> s = geopandas.GeoSeries(
... [
... MultiLineString([[(0, 2), (0, 10)], [(0, 10), (5, 10)]]),
... MultiLineString([[(0, 2), (0, 10)], [(0, 11), (5, 10)]]),
... MultiLineString(),
... MultiLineString([[(0, 0), (1, 0)], [(0, 0), (3, 0)]]),
... Point(0, 0),
... ]
... )
>>> s
0 MULTILINESTRING ((0 2, 0 10), (0 10, 5 10))
1 MULTILINESTRING ((0 2, 0 10), (0 11, 5 10))
2 MULTILINESTRING EMPTY
3 MULTILINESTRING ((0 0, 1 0), (0 0, 3 0))
4 POINT (0 0)
dtype: geometry
>>> s.line_merge()
0 LINESTRING (0 2, 0 10, 5 10)
1 MULTILINESTRING ((0 2, 0 10), (0 11, 5 10))
2 GEOMETRYCOLLECTION EMPTY
3 LINESTRING (1 0, 0 0, 3 0)
4 GEOMETRYCOLLECTION EMPTY
dtype: geometry
With ``directed=True``, you can avoid changing the order of points within lines
and merge only lines where no change of direction is required:
>>> s.line_merge(directed=True)
0 LINESTRING (0 2, 0 10, 5 10)
1 MULTILINESTRING ((0 2, 0 10), (0 11, 5 10))
2 GEOMETRYCOLLECTION EMPTY
3 MULTILINESTRING ((0 0, 1 0), (0 0, 3 0))
4 GEOMETRYCOLLECTION EMPTY
dtype: geometry
"""
return _delegate_geo_method("line_merge", self, directed=directed)
#
# Reduction operations that return a Shapely geometry
#
@property
def unary_union(self):
"""Return a geometry containing the union of all geometries in the
``GeoSeries``.
The ``unary_union`` attribute is deprecated. Use :meth:`union_all`
instead.
Examples
--------
>>> from shapely.geometry import box
>>> s = geopandas.GeoSeries([box(0,0,1,1), box(0,0,2,2)])
>>> s
0 POLYGON ((1 0, 1 1, 0 1, 0 0, 1 0))
1 POLYGON ((2 0, 2 2, 0 2, 0 0, 2 0))
dtype: geometry
>>> union = s.unary_union
>>> print(union)
POLYGON ((0 1, 0 2, 2 2, 2 0, 1 0, 0 0, 0 1))
See Also
--------
GeoSeries.union_all
"""
warn(
"The 'unary_union' attribute is deprecated, "
"use the 'union_all()' method instead.",
DeprecationWarning,
stacklevel=2,
)
return self.geometry.values.union_all()
def union_all(self, method="unary", *, grid_size=None):
"""Return a geometry containing the union of all geometries in the
``GeoSeries``.
By default, the unary union algorithm is used. If the geometries are
non-overlapping (forming a coverage), GeoPandas can use a significantly faster
algorithm to perform the union using the ``method="coverage"`` option.
Alternatively, for situations which can be divided into many disjoint subsets,
``method="disjoint_subset"`` may be preferable.
Parameters
----------
method : str (default ``"unary"``)
The method to use for the union. Options are:
* ``"unary"``: use the unary union algorithm. This option is the most robust
but can be slow for large numbers of geometries (default).
* ``"coverage"``: use the coverage union algorithm. This option is optimized
for non-overlapping polygons and can be significantly faster than the
unary union algorithm. However, it can produce invalid geometries if the
polygons overlap.
* ``"disjoint_subset:``: use the disjoint subset union algorithm. This
option is optimized for inputs that can be divided into subsets that do
not intersect. If there is only one such subset, performance can be
expected to be worse than ``"unary"``. Requires Shapely >= 2.1.
grid_size : float, default None
When grid size is specified, a fixed-precision space is used to perform the
union operations. This can be useful when unioning geometries that are not
perfectly snapped or to avoid geometries not being unioned because of
`robustness issues <https://libgeos.org/usage/faq/#why-doesnt-a-computed-point-lie-exactly-on-a-line>`_.
The inputs are first snapped to a grid of the given size. When a line
segment of a geometry is within tolerance off a vertex of another geometry,
this vertex will be inserted in the line segment. Finally, the result
vertices are computed on the same grid. Is only supported for ``method``
``"unary"``. If None, the highest precision of the inputs will be used.
Defaults to None.
.. versionadded:: 1.1.0
Examples
--------
>>> from shapely.geometry import box
>>> s = geopandas.GeoSeries([box(0, 0, 1, 1), box(0, 0, 2, 2)])
>>> s
0 POLYGON ((1 0, 1 1, 0 1, 0 0, 1 0))
1 POLYGON ((2 0, 2 2, 0 2, 0 0, 2 0))
dtype: geometry
>>> s.union_all()
<POLYGON ((0 1, 0 2, 2 2, 2 0, 1 0, 0 0, 0 1))>
"""
return self.geometry.values.union_all(method=method, grid_size=grid_size)
def intersection_all(self):
"""Return a geometry containing the intersection of all geometries in
the ``GeoSeries``.
This method ignores None values when other geometries are present.
If all elements of the GeoSeries are None, an empty GeometryCollection is
returned.
Examples
--------
>>> from shapely.geometry import box
>>> s = geopandas.GeoSeries(
... [box(0, 0, 2, 2), box(1, 1, 3, 3), box(0, 0, 1.5, 1.5)]
... )
>>> s
0 POLYGON ((2 0, 2 2, 0 2, 0 0, 2 0))
1 POLYGON ((3 1, 3 3, 1 3, 1 1, 3 1))
2 POLYGON ((1.5 0, 1.5 1.5, 0 1.5, 0 0, 1.5 0))
dtype: geometry
>>> s.intersection_all()
<POLYGON ((1 1, 1 1.5, 1.5 1.5, 1.5 1, 1 1))>
"""
return self.geometry.values.intersection_all()
#
# Binary operations that return a pandas Series
#
def contains(self, other, align=None):
"""Return a ``Series`` of ``dtype('bool')`` with value ``True`` for
each aligned geometry that contains `other`.
An object is said to contain `other` if at least one point of `other` lies in
the interior and no points of `other` lie in the exterior of the object.
(Therefore, any given polygon does not contain its own boundary - there is not
any point that lies in the interior.)
If either object is empty, this operation returns ``False``.
This is the inverse of :meth:`within` in the sense that the expression
``a.contains(b) == b.within(a)`` always evaluates to ``True``.
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
Parameters
----------
other : GeoSeries or geometric object
The GeoSeries (elementwise) or geometric object to test if it
is contained.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
Series (bool)
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(0, 0), (0, 2)]),
... LineString([(0, 0), (0, 1)]),
... Point(0, 1),
... ],
... index=range(0, 4),
... )
>>> s2 = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (2, 2), (0, 2)]),
... Polygon([(0, 0), (1, 2), (0, 2)]),
... LineString([(0, 0), (0, 2)]),
... Point(0, 1),
... ],
... index=range(1, 5),
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 LINESTRING (0 0, 0 2)
2 LINESTRING (0 0, 0 1)
3 POINT (0 1)
dtype: geometry
>>> s2
1 POLYGON ((0 0, 2 2, 0 2, 0 0))
2 POLYGON ((0 0, 1 2, 0 2, 0 0))
3 LINESTRING (0 0, 0 2)
4 POINT (0 1)
dtype: geometry
We can check if each geometry of GeoSeries contains a single
geometry:
.. image:: ../../../_static/binary_op-03.svg
:align: center
>>> point = Point(0, 1)
>>> s.contains(point)
0 False
1 True
2 False
3 True
dtype: bool
We can also check two GeoSeries against each other, row by row.
The GeoSeries above have different indices. We can either align both GeoSeries
based on index values and compare elements with the same index using
``align=True`` or ignore index and compare elements based on their matching
order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s2.contains(s, align=True)
0 False
1 False
2 False
3 True
4 False
dtype: bool
>>> s2.contains(s, align=False)
1 True
2 False
3 True
4 True
dtype: bool
Notes
-----
This method works in a row-wise manner. It does not check if an element
of one GeoSeries ``contains`` *any* element of the other one.
See Also
--------
GeoSeries.contains_properly
GeoSeries.within
"""
return _binary_op("contains", self, other, align)
def contains_properly(self, other, align=None):
"""Return a ``Series`` of ``dtype('bool')`` with value ``True`` for
each aligned geometry that is completely inside ``other``, with no common
boundary points.
Geometry A contains geometry B properly if B intersects the interior of A but
not the boundary (or exterior). This means that a geometry A does not “contain
properly” itself, which contrasts with the :meth:`~GeoSeries.contains` method,
where common points on the boundary are allowed.
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
Parameters
----------
other : GeoSeries or geometric object
The GeoSeries (elementwise) or geometric object to test if it
is contained.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
Series (bool)
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(0, 0), (0, 2)]),
... LineString([(0, 0), (0, 1)]),
... Point(0, 1),
... ],
... index=range(0, 4),
... )
>>> s2 = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (2, 2), (0, 2)]),
... Polygon([(0, 0), (1, 2), (0, 2)]),
... LineString([(0, 0), (0, 2)]),
... Point(0, 1),
... ],
... index=range(1, 5),
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 LINESTRING (0 0, 0 2)
2 LINESTRING (0 0, 0 1)
3 POINT (0 1)
dtype: geometry
>>> s2
1 POLYGON ((0 0, 2 2, 0 2, 0 0))
2 POLYGON ((0 0, 1 2, 0 2, 0 0))
3 LINESTRING (0 0, 0 2)
4 POINT (0 1)
dtype: geometry
We can check if each geometry of GeoSeries contains a single
geometry:
.. image:: ../../../_static/binary_op-03.svg
:align: center
>>> point = Point(0, 1)
>>> s.contains_properly(point)
0 False
1 True
2 False
3 True
dtype: bool
We can also check two GeoSeries against each other, row by row.
The GeoSeries above have different indices. We can either align both GeoSeries
based on index values and compare elements with the same index using
``align=True`` or ignore index and compare elements based on their matching
order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s2.contains_properly(s, align=True)
0 False
1 False
2 False
3 True
4 False
dtype: bool
>>> s2.contains_properly(s, align=False)
1 False
2 False
3 False
4 True
dtype: bool
Compare it to the result of :meth:`~GeoSeries.contains`:
>>> s2.contains(s, align=False)
1 True
2 False
3 True
4 True
dtype: bool
Notes
-----
This method works in a row-wise manner. It does not check if an element
of one GeoSeries ``contains_properly`` *any* element of the other one.
See Also
--------
GeoSeries.contains
"""
return _binary_op("contains_properly", self, other, align)
def dwithin(self, other, distance, align=None):
"""Return a ``Series`` of ``dtype('bool')`` with value ``True`` for
each aligned geometry that is within a set distance from ``other``.
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
Parameters
----------
other : GeoSeries or geometric object
The GeoSeries (elementwise) or geometric object to test for
equality.
distance : float, np.array, pd.Series
Distance(s) to test if each geometry is within. A scalar distance will be
applied to all geometries. An array or Series will be applied elementwise.
If np.array or pd.Series are used then it must have same length as the
GeoSeries.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
Series (bool)
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(0, 0), (0, 2)]),
... LineString([(0, 0), (0, 1)]),
... Point(0, 1),
... ],
... index=range(0, 4),
... )
>>> s2 = geopandas.GeoSeries(
... [
... Polygon([(1, 0), (4, 2), (2, 2)]),
... Polygon([(2, 0), (3, 2), (2, 2)]),
... LineString([(2, 0), (2, 2)]),
... Point(1, 1),
... ],
... index=range(1, 5),
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 LINESTRING (0 0, 0 2)
2 LINESTRING (0 0, 0 1)
3 POINT (0 1)
dtype: geometry
>>> s2
1 POLYGON ((1 0, 4 2, 2 2, 1 0))
2 POLYGON ((2 0, 3 2, 2 2, 2 0))
3 LINESTRING (2 0, 2 2)
4 POINT (1 1)
dtype: geometry
We can check if each geometry of GeoSeries contains a single
geometry:
.. image:: ../../../_static/binary_op-03.svg
:align: center
>>> point = Point(0, 1)
>>> s2.dwithin(point, 1.8)
1 True
2 False
3 False
4 True
dtype: bool
We can also check two GeoSeries against each other, row by row.
The GeoSeries above have different indices. We can either align both GeoSeries
based on index values and compare elements with the same index using
``align=True`` or ignore index and compare elements based on their matching
order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s.dwithin(s2, distance=1, align=True)
0 False
1 True
2 False
3 False
4 False
dtype: bool
>>> s.dwithin(s2, distance=1, align=False)
0 True
1 False
2 False
3 True
dtype: bool
Notes
-----
This method works in a row-wise manner. It does not check if an element
of one GeoSeries is within the set distance of *any* element of the other one.
See Also
--------
GeoSeries.within
"""
return _binary_op("dwithin", self, other, distance=distance, align=align)
def geom_equals(self, other, align=None):
"""Return a ``Series`` of ``dtype('bool')`` with value ``True`` for
each aligned geometry equal to `other`.
An object is said to be equal to `other` if its set-theoretic
`boundary`, `interior`, and `exterior` coincides with those of the
other.
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
Parameters
----------
other : GeoSeries or geometric object
The GeoSeries (elementwise) or geometric object to test for
equality.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
Series (bool)
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (2, 2), (0, 2)]),
... Polygon([(0, 0), (1, 2), (0, 2)]),
... LineString([(0, 0), (0, 2)]),
... Point(0, 1),
... ],
... )
>>> s2 = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (2, 2), (0, 2)]),
... Polygon([(0, 0), (1, 2), (0, 2)]),
... Point(0, 1),
... LineString([(0, 0), (0, 2)]),
... ],
... index=range(1, 5),
... )
>>> s
0 POLYGON ((0 0, 2 2, 0 2, 0 0))
1 POLYGON ((0 0, 1 2, 0 2, 0 0))
2 LINESTRING (0 0, 0 2)
3 POINT (0 1)
dtype: geometry
>>> s2
1 POLYGON ((0 0, 2 2, 0 2, 0 0))
2 POLYGON ((0 0, 1 2, 0 2, 0 0))
3 POINT (0 1)
4 LINESTRING (0 0, 0 2)
dtype: geometry
We can check if each geometry of GeoSeries contains a single
geometry:
.. image:: ../../../_static/binary_op-03.svg
:align: center
>>> polygon = Polygon([(0, 0), (2, 2), (0, 2)])
>>> s.geom_equals(polygon)
0 True
1 False
2 False
3 False
dtype: bool
We can also check two GeoSeries against each other, row by row.
The GeoSeries above have different indices. We can either align both GeoSeries
based on index values and compare elements with the same index using
``align=True`` or ignore index and compare elements based on their matching
order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s.geom_equals(s2)
0 False
1 False
2 False
3 True
4 False
dtype: bool
>>> s.geom_equals(s2, align=False)
0 True
1 True
2 False
3 False
dtype: bool
Notes
-----
This method works in a row-wise manner. It does not check if an element
of one GeoSeries is equal to *any* element of the other one.
See Also
--------
GeoSeries.geom_equals_exact
GeoSeries.geom_equals_identical
"""
return _binary_op("geom_equals", self, other, align)
def geom_equals_exact(self, other, tolerance, align=None):
"""Return True for all geometries that equal aligned *other* to a given
tolerance, else False.
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
Parameters
----------
other : GeoSeries or geometric object
The GeoSeries (elementwise) or geometric object to compare to.
tolerance : float
Decimal place precision used when testing for approximate equality.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
Series (bool)
Examples
--------
>>> from shapely.geometry import Point
>>> s = geopandas.GeoSeries(
... [
... Point(0, 1.1),
... Point(0, 1.0),
... Point(0, 1.2),
... ]
... )
>>> s
0 POINT (0 1.1)
1 POINT (0 1)
2 POINT (0 1.2)
dtype: geometry
>>> s.geom_equals_exact(Point(0, 1), tolerance=0.1)
0 False
1 True
2 False
dtype: bool
>>> s.geom_equals_exact(Point(0, 1), tolerance=0.15)
0 True
1 True
2 False
dtype: bool
Notes
-----
This method works in a row-wise manner. It does not check if an element
of one GeoSeries is equal to *any* element of the other one.
See Also
--------
GeoSeries.geom_equals
GeoSeries.geom_equals_identical
"""
return _binary_op(
"geom_equals_exact", self, other, tolerance=tolerance, align=align
)
def geom_equals_identical(self, other, align=None):
"""Return True for all geometries that are identical aligned *other*, else
False.
This function verifies whether geometries are pointwise equivalent by checking
that the structure, ordering, and values of all vertices are identical in all
dimensions.
Similarly to :meth:`geom_equals_exact`, this function uses exact coordinate
equality and requires coordinates to be in the same order for all components
(vertices, rings, or parts) of a geometry. However, in contrast to
:meth:`geom_equals_exact`, this function does not allow specifying specify
a tolerance, and additionally requires all dimensions to be the same
(:meth:`geom_equals_exact` ignores the Z and M dimensions), where NaN values
are considered to be equal to other NaN values.
This function is the vectorized equivalent of scalar equality of geometry
objects (``a == b``, i.e. ``__eq__``).
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
Requires Shapely >= 2.1.
.. versionadded:: 1.1.0
Parameters
----------
other : GeoSeries or geometric object
The GeoSeries (elementwise) or geometric object to compare to.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
Series (bool)
Examples
--------
>>> from shapely.geometry import Point
>>> s = geopandas.GeoSeries(
... [
... Point(0, 1.1),
... Point(0, 1.0),
... Point(0, 1.2),
... ]
... )
>>> s
0 POINT (0 1.1)
1 POINT (0 1)
2 POINT (0 1.2)
dtype: geometry
>>> s.geom_equals_identical(Point(0, 1))
0 False
1 True
2 False
dtype: bool
Notes
-----
This method works in a row-wise manner. It does not check if an element
of one GeoSeries is equal to *any* element of the other one.
See Also
--------
GeoSeries.geom_equals
GeoSeries.geom_equals_exact
"""
return _binary_op("geom_equals_identical", self, other, align=align)
def crosses(self, other, align=None):
"""Return a ``Series`` of ``dtype('bool')`` with value ``True`` for
each aligned geometry that cross `other`.
An object is said to cross `other` if its `interior` intersects the
`interior` of the other but does not contain it, and the dimension of
the intersection is less than the dimension of the one or the other.
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
Parameters
----------
other : GeoSeries or geometric object
The GeoSeries (elementwise) or geometric object to test if is
crossed.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
Series (bool)
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (2, 2), (0, 2)]),
... LineString([(0, 0), (2, 2)]),
... LineString([(2, 0), (0, 2)]),
... Point(0, 1),
... ],
... )
>>> s2 = geopandas.GeoSeries(
... [
... LineString([(1, 0), (1, 3)]),
... LineString([(2, 0), (0, 2)]),
... Point(1, 1),
... Point(0, 1),
... ],
... index=range(1, 5),
... )
>>> s
0 POLYGON ((0 0, 2 2, 0 2, 0 0))
1 LINESTRING (0 0, 2 2)
2 LINESTRING (2 0, 0 2)
3 POINT (0 1)
dtype: geometry
>>> s2
1 LINESTRING (1 0, 1 3)
2 LINESTRING (2 0, 0 2)
3 POINT (1 1)
4 POINT (0 1)
dtype: geometry
We can check if each geometry of GeoSeries crosses a single
geometry:
.. image:: ../../../_static/binary_op-03.svg
:align: center
>>> line = LineString([(-1, 1), (3, 1)])
>>> s.crosses(line)
0 True
1 True
2 True
3 False
dtype: bool
We can also check two GeoSeries against each other, row by row.
The GeoSeries above have different indices. We can either align both GeoSeries
based on index values and compare elements with the same index using
``align=True`` or ignore index and compare elements based on their matching
order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s.crosses(s2, align=True)
0 False
1 True
2 False
3 False
4 False
dtype: bool
>>> s.crosses(s2, align=False)
0 True
1 True
2 False
3 False
dtype: bool
Notice that a line does not cross a point that it contains.
Notes
-----
This method works in a row-wise manner. It does not check if an element
of one GeoSeries ``crosses`` *any* element of the other one.
See Also
--------
GeoSeries.disjoint
GeoSeries.intersects
"""
return _binary_op("crosses", self, other, align)
def disjoint(self, other, align=None):
"""Return a ``Series`` of ``dtype('bool')`` with value ``True`` for
each aligned geometry disjoint to `other`.
An object is said to be disjoint to `other` if its `boundary` and
`interior` does not intersect at all with those of the other.
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
Parameters
----------
other : GeoSeries or geometric object
The GeoSeries (elementwise) or geometric object to test if is
disjoint.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
Series (bool)
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (2, 2), (0, 2)]),
... LineString([(0, 0), (2, 2)]),
... LineString([(2, 0), (0, 2)]),
... Point(0, 1),
... ],
... )
>>> s2 = geopandas.GeoSeries(
... [
... Polygon([(-1, 0), (-1, 2), (0, -2)]),
... LineString([(0, 0), (0, 1)]),
... Point(1, 1),
... Point(0, 0),
... ],
... )
>>> s
0 POLYGON ((0 0, 2 2, 0 2, 0 0))
1 LINESTRING (0 0, 2 2)
2 LINESTRING (2 0, 0 2)
3 POINT (0 1)
dtype: geometry
>>> s2
0 POLYGON ((-1 0, -1 2, 0 -2, -1 0))
1 LINESTRING (0 0, 0 1)
2 POINT (1 1)
3 POINT (0 0)
dtype: geometry
We can check each geometry of GeoSeries to a single
geometry:
.. image:: ../../../_static/binary_op-03.svg
:align: center
>>> line = LineString([(0, 0), (2, 0)])
>>> s.disjoint(line)
0 False
1 False
2 False
3 True
dtype: bool
We can also check two GeoSeries against each other, row by row.
We can either align both GeoSeries
based on index values and compare elements with the same index using
``align=True`` or ignore index and compare elements based on their matching
order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s.disjoint(s2)
0 True
1 False
2 False
3 True
dtype: bool
Notes
-----
This method works in a row-wise manner. It does not check if an element
of one GeoSeries is equal to *any* element of the other one.
See Also
--------
GeoSeries.intersects
GeoSeries.touches
"""
return _binary_op("disjoint", self, other, align)
def intersects(self, other, align=None):
"""Return a ``Series`` of ``dtype('bool')`` with value ``True`` for
each aligned geometry that intersects `other`.
An object is said to intersect `other` if its `boundary` and `interior`
intersects in any way with those of the other.
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
Parameters
----------
other : GeoSeries or geometric object
The GeoSeries (elementwise) or geometric object to test if is
intersected.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
Series (bool)
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (2, 2), (0, 2)]),
... LineString([(0, 0), (2, 2)]),
... LineString([(2, 0), (0, 2)]),
... Point(0, 1),
... ],
... )
>>> s2 = geopandas.GeoSeries(
... [
... LineString([(1, 0), (1, 3)]),
... LineString([(2, 0), (0, 2)]),
... Point(1, 1),
... Point(0, 1),
... ],
... index=range(1, 5),
... )
>>> s
0 POLYGON ((0 0, 2 2, 0 2, 0 0))
1 LINESTRING (0 0, 2 2)
2 LINESTRING (2 0, 0 2)
3 POINT (0 1)
dtype: geometry
>>> s2
1 LINESTRING (1 0, 1 3)
2 LINESTRING (2 0, 0 2)
3 POINT (1 1)
4 POINT (0 1)
dtype: geometry
We can check if each geometry of GeoSeries crosses a single
geometry:
.. image:: ../../../_static/binary_op-03.svg
:align: center
>>> line = LineString([(-1, 1), (3, 1)])
>>> s.intersects(line)
0 True
1 True
2 True
3 True
dtype: bool
We can also check two GeoSeries against each other, row by row.
The GeoSeries above have different indices. We can either align both GeoSeries
based on index values and compare elements with the same index using
``align=True`` or ignore index and compare elements based on their matching
order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s.intersects(s2, align=True)
0 False
1 True
2 True
3 False
4 False
dtype: bool
>>> s.intersects(s2, align=False)
0 True
1 True
2 True
3 True
dtype: bool
Notes
-----
This method works in a row-wise manner. It does not check if an element
of one GeoSeries ``crosses`` *any* element of the other one.
See Also
--------
GeoSeries.disjoint
GeoSeries.crosses
GeoSeries.touches
GeoSeries.intersection
"""
return _binary_op("intersects", self, other, align)
def overlaps(self, other, align=None):
"""Return True for all aligned geometries that overlap *other*, else False.
Geometries overlaps if they have more than one but not all
points in common, have the same dimension, and the intersection of the
interiors of the geometries has the same dimension as the geometries
themselves.
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
Parameters
----------
other : GeoSeries or geometric object
The GeoSeries (elementwise) or geometric object to test if
overlaps.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
Series (bool)
Examples
--------
>>> from shapely.geometry import Polygon, LineString, MultiPoint, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (2, 2), (0, 2)]),
... Polygon([(0, 0), (2, 2), (0, 2)]),
... LineString([(0, 0), (2, 2)]),
... MultiPoint([(0, 0), (0, 1)]),
... ],
... )
>>> s2 = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (2, 0), (0, 2)]),
... LineString([(0, 1), (1, 1)]),
... LineString([(1, 1), (3, 3)]),
... Point(0, 1),
... ],
... index=range(1, 5),
... )
>>> s
0 POLYGON ((0 0, 2 2, 0 2, 0 0))
1 POLYGON ((0 0, 2 2, 0 2, 0 0))
2 LINESTRING (0 0, 2 2)
3 MULTIPOINT ((0 0), (0 1))
dtype: geometry
>>> s2
1 POLYGON ((0 0, 2 0, 0 2, 0 0))
2 LINESTRING (0 1, 1 1)
3 LINESTRING (1 1, 3 3)
4 POINT (0 1)
dtype: geometry
We can check if each geometry of GeoSeries overlaps a single
geometry:
.. image:: ../../../_static/binary_op-03.svg
:align: center
>>> polygon = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
>>> s.overlaps(polygon)
0 True
1 True
2 False
3 False
dtype: bool
We can also check two GeoSeries against each other, row by row.
The GeoSeries above have different indices. We can either align both GeoSeries
based on index values and compare elements with the same index using
``align=True`` or ignore index and compare elements based on their matching
order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s.overlaps(s2)
0 False
1 True
2 False
3 False
4 False
dtype: bool
>>> s.overlaps(s2, align=False)
0 True
1 False
2 True
3 False
dtype: bool
Notes
-----
This method works in a row-wise manner. It does not check if an element
of one GeoSeries ``overlaps`` *any* element of the other one.
See Also
--------
GeoSeries.crosses
GeoSeries.intersects
"""
return _binary_op("overlaps", self, other, align)
def touches(self, other, align=None):
"""Return a ``Series`` of ``dtype('bool')`` with value ``True`` for
each aligned geometry that touches `other`.
An object is said to touch `other` if it has at least one point in
common with `other` and its interior does not intersect with any part
of the other. Overlapping features therefore do not touch.
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
Parameters
----------
other : GeoSeries or geometric object
The GeoSeries (elementwise) or geometric object to test if is
touched.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
Series (bool)
Examples
--------
>>> from shapely.geometry import Polygon, LineString, MultiPoint, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (2, 2), (0, 2)]),
... Polygon([(0, 0), (2, 2), (0, 2)]),
... LineString([(0, 0), (2, 2)]),
... MultiPoint([(0, 0), (0, 1)]),
... ],
... )
>>> s2 = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (-2, 0), (0, -2)]),
... LineString([(0, 1), (1, 1)]),
... LineString([(1, 1), (3, 0)]),
... Point(0, 1),
... ],
... index=range(1, 5),
... )
>>> s
0 POLYGON ((0 0, 2 2, 0 2, 0 0))
1 POLYGON ((0 0, 2 2, 0 2, 0 0))
2 LINESTRING (0 0, 2 2)
3 MULTIPOINT ((0 0), (0 1))
dtype: geometry
>>> s2
1 POLYGON ((0 0, -2 0, 0 -2, 0 0))
2 LINESTRING (0 1, 1 1)
3 LINESTRING (1 1, 3 0)
4 POINT (0 1)
dtype: geometry
We can check if each geometry of GeoSeries touches a single
geometry:
.. image:: ../../../_static/binary_op-03.svg
:align: center
>>> line = LineString([(0, 0), (-1, -2)])
>>> s.touches(line)
0 True
1 True
2 True
3 True
dtype: bool
We can also check two GeoSeries against each other, row by row.
The GeoSeries above have different indices. We can either align both GeoSeries
based on index values and compare elements with the same index using
``align=True`` or ignore index and compare elements based on their matching
order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s.touches(s2, align=True)
0 False
1 True
2 True
3 False
4 False
dtype: bool
>>> s.touches(s2, align=False)
0 True
1 False
2 True
3 False
dtype: bool
Notes
-----
This method works in a row-wise manner. It does not check if an element
of one GeoSeries ``touches`` *any* element of the other one.
See Also
--------
GeoSeries.overlaps
GeoSeries.intersects
"""
return _binary_op("touches", self, other, align)
def within(self, other, align=None):
"""Return a ``Series`` of ``dtype('bool')`` with value ``True`` for
each aligned geometry that is within `other`.
An object is said to be within `other` if at least one of its points is located
in the `interior` and no points are located in the `exterior` of the other.
If either object is empty, this operation returns ``False``.
This is the inverse of :meth:`contains` in the sense that the
expression ``a.within(b) == b.contains(a)`` always evaluates to
``True``.
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
Parameters
----------
other : GeoSeries or geometric object
The GeoSeries (elementwise) or geometric object to test if each
geometry is within.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
Series (bool)
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (2, 2), (0, 2)]),
... Polygon([(0, 0), (1, 2), (0, 2)]),
... LineString([(0, 0), (0, 2)]),
... Point(0, 1),
... ],
... )
>>> s2 = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(0, 0), (0, 2)]),
... LineString([(0, 0), (0, 1)]),
... Point(0, 1),
... ],
... index=range(1, 5),
... )
>>> s
0 POLYGON ((0 0, 2 2, 0 2, 0 0))
1 POLYGON ((0 0, 1 2, 0 2, 0 0))
2 LINESTRING (0 0, 0 2)
3 POINT (0 1)
dtype: geometry
>>> s2
1 POLYGON ((0 0, 1 1, 0 1, 0 0))
2 LINESTRING (0 0, 0 2)
3 LINESTRING (0 0, 0 1)
4 POINT (0 1)
dtype: geometry
We can check if each geometry of GeoSeries is within a single
geometry:
.. image:: ../../../_static/binary_op-03.svg
:align: center
>>> polygon = Polygon([(0, 0), (2, 2), (0, 2)])
>>> s.within(polygon)
0 True
1 True
2 False
3 False
dtype: bool
We can also check two GeoSeries against each other, row by row.
The GeoSeries above have different indices. We can either align both GeoSeries
based on index values and compare elements with the same index using
``align=True`` or ignore index and compare elements based on their matching
order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s2.within(s)
0 False
1 False
2 True
3 False
4 False
dtype: bool
>>> s2.within(s, align=False)
1 True
2 False
3 True
4 True
dtype: bool
Notes
-----
This method works in a row-wise manner. It does not check if an element
of one GeoSeries is ``within`` *any* element of the other one.
See Also
--------
GeoSeries.contains
"""
return _binary_op("within", self, other, align)
def covers(self, other, align=None):
"""Return a ``Series`` of ``dtype('bool')`` with value ``True`` for
each aligned geometry that is entirely covering `other`.
An object A is said to cover another object B if no points of B lie
in the exterior of A.
If either object is empty, this operation returns ``False``.
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
See
https://lin-ear-th-inking.blogspot.com/2007/06/subtleties-of-ogc-covers-spatial.html
for reference.
Parameters
----------
other : Geoseries or geometric object
The Geoseries (elementwise) or geometric object to check is being covered.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
Series (bool)
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),
... Polygon([(0, 0), (2, 2), (0, 2)]),
... LineString([(0, 0), (2, 2)]),
... Point(0, 0),
... ],
... )
>>> s2 = geopandas.GeoSeries(
... [
... Polygon([(0.5, 0.5), (1.5, 0.5), (1.5, 1.5), (0.5, 1.5)]),
... Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),
... LineString([(1, 1), (1.5, 1.5)]),
... Point(0, 0),
... ],
... index=range(1, 5),
... )
>>> s
0 POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))
1 POLYGON ((0 0, 2 2, 0 2, 0 0))
2 LINESTRING (0 0, 2 2)
3 POINT (0 0)
dtype: geometry
>>> s2
1 POLYGON ((0.5 0.5, 1.5 0.5, 1.5 1.5, 0.5 1.5, ...
2 POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))
3 LINESTRING (1 1, 1.5 1.5)
4 POINT (0 0)
dtype: geometry
We can check if each geometry of GeoSeries covers a single
geometry:
.. image:: ../../../_static/binary_op-03.svg
:align: center
>>> poly = Polygon([(0, 0), (2, 0), (2, 2), (0, 2)])
>>> s.covers(poly)
0 True
1 False
2 False
3 False
dtype: bool
We can also check two GeoSeries against each other, row by row.
The GeoSeries above have different indices. We can either align both GeoSeries
based on index values and compare elements with the same index using
``align=True`` or ignore index and compare elements based on their matching
order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s.covers(s2, align=True)
0 False
1 False
2 False
3 False
4 False
dtype: bool
>>> s.covers(s2, align=False)
0 True
1 False
2 True
3 True
dtype: bool
Notes
-----
This method works in a row-wise manner. It does not check if an element
of one GeoSeries ``covers`` *any* element of the other one.
See Also
--------
GeoSeries.covered_by
GeoSeries.overlaps
"""
return _binary_op("covers", self, other, align)
def covered_by(self, other, align=None):
"""Return a ``Series`` of ``dtype('bool')`` with value ``True`` for
each aligned geometry that is entirely covered by `other`.
An object A is said to cover another object B if no points of B lie
in the exterior of A.
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
See
https://lin-ear-th-inking.blogspot.com/2007/06/subtleties-of-ogc-covers-spatial.html
for reference.
Parameters
----------
other : Geoseries or geometric object
The Geoseries (elementwise) or geometric object to check is being covered.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
Series (bool)
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0.5, 0.5), (1.5, 0.5), (1.5, 1.5), (0.5, 1.5)]),
... Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),
... LineString([(1, 1), (1.5, 1.5)]),
... Point(0, 0),
... ],
... )
>>> s2 = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),
... Polygon([(0, 0), (2, 2), (0, 2)]),
... LineString([(0, 0), (2, 2)]),
... Point(0, 0),
... ],
... index=range(1, 5),
... )
>>> s
0 POLYGON ((0.5 0.5, 1.5 0.5, 1.5 1.5, 0.5 1.5, ...
1 POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))
2 LINESTRING (1 1, 1.5 1.5)
3 POINT (0 0)
dtype: geometry
>>>
>>> s2
1 POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))
2 POLYGON ((0 0, 2 2, 0 2, 0 0))
3 LINESTRING (0 0, 2 2)
4 POINT (0 0)
dtype: geometry
We can check if each geometry of GeoSeries is covered by a single
geometry:
.. image:: ../../../_static/binary_op-03.svg
:align: center
>>> poly = Polygon([(0, 0), (2, 0), (2, 2), (0, 2)])
>>> s.covered_by(poly)
0 True
1 True
2 True
3 True
dtype: bool
We can also check two GeoSeries against each other, row by row.
The GeoSeries above have different indices. We can either align both GeoSeries
based on index values and compare elements with the same index using
``align=True`` or ignore index and compare elements based on their matching
order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s.covered_by(s2, align=True)
0 False
1 True
2 True
3 True
4 False
dtype: bool
>>> s.covered_by(s2, align=False)
0 True
1 False
2 True
3 True
dtype: bool
Notes
-----
This method works in a row-wise manner. It does not check if an element
of one GeoSeries is ``covered_by`` *any* element of the other one.
See Also
--------
GeoSeries.covers
GeoSeries.overlaps
"""
return _binary_op("covered_by", self, other, align)
def distance(self, other, align=None):
"""Return a ``Series`` containing the distance to aligned `other`.
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
Parameters
----------
other : Geoseries or geometric object
The Geoseries (elementwise) or geometric object to find the
distance to.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
Series (float)
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 0), (1, 1)]),
... Polygon([(0, 0), (-1, 0), (-1, 1)]),
... LineString([(1, 1), (0, 0)]),
... Point(0, 0),
... ],
... )
>>> s2 = geopandas.GeoSeries(
... [
... Polygon([(0.5, 0.5), (1.5, 0.5), (1.5, 1.5), (0.5, 1.5)]),
... Point(3, 1),
... LineString([(1, 0), (2, 0)]),
... Point(0, 1),
... ],
... index=range(1, 5),
... )
>>> s
0 POLYGON ((0 0, 1 0, 1 1, 0 0))
1 POLYGON ((0 0, -1 0, -1 1, 0 0))
2 LINESTRING (1 1, 0 0)
3 POINT (0 0)
dtype: geometry
>>> s2
1 POLYGON ((0.5 0.5, 1.5 0.5, 1.5 1.5, 0.5 1.5, ...
2 POINT (3 1)
3 LINESTRING (1 0, 2 0)
4 POINT (0 1)
dtype: geometry
We can check the distance of each geometry of GeoSeries to a single
geometry:
.. image:: ../../../_static/binary_op-03.svg
:align: center
>>> point = Point(-1, 0)
>>> s.distance(point)
0 1.0
1 0.0
2 1.0
3 1.0
dtype: float64
We can also check two GeoSeries against each other, row by row.
The GeoSeries above have different indices. We can either align both GeoSeries
based on index values and use elements with the same index using
``align=True`` or ignore index and use elements based on their matching
order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s.distance(s2, align=True)
0 NaN
1 0.707107
2 2.000000
3 1.000000
4 NaN
dtype: float64
>>> s.distance(s2, align=False)
0 0.000000
1 3.162278
2 0.707107
3 1.000000
dtype: float64
"""
return _binary_op("distance", self, other, align)
def hausdorff_distance(self, other, align=None, densify=None):
"""Return a ``Series`` containing the Hausdorff distance to aligned `other`.
The Hausdorff distance is the largest distance consisting of any point in `self`
with the nearest point in `other`.
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
Parameters
----------
other : GeoSeries or geometric object
The Geoseries (elementwise) or geometric object to find the
distance to.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
densify : float (default None)
A value between 0 and 1, that splits each subsegment of a line string
into equal length segments, making the approximation less coarse.
A densify value of 0.5 will add a point halfway between each pair of
points. A densify value of 0.25 will add a point a quarter of the way
between each pair of points.
Returns
-------
Series (float)
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 0), (1, 1)]),
... Polygon([(0, 0), (-1, 0), (-1, 1)]),
... LineString([(1, 1), (0, 0)]),
... Point(0, 0),
... ],
... )
>>> s2 = geopandas.GeoSeries(
... [
... Polygon([(0.5, 0.5), (1.5, 0.5), (1.5, 1.5), (0.5, 1.5)]),
... Point(3, 1),
... LineString([(1, 0), (2, 0)]),
... Point(0, 1),
... ],
... index=range(1, 5),
... )
>>> s
0 POLYGON ((0 0, 1 0, 1 1, 0 0))
1 POLYGON ((0 0, -1 0, -1 1, 0 0))
2 LINESTRING (1 1, 0 0)
3 POINT (0 0)
dtype: geometry
>>> s2
1 POLYGON ((0.5 0.5, 1.5 0.5, 1.5 1.5, 0.5 1.5, ...
2 POINT (3 1)
3 LINESTRING (1 0, 2 0)
4 POINT (0 1)
dtype: geometry
We can check the hausdorff distance of each geometry of GeoSeries
to a single geometry:
>>> point = Point(-1, 0)
>>> s.hausdorff_distance(point)
0 2.236068
1 1.000000
2 2.236068
3 1.000000
dtype: float64
We can also check two GeoSeries against each other, row by row.
The GeoSeries above have different indices. We can either align both GeoSeries
based on index values and use elements with the same index using
``align=True`` or ignore index and use elements based on their matching
order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s.hausdorff_distance(s2, align=True)
0 NaN
1 2.121320
2 3.162278
3 2.000000
4 NaN
dtype: float64
>>> s.hausdorff_distance(s2, align=False)
0 0.707107
1 4.123106
2 1.414214
3 1.000000
dtype: float64
We can also set a densify value, which is a float between 0 and 1 and
signifies the fraction of the distance between each pair of points that will
be used as the distance between the points when densifying.
>>> l1 = geopandas.GeoSeries([LineString([(130, 0), (0, 0), (0, 150)])])
>>> l2 = geopandas.GeoSeries([LineString([(10, 10), (10, 150), (130, 10)])])
>>> l1.hausdorff_distance(l2)
0 14.142136
dtype: float64
>>> l1.hausdorff_distance(l2, densify=0.25)
0 70.0
dtype: float64
"""
return _binary_op("hausdorff_distance", self, other, align, densify=densify)
def frechet_distance(self, other, align=None, densify=None):
"""Return a ``Series`` containing the Frechet distance to aligned `other`.
The Fréchet distance is a measure of similarity: it is the greatest distance
between any point in A and the closest point in B. The discrete distance is an
approximation of this metric: only vertices are considered. The parameter
``densify`` makes this approximation less coarse by splitting the line segments
between vertices before computing the distance.
Fréchet distance sweep continuously along their respective curves and the
direction of curves is significant. This makes it a better measure of similarity
than Hausdorff distance for curve or surface matching.
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
Parameters
----------
other : GeoSeries or geometric object
The Geoseries (elementwise) or geometric object to find the
distance to.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
densify : float (default None)
A value between 0 and 1, that splits each subsegment of a line string
into equal length segments, making the approximation less coarse.
A densify value of 0.5 will add a point halfway between each pair of
points. A densify value of 0.25 will add a point every quarter of the way
between each pair of points.
Returns
-------
Series (float)
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 0), (1, 1)]),
... Polygon([(0, 0), (-1, 0), (-1, 1)]),
... LineString([(1, 1), (0, 0)]),
... Point(0, 0),
... ],
... )
>>> s2 = geopandas.GeoSeries(
... [
... Polygon([(0.5, 0.5), (1.5, 0.5), (1.5, 1.5), (0.5, 1.5)]),
... Point(3, 1),
... LineString([(1, 0), (2, 0)]),
... Point(0, 1),
... ],
... index=range(1, 5),
... )
>>> s
0 POLYGON ((0 0, 1 0, 1 1, 0 0))
1 POLYGON ((0 0, -1 0, -1 1, 0 0))
2 LINESTRING (1 1, 0 0)
3 POINT (0 0)
dtype: geometry
>>> s2
1 POLYGON ((0.5 0.5, 1.5 0.5, 1.5 1.5, 0.5 1.5, ...
2 POINT (3 1)
3 LINESTRING (1 0, 2 0)
4 POINT (0 1)
dtype: geometry
We can check the frechet distance of each geometry of GeoSeries
to a single geometry:
>>> point = Point(-1, 0)
>>> s.frechet_distance(point)
0 2.236068
1 1.000000
2 2.236068
3 1.000000
dtype: float64
We can also check two GeoSeries against each other, row by row.
The GeoSeries above have different indices. We can either align both GeoSeries
based on index values and use elements with the same index using
``align=True`` or ignore index and use elements based on their matching
order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s.frechet_distance(s2, align=True)
0 NaN
1 2.121320
2 3.162278
3 2.000000
4 NaN
dtype: float64
>>> s.frechet_distance(s2, align=False)
0 0.707107
1 4.123106
2 2.000000
3 1.000000
dtype: float64
We can also set a ``densify`` value, which is a float between 0 and 1 and
signifies the fraction of the distance between each pair of points that will
be used as the distance between the points when densifying.
>>> l1 = geopandas.GeoSeries([LineString([(0, 0), (10, 0), (0, 15)])])
>>> l2 = geopandas.GeoSeries([LineString([(0, 0), (20, 15), (9, 11)])])
>>> l1.frechet_distance(l2)
0 18.027756
dtype: float64
>>> l1.frechet_distance(l2, densify=0.25)
0 16.77051
dtype: float64
"""
return _binary_op("frechet_distance", self, other, align, densify=densify)
#
# Binary operations that return a GeoSeries
#
def difference(self, other, align=None):
"""Return a ``GeoSeries`` of the points in each aligned geometry that
are not in `other`.
.. image:: ../../../_static/binary_geo-difference.svg
:align: center
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
Parameters
----------
other : Geoseries or geometric object
The Geoseries (elementwise) or geometric object to find the
difference to.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
GeoSeries
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (2, 2), (0, 2)]),
... Polygon([(0, 0), (2, 2), (0, 2)]),
... LineString([(0, 0), (2, 2)]),
... LineString([(2, 0), (0, 2)]),
... Point(0, 1),
... ],
... )
>>> s2 = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(1, 0), (1, 3)]),
... LineString([(2, 0), (0, 2)]),
... Point(1, 1),
... Point(0, 1),
... ],
... index=range(1, 6),
... )
>>> s
0 POLYGON ((0 0, 2 2, 0 2, 0 0))
1 POLYGON ((0 0, 2 2, 0 2, 0 0))
2 LINESTRING (0 0, 2 2)
3 LINESTRING (2 0, 0 2)
4 POINT (0 1)
dtype: geometry
>>> s2
1 POLYGON ((0 0, 1 1, 0 1, 0 0))
2 LINESTRING (1 0, 1 3)
3 LINESTRING (2 0, 0 2)
4 POINT (1 1)
5 POINT (0 1)
dtype: geometry
We can do difference of each geometry and a single
shapely geometry:
.. image:: ../../../_static/binary_op-03.svg
:align: center
>>> s.difference(Polygon([(0, 0), (1, 1), (0, 1)]))
0 POLYGON ((0 2, 2 2, 1 1, 0 1, 0 2))
1 POLYGON ((0 2, 2 2, 1 1, 0 1, 0 2))
2 LINESTRING (1 1, 2 2)
3 MULTILINESTRING ((2 0, 1 1), (1 1, 0 2))
4 POINT EMPTY
dtype: geometry
We can also check two GeoSeries against each other, row by row.
The GeoSeries above have different indices. We can either align both GeoSeries
based on index values and compare elements with the same index using
``align=True`` or ignore index and compare elements based on their matching
order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s.difference(s2, align=True)
0 None
1 POLYGON ((0 2, 2 2, 1 1, 0 1, 0 2))
2 MULTILINESTRING ((0 0, 1 1), (1 1, 2 2))
3 LINESTRING EMPTY
4 POINT (0 1)
5 None
dtype: geometry
>>> s.difference(s2, align=False)
0 POLYGON ((0 2, 2 2, 1 1, 0 1, 0 2))
1 POLYGON ((0 0, 0 2, 1 2, 2 2, 1 1, 0 0))
2 MULTILINESTRING ((0 0, 1 1), (1 1, 2 2))
3 LINESTRING (2 0, 0 2)
4 POINT EMPTY
dtype: geometry
See Also
--------
GeoSeries.symmetric_difference
GeoSeries.union
GeoSeries.intersection
"""
return _binary_geo("difference", self, other, align)
def symmetric_difference(self, other, align=None):
"""Return a ``GeoSeries`` of the symmetric difference of points in
each aligned geometry with `other`.
For each geometry, the symmetric difference consists of points in the
geometry not in `other`, and points in `other` not in the geometry.
.. image:: ../../../_static/binary_geo-symm_diff.svg
:align: center
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
Parameters
----------
other : Geoseries or geometric object
The Geoseries (elementwise) or geometric object to find the
symmetric difference to.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
GeoSeries
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (2, 2), (0, 2)]),
... Polygon([(0, 0), (2, 2), (0, 2)]),
... LineString([(0, 0), (2, 2)]),
... LineString([(2, 0), (0, 2)]),
... Point(0, 1),
... ],
... )
>>> s2 = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(1, 0), (1, 3)]),
... LineString([(2, 0), (0, 2)]),
... Point(1, 1),
... Point(0, 1),
... ],
... index=range(1, 6),
... )
>>> s
0 POLYGON ((0 0, 2 2, 0 2, 0 0))
1 POLYGON ((0 0, 2 2, 0 2, 0 0))
2 LINESTRING (0 0, 2 2)
3 LINESTRING (2 0, 0 2)
4 POINT (0 1)
dtype: geometry
>>> s2
1 POLYGON ((0 0, 1 1, 0 1, 0 0))
2 LINESTRING (1 0, 1 3)
3 LINESTRING (2 0, 0 2)
4 POINT (1 1)
5 POINT (0 1)
dtype: geometry
We can do symmetric difference of each geometry and a single
shapely geometry:
.. image:: ../../../_static/binary_op-03.svg
:align: center
>>> s.symmetric_difference(Polygon([(0, 0), (1, 1), (0, 1)]))
0 POLYGON ((0 2, 2 2, 1 1, 0 1, 0 2))
1 POLYGON ((0 2, 2 2, 1 1, 0 1, 0 2))
2 GEOMETRYCOLLECTION (POLYGON ((0 0, 0 1, 1 1, 0...
3 GEOMETRYCOLLECTION (POLYGON ((0 0, 0 1, 1 1, 0...
4 POLYGON ((0 1, 1 1, 0 0, 0 1))
dtype: geometry
We can also check two GeoSeries against each other, row by row.
The GeoSeries above have different indices. We can either align both GeoSeries
based on index values and compare elements with the same index using
``align=True`` or ignore index and compare elements based on their matching
order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s.symmetric_difference(s2, align=True)
0 None
1 POLYGON ((0 2, 2 2, 1 1, 0 1, 0 2))
2 MULTILINESTRING ((0 0, 1 1), (1 1, 2 2), (1 0,...
3 LINESTRING EMPTY
4 MULTIPOINT ((0 1), (1 1))
5 None
dtype: geometry
>>> s.symmetric_difference(s2, align=False)
0 POLYGON ((0 2, 2 2, 1 1, 0 1, 0 2))
1 GEOMETRYCOLLECTION (POLYGON ((0 0, 0 2, 1 2, 2...
2 MULTILINESTRING ((0 0, 1 1), (1 1, 2 2), (2 0,...
3 LINESTRING (2 0, 0 2)
4 POINT EMPTY
dtype: geometry
See Also
--------
GeoSeries.difference
GeoSeries.union
GeoSeries.intersection
"""
return _binary_geo("symmetric_difference", self, other, align)
def union(self, other, align=None):
"""Return a ``GeoSeries`` of the union of points in each aligned geometry with
`other`.
.. image:: ../../../_static/binary_geo-union.svg
:align: center
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
Parameters
----------
other : Geoseries or geometric object
The Geoseries (elementwise) or geometric object to find the union
with.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
GeoSeries
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (2, 2), (0, 2)]),
... Polygon([(0, 0), (2, 2), (0, 2)]),
... LineString([(0, 0), (2, 2)]),
... LineString([(2, 0), (0, 2)]),
... Point(0, 1),
... ],
... )
>>> s2 = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(1, 0), (1, 3)]),
... LineString([(2, 0), (0, 2)]),
... Point(1, 1),
... Point(0, 1),
... ],
... index=range(1, 6),
... )
>>> s
0 POLYGON ((0 0, 2 2, 0 2, 0 0))
1 POLYGON ((0 0, 2 2, 0 2, 0 0))
2 LINESTRING (0 0, 2 2)
3 LINESTRING (2 0, 0 2)
4 POINT (0 1)
dtype: geometry
>>>
>>> s2
1 POLYGON ((0 0, 1 1, 0 1, 0 0))
2 LINESTRING (1 0, 1 3)
3 LINESTRING (2 0, 0 2)
4 POINT (1 1)
5 POINT (0 1)
dtype: geometry
We can do union of each geometry and a single
shapely geometry:
.. image:: ../../../_static/binary_op-03.svg
:align: center
>>> s.union(Polygon([(0, 0), (1, 1), (0, 1)]))
0 POLYGON ((0 0, 0 1, 0 2, 2 2, 1 1, 0 0))
1 POLYGON ((0 0, 0 1, 0 2, 2 2, 1 1, 0 0))
2 GEOMETRYCOLLECTION (POLYGON ((0 0, 0 1, 1 1, 0...
3 GEOMETRYCOLLECTION (POLYGON ((0 0, 0 1, 1 1, 0...
4 POLYGON ((0 1, 1 1, 0 0, 0 1))
dtype: geometry
We can also check two GeoSeries against each other, row by row.
The GeoSeries above have different indices. We can either align both GeoSeries
based on index values and compare elements with the same index using
``align=True`` or ignore index and compare elements based on their matching
order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s.union(s2, align=True)
0 None
1 POLYGON ((0 0, 0 1, 0 2, 2 2, 1 1, 0 0))
2 MULTILINESTRING ((0 0, 1 1), (1 1, 2 2), (1 0,...
3 LINESTRING (2 0, 0 2)
4 MULTIPOINT ((0 1), (1 1))
5 None
dtype: geometry
>>> s.union(s2, align=False)
0 POLYGON ((0 0, 0 1, 0 2, 2 2, 1 1, 0 0))
1 GEOMETRYCOLLECTION (POLYGON ((0 0, 0 2, 1 2, 2...
2 MULTILINESTRING ((0 0, 1 1), (1 1, 2 2), (2 0,...
3 LINESTRING (2 0, 0 2)
4 POINT (0 1)
dtype: geometry
See Also
--------
GeoSeries.symmetric_difference
GeoSeries.difference
GeoSeries.intersection
"""
return _binary_geo("union", self, other, align)
def intersection(self, other, align=None):
"""Return a ``GeoSeries`` of the intersection of points in each
aligned geometry with `other`.
.. image:: ../../../_static/binary_geo-intersection.svg
:align: center
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
Parameters
----------
other : Geoseries or geometric object
The Geoseries (elementwise) or geometric object to find the
intersection with.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
GeoSeries
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (2, 2), (0, 2)]),
... Polygon([(0, 0), (2, 2), (0, 2)]),
... LineString([(0, 0), (2, 2)]),
... LineString([(2, 0), (0, 2)]),
... Point(0, 1),
... ],
... )
>>> s2 = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(1, 0), (1, 3)]),
... LineString([(2, 0), (0, 2)]),
... Point(1, 1),
... Point(0, 1),
... ],
... index=range(1, 6),
... )
>>> s
0 POLYGON ((0 0, 2 2, 0 2, 0 0))
1 POLYGON ((0 0, 2 2, 0 2, 0 0))
2 LINESTRING (0 0, 2 2)
3 LINESTRING (2 0, 0 2)
4 POINT (0 1)
dtype: geometry
>>> s2
1 POLYGON ((0 0, 1 1, 0 1, 0 0))
2 LINESTRING (1 0, 1 3)
3 LINESTRING (2 0, 0 2)
4 POINT (1 1)
5 POINT (0 1)
dtype: geometry
We can also do intersection of each geometry and a single
shapely geometry:
.. image:: ../../../_static/binary_op-03.svg
:align: center
>>> s.intersection(Polygon([(0, 0), (1, 1), (0, 1)]))
0 POLYGON ((0 0, 0 1, 1 1, 0 0))
1 POLYGON ((0 0, 0 1, 1 1, 0 0))
2 LINESTRING (0 0, 1 1)
3 POINT (1 1)
4 POINT (0 1)
dtype: geometry
We can also check two GeoSeries against each other, row by row.
The GeoSeries above have different indices. We can either align both GeoSeries
based on index values and compare elements with the same index using
``align=True`` or ignore index and compare elements based on their matching
order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s.intersection(s2, align=True)
0 None
1 POLYGON ((0 0, 0 1, 1 1, 0 0))
2 POINT (1 1)
3 LINESTRING (2 0, 0 2)
4 POINT EMPTY
5 None
dtype: geometry
>>> s.intersection(s2, align=False)
0 POLYGON ((0 0, 0 1, 1 1, 0 0))
1 LINESTRING (1 1, 1 2)
2 POINT (1 1)
3 POINT (1 1)
4 POINT (0 1)
dtype: geometry
See Also
--------
GeoSeries.difference
GeoSeries.symmetric_difference
GeoSeries.union
"""
return _binary_geo("intersection", self, other, align)
def clip_by_rect(self, xmin, ymin, xmax, ymax):
"""Return a ``GeoSeries`` of the portions of geometry within the given
rectangle.
Note that the results are not exactly equal to
:meth:`~GeoSeries.intersection()`. E.g. in edge cases,
:meth:`~GeoSeries.clip_by_rect()` will not return a point just touching the
rectangle. Check the examples section below for some of these exceptions.
The geometry is clipped in a fast but possibly dirty way. The output is not
guaranteed to be valid. No exceptions will be raised for topological errors.
Note: empty geometries or geometries that do not overlap with the specified
bounds will result in ``GEOMETRYCOLLECTION EMPTY``.
Parameters
----------
xmin: float
Minimum x value of the rectangle
ymin: float
Minimum y value of the rectangle
xmax: float
Maximum x value of the rectangle
ymax: float
Maximum y value of the rectangle
Returns
-------
GeoSeries
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (2, 2), (0, 2)]),
... Polygon([(0, 0), (2, 2), (0, 2)]),
... LineString([(0, 0), (2, 2)]),
... LineString([(2, 0), (0, 2)]),
... Point(0, 1),
... ],
... )
>>> bounds = (0, 0, 1, 1)
>>> s
0 POLYGON ((0 0, 2 2, 0 2, 0 0))
1 POLYGON ((0 0, 2 2, 0 2, 0 0))
2 LINESTRING (0 0, 2 2)
3 LINESTRING (2 0, 0 2)
4 POINT (0 1)
dtype: geometry
>>> s.clip_by_rect(*bounds)
0 POLYGON ((0 0, 0 1, 1 1, 0 0))
1 POLYGON ((0 0, 0 1, 1 1, 0 0))
2 LINESTRING (0 0, 1 1)
3 GEOMETRYCOLLECTION EMPTY
4 GEOMETRYCOLLECTION EMPTY
dtype: geometry
See Also
--------
GeoSeries.intersection
"""
from .geoseries import GeoSeries
geometry_array = GeometryArray(self.geometry.values)
clipped_geometry = geometry_array.clip_by_rect(xmin, ymin, xmax, ymax)
return GeoSeries(clipped_geometry, index=self.index, crs=self.crs)
def shortest_line(self, other, align=None):
"""Return the shortest two-point line between two geometries.
The resulting line consists of two points, representing the nearest points
between the geometry pair. The line always starts in the first geometry a
and ends in he second geometry b. The endpoints of the line will not
necessarily be existing vertices of the input geometries a and b, but
can also be a point along a line segment.
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
Parameters
----------
other : Geoseries or geometric object
The Geoseries (elementwise) or geometric object to find the
shortest line with.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
GeoSeries
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (2, 2), (0, 2)]),
... Polygon([(0, 0), (2, 2), (0, 2)]),
... LineString([(0, 0), (2, 2)]),
... LineString([(2, 0), (0, 2)]),
... Point(0, 1),
... ],
... )
>>> s
0 POLYGON ((0 0, 2 2, 0 2, 0 0))
1 POLYGON ((0 0, 2 2, 0 2, 0 0))
2 LINESTRING (0 0, 2 2)
3 LINESTRING (2 0, 0 2)
4 POINT (0 1)
dtype: geometry
We can also do intersection of each geometry and a single
shapely geometry:
.. image:: ../../../_static/binary_op-03.svg
:align: center
>>> p = Point(3, 3)
>>> s.shortest_line(p)
0 LINESTRING (2 2, 3 3)
1 LINESTRING (2 2, 3 3)
2 LINESTRING (2 2, 3 3)
3 LINESTRING (1 1, 3 3)
4 LINESTRING (0 1, 3 3)
dtype: geometry
We can also check two GeoSeries against each other, row by row.
The GeoSeries above have different indices than the one below. We can either
align both GeoSeries based on index values and compare elements with the same
index using ``align=True`` or ignore index and compare elements based on their
matching order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s2 = geopandas.GeoSeries(
... [
... Polygon([(0.5, 0.5), (1.5, 0.5), (1.5, 1.5), (0.5, 1.5)]),
... Point(3, 1),
... LineString([(1, 0), (2, 0)]),
... Point(10, 15),
... Point(0, 1),
... ],
... index=range(1, 6),
... )
>>> s.shortest_line(s2, align=True)
0 None
1 LINESTRING (0.5 0.5, 0.5 0.5)
2 LINESTRING (2 2, 3 1)
3 LINESTRING (2 0, 2 0)
4 LINESTRING (0 1, 10 15)
5 None
dtype: geometry
>>>
>>> s.shortest_line(s2, align=False)
0 LINESTRING (0.5 0.5, 0.5 0.5)
1 LINESTRING (2 2, 3 1)
2 LINESTRING (0.5 0.5, 1 0)
3 LINESTRING (0 2, 10 15)
4 LINESTRING (0 1, 0 1)
dtype: geometry
"""
return _binary_geo("shortest_line", self, other, align)
def snap(self, other, tolerance, align=None):
"""Snap the vertices and segments of the geometry to vertices of the reference.
Vertices and segments of the input geometry are snapped to vertices of the
reference geometry, returning a new geometry; the input geometries are not
modified. The result geometry is the input geometry with the vertices and
segments snapped. If no snapping occurs then the input geometry is returned
unchanged. The tolerance is used to control where snapping is performed.
Where possible, this operation tries to avoid creating invalid geometries;
however, it does not guarantee that output geometries will be valid. It is
the responsibility of the caller to check for and handle invalid geometries.
Because too much snapping can result in invalid geometries being created,
heuristics are used to determine the number and location of snapped
vertices that are likely safe to snap. These heuristics may omit
some potential snaps that are otherwise within the tolerance.
The operation works in a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
Parameters
----------
other : GeoSeries or geometric object
The Geoseries (elementwise) or geometric object to snap to.
tolerance : float or array like
Maximum distance between vertices that shall be snapped
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
GeoSeries
Examples
--------
>>> from shapely import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Point(0.5, 2.5),
... LineString([(0.1, 0.1), (0.49, 0.51), (1.01, 0.89)]),
... Polygon([(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)]),
... ],
... )
>>> s
0 POINT (0.5 2.5)
1 LINESTRING (0.1 0.1, 0.49 0.51, 1.01 0.89)
2 POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))
dtype: geometry
>>> s2 = geopandas.GeoSeries(
... [
... Point(0, 2),
... LineString([(0, 0), (0.5, 0.5), (1.0, 1.0)]),
... Point(8, 10),
... ],
... index=range(1, 4),
... )
>>> s2
1 POINT (0 2)
2 LINESTRING (0 0, 0.5 0.5, 1 1)
3 POINT (8 10)
dtype: geometry
We can snap each geometry to a single shapely geometry:
.. image:: ../../../_static/binary_op-03.svg
:align: center
>>> s.snap(Point(0, 2), tolerance=1)
0 POINT (0 2)
1 LINESTRING (0.1 0.1, 0.49 0.51, 1.01 0.89)
2 POLYGON ((0 0, 0 2, 0 10, 10 10, 10 0, 0 0))
dtype: geometry
We can also snap two GeoSeries to each other, row by row.
The GeoSeries above have different indices. We can either align both GeoSeries
based on index values and snap elements with the same index using
``align=True`` or ignore index and snap elements based on their matching
order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s.snap(s2, tolerance=1, align=True)
0 None
1 LINESTRING (0.1 0.1, 0.49 0.51, 1.01 0.89)
2 POLYGON ((0.5 0.5, 1 1, 0 10, 10 10, 10 0, 0.5...
3 None
dtype: geometry
>>> s.snap(s2, tolerance=1, align=False)
0 POINT (0 2)
1 LINESTRING (0 0, 0.5 0.5, 1 1)
2 POLYGON ((0 0, 0 10, 8 10, 10 10, 10 0, 0 0))
dtype: geometry
"""
return _binary_geo("snap", self, other, align, tolerance=tolerance)
def shared_paths(self, other, align=None):
"""Return the shared paths between two geometries.
Geometries within the GeoSeries should be only (Multi)LineStrings or
LinearRings. A GeoSeries of GeometryCollections is returned with two elements
in each GeometryCollection. The first element is a MultiLineString containing
shared paths with the same direction for both inputs. The second element is a
MultiLineString containing shared paths with the opposite direction for the two
inputs.
You can extract individual geometries of the resulting GeometryCollection using
the :meth:`GeoSeries.get_geometry` method.
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
Parameters
----------
other : Geoseries or geometric object
The Geoseries (elementwise) or geometric object to find the shared paths
with. Has to contain only (Multi)LineString or LinearRing geometry types.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
GeoSeries
Examples
--------
>>> from shapely.geometry import LineString, MultiLineString
>>> s = geopandas.GeoSeries(
... [
... LineString([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]),
... LineString([(1, 0), (2, 0), (2, 1), (1, 1), (1, 0)]),
... MultiLineString([[(1, 0), (2, 0)], [(2, 1), (1, 1), (1, 0)]]),
... ],
... )
>>> s
0 LINESTRING (0 0, 1 0, 1 1, 0 1, 0 0)
1 LINESTRING (1 0, 2 0, 2 1, 1 1, 1 0)
2 MULTILINESTRING ((1 0, 2 0), (2 1, 1 1, 1 0))
dtype: geometry
We can find the shared paths between each geometry and a single shapely
geometry:
.. image:: ../../../_static/binary_op-03.svg
:align: center
>>> l = LineString([(1, 1), (0, 1)])
>>> s.shared_paths(l)
0 GEOMETRYCOLLECTION (MULTILINESTRING ((1 1, 0 1...
1 GEOMETRYCOLLECTION (MULTILINESTRING EMPTY, MUL...
2 GEOMETRYCOLLECTION (MULTILINESTRING EMPTY, MUL...
dtype: geometry
We can also check two GeoSeries against each other, row by row. The GeoSeries
above have different indices than the one below. We can either align both
GeoSeries based on index values and compare elements with the same index using
``align=True`` or ignore index and compare elements based on their matching
order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s2 = geopandas.GeoSeries(
... [
... LineString([(1, 1), (0, 1)]),
... LineString([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]),
... LineString([(1, 0), (2, 0), (2, 1), (1, 1), (1, 0)]),
... ],
... index=[1, 2, 3]
... )
>>> s.shared_paths(s2, align=True)
0 None
1 GEOMETRYCOLLECTION (MULTILINESTRING EMPTY, MUL...
2 GEOMETRYCOLLECTION (MULTILINESTRING EMPTY, MUL...
3 None
dtype: geometry
>>>
>>> s.shared_paths(s2, align=False)
0 GEOMETRYCOLLECTION (MULTILINESTRING ((1 1, 0 1...
1 GEOMETRYCOLLECTION (MULTILINESTRING EMPTY, MUL...
2 GEOMETRYCOLLECTION (MULTILINESTRING ((1 0, 2 0...
dtype: geometry
See Also
--------
GeoSeries.get_geometry
"""
return _binary_geo("shared_paths", self, other, align)
#
# Other operations
#
@property
def bounds(self):
"""Return a ``DataFrame`` with columns ``minx``, ``miny``, ``maxx``,
``maxy`` values containing the bounds for each geometry.
See ``GeoSeries.total_bounds`` for the limits of the entire series.
Examples
--------
>>> from shapely.geometry import Point, Polygon, LineString
>>> d = {'geometry': [Point(2, 1), Polygon([(0, 0), (1, 1), (1, 0)]),
... LineString([(0, 1), (1, 2)])]}
>>> gdf = geopandas.GeoDataFrame(d, crs="EPSG:4326")
>>> gdf.bounds
minx miny maxx maxy
0 2.0 1.0 2.0 1.0
1 0.0 0.0 1.0 1.0
2 0.0 1.0 1.0 2.0
You can assign the bounds to the ``GeoDataFrame`` as:
>>> import pandas as pd
>>> gdf = pd.concat([gdf, gdf.bounds], axis=1)
>>> gdf
geometry minx miny maxx maxy
0 POINT (2 1) 2.0 1.0 2.0 1.0
1 POLYGON ((0 0, 1 1, 1 0, 0 0)) 0.0 0.0 1.0 1.0
2 LINESTRING (0 1, 1 2) 0.0 1.0 1.0 2.0
"""
bounds = GeometryArray(self.geometry.values).bounds
return DataFrame(
bounds, columns=["minx", "miny", "maxx", "maxy"], index=self.index
)
@property
def total_bounds(self):
"""Return a tuple containing ``minx``, ``miny``, ``maxx``, ``maxy``
values for the bounds of the series as a whole.
See ``GeoSeries.bounds`` for the bounds of the geometries contained in
the series.
Examples
--------
>>> from shapely.geometry import Point, Polygon, LineString
>>> d = {'geometry': [Point(3, -1), Polygon([(0, 0), (1, 1), (1, 0)]),
... LineString([(0, 1), (1, 2)])]}
>>> gdf = geopandas.GeoDataFrame(d, crs="EPSG:4326")
>>> gdf.total_bounds
array([ 0., -1., 3., 2.])
"""
return GeometryArray(self.geometry.values).total_bounds
@property
def sindex(self):
"""Generate the spatial index.
Creates R-tree spatial index based on ``shapely.STRtree``.
Note that the spatial index may not be fully
initialized until the first use.
Examples
--------
>>> from shapely.geometry import box
>>> s = geopandas.GeoSeries(geopandas.points_from_xy(range(5), range(5)))
>>> s
0 POINT (0 0)
1 POINT (1 1)
2 POINT (2 2)
3 POINT (3 3)
4 POINT (4 4)
dtype: geometry
Query the spatial index with a single geometry based on the bounding box:
>>> s.sindex.query(box(1, 1, 3, 3))
array([1, 2, 3])
Query the spatial index with a single geometry based on the predicate:
>>> s.sindex.query(box(1, 1, 3, 3), predicate="contains")
array([2])
Query the spatial index with an array of geometries based on the bounding
box:
>>> s2 = geopandas.GeoSeries([box(1, 1, 3, 3), box(4, 4, 5, 5)])
>>> s2
0 POLYGON ((3 1, 3 3, 1 3, 1 1, 3 1))
1 POLYGON ((5 4, 5 5, 4 5, 4 4, 5 4))
dtype: geometry
>>> s.sindex.query(s2)
array([[0, 0, 0, 1],
[1, 2, 3, 4]])
Query the spatial index with an array of geometries based on the predicate:
>>> s.sindex.query(s2, predicate="contains")
array([[0],
[2]])
"""
return self.geometry.values.sindex
@property
def has_sindex(self):
"""Check the existence of the spatial index without generating it.
Use the `.sindex` attribute on a GeoDataFrame or GeoSeries
to generate a spatial index if it does not yet exist,
which may take considerable time based on the underlying index
implementation.
Note that the underlying spatial index may not be fully
initialized until the first use.
Examples
--------
>>> from shapely.geometry import Point
>>> d = {'geometry': [Point(1, 2), Point(2, 1)]}
>>> gdf = geopandas.GeoDataFrame(d)
>>> gdf.has_sindex
False
>>> index = gdf.sindex
>>> gdf.has_sindex
True
Returns
-------
bool
`True` if the spatial index has been generated or
`False` if not.
"""
return self.geometry.values.has_sindex
def buffer(
self,
distance,
resolution=16,
cap_style="round",
join_style="round",
mitre_limit=5.0,
single_sided=False,
**kwargs,
):
"""Return a ``GeoSeries`` of geometries representing all points within
a given ``distance`` of each geometric object.
Computes the buffer of a geometry for positive and negative buffer distance.
The buffer of a geometry is defined as the Minkowski sum (or difference, for
negative distance) of the geometry with a circle with radius equal to the
absolute value of the buffer distance.
The buffer operation always returns a polygonal result. The negative or
zero-distance buffer of lines and points is always empty.
Parameters
----------
distance : float, np.array, pd.Series
The radius of the buffer in the Minkowski sum (or difference). If np.array
or pd.Series are used then it must have same length as the GeoSeries.
resolution : int (optional, default 16)
The resolution of the buffer around each vertex. Specifies the number of
linear segments in a quarter circle in the approximation of circular arcs.
cap_style : {'round', 'square', 'flat'}, default 'round'
Specifies the shape of buffered line endings. ``'round'`` results in
circular line endings (see ``resolution``). Both ``'square'`` and ``'flat'``
result in rectangular line endings, ``'flat'`` will end at the original
vertex, while ``'square'`` involves adding the buffer width.
join_style : {'round', 'mitre', 'bevel'}, default 'round'
Specifies the shape of buffered line midpoints. ``'round'`` results in
rounded shapes. ``'bevel'`` results in a beveled edge that touches the
original vertex. ``'mitre'`` results in a single vertex that is beveled
depending on the ``mitre_limit`` parameter.
mitre_limit : float, default 5.0
Crops of ``'mitre'``-style joins if the point is displaced from the
buffered vertex by more than this limit.
single_sided : bool, default False
Only buffer at one side of the geometry.
Examples
--------
>>> from shapely.geometry import Point, LineString, Polygon
>>> s = geopandas.GeoSeries(
... [
... Point(0, 0),
... LineString([(1, -1), (1, 0), (2, 0), (2, 1)]),
... Polygon([(3, -1), (4, 0), (3, 1)]),
... ]
... )
>>> s
0 POINT (0 0)
1 LINESTRING (1 -1, 1 0, 2 0, 2 1)
2 POLYGON ((3 -1, 4 0, 3 1, 3 -1))
dtype: geometry
>>> s.buffer(0.2)
0 POLYGON ((0.2 0, 0.19904 -0.0196, 0.19616 -0.0...
1 POLYGON ((0.8 0, 0.80096 0.0196, 0.80384 0.039...
2 POLYGON ((2.8 -1, 2.8 1, 2.80096 1.0196, 2.803...
dtype: geometry
``Further specification as ``join_style`` and ``cap_style`` are shown in the
following illustration:
.. plot:: _static/code/buffer.py
"""
return _delegate_geo_method(
"buffer",
self,
distance=distance,
resolution=resolution,
cap_style=cap_style,
join_style=join_style,
mitre_limit=mitre_limit,
single_sided=single_sided,
**kwargs,
)
def simplify(self, tolerance, preserve_topology=True):
"""Return a ``GeoSeries`` containing a simplified representation of
each geometry.
The algorithm (Douglas-Peucker) recursively splits the original line
into smaller parts and connects these parts' endpoints
by a straight line. Then, it removes all points whose distance
to the straight line is smaller than `tolerance`. It does not
move any points and it always preserves endpoints of
the original line or polygon.
See https://shapely.readthedocs.io/en/latest/manual.html#object.simplify
for details
Simplifies individual geometries independently, without considering
the topology of a potential polygonal coverage. If you would like to treat
the ``GeoSeries`` as a coverage and simplify its edges, while preserving the
coverage topology, see :meth:`simplify_coverage`.
Parameters
----------
tolerance : float
All parts of a simplified geometry will be no more than
`tolerance` distance from the original. It has the same units
as the coordinate reference system of the GeoSeries.
For example, using `tolerance=100` in a projected CRS with meters
as units means a distance of 100 meters in reality.
preserve_topology: bool (default True)
False uses a quicker algorithm, but may produce self-intersecting
or otherwise invalid geometries.
Notes
-----
Invalid geometric objects may result from simplification that does not
preserve topology and simplification may be sensitive to the order of
coordinates: two geometries differing only in order of coordinates may be
simplified differently.
See Also
--------
simplify_coverage : simplify geometries using coverage simplification
Examples
--------
>>> from shapely.geometry import Point, LineString
>>> s = geopandas.GeoSeries(
... [Point(0, 0).buffer(1), LineString([(0, 0), (1, 10), (0, 20)])]
... )
>>> s
0 POLYGON ((1 0, 0.99518 -0.09802, 0.98079 -0.19...
1 LINESTRING (0 0, 1 10, 0 20)
dtype: geometry
>>> s.simplify(1)
0 POLYGON ((0 1, 0 -1, -1 0, 0 1))
1 LINESTRING (0 0, 0 20)
dtype: geometry
"""
return _delegate_geo_method(
"simplify", self, tolerance=tolerance, preserve_topology=preserve_topology
)
def simplify_coverage(self, tolerance, *, simplify_boundary=True):
"""Return a ``GeoSeries`` containing a simplified representation of
polygonal coverage.
Assumes that the ``GeoSeries`` forms a polygonal coverage. Under this
assumption, the method simplifies the edges using the Visvalingam-Whyatt
algorithm, while preserving a valid coverage. In the most simplified case,
polygons are reduced to triangles.
A ``GeoSeries`` of valid polygons is considered a coverage if the polygons are:
* **Non-overlapping** - polygons do not overlap (their interiors do not
intersect)
* **Edge-Matched** - vertices along shared edges are identical
The method allows simplification of all edges including the outer boundaries of
the coverage or simplification of only the inner (shared) edges.
If there are other geometry types than Polygons or MultiPolygons present, the
method will raise an error.
If the geometry is polygonal but does not form a valid coverage due to overlaps,
it will be simplified but it may result in invalid coverage topology.
Requires Shapely >= 2.1.
.. versionadded:: 1.1.0
Parameters
----------
tolerance : float
The degree of simplification roughly equal to the square root of the area
of triangles that will be removed. It has the same units
as the coordinate reference system of the GeoSeries.
For example, using `tolerance=100` in a projected CRS with meters
as units means a distance of 100 meters in reality.
simplify_boundary: bool (default True)
By default (True), simplifies both internal edges of the coverage as well
as its boundary. If set to False, only simplifies internal edges.
See Also
--------
simplify : simplification of individual geometries
Examples
--------
>>> from shapely.geometry import Polygon
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1.1), (2, 0), (1.5, 1), (2, 2), (0, 2)]),
... Polygon([(2, 0), (4, 0), (4, 2), (2, 2), (1.5, 1)]),
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1.1, 2 0, 1.5 1, 2 2, 0 2, 0 0))
1 POLYGON ((2 0, 4 0, 4 2, 2 2, 1.5 1, 2 0))
dtype: geometry
>>> s.simplify_coverage(1)
0 POLYGON ((2 0, 2 2, 0 2, 2 0))
1 POLYGON ((2 0, 4 0, 4 2, 2 2, 2 0))
dtype: geometry
>>> s.simplify_coverage(1, simplify_boundary=False)
0 POLYGON ((2 0, 2 2, 0 2, 0 0, 1 1.1, 2 0))
1 POLYGON ((2 0, 4 0, 4 2, 2 2, 2 0))
dtype: geometry
"""
return _delegate_geo_method(
"simplify_coverage",
self,
tolerance=tolerance,
simplify_boundary=simplify_boundary,
)
def relate(self, other, align=None):
"""Return the DE-9IM intersection matrices for the geometries.
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
Parameters
----------
other : BaseGeometry or GeoSeries
The other geometry to computed
the DE-9IM intersection matrices from.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
spatial_relations: Series of strings
The DE-9IM intersection matrices which describe
the spatial relations of the other geometry.
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (2, 2), (0, 2)]),
... Polygon([(0, 0), (2, 2), (0, 2)]),
... LineString([(0, 0), (2, 2)]),
... LineString([(2, 0), (0, 2)]),
... Point(0, 1),
... ],
... )
>>> s2 = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(1, 0), (1, 3)]),
... LineString([(2, 0), (0, 2)]),
... Point(1, 1),
... Point(0, 1),
... ],
... index=range(1, 6),
... )
>>> s
0 POLYGON ((0 0, 2 2, 0 2, 0 0))
1 POLYGON ((0 0, 2 2, 0 2, 0 0))
2 LINESTRING (0 0, 2 2)
3 LINESTRING (2 0, 0 2)
4 POINT (0 1)
dtype: geometry
>>> s2
1 POLYGON ((0 0, 1 1, 0 1, 0 0))
2 LINESTRING (1 0, 1 3)
3 LINESTRING (2 0, 0 2)
4 POINT (1 1)
5 POINT (0 1)
dtype: geometry
We can relate each geometry and a single
shapely geometry:
.. image:: ../../../_static/binary_op-03.svg
:align: center
>>> s.relate(Polygon([(0, 0), (1, 1), (0, 1)]))
0 212F11FF2
1 212F11FF2
2 F11F00212
3 F01FF0212
4 F0FFFF212
dtype: object
We can also check two GeoSeries against each other, row by row.
The GeoSeries above have different indices. We can either align both GeoSeries
based on index values and compare elements with the same index using
``align=True`` or ignore index and compare elements based on their matching
order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s.relate(s2, align=True)
0 None
1 212F11FF2
2 0F1FF0102
3 1FFF0FFF2
4 FF0FFF0F2
5 None
dtype: object
>>> s.relate(s2, align=False)
0 212F11FF2
1 1F20F1102
2 0F1FF0102
3 0F1FF0FF2
4 0FFFFFFF2
dtype: object
"""
return _binary_op("relate", self, other, align)
def relate_pattern(self, other, pattern, align=None):
"""Return True if the DE-9IM string code for the relationship between
the geometries satisfies the pattern, else False.
This function compares the DE-9IM code string for two geometries
against a specified pattern. If the string matches the pattern then
``True`` is returned, otherwise ``False``. The pattern specified can
be an exact match (``0``, ``1`` or ``2``), a boolean match
(uppercase ``T`` or ``F``), or a wildcard (``*``). For example,
the pattern for the ``within`` predicate is ``'T*F**F***'``
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
Parameters
----------
other : BaseGeometry or GeoSeries
The other geometry to be tested agains the pattern.
pattern : str
The DE-9IM pattern to test against.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
Series
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (2, 2), (0, 2)]),
... Polygon([(0, 0), (2, 2), (0, 2)]),
... LineString([(0, 0), (2, 2)]),
... LineString([(2, 0), (0, 2)]),
... Point(0, 1),
... ],
... )
>>> s2 = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(1, 0), (1, 3)]),
... LineString([(2, 0), (0, 2)]),
... Point(1, 1),
... Point(0, 1),
... ],
... index=range(1, 6),
... )
>>> s
0 POLYGON ((0 0, 2 2, 0 2, 0 0))
1 POLYGON ((0 0, 2 2, 0 2, 0 0))
2 LINESTRING (0 0, 2 2)
3 LINESTRING (2 0, 0 2)
4 POINT (0 1)
dtype: geometry
>>> s2
1 POLYGON ((0 0, 1 1, 0 1, 0 0))
2 LINESTRING (1 0, 1 3)
3 LINESTRING (2 0, 0 2)
4 POINT (1 1)
5 POINT (0 1)
dtype: geometry
We can check the relate pattern of each geometry and a single
shapely geometry:
.. image:: ../../../_static/binary_op-03.svg
:align: center
>>> s.relate_pattern(Polygon([(0, 0), (1, 1), (0, 1)]), "2*T***F**")
0 True
1 True
2 False
3 False
4 False
dtype: bool
We can also check two GeoSeries against each other, row by row.
The GeoSeries above have different indices. We can either align both GeoSeries
based on index values and compare elements with the same index using
``align=True`` or ignore index and compare elements based on their matching
order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s.relate_pattern(s2, "TF******T", align=True)
0 False
1 False
2 True
3 True
4 False
5 False
dtype: bool
>>> s.relate_pattern(s2, "TF******T", align=False)
0 False
1 True
2 True
3 True
4 True
dtype: bool
"""
return _binary_op("relate_pattern", self, other, pattern=pattern, align=align)
def project(self, other, normalized=False, align=None):
"""Return the distance along each geometry nearest to *other*.
The operation works on a 1-to-1 row-wise manner:
.. image:: ../../../_static/binary_op-01.svg
:align: center
The project method is the inverse of interpolate.
In shapely, this is equal to ``line_locate_point``.
Parameters
----------
other : BaseGeometry or GeoSeries
The *other* geometry to computed projected point from.
normalized : boolean
If normalized is True, return the distance normalized to
the length of the object.
align : bool | None (default None)
If True, automatically aligns GeoSeries based on their indices.
If False, the order of elements is preserved. None defaults to True.
Returns
-------
Series
Examples
--------
>>> from shapely.geometry import LineString, Point
>>> s = geopandas.GeoSeries(
... [
... LineString([(0, 0), (2, 0), (0, 2)]),
... LineString([(0, 0), (2, 2)]),
... LineString([(2, 0), (0, 2)]),
... ],
... )
>>> s2 = geopandas.GeoSeries(
... [
... Point(1, 0),
... Point(1, 0),
... Point(2, 1),
... ],
... index=range(1, 4),
... )
>>> s
0 LINESTRING (0 0, 2 0, 0 2)
1 LINESTRING (0 0, 2 2)
2 LINESTRING (2 0, 0 2)
dtype: geometry
>>> s2
1 POINT (1 0)
2 POINT (1 0)
3 POINT (2 1)
dtype: geometry
We can project each geometry on a single
shapely geometry:
.. image:: ../../../_static/binary_op-03.svg
:align: center
>>> s.project(Point(1, 0))
0 1.000000
1 0.707107
2 0.707107
dtype: float64
We can also check two GeoSeries against each other, row by row.
The GeoSeries above have different indices. We can either align both GeoSeries
based on index values and project elements with the same index using
``align=True`` or ignore index and project elements based on their matching
order using ``align=False``:
.. image:: ../../../_static/binary_op-02.svg
>>> s.project(s2, align=True)
0 NaN
1 0.707107
2 0.707107
3 NaN
dtype: float64
>>> s.project(s2, align=False)
0 1.000000
1 0.707107
2 0.707107
dtype: float64
See Also
--------
GeoSeries.interpolate
"""
return _binary_op("project", self, other, normalized=normalized, align=align)
def interpolate(self, distance, normalized=False):
"""Return a point at the specified distance along each geometry.
Parameters
----------
distance : float or Series of floats
Distance(s) along the geometries at which a point should be
returned. If np.array or pd.Series are used then it must have
same length as the GeoSeries.
normalized : boolean
If normalized is True, distance will be interpreted as a fraction
of the geometric object's length.
Examples
--------
>>> from shapely.geometry import LineString, Point
>>> s = geopandas.GeoSeries(
... [
... LineString([(0, 0), (2, 0), (0, 2)]),
... LineString([(0, 0), (2, 2)]),
... LineString([(2, 0), (0, 2)]),
... ],
... )
>>> s
0 LINESTRING (0 0, 2 0, 0 2)
1 LINESTRING (0 0, 2 2)
2 LINESTRING (2 0, 0 2)
dtype: geometry
>>> s.interpolate(1)
0 POINT (1 0)
1 POINT (0.70711 0.70711)
2 POINT (1.29289 0.70711)
dtype: geometry
>>> s.interpolate([1, 2, 3])
0 POINT (1 0)
1 POINT (1.41421 1.41421)
2 POINT (0 2)
dtype: geometry
"""
return _delegate_geo_method(
"interpolate", self, distance=distance, normalized=normalized
)
def affine_transform(self, matrix):
"""Return a ``GeoSeries`` with translated geometries.
See http://shapely.readthedocs.io/en/stable/manual.html#shapely.affinity.affine_transform
for details.
Parameters
----------
matrix: List or tuple
6 or 12 items for 2D or 3D transformations respectively.
For 2D affine transformations,
the 6 parameter matrix is ``[a, b, d, e, xoff, yoff]``
For 3D affine transformations,
the 12 parameter matrix is ``[a, b, c, d, e, f, g, h, i, xoff, yoff, zoff]``
Examples
--------
>>> from shapely.geometry import Point, LineString, Polygon
>>> s = geopandas.GeoSeries(
... [
... Point(1, 1),
... LineString([(1, -1), (1, 0)]),
... Polygon([(3, -1), (4, 0), (3, 1)]),
... ]
... )
>>> s
0 POINT (1 1)
1 LINESTRING (1 -1, 1 0)
2 POLYGON ((3 -1, 4 0, 3 1, 3 -1))
dtype: geometry
>>> s.affine_transform([2, 3, 2, 4, 5, 2])
0 POINT (10 8)
1 LINESTRING (4 0, 7 4)
2 POLYGON ((8 4, 13 10, 14 12, 8 4))
dtype: geometry
""" # (E501 link is longer than max line length)
return _delegate_geo_method("affine_transform", self, matrix=matrix)
def translate(self, xoff=0.0, yoff=0.0, zoff=0.0):
"""Return a ``GeoSeries`` with translated geometries.
See http://shapely.readthedocs.io/en/latest/manual.html#shapely.affinity.translate
for details.
Parameters
----------
xoff, yoff, zoff : float, float, float
Amount of offset along each dimension.
xoff, yoff, and zoff for translation along the x, y, and z
dimensions respectively.
Examples
--------
>>> from shapely.geometry import Point, LineString, Polygon
>>> s = geopandas.GeoSeries(
... [
... Point(1, 1),
... LineString([(1, -1), (1, 0)]),
... Polygon([(3, -1), (4, 0), (3, 1)]),
... ]
... )
>>> s
0 POINT (1 1)
1 LINESTRING (1 -1, 1 0)
2 POLYGON ((3 -1, 4 0, 3 1, 3 -1))
dtype: geometry
>>> s.translate(2, 3)
0 POINT (3 4)
1 LINESTRING (3 2, 3 3)
2 POLYGON ((5 2, 6 3, 5 4, 5 2))
dtype: geometry
""" # (E501 link is longer than max line length)
return _delegate_geo_method("translate", self, xoff=xoff, yoff=yoff, zoff=zoff)
def rotate(self, angle, origin="center", use_radians=False):
"""Return a ``GeoSeries`` with rotated geometries.
See http://shapely.readthedocs.io/en/latest/manual.html#shapely.affinity.rotate
for details.
Parameters
----------
angle : float
The angle of rotation can be specified in either degrees (default)
or radians by setting use_radians=True. Positive angles are
counter-clockwise and negative are clockwise rotations.
origin : string, Point, or tuple (x, y)
The point of origin can be a keyword 'center' for the bounding box
center (default), 'centroid' for the geometry's centroid, a Point
object or a coordinate tuple (x, y).
use_radians : boolean
Whether to interpret the angle of rotation as degrees or radians
Examples
--------
>>> from shapely.geometry import Point, LineString, Polygon
>>> s = geopandas.GeoSeries(
... [
... Point(1, 1),
... LineString([(1, -1), (1, 0)]),
... Polygon([(3, -1), (4, 0), (3, 1)]),
... ]
... )
>>> s
0 POINT (1 1)
1 LINESTRING (1 -1, 1 0)
2 POLYGON ((3 -1, 4 0, 3 1, 3 -1))
dtype: geometry
>>> s.rotate(90)
0 POINT (1 1)
1 LINESTRING (1.5 -0.5, 0.5 -0.5)
2 POLYGON ((4.5 -0.5, 3.5 0.5, 2.5 -0.5, 4.5 -0.5))
dtype: geometry
>>> s.rotate(90, origin=(0, 0))
0 POINT (-1 1)
1 LINESTRING (1 1, 0 1)
2 POLYGON ((1 3, 0 4, -1 3, 1 3))
dtype: geometry
"""
return _delegate_geo_method(
"rotate", self, angle=angle, origin=origin, use_radians=use_radians
)
def scale(self, xfact=1.0, yfact=1.0, zfact=1.0, origin="center"):
"""Return a ``GeoSeries`` with scaled geometries.
The geometries can be scaled by different factors along each
dimension. Negative scale factors will mirror or reflect coordinates.
See http://shapely.readthedocs.io/en/latest/manual.html#shapely.affinity.scale
for details.
Parameters
----------
xfact, yfact, zfact : float, float, float
Scaling factors for the x, y, and z dimensions respectively.
origin : string, Point, or tuple
The point of origin can be a keyword 'center' for the 2D bounding
box center (default), 'centroid' for the geometry's 2D centroid, a
Point object or a coordinate tuple (x, y, z).
Examples
--------
>>> from shapely.geometry import Point, LineString, Polygon
>>> s = geopandas.GeoSeries(
... [
... Point(1, 1),
... LineString([(1, -1), (1, 0)]),
... Polygon([(3, -1), (4, 0), (3, 1)]),
... ]
... )
>>> s
0 POINT (1 1)
1 LINESTRING (1 -1, 1 0)
2 POLYGON ((3 -1, 4 0, 3 1, 3 -1))
dtype: geometry
>>> s.scale(2, 3)
0 POINT (1 1)
1 LINESTRING (1 -2, 1 1)
2 POLYGON ((2.5 -3, 4.5 0, 2.5 3, 2.5 -3))
dtype: geometry
>>> s.scale(2, 3, origin=(0, 0))
0 POINT (2 3)
1 LINESTRING (2 -3, 2 0)
2 POLYGON ((6 -3, 8 0, 6 3, 6 -3))
dtype: geometry
"""
return _delegate_geo_method(
"scale", self, xfact=xfact, yfact=yfact, zfact=zfact, origin=origin
)
def skew(self, xs=0.0, ys=0.0, origin="center", use_radians=False):
"""Return a ``GeoSeries`` with skewed geometries.
The geometries are sheared by angles along the x and y dimensions.
See http://shapely.readthedocs.io/en/latest/manual.html#shapely.affinity.skew
for details.
Parameters
----------
xs, ys : float, float
The shear angle(s) for the x and y axes respectively. These can be
specified in either degrees (default) or radians by setting
use_radians=True.
origin : string, Point, or tuple (x, y)
The point of origin can be a keyword 'center' for the bounding box
center (default), 'centroid' for the geometry's centroid, a Point
object or a coordinate tuple (x, y).
use_radians : boolean
Whether to interpret the shear angle(s) as degrees or radians
Examples
--------
>>> from shapely.geometry import Point, LineString, Polygon
>>> s = geopandas.GeoSeries(
... [
... Point(1, 1),
... LineString([(1, -1), (1, 0)]),
... Polygon([(3, -1), (4, 0), (3, 1)]),
... ]
... )
>>> s
0 POINT (1 1)
1 LINESTRING (1 -1, 1 0)
2 POLYGON ((3 -1, 4 0, 3 1, 3 -1))
dtype: geometry
>>> s.skew(45, 30)
0 POINT (1 1)
1 LINESTRING (0.5 -1, 1.5 0)
2 POLYGON ((2 -1.28868, 4 0.28868, 4 0.71132, 2 ...
dtype: geometry
>>> s.skew(45, 30, origin=(0, 0))
0 POINT (2 1.57735)
1 LINESTRING (1.11022e-16 -0.42265, 1 0.57735)
2 POLYGON ((2 0.73205, 4 2.3094, 4 2.73205, 2 0....
dtype: geometry
"""
return _delegate_geo_method(
"skew", self, xs=xs, ys=ys, origin=origin, use_radians=use_radians
)
@property
def cx(self):
"""
Coordinate based indexer to select by intersection with bounding box.
Format of input should be ``.cx[xmin:xmax, ymin:ymax]``. Any of
``xmin``, ``xmax``, ``ymin``, and ``ymax`` can be provided, but input
must include a comma separating x and y slices. That is, ``.cx[:, :]``
will return the full series/frame, but ``.cx[:]`` is not implemented.
Examples
--------
>>> from shapely.geometry import LineString, Point
>>> s = geopandas.GeoSeries(
... [Point(0, 0), Point(1, 2), Point(3, 3), LineString([(0, 0), (3, 3)])]
... )
>>> s
0 POINT (0 0)
1 POINT (1 2)
2 POINT (3 3)
3 LINESTRING (0 0, 3 3)
dtype: geometry
>>> s.cx[0:1, 0:1]
0 POINT (0 0)
3 LINESTRING (0 0, 3 3)
dtype: geometry
>>> s.cx[:, 1:]
1 POINT (1 2)
2 POINT (3 3)
3 LINESTRING (0 0, 3 3)
dtype: geometry
"""
return _CoordinateIndexer(self)
def get_coordinates(
self, include_z=False, ignore_index=False, index_parts=False, *, include_m=False
):
"""Get coordinates from a :class:`GeoSeries` as a :class:`~pandas.DataFrame` of
floats.
The shape of the returned :class:`~pandas.DataFrame` is (N, 2), with N being the
number of coordinate pairs. With the default of ``include_z=False``,
three-dimensional data is ignored. When specifying ``include_z=True``, the shape
of the returned :class:`~pandas.DataFrame` is (N, 3).
Parameters
----------
include_z : bool, default False
Include Z coordinates
ignore_index : bool, default False
If True, the resulting index will be labelled 0, 1, …, n - 1, ignoring
``index_parts``.
index_parts : bool, default False
If True, the resulting index will be a :class:`~pandas.MultiIndex` (original
index with an additional level indicating the ordering of the coordinate
pairs: a new zero-based index for each geometry in the original GeoSeries).
include_m : bool, default False
Include M coordinates. Requires shapely >= 2.1.
Returns
-------
pandas.DataFrame
Examples
--------
>>> from shapely.geometry import Point, LineString, Polygon
>>> s = geopandas.GeoSeries(
... [
... Point(1, 1),
... LineString([(1, -1), (1, 0)]),
... Polygon([(3, -1), (4, 0), (3, 1)]),
... ]
... )
>>> s
0 POINT (1 1)
1 LINESTRING (1 -1, 1 0)
2 POLYGON ((3 -1, 4 0, 3 1, 3 -1))
dtype: geometry
>>> s.get_coordinates()
x y
0 1.0 1.0
1 1.0 -1.0
1 1.0 0.0
2 3.0 -1.0
2 4.0 0.0
2 3.0 1.0
2 3.0 -1.0
>>> s.get_coordinates(ignore_index=True)
x y
0 1.0 1.0
1 1.0 -1.0
2 1.0 0.0
3 3.0 -1.0
4 4.0 0.0
5 3.0 1.0
6 3.0 -1.0
>>> s.get_coordinates(index_parts=True)
x y
0 0 1.0 1.0
1 0 1.0 -1.0
1 1.0 0.0
2 0 3.0 -1.0
1 4.0 0.0
2 3.0 1.0
3 3.0 -1.0
"""
if include_m:
if not compat.SHAPELY_GE_21:
raise ImportError("Shapely >= 2.1 is required for include_m=True.")
# can be merged with the one below once min requirement is shapely 2.1
coords, outer_idx = shapely.get_coordinates(
self.geometry.values._data,
include_z=include_z,
include_m=include_m,
return_index=True,
)
else:
coords, outer_idx = shapely.get_coordinates(
self.geometry.values._data, include_z=include_z, return_index=True
)
column_names = ["x", "y"]
if include_z:
column_names.append("z")
if include_m:
column_names.append("m")
index = _get_index_for_parts(
self.index,
outer_idx,
ignore_index=ignore_index,
index_parts=index_parts,
)
return pd.DataFrame(coords, index=index, columns=column_names)
def hilbert_distance(self, total_bounds=None, level=16):
"""
Calculate the distance along a Hilbert curve.
The distances are calculated for the midpoints of the geometries in the
GeoDataFrame, and using the total bounds of the GeoDataFrame.
The Hilbert distance can be used to spatially sort GeoPandas
objects, by mapping two dimensional geometries along the Hilbert curve.
Parameters
----------
total_bounds : 4-element array, optional
The spatial extent in which the curve is constructed (used to
rescale the geometry midpoints). By default, the total bounds
of the full GeoDataFrame or GeoSeries will be computed. If known,
you can pass the total bounds to avoid this extra computation.
level : int (1 - 16), default 16
Determines the precision of the curve (points on the curve will
have coordinates in the range [0, 2^level - 1]).
Returns
-------
Series
Series containing distance along the curve for geometry
"""
from geopandas.tools.hilbert_curve import _hilbert_distance
distances = _hilbert_distance(
self.geometry.values, total_bounds=total_bounds, level=level
)
return pd.Series(distances, index=self.index, name="hilbert_distance")
def sample_points(self, size, method="uniform", seed=None, rng=None, **kwargs):
"""
Sample points from each geometry.
Generate a MultiPoint per each geometry containing points sampled from the
geometry. You can either sample randomly from a uniform distribution or use an
advanced sampling algorithm from the ``pointpats`` package.
For polygons, this samples within the area of the polygon. For lines,
this samples along the length of the linestring. For multi-part
geometries, the weights of each part are selected according to their relevant
attribute (area for Polygons, length for LineStrings), and then points are
sampled from each part.
Any other geometry type (e.g. Point, GeometryCollection) is ignored, and an
empty MultiPoint geometry is returned.
Parameters
----------
size : int | array-like
The size of the sample requested. Indicates the number of samples to draw
from each geometry. If an array of the same length as a GeoSeries is
passed, it denotes the size of a sample per geometry.
method : str, default "uniform"
The sampling method. ``uniform`` samples uniformly at random from a
geometry using ``numpy.random.uniform``. Other allowed strings
(e.g. ``"cluster_poisson"``) denote sampling function name from the
``pointpats.random`` module (see
http://pysal.org/pointpats/api.html#random-distributions). Pointpats methods
are implemented for (Multi)Polygons only and will return an empty MultiPoint
for other geometry types.
rng : {None, int, array_like[ints], SeedSequence, BitGenerator, Generator}, optional
A random generator or seed to initialize the numpy BitGenerator. If None, then fresh,
unpredictable entropy will be pulled from the OS.
**kwargs : dict
Options for the pointpats sampling algorithms.
Returns
-------
GeoSeries
Points sampled within (or along) each geometry.
Examples
--------
>>> from shapely.geometry import Polygon
>>> s = geopandas.GeoSeries(
... [
... Polygon([(1, -1), (1, 0), (0, 0)]),
... Polygon([(3, -1), (4, 0), (3, 1)]),
... ]
... )
>>> s.sample_points(size=10) # doctest: +SKIP
0 MULTIPOINT ((0.1045 -0.10294), (0.35249 -0.264...
1 MULTIPOINT ((3.03261 -0.43069), (3.10068 0.114...
Name: sampled_points, dtype: geometry
""" # noqa: E501
from .geoseries import GeoSeries
from .tools._random import uniform
if seed is not None:
warn(
"The 'seed' keyword is deprecated. Use 'rng' instead.",
FutureWarning,
stacklevel=2,
)
rng = seed
if method == "uniform":
if pd.api.types.is_list_like(size):
result = [uniform(geom, s, rng) for geom, s in zip(self.geometry, size)]
else:
result = self.geometry.apply(uniform, size=size, rng=rng)
else:
pointpats = compat.import_optional_dependency(
"pointpats",
f"For complex sampling methods, the pointpats module is required. "
f"Your requested method, '{method}' was not a supported option "
f"and the pointpats package was not able to be imported.",
)
if not hasattr(pointpats.random, method):
raise AttributeError(
f"pointpats.random module has no sampling method {method}."
f"Consult the pointpats.random module documentation for"
f" available random sampling methods."
)
sample_function = getattr(pointpats.random, method)
result = self.geometry.apply(
lambda x: (
points_from_xy(
*sample_function(x, size=size, **kwargs).T
).union_all()
if not (x.is_empty or x is None or "Polygon" not in x.geom_type)
else MultiPoint()
),
)
return GeoSeries(result, name="sampled_points", crs=self.crs, index=self.index)
def build_area(self, node=True):
"""Create an areal geometry formed by the constituent linework.
Builds areas from the GeoSeries that contain linework which represents the edges
of a planar graph. Any geometry type may be provided as input; only the
constituent lines and rings will be used to create the output polygons. All
geometries within the GeoSeries are considered together and the resulting
polygons therefore do not map 1:1 to input geometries.
This function converts inner rings into holes. To turn inner rings into polygons
as well, use polygonize.
Unless you know that the input GeoSeries represents a planar graph with a clean
topology (e.g. there is a node on both lines where they intersect), it is
recommended to use ``node=True`` which performs noding prior to building areal
geometry. Using ``node=False`` will provide performance benefits but may result
in incorrect polygons if the input is not of the proper topology.
If the input linework crosses, this function may produce invalid polygons. Use
:meth:`GeoSeries.make_valid` to ensure valid geometries.
Parameters
----------
node : bool, default True
Perform noding prior to building the areas, by default True.
Returns
-------
GeoSeries
GeoSeries with polygons
Examples
--------
>>> from shapely.geometry import LineString, Polygon
>>> s = geopandas.GeoSeries([
... LineString([(18, 4), (4, 2), (2, 9)]),
... LineString([(18, 4), (16, 16)]),
... LineString([(16, 16), (8, 19), (8, 12), (2, 9)]),
... LineString([(8, 6), (12, 13), (15, 8)]),
... LineString([(8, 6), (15, 8)]),
... LineString([(0, 0), (0, 3), (3, 3), (3, 0), (0, 0)]),
... Polygon([(1, 1), (2, 2), (1, 2), (1, 1)]),
... ])
>>> s.build_area()
0 POLYGON ((0 3, 3 3, 3 0, 0 0, 0 3), (1 1, 2 2,...
1 POLYGON ((4 2, 2 9, 8 12, 8 19, 16 16, 18 4, 4...
Name: polygons, dtype: geometry
"""
from .geoseries import GeoSeries
if node:
geometry_input = self.geometry.union_all()
else:
geometry_input = shapely.geometrycollections(self.geometry.values._data)
polygons = shapely.build_area(geometry_input)
return GeoSeries(polygons, crs=self.crs, name="polygons").explode(
ignore_index=True
)
def polygonize(self, node=True, full=False):
"""Create polygons formed from the linework of a GeoSeries.
Polygonizes the GeoSeries that contain linework which represents the
edges of a planar graph. Any geometry type may be provided as input; only the
constituent lines and rings will be used to create the output polygons.
Lines or rings that when combined do not completely close a polygon will be
ignored. Duplicate segments are ignored.
Unless you know that the input GeoSeries represents a planar graph with a clean
topology (e.g. there is a node on both lines where they intersect), it is
recommended to use ``node=True`` which performs noding prior to polygonization.
Using ``node=False`` will provide performance benefits but may result in
incorrect polygons if the input is not of the proper topology.
When ``full=True``, the return value is a 4-tuple containing output polygons,
along with lines which could not be converted to polygons. The return value
consists of 4 elements or varying lenghts:
- GeoSeries of the valid polygons (same as with ``full=False``)
- GeoSeries of cut edges: edges connected on both ends but not part of
polygonal output
- GeoSeries of dangles: edges connected on one end but not part of polygonal
output
- GeoSeries of invalid rings: polygons that are formed but are not valid
(bowties, etc)
Parameters
----------
node : bool, default True
Perform noding prior to polygonization, by default True.
full : bool, default False
Return the full output composed of a tuple of GeoSeries, by default False.
Returns
-------
GeoSeries | tuple(GeoSeries, GeoSeries, GeoSeries, GeoSeries)
GeoSeries with the polygons or a tuple of four GeoSeries as
``(polygons, cuts, dangles, invalid)``
Examples
--------
>>> from shapely.geometry import LineString
>>> s = geopandas.GeoSeries([
... LineString([(0, 0), (1, 1)]),
... LineString([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)]),
... LineString([(0.5, 0.2), (0.5, 0.8)]),
... ])
>>> s.polygonize()
0 POLYGON ((0 0, 0.5 0.5, 1 1, 1 0, 0 0))
1 POLYGON ((0.5 0.5, 0 0, 0 1, 1 1, 0.5 0.5))
Name: polygons, dtype: geometry
>>> polygons, cuts, dangles, invalid = s.polygonize(full=True)
"""
from .geoseries import GeoSeries
if node:
geometry_input = [self.geometry.union_all()]
else:
geometry_input = self.geometry.values
if full:
polygons, cuts, dangles, invalid = shapely.polygonize_full(geometry_input)
cuts = GeoSeries(cuts, crs=self.crs, name="cut_edges").explode(
ignore_index=True
)
dangles = GeoSeries(dangles, crs=self.crs, name="dangles").explode(
ignore_index=True
)
invalid = GeoSeries(invalid, crs=self.crs, name="invalid_rings").explode(
ignore_index=True
)
polygons = GeoSeries(polygons, crs=self.crs, name="polygons").explode(
ignore_index=True
)
return (polygons, cuts, dangles, invalid)
polygons = shapely.polygonize(geometry_input)
return GeoSeries(polygons, crs=self.crs, name="polygons").explode(
ignore_index=True
)
def _get_index_for_parts(orig_idx, outer_idx, ignore_index, index_parts):
"""Handle index when geometries get exploded to parts.
Helper function used in get_coordinates and explode.
Parameters
----------
orig_idx : pandas.Index
original index
outer_idx : array
the index of each returned geometry as a separate ndarray of integers
ignore_index : bool
index_parts : bool
Returns
-------
pandas.Index
index or multiindex
"""
if ignore_index:
return None
else:
if len(outer_idx):
# Generate inner index as a range per value of outer_idx
# 1. identify the start of each run of values in outer_idx
# 2. count number of values per run
# 3. use cumulative sums to create an incremental range
# starting at 0 in each run
run_start = np.r_[True, outer_idx[:-1] != outer_idx[1:]]
counts = np.diff(np.r_[np.nonzero(run_start)[0], len(outer_idx)])
inner_index = (~run_start).cumsum(dtype=outer_idx.dtype)
inner_index -= np.repeat(inner_index[run_start], counts)
else:
inner_index = []
# extract original index values based on integer index
outer_index = orig_idx.take(outer_idx)
if index_parts:
nlevels = outer_index.nlevels
index_arrays = [outer_index.get_level_values(lvl) for lvl in range(nlevels)]
index_arrays.append(inner_index)
index = pd.MultiIndex.from_arrays(
index_arrays, names=list(orig_idx.names) + [None]
)
else:
index = outer_index
return index
class _CoordinateIndexer:
# see docstring GeoPandasBase.cx property above
def __init__(self, obj):
self.obj = obj
def __getitem__(self, key):
obj = self.obj
xs, ys = key
# handle numeric values as x and/or y coordinate index
if type(xs) is not slice:
xs = slice(xs, xs)
if type(ys) is not slice:
ys = slice(ys, ys)
# don't know how to handle step; should this raise?
if xs.step is not None or ys.step is not None:
warn(
"Ignoring step - full interval is used.",
stacklevel=2,
)
if xs.start is None or xs.stop is None or ys.start is None or ys.stop is None:
xmin, ymin, xmax, ymax = obj.total_bounds
bbox = box(
xs.start if xs.start is not None else xmin,
ys.start if ys.start is not None else ymin,
xs.stop if xs.stop is not None else xmax,
ys.stop if ys.stop is not None else ymax,
)
idx = obj.intersects(bbox)
return obj[idx]
|