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
|
<?xml version="1.0" encoding="UTF-8"?>
<sect1 id="Spatial_Relationships_Measurements">
<title>Spatial Relationships and Measurements</title>
<refentry id="ST_3DClosestPoint">
<refnamediv>
<refname>ST_3DClosestPoint</refname>
<refpurpose>Returns the 3-dimensional point on g1 that is closest to g2. This is the first point of
the 3D shortest line. </refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>geometry <function>ST_3DClosestPoint</function></funcdef>
<paramdef><type>geometry </type>
<parameter>g1</parameter></paramdef>
<paramdef><type>geometry </type>
<parameter>g2</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns the 3-dimensional point on g1 that is closest to g2. This is the first point of
the 3D shortest line. The 3D length of the 3D shortest line is the 3D distance.
</para>
<para>&Z_support;</para>
<!-- Optionally mention supports Polyhedral Surface -->
<para>&P_support;</para>
<para>Availability: 2.0.0</para>
<para>Changed: 2.2.0 - if 2 2D geometries are input, a 2D point is returned (instead of old behavior assuming 0 for missing Z). In case of 2D and 3D, Z is no longer assumed to be 0 for missing Z.</para>
</refsection>
<refsection>
<title>Examples</title>
<informaltable>
<tgroup cols="1">
<tbody>
<row>
<entry><para>linestring and point -- both 3d and 2d closest point
<programlisting>
SELECT ST_AsEWKT(ST_3DClosestPoint(line,pt)) AS cp3d_line_pt,
ST_AsEWKT(ST_ClosestPoint(line,pt)) As cp2d_line_pt
FROM (SELECT 'POINT(100 100 30)'::geometry As pt,
'LINESTRING (20 80 20, 98 190 1, 110 180 3, 50 75 1000)'::geometry As line
) As foo;
cp3d_line_pt | cp2d_line_pt
-----------------------------------------------------------+------------------------------------------
POINT(54.6993798867619 128.935022917228 11.5475869506606) | POINT(73.0769230769231 115.384615384615)
</programlisting>
</para></entry>
</row>
<row>
<entry><para>linestring and multipoint -- both 3d and 2d closest point
<programlisting>SELECT ST_AsEWKT(ST_3DClosestPoint(line,pt)) AS cp3d_line_pt,
ST_AsEWKT(ST_ClosestPoint(line,pt)) As cp2d_line_pt
FROM (SELECT 'MULTIPOINT(100 100 30, 50 74 1000)'::geometry As pt,
'LINESTRING (20 80 20, 98 190 1, 110 180 3, 50 75 900)'::geometry As line
) As foo;
cp3d_line_pt | cp2d_line_pt
-----------------------------------------------------------+--------------
POINT(54.6993798867619 128.935022917228 11.5475869506606) | POINT(50 75)
</programlisting>
</para></entry>
</row>
<row>
<entry><para>Multilinestring and polygon both 3d and 2d closest point
<programlisting>SELECT ST_AsEWKT(ST_3DClosestPoint(poly, mline)) As cp3d,
ST_AsEWKT(ST_ClosestPoint(poly, mline)) As cp2d
FROM (SELECT ST_GeomFromEWKT('POLYGON((175 150 5, 20 40 5, 35 45 5, 50 60 5, 100 100 5, 175 150 5))') As poly,
ST_GeomFromEWKT('MULTILINESTRING((175 155 2, 20 40 20, 50 60 -2, 125 100 1, 175 155 1),
(1 10 2, 5 20 1))') As mline ) As foo;
cp3d | cp2d
-------------------------------------------+--------------
POINT(39.993580415989 54.1889925532825 5) | POINT(20 40)
</programlisting>
</para></entry>
</row>
</tbody>
</tgroup>
</informaltable>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_AsEWKT" />, <xref linkend="ST_ClosestPoint"/>, <xref linkend="ST_3DDistance"/>, <xref linkend="ST_3DShortestLine"/></para>
</refsection>
</refentry>
<refentry id="ST_3DDistance">
<refnamediv>
<refname>ST_3DDistance</refname>
<refpurpose>For geometry type Returns the 3-dimensional cartesian minimum distance (based on spatial ref) between two geometries in
projected units. </refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>float <function>ST_3DDistance</function></funcdef>
<paramdef><type>geometry </type>
<parameter>g1</parameter></paramdef>
<paramdef><type>geometry </type>
<parameter>g2</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>For geometry type returns the 3-dimensional minimum cartesian distance between two geometries in
projected units (spatial ref units).</para>
<para>&Z_support;</para>
<!-- Optionally mention supports Polyhedral Surface -->
<para>&P_support;</para>
<para>&sqlmm_compliant; SQL-MM ?</para>
<para>&sfcgal_enhanced;</para>
<para>Availability: 2.0.0</para>
<para>Changed: 2.2.0 - In case of 2D and 3D, Z is no longer assumed to be 0 for missing Z.</para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting>
-- Geometry example - units in meters (SRID: 2163 US National Atlas Equal area) (3D point and line compared 2D point and line)
-- Note: currently no vertical datum support so Z is not transformed and assumed to be same units as final.
SELECT ST_3DDistance(
ST_Transform(ST_GeomFromEWKT('SRID=4326;POINT(-72.1235 42.3521 4)'),2163),
ST_Transform(ST_GeomFromEWKT('SRID=4326;LINESTRING(-72.1260 42.45 15, -72.123 42.1546 20)'),2163)
) As dist_3d,
ST_Distance(
ST_Transform(ST_GeomFromText('POINT(-72.1235 42.3521)',4326),2163),
ST_Transform(ST_GeomFromText('LINESTRING(-72.1260 42.45, -72.123 42.1546)', 4326),2163)
) As dist_2d;
dist_3d | dist_2d
------------------+-----------------
127.295059324629 | 126.66425605671
</programlisting>
<programlisting>
-- Multilinestring and polygon both 3d and 2d distance
-- Same example as 3D closest point example
SELECT ST_3DDistance(poly, mline) As dist3d,
ST_Distance(poly, mline) As dist2d
FROM (SELECT ST_GeomFromEWKT('POLYGON((175 150 5, 20 40 5, 35 45 5, 50 60 5, 100 100 5, 175 150 5))') As poly,
ST_GeomFromEWKT('MULTILINESTRING((175 155 2, 20 40 20, 50 60 -2, 125 100 1, 175 155 1),
(1 10 2, 5 20 1))') As mline ) As foo;
dist3d | dist2d
-------------------+--------
0.716635696066337 | 0
</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_Distance"/>, <xref linkend="ST_3DClosestPoint"/>, <xref linkend="ST_3DDWithin"/>, <xref linkend="ST_3DMaxDistance" />, <xref linkend="ST_3DShortestLine"/>, <xref linkend="ST_Transform" /></para>
</refsection>
</refentry>
<refentry id="ST_3DDWithin">
<refnamediv>
<refname>ST_3DDWithin</refname>
<refpurpose>For 3d (z) geometry type Returns true if two geometries 3d distance is within number of units. </refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>boolean <function>ST_3DDWithin</function></funcdef>
<paramdef><type>geometry </type>
<parameter>g1</parameter></paramdef>
<paramdef><type>geometry </type>
<parameter>g2</parameter></paramdef>
<paramdef><type>double precision </type>
<parameter>distance_of_srid</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>For geometry type returns true if the 3d distance between two objects is within distance_of_srid specified
projected units (spatial ref units). </para>
<para>&Z_support;</para>
<!-- Optionally mention supports Polyhedral Surface -->
<para>&P_support;</para>
<para>&sqlmm_compliant; SQL-MM ?</para>
<para>Availability: 2.0.0</para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting>
-- Geometry example - units in meters (SRID: 2163 US National Atlas Equal area) (3D point and line compared 2D point and line)
-- Note: currently no vertical datum support so Z is not transformed and assumed to be same units as final.
SELECT ST_3DDWithin(
ST_Transform(ST_GeomFromEWKT('SRID=4326;POINT(-72.1235 42.3521 4)'),2163),
ST_Transform(ST_GeomFromEWKT('SRID=4326;LINESTRING(-72.1260 42.45 15, -72.123 42.1546 20)'),2163),
126.8
) As within_dist_3d,
ST_DWithin(
ST_Transform(ST_GeomFromEWKT('SRID=4326;POINT(-72.1235 42.3521 4)'),2163),
ST_Transform(ST_GeomFromEWKT('SRID=4326;LINESTRING(-72.1260 42.45 15, -72.123 42.1546 20)'),2163),
126.8
) As within_dist_2d;
within_dist_3d | within_dist_2d
----------------+----------------
f | t
</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_3DDistance"/>, <xref linkend="ST_Distance"/>, <xref linkend="ST_DWithin"/>, <xref linkend="ST_3DMaxDistance" />, <xref linkend="ST_Transform" /></para>
</refsection>
</refentry>
<refentry id="ST_3DDFullyWithin">
<refnamediv>
<refname>ST_3DDFullyWithin</refname>
<refpurpose>Returns true if all of the 3D geometries are within the specified
distance of one another. </refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>boolean <function>ST_3DDFullyWithin</function></funcdef>
<paramdef><type>geometry </type>
<parameter>g1</parameter></paramdef>
<paramdef><type>geometry </type>
<parameter>g2</parameter></paramdef>
<paramdef><type>double precision </type>
<parameter>distance</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns true if the 3D geometries are fully within the specified distance
of one another. The distance is specified in units defined by the
spatial reference system of the geometries. For this function to make
sense, the source geometries must both be of the same coordinate projection,
having the same SRID.</para>
<note>
<para>This function call will automatically include a bounding box
comparison that will make use of any indexes that are available on
the geometries.</para>
</note>
<para>Availability: 2.0.0</para>
<para>&Z_support;</para>
<!-- Optionally mention supports Polyhedral Surface -->
<para>&P_support;</para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting>
-- This compares the difference between fully within and distance within as well
-- as the distance fully within for the 2D footprint of the line/point vs. the 3d fully within
SELECT ST_3DDFullyWithin(geom_a, geom_b, 10) as D3DFullyWithin10, ST_3DDWithin(geom_a, geom_b, 10) as D3DWithin10,
ST_DFullyWithin(geom_a, geom_b, 20) as D2DFullyWithin20,
ST_3DDFullyWithin(geom_a, geom_b, 20) as D3DFullyWithin20 from
(select ST_GeomFromEWKT('POINT(1 1 2)') as geom_a,
ST_GeomFromEWKT('LINESTRING(1 5 2, 2 7 20, 1 9 100, 14 12 3)') as geom_b) t1;
d3dfullywithin10 | d3dwithin10 | d2dfullywithin20 | d3dfullywithin20
------------------+-------------+------------------+------------------
f | t | t | f </programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_3DMaxDistance"/>, <xref linkend="ST_3DDWithin"/>, <xref linkend="ST_DWithin"/>, <xref linkend="ST_DFullyWithin"/></para>
</refsection>
</refentry>
<refentry id="ST_3DIntersects">
<refnamediv>
<refname>ST_3DIntersects</refname>
<refpurpose>Returns TRUE if the Geometries "spatially
intersect" in 3d - only for points, linestrings, polygons, polyhedral surface (area). With SFCGAL backend enabled also supports TINS
</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>boolean <function>ST_3DIntersects</function></funcdef>
<paramdef>
<type>geometry</type>
<parameter>geomA</parameter>
</paramdef>
<paramdef>
<type>geometry</type>
<parameter>geomB</parameter>
</paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Overlaps, Touches, Within all imply spatial intersection. If any of the aforementioned
returns true, then the geometries also spatially intersect.
Disjoint implies false for spatial intersection.</para>
<para>Availability: 2.0.0</para>
<note>
<para>This function call will automatically include a bounding box
comparison that will make use of any indexes that are available on the
geometries.</para>
</note>
<note><para>In order to take advantage of support for TINS, you need to enable the SFCGAL backend. This can be done at session time with: <code>set postgis.backend = sfcgal;</code> or at the database or system level. Database level can be done with <code>ALTER DATABASE gisdb SET postgis.backend = sfcgal;</code>.</para></note>
<para>&Z_support;</para>
<!-- Optionally mention supports Polyhedral Surface -->
<para>&P_support;</para>
<para>&T_support;</para>
<para>&sfcgal_enhanced;</para>
<para>&sqlmm_compliant; SQL-MM 3: ?</para>
</refsection>
<refsection>
<title>Geometry Examples</title>
<programlisting>SELECT ST_3DIntersects(pt, line), ST_Intersects(pt,line)
FROM (SELECT 'POINT(0 0 2)'::geometry As pt,
'LINESTRING (0 0 1, 0 2 3 )'::geometry As line) As foo;
st_3dintersects | st_intersects
-----------------+---------------
f | t
(1 row)
</programlisting>
</refsection>
<refsection><title>TIN Examples</title>
<programlisting>set postgis.backend = sfcgal;
SELECT ST_3DIntersects('TIN(((0 0,1 0,0 1,0 0)))'::geometry, 'POINT(.1 .1)'::geometry);
st_3dintersects
-----------------
t</programlisting></refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_Intersects"/></para>
</refsection>
</refentry>
<refentry id="ST_3DLongestLine">
<refnamediv>
<refname>ST_3DLongestLine</refname>
<refpurpose>Returns the 3-dimensional longest line between two geometries</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>geometry <function>ST_3DLongestLine</function></funcdef>
<paramdef><type>geometry </type>
<parameter>g1</parameter></paramdef>
<paramdef><type>geometry </type>
<parameter>g2</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns the 3-dimensional longest line between two geometries. The function will
only return the first longest line if more than one.
The line returned will always start in g1 and end in g2.
The 3D length of the line this function returns will always be the same as <xref linkend="ST_3DMaxDistance" /> returns for g1 and g2.
</para>
<para>Availability: 2.0.0</para>
<para>Changed: 2.2.0 - if 2 2D geometries are input, a 2D point is returned (instead of old behavior assuming 0 for missing Z). In case of 2D and 3D, Z is no longer assumed to be 0 for missing Z.</para>
<para>&Z_support;</para>
<!-- Optionally mention supports Polyhedral Surface -->
<para>&P_support;</para>
</refsection>
<refsection>
<title>Examples</title>
<informaltable>
<tgroup cols="1">
<tbody>
<row>
<entry><para>linestring and point -- both 3d and 2d longest line
<programlisting>
SELECT ST_AsEWKT(ST_3DLongestLine(line,pt)) AS lol3d_line_pt,
ST_AsEWKT(ST_LongestLine(line,pt)) As lol2d_line_pt
FROM (SELECT 'POINT(100 100 30)'::geometry As pt,
'LINESTRING (20 80 20, 98 190 1, 110 180 3, 50 75 1000)'::geometry As line
) As foo;
lol3d_line_pt | lol2d_line_pt
-----------------------------------+----------------------------
LINESTRING(50 75 1000,100 100 30) | LINESTRING(98 190,100 100)
</programlisting>
</para></entry>
</row>
<row>
<entry><para>linestring and multipoint -- both 3d and 2d longest line
<programlisting>SELECT ST_AsEWKT(ST_3DLongestLine(line,pt)) AS lol3d_line_pt,
ST_AsEWKT(ST_LongestLine(line,pt)) As lol2d_line_pt
FROM (SELECT 'MULTIPOINT(100 100 30, 50 74 1000)'::geometry As pt,
'LINESTRING (20 80 20, 98 190 1, 110 180 3, 50 75 900)'::geometry As line
) As foo;
lol3d_line_pt | lol2d_line_pt
---------------------------------+--------------------------
LINESTRING(98 190 1,50 74 1000) | LINESTRING(98 190,50 74)
</programlisting>
</para></entry>
</row>
<row>
<entry><para>Multilinestring and polygon both 3d and 2d longest line
<programlisting>SELECT ST_AsEWKT(ST_3DLongestLine(poly, mline)) As lol3d,
ST_AsEWKT(ST_LongestLine(poly, mline)) As lol2d
FROM (SELECT ST_GeomFromEWKT('POLYGON((175 150 5, 20 40 5, 35 45 5, 50 60 5, 100 100 5, 175 150 5))') As poly,
ST_GeomFromEWKT('MULTILINESTRING((175 155 2, 20 40 20, 50 60 -2, 125 100 1, 175 155 1),
(1 10 2, 5 20 1))') As mline ) As foo;
lol3d | lol2d
------------------------------+--------------------------
LINESTRING(175 150 5,1 10 2) | LINESTRING(175 150,1 10)
</programlisting>
</para></entry>
</row>
</tbody>
</tgroup>
</informaltable>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_3DClosestPoint"/>, <xref linkend="ST_3DDistance"/>, <xref linkend="ST_LongestLine"/>, <xref linkend="ST_3DShortestLine"/>, <xref linkend="ST_3DMaxDistance"/></para>
</refsection>
</refentry>
<refentry id="ST_3DMaxDistance">
<refnamediv>
<refname>ST_3DMaxDistance</refname>
<refpurpose>For geometry type Returns the 3-dimensional cartesian maximum distance (based on spatial ref) between two geometries in
projected units. </refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>float <function>ST_3DMaxDistance</function></funcdef>
<paramdef><type>geometry </type>
<parameter>g1</parameter></paramdef>
<paramdef><type>geometry </type>
<parameter>g2</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>For geometry type returns the 3-dimensional maximum cartesian distance between two geometries in
projected units (spatial ref units). </para>
<para>&Z_support;</para>
<!-- Optionally mention supports Polyhedral Surface -->
<para>&P_support;</para>
<para>Availability: 2.0.0</para>
<para>Changed: 2.2.0 - In case of 2D and 3D, Z is no longer assumed to be 0 for missing Z.</para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting>
-- Geometry example - units in meters (SRID: 2163 US National Atlas Equal area) (3D point and line compared 2D point and line)
-- Note: currently no vertical datum support so Z is not transformed and assumed to be same units as final.
SELECT ST_3DMaxDistance(
ST_Transform(ST_GeomFromEWKT('SRID=4326;POINT(-72.1235 42.3521 10000)'),2163),
ST_Transform(ST_GeomFromEWKT('SRID=4326;LINESTRING(-72.1260 42.45 15, -72.123 42.1546 20)'),2163)
) As dist_3d,
ST_MaxDistance(
ST_Transform(ST_GeomFromEWKT('SRID=4326;POINT(-72.1235 42.3521 10000)'),2163),
ST_Transform(ST_GeomFromEWKT('SRID=4326;LINESTRING(-72.1260 42.45 15, -72.123 42.1546 20)'),2163)
) As dist_2d;
dist_3d | dist_2d
------------------+------------------
24383.7467488441 | 22247.8472107251
</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_Distance"/>, <xref linkend="ST_3DDWithin"/>, <xref linkend="ST_3DMaxDistance" />, <xref linkend="ST_Transform" /></para>
</refsection>
</refentry>
<refentry id="ST_3DShortestLine">
<refnamediv>
<refname>ST_3DShortestLine</refname>
<refpurpose>Returns the 3-dimensional shortest line between two geometries</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>geometry <function>ST_3DShortestLine</function></funcdef>
<paramdef><type>geometry </type>
<parameter>g1</parameter></paramdef>
<paramdef><type>geometry </type>
<parameter>g2</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns the 3-dimensional shortest line between two geometries. The function will
only return the first shortest line if more than one, that the function finds.
If g1 and g2 intersects in just one point the function will return a line with both start
and end in that intersection-point.
If g1 and g2 are intersecting with more than one point the function will return a line with start
and end in the same point but it can be any of the intersecting points.
The line returned will always start in g1 and end in g2.
The 3D length of the line this function returns will always be the same as <xref linkend="ST_3DDistance" /> returns for g1 and g2.
</para>
<para>Availability: 2.0.0</para>
<para>Changed: 2.2.0 - if 2 2D geometries are input, a 2D point is returned (instead of old behavior assuming 0 for missing Z). In case of 2D and 3D, Z is no longer assumed to be 0 for missing Z.</para>
<para>&Z_support;</para>
<!-- Optionally mention supports Polyhedral Surface -->
<para>&P_support;</para>
</refsection>
<refsection>
<title>Examples</title>
<informaltable>
<tgroup cols="1">
<tbody>
<row>
<entry><para>linestring and point -- both 3d and 2d shortest line
<programlisting>
SELECT ST_AsEWKT(ST_3DShortestLine(line,pt)) AS shl3d_line_pt,
ST_AsEWKT(ST_ShortestLine(line,pt)) As shl2d_line_pt
FROM (SELECT 'POINT(100 100 30)'::geometry As pt,
'LINESTRING (20 80 20, 98 190 1, 110 180 3, 50 75 1000)'::geometry As line
) As foo;
shl3d_line_pt | shl2d_line_pt
----------------------------------------------------------------------------+------------------------------------------------------
LINESTRING(54.6993798867619 128.935022917228 11.5475869506606,100 100 30) | LINESTRING(73.0769230769231 115.384615384615,100 100)
</programlisting>
</para></entry>
</row>
<row>
<entry><para>linestring and multipoint -- both 3d and 2d shortest line
<programlisting>SELECT ST_AsEWKT(ST_3DShortestLine(line,pt)) AS shl3d_line_pt,
ST_AsEWKT(ST_ShortestLine(line,pt)) As shl2d_line_pt
FROM (SELECT 'MULTIPOINT(100 100 30, 50 74 1000)'::geometry As pt,
'LINESTRING (20 80 20, 98 190 1, 110 180 3, 50 75 900)'::geometry As line
) As foo;
shl3d_line_pt | shl2d_line_pt
---------------------------------------------------------------------------+------------------------
LINESTRING(54.6993798867619 128.935022917228 11.5475869506606,100 100 30) | LINESTRING(50 75,50 74)
</programlisting>
</para></entry>
</row>
<row>
<entry><para>Multilinestring and polygon both 3d and 2d shortest line
<programlisting>SELECT ST_AsEWKT(ST_3DShortestLine(poly, mline)) As shl3d,
ST_AsEWKT(ST_ShortestLine(poly, mline)) As shl2d
FROM (SELECT ST_GeomFromEWKT('POLYGON((175 150 5, 20 40 5, 35 45 5, 50 60 5, 100 100 5, 175 150 5))') As poly,
ST_GeomFromEWKT('MULTILINESTRING((175 155 2, 20 40 20, 50 60 -2, 125 100 1, 175 155 1),
(1 10 2, 5 20 1))') As mline ) As foo;
shl3d | shl2d
---------------------------------------------------------------------------------------------------+------------------------
LINESTRING(39.993580415989 54.1889925532825 5,40.4078575708294 53.6052383805529 5.03423778139177) | LINESTRING(20 40,20 40)
</programlisting>
</para></entry>
</row>
</tbody>
</tgroup>
</informaltable>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_3DClosestPoint"/>, <xref linkend="ST_3DDistance"/>, <xref linkend="ST_LongestLine"/>, <xref linkend="ST_ShortestLine"/>, <xref linkend="ST_3DMaxDistance"/></para>
</refsection>
</refentry>
<refentry id="ST_Area">
<refnamediv>
<refname>ST_Area</refname>
<refpurpose>Returns the area of the surface if it is a Polygon or
MultiPolygon. For geometry, a 2D Cartesian area is determined with units specified by the SRID. For geography, area is determined on a curved surface with units in square meters.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>float <function>ST_Area</function></funcdef>
<paramdef><type>geometry </type><parameter>g1</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>float <function>ST_Area</function></funcdef>
<paramdef><type>geography </type><parameter>geog</parameter></paramdef>
<paramdef choice="opt"><type>boolean </type><parameter>use_spheroid=true</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns the area of the geometry if it is a Polygon or
MultiPolygon. Return the area measurement of an ST_Surface or
ST_MultiSurface value. For geometry, a 2D Cartesian area is determined with units specified by the SRID. For geography, by default area is determined on a spheroid with units in square meters.
To measure around the faster but less accurate sphere, use ST_Area(geog,false).
</para>
<para>Enhanced: 2.0.0 - support for 2D polyhedral surfaces was introduced.</para>
<para>Enhanced: 2.2.0 - measurement on spheroid performed with GeographicLib for improved accuracy and robustness. Requires Proj >= 4.9.0 to take advantage of the new feature.</para>
<para>&sfs_compliant;</para>
<para>&sqlmm_compliant; SQL-MM 3: 8.1.2, 9.5.3</para>
<para>&P_support;</para>
<note><para>For polyhedral surfaces, only supports 2D polyhedral surfaces (not 2.5D). For 2.5D, may give a non-zero answer, but only for the faces that
sit completely in XY plane.</para></note>
<para>&sfcgal_enhanced;</para>
</refsection>
<refsection>
<title>Examples</title>
<para>Return area in square feet for a plot of Massachusetts land and multiply by conversion to get square meters.
Note this is in square feet because EPSG:2249 is
Massachusetts State Plane Feet </para>
<programlisting>
SELECT ST_Area(the_geom) As sqft, ST_Area(the_geom)*POWER(0.3048,2) As sqm
FROM (SELECT
ST_GeomFromText('POLYGON((743238 2967416,743238 2967450,
743265 2967450,743265.625 2967416,743238 2967416))',2249) ) As foo(the_geom);
sqft | sqm
---------+-------------
928.625 | 86.27208552
</programlisting>
<para>Return area square feet and transform to Massachusetts state plane meters (EPSG:26986) to get square meters.
Note this is in square feet because 2249 is
Massachusetts State Plane Feet and transformed area is in square meters since EPSG:26986 is state plane Massachusetts meters </para>
<programlisting>
SELECT ST_Area(the_geom) As sqft, ST_Area(ST_Transform(the_geom,26986)) As sqm
FROM (SELECT
ST_GeomFromText('POLYGON((743238 2967416,743238 2967450,
743265 2967450,743265.625 2967416,743238 2967416))',2249) ) As foo(the_geom);
sqft | sqm
---------+------------------
928.625 | 86.2724304199219
</programlisting>
<para>Return area square feet and square meters using geography data type. Note that we transform to our geometry to geography
(before you can do that make sure your geometry is in WGS 84 long lat 4326). Geography always measures in meters.
This is just for demonstration to compare. Normally your table will be stored in geography data type already.</para>
<programlisting>
SELECT ST_Area(the_geog)/POWER(0.3048,2) As sqft_spheroid, ST_Area(the_geog,false)/POWER(0.3048,2) As sqft_sphere, ST_Area(the_geog) As sqm_spheroid
FROM (SELECT
geography(
ST_Transform(
ST_GeomFromText('POLYGON((743238 2967416,743238 2967450,743265 2967450,743265.625 2967416,743238 2967416))',
2249
) ,4326
)
)
) As foo(the_geog);
sqft_spheroid | sqft_sphere | sqm_spheroid
------------------+------------------+------------------
928.684403538925 | 927.049336105925 | 86.2776042893529
--if your data is in geography already
SELECT ST_Area(the_geog)/POWER(0.3048,2) As sqft, ST_Area(the_geog) As sqm
FROM somegeogtable;</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_GeomFromText" />, <xref linkend="ST_GeographyFromText" />, <xref linkend="ST_SetSRID" />, <xref linkend="ST_Transform" /></para>
</refsection>
</refentry>
<refentry id="ST_Azimuth">
<refnamediv>
<refname>ST_Azimuth</refname>
<refpurpose>Returns the north-based azimuth as the angle in radians measured clockwise from the vertical on pointA to pointB.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>float <function>ST_Azimuth</function></funcdef>
<paramdef><type>geometry </type><parameter>pointA</parameter></paramdef>
<paramdef><type>geometry </type><parameter>pointB</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>float <function>ST_Azimuth</function></funcdef>
<paramdef><type>geography </type><parameter>pointA</parameter></paramdef>
<paramdef><type>geography </type><parameter>pointB</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns the azimuth in radians of the segment defined by the given
point geometries, or NULL if the two points are coincident. The azimuth is angle is referenced from north, and is positive clockwise: North = 0; East = π/2; South = π; West = 3π/2.</para>
<para>For the geography type, the forward azimuth is solved as part of the inverse geodesic problem.</para>
<para>The azimuth is mathematical concept defined as the angle between a reference plane and a point, with angular units in radians.
Units can be converted to degrees using a built-in PostgreSQL function degrees(), as shown in the example.</para>
<para>Availability: 1.1.0</para>
<para>Enhanced: 2.0.0 support for geography was introduced.</para>
<para>Enhanced: 2.2.0 measurement on spheroid performed with GeographicLib for improved accuracy and robustness. Requires Proj >= 4.9.0 to take advantage of the new feature.</para>
<para>Azimuth is especially useful in conjunction with ST_Translate for shifting an object along its perpendicular axis. See
upgis_lineshift <ulink url="http://trac.osgeo.org/postgis/wiki/UsersWikiplpgsqlfunctions">Plpgsqlfunctions PostGIS wiki section</ulink> for example of this.</para>
</refsection>
<refsection>
<title>Examples</title>
<para>Geometry Azimuth in degrees </para>
<programlisting>
SELECT degrees(ST_Azimuth(ST_Point(25, 45), ST_Point(75, 100))) AS degA_B,
degrees(ST_Azimuth(ST_Point(75, 100), ST_Point(25, 45))) AS degB_A;
dega_b | degb_a
------------------+------------------
42.2736890060937 | 222.273689006094
</programlisting>
<informaltable>
<tgroup cols="2">
<tbody>
<row>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_azimuth01.png" />
</imageobject>
<caption><para>Green: the start Point(25,45) with its vertical. Yellow: degA_B as the path to travel (azimuth).</para></caption>
</mediaobject>
</informalfigure>
</para></entry>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_azimuth02.png" />
</imageobject>
<caption><para>Green: the start Point(75,100) with its vertical. Yellow: degB_A as the path to travel (azimuth).</para></caption>
</mediaobject>
</informalfigure>
</para></entry>
</row>
</tbody>
</tgroup>
</informaltable>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_Point" />, <xref linkend="ST_Translate" />, <xref linkend="ST_Project" />, <ulink url="http://www.postgresql.org/docs/current/interactive/functions-math.html">PostgreSQL Math Functions</ulink></para>
</refsection>
</refentry>
<refentry id="ST_Centroid">
<refnamediv>
<refname>ST_Centroid</refname>
<refpurpose>Returns the geometric center of a geometry.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>geometry <function>ST_Centroid</function></funcdef>
<paramdef><type>geometry </type>
<parameter>g1</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Computes the geometric center of a geometry, or equivalently,
the center of mass of the geometry as a <varname>POINT</varname>. For
[<varname>MULTI</varname>]<varname>POINT</varname>s, this is computed
as the arithmetic mean of the input coordinates. For
[<varname>MULTI</varname>]<varname>LINESTRING</varname>s, this is
computed as the weighted length of each line segment. For
[<varname>MULTI</varname>]<varname>POLYGON</varname>s, "weight" is
thought in terms of area. If an empty geometry is supplied, an empty
<varname>GEOMETRYCOLLECTION</varname> is returned. If
<varname>NULL</varname> is supplied, <varname>NULL</varname> is
returned.
If <varname>CIRCULARSTRING</varname> or <varname>COMPOUNDCURVE</varname>
are supplied, they are converted to linestring wtih CurveToLine first,
then same than for <varname>LINESTRING</varname>
</para>
<para>New in 2.3.0 : support <varname>CIRCULARSTRING</varname> and <varname>COMPOUNDCURVE</varname> (using CurveToLine)</para>
<para>The centroid is equal to the centroid of the set of component
Geometries of highest dimension (since the lower-dimension geometries
contribute zero "weight" to the centroid).</para>
<para>&sfs_compliant;</para>
<para>&sqlmm_compliant; SQL-MM 3: 8.1.4, 9.5.5</para>
</refsection>
<refsection>
<title>Examples</title>
<para>In each of the following illustrations, the blue dot represents
the centroid of the source geometry.</para>
<informaltable>
<tgroup cols="2">
<tbody>
<row>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_centroid01.png" />
</imageobject>
<caption><para>Centroid of a
<varname>MULTIPOINT</varname></para></caption>
</mediaobject>
</informalfigure></para></entry>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_centroid02.png" />
</imageobject>
<caption><para>Centroid of a
<varname>LINESTRING</varname></para></caption>
</mediaobject>
</informalfigure></para></entry>
</row>
<row>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_centroid03.png" />
</imageobject>
<caption><para>Centroid of a
<varname>POLYGON</varname></para></caption>
</mediaobject>
</informalfigure></para></entry>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_centroid04.png" />
</imageobject>
<caption><para>Centroid of a
<varname>GEOMETRYCOLLECTION</varname></para></caption>
</mediaobject>
</informalfigure></para></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<programlisting>SELECT ST_AsText(ST_Centroid('MULTIPOINT ( -1 0, -1 2, -1 3, -1 4, -1 7, 0 1, 0 3, 1 1, 2 0, 6 0, 7 8, 9 8, 10 6 )'));
st_astext
------------------------------------------
POINT(2.30769230769231 3.30769230769231)
(1 row)
SELECT ST_AsText(ST_centroid(g))
FROM ST_GeomFromText('CIRCULARSTRING(0 2, -1 1,0 0, 0.5 0, 1 0, 2 1, 1 2, 0.5 2, 0 2)') AS g ;
------------------------------------------
POINT(0.5 1)
SELECT ST_AsText(ST_centroid(g))
FROM ST_GeomFromText('COMPOUNDCURVE(CIRCULARSTRING(0 2, -1 1,0 0),(0 0, 0.5 0, 1 0),CIRCULARSTRING( 1 0, 2 1, 1 2),(1 2, 0.5 2, 0 2))' ) AS g;
------------------------------------------
POINT(0.5 1)
</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_PointOnSurface" /></para>
</refsection>
</refentry>
<refentry id="ST_ClosestPoint">
<refnamediv>
<refname>ST_ClosestPoint</refname>
<refpurpose>Returns the 2-dimensional point on g1 that is closest to g2. This is the first point of
the shortest line.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>geometry <function>ST_ClosestPoint</function></funcdef>
<paramdef><type>geometry </type>
<parameter>g1</parameter></paramdef>
<paramdef><type>geometry </type>
<parameter>g2</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns the 2-dimensional point on g1 that is closest to g2. This is the first point of
the shortest line.
</para>
<note><para>If you have a 3D Geometry, you may prefer to use <xref linkend="ST_3DClosestPoint" />.</para></note>
<para>Availability: 1.5.0</para>
</refsection>
<refsection>
<title>Examples</title>
<informaltable>
<tgroup cols="2">
<tbody>
<row>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_closestpoint01.png" />
</imageobject>
<caption><para>Closest between point and linestring is the point itself, but closest
point between a linestring and point is the point on line string that is closest.</para></caption>
</mediaobject>
</informalfigure>
<programlisting>
SELECT ST_AsText(ST_ClosestPoint(pt,line)) AS cp_pt_line,
ST_AsText(ST_ClosestPoint(line,pt)) As cp_line_pt
FROM (SELECT 'POINT(100 100)'::geometry As pt,
'LINESTRING (20 80, 98 190, 110 180, 50 75 )'::geometry As line
) As foo;
cp_pt_line | cp_line_pt
----------------+------------------------------------------
POINT(100 100) | POINT(73.0769230769231 115.384615384615)
</programlisting>
</para></entry>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_closestpoint02.png" />
</imageobject>
<caption><para>closest point on polygon A to polygon B</para></caption>
</mediaobject>
</informalfigure>
<programlisting>
SELECT ST_AsText(
ST_ClosestPoint(
ST_GeomFromText('POLYGON((175 150, 20 40, 50 60, 125 100, 175 150))'),
ST_Buffer(ST_GeomFromText('POINT(110 170)'), 20)
)
) As ptwkt;
ptwkt
------------------------------------------
POINT(140.752120669087 125.695053378061)
</programlisting>
</para></entry>
</row>
</tbody>
</tgroup>
</informaltable>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_3DClosestPoint" />,<xref linkend="ST_Distance"/>, <xref linkend="ST_LongestLine"/>, <xref linkend="ST_ShortestLine"/>, <xref linkend="ST_MaxDistance"/></para>
</refsection>
</refentry>
<refentry id="ST_ClusterDBSCAN">
<refnamediv>
<refname>ST_ClusterDBSCAN</refname>
<refpurpose>Windowing function that returns integer id for the cluster each input geometry is in based on 2D implementation of Density-based spatial clustering of applications with noise (DBSCAN) algorithm.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>integer <function>ST_ClusterDBSCAN</function></funcdef>
<paramdef><type>geometry winset </type>
<parameter>geom</parameter></paramdef>
<paramdef><type>float8 </type>
<parameter>eps</parameter></paramdef>
<paramdef><type>integer </type>
<parameter>minpoints</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>
Returns cluster number for each input geometry, based on a 2D implementation of the
<ulink url="https://en.wikipedia.org/wiki/DBSCAN">Density-based spatial clustering of applications with noise (DBSCAN)</ulink>
algorithm. Unlike <xref linkend="ST_ClusterKMeans" />, it does not require the number of clusters to be specified, but instead
uses the desired distance (<varname>eps</varname>) and density(<varname>minpoints</varname>) parameters to construct each cluster.
</para>
<para>
An input geometry will be added to a cluster if it is either:
<itemizedlist>
<listitem>
<para>
A "core" geometry, that is within <varname>eps</varname> distance of at least <varname>minpoints</varname> other input geometries, or
</para>
</listitem>
<listitem>
<para>
A "border" geometry, that is within <varname>eps</varname> distance of a core geometry.
</para>
</listitem>
</itemizedlist>
</para>
<para>
Note that border geometries may be within <varname>eps</varname> distance of core geometries in more than one cluster; in this
case, either assignment would be correct, and the border geometry will be arbitrarily asssigned to one of the available clusters.
In these cases, it is possible for a correct cluster to be generated with fewer than <varname>minpoints</varname> geometries.
When assignment of a border geometry is ambiguous, repeated calls to ST_ClusterDBSCAN will produce identical results if an ORDER BY
clause is included in the window definition, but cluster assignments may differ from other implementations of the same algorithm.
</para>
<note><para>
Input geometries that do not meet the criteria to join any other cluster will be assigned a cluster number of NULL.
</para></note>
<para>Availability: 2.3.0 - requires GEOS </para>
</refsection>
<refsection>
<title>Examples</title>
<para>
Assigning a cluster number to each parcel point:
</para>
<programlisting>
SELECT parcel_id, ST_ClusterDBSCAN(geom, eps := 0.5, minpoints := 5) over () AS cid
FROM parcels;
</programlisting>
<para>
Combining parcels with the same cluster number into a single geometry. This uses named argument calling
</para>
<programlisting>
SELECT cid, ST_Collect(geom) AS cluster_geom, array_agg(parcel_id) AS ids_in_cluster FROM (
SELECT parcel_id, ST_ClusterDBSCAN(geom, eps := 0.5, minpoints := 5) over () AS cid, geom
FROM parcels) sq
GROUP BY cid;
</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para>
<xref linkend="ST_ClusterKMeans"/>,
<xref linkend="ST_ClusterIntersecting"/>,
<xref linkend="ST_ClusterWithin"/>
</para>
</refsection>
</refentry>
<refentry id="ST_ClusterIntersecting">
<refnamediv>
<refname>ST_ClusterIntersecting</refname>
<refpurpose>Aggregate. Returns an array with the connected components of a set of geometries</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>geometry[] <function>ST_ClusterIntersecting</function></funcdef>
<paramdef><type>geometry set</type> <parameter>g</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>ST_ClusterIntersecting is an aggregate function that returns an array of GeometryCollections, where each GeometryCollection represents an interconnected set of geometries.</para>
<para>Availability: 2.2.0 - requires GEOS </para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting>
WITH testdata AS
(SELECT unnest(ARRAY['LINESTRING (0 0, 1 1)'::geometry,
'LINESTRING (5 5, 4 4)'::geometry,
'LINESTRING (6 6, 7 7)'::geometry,
'LINESTRING (0 0, -1 -1)'::geometry,
'POLYGON ((0 0, 4 0, 4 4, 0 4, 0 0))'::geometry]) AS geom)
SELECT ST_AsText(unnest(ST_ClusterIntersecting(geom))) FROM testdata;
--result
st_astext
---------
GEOMETRYCOLLECTION(LINESTRING(0 0,1 1),LINESTRING(5 5,4 4),LINESTRING(0 0,-1 -1),POLYGON((0 0,4 0,4 4,0 4,0 0)))
GEOMETRYCOLLECTION(LINESTRING(6 6,7 7))
</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para>
<xref linkend="ST_ClusterDBSCAN" />,
<xref linkend="ST_ClusterKMeans" />,
<xref linkend="ST_ClusterWithin" />
</para>
</refsection>
</refentry>
<refentry id="ST_ClusterKMeans">
<refnamediv>
<refname>ST_ClusterKMeans</refname>
<refpurpose>Windowing function that returns integer id for the cluster each input geometry is in.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>integer <function>ST_ClusterKMeans</function></funcdef>
<paramdef><type>geometry winset </type>
<parameter>geom</parameter></paramdef>
<paramdef><type>integer </type>
<parameter>number_of_clusters</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns 2D distance based
<ulink url="https://en.wikipedia.org/wiki/K-means_clustering">k-means</ulink>
cluster number for each input geometry. The distance used for clustering is the
distance between the centroids of the geometries.
</para>
<para>Availability: 2.3.0 - requires GEOS </para>
</refsection>
<refsection>
<title>Examples</title>
<para>Generate dummy set of parcels for examples</para>
<programlisting>CREATE TABLE parcels AS
SELECT lpad((row_number() over())::text,3,'0') As parcel_id, geom,
('{residential, commercial}'::text[])[1 + mod(row_number()OVER(),2)] As type
FROM
ST_Subdivide(ST_Buffer('LINESTRING(40 100, 98 100, 100 150, 60 90)'::geometry,
40, 'endcap=square'),12) As geom;
</programlisting>
<informaltable>
<tgroup cols="1">
<tbody>
<row>
<entry>
<para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_clusterkmeans01.png" />
</imageobject>
<caption><para>Original Parcels</para></caption>
</mediaobject>
</informalfigure>
</para>
</entry>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_clusterkmeans02.png" />
</imageobject>
<caption><para>Parcels color-coded by cluster number (cid)</para></caption>
</mediaobject>
</informalfigure>
<programlisting>SELECT ST_ClusterKMeans(geom, 5) OVER() AS cid, parcel_id, geom
FROM parcels;
-- result
cid | parcel_id | geom
-----+-----------+---------------
0 | 001 | 0103000000...
0 | 002 | 0103000000...
1 | 003 | 0103000000...
0 | 004 | 0103000000...
1 | 005 | 0103000000...
2 | 006 | 0103000000...
2 | 007 | 0103000000...
(7 rows)</programlisting>
</para></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<programlisting> -- Partitioning parcel clusters by type
SELECT ST_ClusterKMeans(geom,3) over (PARTITION BY type) AS cid, parcel_id, type
FROM parcels;
-- result
cid | parcel_id | type
-----+-----------+-------------
1 | 005 | commercial
1 | 003 | commercial
2 | 007 | commercial
0 | 001 | commercial
1 | 004 | residential
0 | 002 | residential
2 | 006 | residential
(7 rows)</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para>
<xref linkend="ST_ClusterDBSCAN"/>,
<xref linkend="ST_ClusterIntersecting" />,
<xref linkend="ST_ClusterWithin" />, <xref linkend="ST_Subdivide" />
</para>
</refsection>
</refentry>
<refentry id="ST_ClusterWithin">
<refnamediv>
<refname>ST_ClusterWithin</refname>
<refpurpose>Aggregate. Returns an array of GeometryCollections, where each GeometryCollection represents a set of geometries separated by no more than the specified distance.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>geometry[] <function>ST_ClusterWithin</function></funcdef>
<paramdef><type>geometry set </type> <parameter>g</parameter></paramdef>
<paramdef><type>float8 </type> <parameter>distance</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>ST_ClusterWithin is an aggregate function that returns an array of GeometryCollections, where each GeometryCollection represents a set of geometries separated by no more than the specified distance.</para>
<para>Availability: 2.2.0 - requires GEOS</para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting>
WITH testdata AS
(SELECT unnest(ARRAY['LINESTRING (0 0, 1 1)'::geometry,
'LINESTRING (5 5, 4 4)'::geometry,
'LINESTRING (6 6, 7 7)'::geometry,
'LINESTRING (0 0, -1 -1)'::geometry,
'POLYGON ((0 0, 4 0, 4 4, 0 4, 0 0))'::geometry]) AS geom)
SELECT ST_AsText(unnest(ST_ClusterWithin(geom, 1.4))) FROM testdata;
--result
st_astext
---------
GEOMETRYCOLLECTION(LINESTRING(0 0,1 1),LINESTRING(5 5,4 4),LINESTRING(0 0,-1 -1),POLYGON((0 0,4 0,4 4,0 4,0 0)))
GEOMETRYCOLLECTION(LINESTRING(6 6,7 7))
</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para>
<xref linkend="ST_ClusterDBSCAN" />,
<xref linkend="ST_ClusterKMeans" />,
<xref linkend="ST_ClusterIntersecting" />
</para>
</refsection>
</refentry>
<refentry id="ST_Contains">
<refnamediv>
<refname>ST_Contains</refname>
<refpurpose>Returns true if and only if no points of B lie in the exterior of A, and at least one point of the interior of B lies in the interior of A. </refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>boolean <function>ST_Contains</function></funcdef>
<paramdef><type>geometry </type>
<parameter>geomA</parameter></paramdef>
<paramdef><type>geometry </type>
<parameter>geomB</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Geometry A contains Geometry B if and only if no points of B lie in the exterior of A, and at least one point of the interior of B lies in the interior of A.
An important subtlety of this definition is that A does not contain its boundary, but A does contain itself. Contrast that to <xref linkend="ST_ContainsProperly" /> where geometry
A does not Contain Properly itself.</para>
<para>Returns TRUE if geometry B is completely inside geometry A. For this function to make
sense, the source geometries must both be of the same coordinate projection,
having the same SRID. ST_Contains is the inverse of ST_Within. So ST_Contains(A,B) implies ST_Within(B,A) except in the case of
invalid geometries where the result is always false regardless or not defined.</para>
<para>Performed by the GEOS module</para>
<para>Enhanced: 2.3.0 Enhancement to PIP short-circuit extended to support MultiPoints with few points. Prior versions only supported point in polygon.</para>
<important>
<para>Do not call with a <varname>GEOMETRYCOLLECTION</varname> as an argument</para>
</important>
<important>
<para>Do not use this function with invalid geometries. You will get unexpected results.</para>
</important>
<para>This function call will automatically include a bounding box
comparison that will make use of any indexes that are available on
the geometries. To avoid index use, use the function
_ST_Contains.</para>
<para>NOTE: this is the "allowable" version that returns a
boolean, not an integer.</para>
<para>&sfs_compliant; s2.1.1.2 // s2.1.13.3
- same as within(geometry B, geometry A)</para>
<para>&sqlmm_compliant; SQL-MM 3: 5.1.31</para>
<para>There are certain subtleties to ST_Contains and ST_Within that are not intuitively obvious.
For details check out <ulink url="http://lin-ear-th-inking.blogspot.com/2007/06/subtleties-of-ogc-covers-spatial.html">Subtleties of OGC Covers, Contains, Within</ulink></para>
</refsection>
<refsection>
<title>Examples</title>
<para>The <function>ST_Contains</function> predicate returns <varname>TRUE</varname> in all the following illustrations.</para>
<informaltable>
<tgroup cols="2">
<tbody>
<row>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_contains01.png" />
</imageobject>
<caption><para><varname>LINESTRING</varname> / <varname>MULTIPOINT</varname></para></caption>
</mediaobject>
</informalfigure></para></entry>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_contains02.png" />
</imageobject>
<caption><para><varname>POLYGON</varname> / <varname>POINT</varname></para></caption>
</mediaobject>
</informalfigure></para></entry>
</row>
<row>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_contains03.png" />
</imageobject>
<caption><para><varname>POLYGON</varname> / <varname>LINESTRING</varname></para></caption>
</mediaobject>
</informalfigure></para></entry>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_contains04.png" />
</imageobject>
<caption><para><varname>POLYGON</varname> / <varname>POLYGON</varname></para></caption>
</mediaobject>
</informalfigure></para></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>The <function>ST_Contains</function> predicate returns <varname>FALSE</varname> in all the following illustrations.</para>
<informaltable>
<tgroup cols="2">
<tbody>
<row>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_contains05.png" />
</imageobject>
<caption><para><varname>POLYGON</varname> / <varname>MULTIPOINT</varname></para></caption>
</mediaobject>
</informalfigure></para></entry>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_contains06.png" />
</imageobject>
<caption><para><varname>POLYGON</varname> / <varname>LINESTRING</varname></para></caption>
</mediaobject>
</informalfigure></para></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<programlisting>
-- A circle within a circle
SELECT ST_Contains(smallc, bigc) As smallcontainsbig,
ST_Contains(bigc,smallc) As bigcontainssmall,
ST_Contains(bigc, ST_Union(smallc, bigc)) as bigcontainsunion,
ST_Equals(bigc, ST_Union(smallc, bigc)) as bigisunion,
ST_Covers(bigc, ST_ExteriorRing(bigc)) As bigcoversexterior,
ST_Contains(bigc, ST_ExteriorRing(bigc)) As bigcontainsexterior
FROM (SELECT ST_Buffer(ST_GeomFromText('POINT(1 2)'), 10) As smallc,
ST_Buffer(ST_GeomFromText('POINT(1 2)'), 20) As bigc) As foo;
-- Result
smallcontainsbig | bigcontainssmall | bigcontainsunion | bigisunion | bigcoversexterior | bigcontainsexterior
------------------+------------------+------------------+------------+-------------------+---------------------
f | t | t | t | t | f
-- Example demonstrating difference between contains and contains properly
SELECT ST_GeometryType(geomA) As geomtype, ST_Contains(geomA,geomA) AS acontainsa, ST_ContainsProperly(geomA, geomA) AS acontainspropa,
ST_Contains(geomA, ST_Boundary(geomA)) As acontainsba, ST_ContainsProperly(geomA, ST_Boundary(geomA)) As acontainspropba
FROM (VALUES ( ST_Buffer(ST_Point(1,1), 5,1) ),
( ST_MakeLine(ST_Point(1,1), ST_Point(-1,-1) ) ),
( ST_Point(1,1) )
) As foo(geomA);
geomtype | acontainsa | acontainspropa | acontainsba | acontainspropba
--------------+------------+----------------+-------------+-----------------
ST_Polygon | t | f | f | f
ST_LineString | t | f | f | f
ST_Point | t | t | f | f
</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_Boundary" />, <xref linkend="ST_ContainsProperly" />, <xref linkend="ST_Covers" />, <xref linkend="ST_CoveredBy" />, <xref linkend="ST_Equals"/>, <xref linkend="ST_Within"/></para>
</refsection>
</refentry>
<refentry id="ST_ContainsProperly">
<refnamediv>
<refname>ST_ContainsProperly</refname>
<refpurpose>Returns true if B intersects the interior of A but not the boundary (or exterior). A does not contain properly itself, but does contain itself.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>boolean <function>ST_ContainsProperly</function></funcdef>
<paramdef><type>geometry </type>
<parameter>geomA</parameter></paramdef>
<paramdef><type>geometry </type>
<parameter>geomB</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns true if B intersects the interior of A but not the boundary (or exterior).</para>
<para>A does not contain properly itself, but does contain itself.</para>
<para>Every point of the other geometry is a point of this geometry's interior. The DE-9IM Intersection Matrix for the two geometries matches
[T**FF*FF*] used in <xref linkend="ST_Relate" /></para>
<note>
<para>From JTS docs slightly reworded: The advantage to using this predicate over <xref linkend="ST_Contains" /> and <xref linkend="ST_Intersects" /> is that it can be computed
efficiently, with no need to compute topology at individual points.</para>
<para>
An example use case for this predicate is computing the intersections
of a set of geometries with a large polygonal geometry.
Since intersection is a fairly slow operation, it can be more efficient
to use containsProperly to filter out test geometries which lie
wholly inside the area. In these cases the intersection is
known a priori to be exactly the original test geometry.
</para>
</note>
<para>Availability: 1.4.0 - requires GEOS >= 3.1.0.</para>
<important>
<para>Do not call with a <varname>GEOMETRYCOLLECTION</varname> as an argument</para>
</important>
<important>
<para>Do not use this function with invalid geometries. You will get unexpected results.</para>
</important>
<para>This function call will automatically include a bounding box
comparison that will make use of any indexes that are available on
the geometries. To avoid index use, use the function
_ST_ContainsProperly.</para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting>
--a circle within a circle
SELECT ST_ContainsProperly(smallc, bigc) As smallcontainspropbig,
ST_ContainsProperly(bigc,smallc) As bigcontainspropsmall,
ST_ContainsProperly(bigc, ST_Union(smallc, bigc)) as bigcontainspropunion,
ST_Equals(bigc, ST_Union(smallc, bigc)) as bigisunion,
ST_Covers(bigc, ST_ExteriorRing(bigc)) As bigcoversexterior,
ST_ContainsProperly(bigc, ST_ExteriorRing(bigc)) As bigcontainsexterior
FROM (SELECT ST_Buffer(ST_GeomFromText('POINT(1 2)'), 10) As smallc,
ST_Buffer(ST_GeomFromText('POINT(1 2)'), 20) As bigc) As foo;
--Result
smallcontainspropbig | bigcontainspropsmall | bigcontainspropunion | bigisunion | bigcoversexterior | bigcontainsexterior
------------------+------------------+------------------+------------+-------------------+---------------------
f | t | f | t | t | f
--example demonstrating difference between contains and contains properly
SELECT ST_GeometryType(geomA) As geomtype, ST_Contains(geomA,geomA) AS acontainsa, ST_ContainsProperly(geomA, geomA) AS acontainspropa,
ST_Contains(geomA, ST_Boundary(geomA)) As acontainsba, ST_ContainsProperly(geomA, ST_Boundary(geomA)) As acontainspropba
FROM (VALUES ( ST_Buffer(ST_Point(1,1), 5,1) ),
( ST_MakeLine(ST_Point(1,1), ST_Point(-1,-1) ) ),
( ST_Point(1,1) )
) As foo(geomA);
geomtype | acontainsa | acontainspropa | acontainsba | acontainspropba
--------------+------------+----------------+-------------+-----------------
ST_Polygon | t | f | f | f
ST_LineString | t | f | f | f
ST_Point | t | t | f | f
</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_GeometryType" />, <xref linkend="ST_Boundary" />, <xref linkend="ST_Contains" />, <xref linkend="ST_Covers" />, <xref linkend="ST_CoveredBy" />, <xref linkend="ST_Equals"/>, <xref linkend="ST_Relate" />, <xref linkend="ST_Within"/></para>
</refsection>
</refentry>
<refentry id="ST_Covers">
<refnamediv>
<refname>ST_Covers</refname>
<refpurpose>Returns 1 (TRUE) if no point in Geometry B is outside
Geometry A</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>boolean <function>ST_Covers</function></funcdef>
<paramdef><type>geometry </type>
<parameter>geomA</parameter></paramdef>
<paramdef><type>geometry </type>
<parameter>geomB</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>boolean <function>ST_Covers</function></funcdef>
<paramdef><type>geography </type>
<parameter>geogpolyA</parameter></paramdef>
<paramdef><type>geography </type>
<parameter>geogpointB</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns 1 (TRUE) if no point in Geometry/Geography B is outside
Geometry/Geography A</para>
<para>Performed by the GEOS module</para>
<important>
<para>Do not call with a <varname>GEOMETRYCOLLECTION</varname> as an argument</para>
</important>
<important>
<para>For geography only Polygon covers point is supported.</para>
</important>
<important>
<para>Do not use this function with invalid geometries. You will get unexpected results.</para>
</important>
<para>This function call will automatically include a bounding box
comparison that will make use of any indexes that are available on
the geometries. To avoid index use, use the function
_ST_Covers.</para>
<para>Enhanced: 2.3.0 Enhancement to PIP short-circuit for geometry extended to support MultiPoints with few points. Prior versions only supported point in polygon.</para>
<para>Availability: 1.5 - support for geography was introduced. </para>
<para>Availability: 1.2.2 - requires GEOS >= 3.0</para>
<para>NOTE: this is the "allowable" version that returns a
boolean, not an integer.</para>
<para>Not an OGC standard, but Oracle has it too.</para>
<para>There are certain subtleties to ST_Contains and ST_Within that are not intuitively obvious.
For details check out <ulink url="http://lin-ear-th-inking.blogspot.com/2007/06/subtleties-of-ogc-covers-spatial.html">Subtleties of OGC Covers, Contains, Within</ulink></para>
</refsection>
<refsection>
<title>Examples</title>
<para> Geometry example </para>
<programlisting>
--a circle covering a circle
SELECT ST_Covers(smallc,smallc) As smallinsmall,
ST_Covers(smallc, bigc) As smallcoversbig,
ST_Covers(bigc, ST_ExteriorRing(bigc)) As bigcoversexterior,
ST_Contains(bigc, ST_ExteriorRing(bigc)) As bigcontainsexterior
FROM (SELECT ST_Buffer(ST_GeomFromText('POINT(1 2)'), 10) As smallc,
ST_Buffer(ST_GeomFromText('POINT(1 2)'), 20) As bigc) As foo;
--Result
smallinsmall | smallcoversbig | bigcoversexterior | bigcontainsexterior
--------------+----------------+-------------------+---------------------
t | f | t | f
(1 row) </programlisting>
<para>Geeography Example</para>
<programlisting>
-- a point with a 300 meter buffer compared to a point, a point and its 10 meter buffer
SELECT ST_Covers(geog_poly, geog_pt) As poly_covers_pt,
ST_Covers(ST_Buffer(geog_pt,10), geog_pt) As buff_10m_covers_cent
FROM (SELECT ST_Buffer(ST_GeogFromText('SRID=4326;POINT(-99.327 31.4821)'), 300) As geog_poly,
ST_GeogFromText('SRID=4326;POINT(-99.33 31.483)') As geog_pt ) As foo;
poly_covers_pt | buff_10m_covers_cent
----------------+------------------
f | t
</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_Contains"/>, <xref linkend="ST_CoveredBy" />, <xref linkend="ST_Within"/></para>
</refsection>
</refentry>
<refentry id="ST_CoveredBy">
<refnamediv>
<refname>ST_CoveredBy</refname>
<refpurpose>Returns 1 (TRUE) if no point in Geometry/Geography A is outside
Geometry/Geography B</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>boolean <function>ST_CoveredBy</function></funcdef>
<paramdef><type>geometry </type>
<parameter>geomA</parameter></paramdef>
<paramdef><type>geometry </type>
<parameter>geomB</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>boolean <function>ST_CoveredBy</function></funcdef>
<paramdef><type>geography </type>
<parameter>geogA</parameter></paramdef>
<paramdef><type>geography </type>
<parameter>geogB</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns 1 (TRUE) if no point in Geometry/Geography A is outside
Geometry/Geography B</para>
<para>Performed by the GEOS module</para>
<important>
<para>Do not call with a <varname>GEOMETRYCOLLECTION</varname> as an argument</para>
</important>
<important>
<para>Do not use this function with invalid geometries. You will get unexpected results.</para>
</important>
<para>Availability: 1.2.2 - requires GEOS >= 3.0</para>
<para>This function call will automatically include a bounding box
comparison that will make use of any indexes that are available on
the geometries. To avoid index use, use the function
_ST_CoveredBy.</para>
<para>NOTE: this is the "allowable" version that returns a
boolean, not an integer.</para>
<para>Not an OGC standard, but Oracle has it too.</para>
<para>There are certain subtleties to ST_Contains and ST_Within that are not intuitively obvious.
For details check out <ulink url="http://lin-ear-th-inking.blogspot.com/2007/06/subtleties-of-ogc-covers-spatial.html">Subtleties of OGC Covers, Contains, Within</ulink></para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting>
--a circle coveredby a circle
SELECT ST_CoveredBy(smallc,smallc) As smallinsmall,
ST_CoveredBy(smallc, bigc) As smallcoveredbybig,
ST_CoveredBy(ST_ExteriorRing(bigc), bigc) As exteriorcoveredbybig,
ST_Within(ST_ExteriorRing(bigc),bigc) As exeriorwithinbig
FROM (SELECT ST_Buffer(ST_GeomFromText('POINT(1 2)'), 10) As smallc,
ST_Buffer(ST_GeomFromText('POINT(1 2)'), 20) As bigc) As foo;
--Result
smallinsmall | smallcoveredbybig | exteriorcoveredbybig | exeriorwithinbig
--------------+-------------------+----------------------+------------------
t | t | t | f
(1 row) </programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_Contains"/>, <xref linkend="ST_Covers" />, <xref linkend="ST_ExteriorRing"/>, <xref linkend="ST_Within"/></para>
</refsection>
</refentry>
<refentry id="ST_Crosses">
<refnamediv>
<refname>ST_Crosses</refname>
<refpurpose>Returns <varname>TRUE</varname> if the supplied geometries have some, but not all,
interior points in common.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>boolean <function>ST_Crosses</function></funcdef>
<paramdef><type>geometry </type><parameter>g1</parameter></paramdef>
<paramdef><type>geometry </type><parameter>g2</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para><function>ST_Crosses</function> takes two geometry objects and
returns <varname>TRUE</varname> if their intersection "spatially cross", that is, the
geometries have some, but not all interior points in common. The
intersection of the interiors of the geometries must not be the empty
set and must have a dimensionality less than the maximum dimension
of the two input geometries. Additionally, the intersection of the two
geometries must not equal either of the source geometries. Otherwise, it
returns <varname>FALSE</varname>.</para>
<para>In mathematical terms, this is expressed as:</para>
<remark>TODO: Insert appropriate MathML markup here or use a gif.
Simple HTML markup does not work well in both IE and Firefox.</remark>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_crosses-math.gif" />
</imageobject>
</mediaobject>
</informalfigure>
<para>The DE-9IM Intersection Matrix for the two geometries is:</para>
<itemizedlist>
<listitem>
<para><markup>T*T******</markup> (for Point/Line, Point/Area, and
Line/Area situations)</para>
</listitem>
<listitem>
<para><markup>T*****T**</markup> (for Line/Point, Area/Point, and
Area/Line situations)</para>
</listitem>
<listitem>
<para><markup>0********</markup> (for Line/Line situations)</para>
</listitem>
</itemizedlist>
<para>For any other combination of dimensions this predicate returns
false.</para>
<para>The OpenGIS Simple Features Specification defines this predicate
only for Point/Line, Point/Area, Line/Line, and Line/Area situations.
JTS / GEOS extends the definition to apply to Line/Point, Area/Point and
Area/Line situations as well. This makes the relation
symmetric.</para>
<important>
<para>Do not call with a <varname>GEOMETRYCOLLECTION</varname> as an argument</para>
</important>
<note>
<para>This function call will automatically include a bounding box
comparison that will make use of any indexes that are available on the
geometries.</para>
</note>
<para>&sfs_compliant; s2.1.13.3</para>
<para>&sqlmm_compliant; SQL-MM 3: 5.1.29</para>
</refsection>
<refsection>
<title>Examples</title>
<para>The following illustrations all return <varname>TRUE</varname>.</para>
<informaltable>
<tgroup cols="2">
<tbody>
<row>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_crosses01.png" />
</imageobject>
<caption><para><varname>MULTIPOINT</varname> / <varname>LINESTRING</varname></para></caption>
</mediaobject>
</informalfigure></para></entry>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_crosses02.png" />
</imageobject>
<caption><para><varname>MULTIPOINT</varname> / <varname>POLYGON</varname></para></caption>
</mediaobject>
</informalfigure></para></entry>
</row>
<row>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_crosses03.png" />
</imageobject>
<caption><para><varname>LINESTRING</varname> / <varname>POLYGON</varname></para></caption>
</mediaobject>
</informalfigure></para></entry>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_crosses04.png" />
</imageobject>
<caption><para><varname>LINESTRING</varname> / <varname>LINESTRING</varname></para></caption>
</mediaobject>
</informalfigure></para></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>Consider a situation where a user has two tables: a table of roads
and a table of highways.</para>
<informaltable>
<tgroup cols="2">
<tbody>
<row>
<entry><para> <informalexample>
<programlisting>CREATE TABLE roads (
id serial NOT NULL,
the_geom geometry,
CONSTRAINT roads_pkey PRIMARY KEY (road_id)
);</programlisting>
</informalexample> </para></entry>
<entry><para> <informalexample>
<programlisting>CREATE TABLE highways (
id serial NOT NULL,
the_gem geometry,
CONSTRAINT roads_pkey PRIMARY KEY (road_id)
);</programlisting>
</informalexample> </para></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>To determine a list of roads that cross a highway, use a query
similiar to:</para>
<para><informalexample>
<programlisting>SELECT roads.id
FROM roads, highways
WHERE ST_Crosses(roads.the_geom, highways.the_geom);</programlisting>
</informalexample></para>
</refsection>
</refentry>
<refentry id="ST_LineCrossingDirection">
<refnamediv>
<refname>ST_LineCrossingDirection</refname>
<refpurpose>Given 2 linestrings, returns a number between -3 and 3 denoting what kind of crossing behavior. 0 is no crossing.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>integer <function>ST_LineCrossingDirection</function></funcdef>
<paramdef><type>geometry </type> <parameter>linestringA</parameter></paramdef>
<paramdef><type>geometry </type> <parameter>linestringB</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Given 2 linestrings, returns a number between -3 and 3 denoting what kind of crossing behavior. 0 is no crossing. This is only supported for <varname>LINESTRING</varname></para>
<para>Definition of integer constants is as follows:
<itemizedlist>
<listitem>
<para> 0: LINE NO CROSS</para>
</listitem>
<listitem>
<para>-1: LINE CROSS LEFT</para>
</listitem>
<listitem>
<para> 1: LINE CROSS RIGHT</para>
</listitem>
<listitem>
<para>-2: LINE MULTICROSS END LEFT</para>
</listitem>
<listitem>
<para> 2: LINE MULTICROSS END RIGHT</para>
</listitem>
<listitem>
<para>-3: LINE MULTICROSS END SAME FIRST LEFT</para>
</listitem>
<listitem>
<para> 3: LINE MULTICROSS END SAME FIRST RIGHT</para>
</listitem>
</itemizedlist>
</para>
<para>Availability: 1.4</para>
<!-- optionally mention that this function uses indexes if appropriate -->
</refsection>
<refsection>
<title>Examples</title>
<!-- TODO: We really badly need diagrams here and more examples -->
<informaltable>
<tgroup cols="2">
<tbody>
<row>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_linecrossingdirection01.png" />
</imageobject>
<caption><para>Line 1 (green), Line 2 ball is start point,
triangle are end points. Query below. </para></caption>
</mediaobject>
</informalfigure>
<programlisting>
SELECT ST_LineCrossingDirection(foo.line1, foo.line2) As l1_cross_l2 ,
ST_LineCrossingDirection(foo.line2, foo.line1) As l2_cross_l1
FROM (
SELECT
ST_GeomFromText('LINESTRING(25 169,89 114,40 70,86 43)') As line1,
ST_GeomFromText('LINESTRING(171 154,20 140,71 74,161 53)') As line2
) As foo;
l1_cross_l2 | l2_cross_l1
-------------+-------------
3 | -3
</programlisting>
</para>
</entry>
<entry>
<para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_linecrossingdirection02.png" />
</imageobject>
<caption><para>Line 1 (green), Line 2 (blue) ball is start point,
triangle are end points. Query below.</para></caption>
</mediaobject>
</informalfigure>
<programlisting>
SELECT ST_LineCrossingDirection(foo.line1, foo.line2) As l1_cross_l2 ,
ST_LineCrossingDirection(foo.line2, foo.line1) As l2_cross_l1
FROM (
SELECT
ST_GeomFromText('LINESTRING(25 169,89 114,40 70,86 43)') As line1,
ST_GeomFromText('LINESTRING (171 154, 20 140, 71 74, 2.99 90.16)') As line2
) As foo;
l1_cross_l2 | l2_cross_l1
-------------+-------------
2 | -2
</programlisting>
</para>
</entry>
</row>
<row>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_linecrossingdirection03.png" />
</imageobject>
<caption><para>Line 1 (green), Line 2 (blue) ball is start point,
triangle are end points. Query below. </para></caption>
</mediaobject>
</informalfigure>
<programlisting>
SELECT
ST_LineCrossingDirection(foo.line1, foo.line2) As l1_cross_l2 ,
ST_LineCrossingDirection(foo.line2, foo.line1) As l2_cross_l1
FROM (
SELECT
ST_GeomFromText('LINESTRING(25 169,89 114,40 70,86 43)') As line1,
ST_GeomFromText('LINESTRING (20 140, 71 74, 161 53)') As line2
) As foo;
l1_cross_l2 | l2_cross_l1
-------------+-------------
-1 | 1
</programlisting>
</para>
</entry>
<entry>
<para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_linecrossingdirection04.png" />
</imageobject>
<caption><para>Line 1 (green), Line 2 (blue) ball is start point,
triangle are end points. Query below.</para></caption>
</mediaobject>
</informalfigure>
<programlisting>
SELECT ST_LineCrossingDirection(foo.line1, foo.line2) As l1_cross_l2 ,
ST_LineCrossingDirection(foo.line2, foo.line1) As l2_cross_l1
FROM (SELECT
ST_GeomFromText('LINESTRING(25 169,89 114,40 70,86 43)') As line1,
ST_GeomFromText('LINESTRING(2.99 90.16,71 74,20 140,171 154)') As line2
) As foo;
l1_cross_l2 | l2_cross_l1
-------------+-------------
-2 | 2
</programlisting>
</para>
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<programlisting>
SELECT s1.gid, s2.gid, ST_LineCrossingDirection(s1.the_geom, s2.the_geom)
FROM streets s1 CROSS JOIN streets s2 ON (s1.gid != s2.gid AND s1.the_geom && s2.the_geom )
WHERE ST_CrossingDirection(s1.the_geom, s2.the_geom) > 0;
</programlisting>
</refsection>
<!-- Optionally add a "See Also" section -->
<refsection>
<title>See Also</title>
<para><xref linkend="ST_Crosses" /></para>
</refsection>
</refentry>
<refentry id="ST_Disjoint">
<refnamediv>
<refname>ST_Disjoint</refname>
<refpurpose>Returns TRUE if the Geometries do not "spatially
intersect" - if they do not share any space together.
</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>boolean <function>ST_Disjoint</function></funcdef>
<paramdef>
<type>geometry</type>
<parameter>A</parameter>
</paramdef>
<paramdef>
<type>geometry</type>
<parameter>B</parameter>
</paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Overlaps, Touches, Within all imply geometries are not spatially disjoint. If any of the aforementioned
returns true, then the geometries are not spatially disjoint.
Disjoint implies false for spatial intersection.</para>
<important>
<para>Do not call with a <varname>GEOMETRYCOLLECTION</varname> as an argument</para>
</important>
<para>Performed by the GEOS module</para>
<note>
<para>This function call does not use indexes</para>
</note>
<note>
<para>NOTE: this is the "allowable" version that returns a
boolean, not an integer.</para>
</note>
<para>&sfs_compliant; s2.1.1.2 //s2.1.13.3
- a.Relate(b, 'FF*FF****')</para>
<para>&sqlmm_compliant; SQL-MM 3: 5.1.26</para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting>SELECT ST_Disjoint('POINT(0 0)'::geometry, 'LINESTRING ( 2 0, 0 2 )'::geometry);
st_disjoint
---------------
t
(1 row)
SELECT ST_Disjoint('POINT(0 0)'::geometry, 'LINESTRING ( 0 0, 0 2 )'::geometry);
st_disjoint
---------------
f
(1 row)
</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_Intersects"/>ST_Intersects</para>
</refsection>
</refentry>
<refentry id="ST_Distance">
<refnamediv>
<refname>ST_Distance</refname>
<refpurpose>For geometry type Returns the 2D Cartesian distance between two geometries in
projected units (based on spatial ref). For geography type defaults to return minimum geodesic distance between two geographies in meters.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>float <function>ST_Distance</function></funcdef>
<paramdef><type>geometry </type>
<parameter>g1</parameter></paramdef>
<paramdef><type>geometry </type>
<parameter>g2</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>float <function>ST_Distance</function></funcdef>
<paramdef><type>geography </type>
<parameter>gg1</parameter></paramdef>
<paramdef><type>geography </type>
<parameter>gg2</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>float <function>ST_Distance</function></funcdef>
<paramdef><type>geography </type>
<parameter>gg1</parameter></paramdef>
<paramdef><type>geography </type>
<parameter>gg2</parameter></paramdef>
<paramdef><type>boolean </type>
<parameter>use_spheroid</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>For geometry type returns the minimum 2D Cartesian distance between two geometries in
projected units (spatial ref units). For geography type defaults to return the minimum geodesic distance between two geographies in meters. If use_spheroid is
false, a faster sphere calculation is used instead of a spheroid.</para>
<para>&sfs_compliant;</para>
<para>&sqlmm_compliant; SQL-MM 3: 5.1.23</para>
<para>&curve_support;</para>
<para>&sfcgal_enhanced;</para>
<para>Availability: 1.5.0 geography support was introduced in 1.5. Speed improvements for planar to better handle large or many vertex geometries</para>
<para>Enhanced: 2.1.0 improved speed for geography. See <ulink url="http://boundlessgeo.com/2012/07/making-geography-faster/">Making Geography faster</ulink> for details.</para>
<para>Enhanced: 2.1.0 - support for curved geometries was introduced.</para>
<para>Enhanced: 2.2.0 - measurement on spheroid performed with GeographicLib for improved accuracy and robustness. Requires Proj >= 4.9.0 to take advantage of the new feature.</para>
</refsection>
<refsection>
<title>Basic Geometry Examples</title>
<programlisting>
--Geometry example - units in planar degrees 4326 is WGS 84 long lat unit=degrees
SELECT ST_Distance(
ST_GeomFromText('POINT(-72.1235 42.3521)',4326),
ST_GeomFromText('LINESTRING(-72.1260 42.45, -72.123 42.1546)', 4326)
);
st_distance
-----------------
0.00150567726382282
-- Geometry example - units in meters (SRID: 26986 Massachusetts state plane meters) (most accurate for Massachusetts)
SELECT ST_Distance(
ST_Transform(ST_GeomFromText('POINT(-72.1235 42.3521)',4326),26986),
ST_Transform(ST_GeomFromText('LINESTRING(-72.1260 42.45, -72.123 42.1546)', 4326),26986)
);
st_distance
-----------------
123.797937878454
-- Geometry example - units in meters (SRID: 2163 US National Atlas Equal area) (least accurate)
SELECT ST_Distance(
ST_Transform(ST_GeomFromText('POINT(-72.1235 42.3521)',4326),2163),
ST_Transform(ST_GeomFromText('LINESTRING(-72.1260 42.45, -72.123 42.1546)', 4326),2163)
);
st_distance
------------------
126.664256056812
</programlisting>
</refsection>
<refsection>
<title>Geography Examples</title>
<programlisting>-- same as geometry example but note units in meters - use sphere for slightly faster less accurate
SELECT ST_Distance(gg1, gg2) As spheroid_dist, ST_Distance(gg1, gg2, false) As sphere_dist
FROM (SELECT
ST_GeogFromText('SRID=4326;POINT(-72.1235 42.3521)') As gg1,
ST_GeogFromText('SRID=4326;LINESTRING(-72.1260 42.45, -72.123 42.1546)') As gg2
) As foo ;
spheroid_dist | sphere_dist
------------------+------------------
123.802076746848 | 123.475736916397
</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_3DDistance"/>, <xref linkend="ST_DWithin"/>, <xref linkend="ST_DistanceSphere"/>, <xref linkend="ST_Distance_Spheroid"/>, <xref linkend="ST_MaxDistance" />, <xref linkend="ST_Transform" /></para>
</refsection>
</refentry>
<refentry id="ST_MinimumClearance">
<refnamediv>
<refname>ST_MinimumClearance</refname>
<refpurpose>Returns the minimum clearance of a geometry, a measure of a geometry's robustness.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>float <function>ST_MinimumClearance</function></funcdef>
<paramdef><type>geometry </type><parameter>g</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>
It is not uncommon to have a geometry that, while meeting the criteria for validity according to ST_IsValid (polygons)
or ST_IsSimple (lines), would become invalid if one of the vertices moved by a slight distance, as can happen during
conversion to text-based formats (such as WKT, KML, GML GeoJSON), or binary formats that do not use double-precision
floating point coordinates (MapInfo TAB).
</para>
<para>
A geometry's "minimum clearance" is the smallest distance by which a vertex of the geometry could be moved to produce
an invalid geometry. It can be thought of as a quantitative measure of a geometry's robustness, where increasing values
of minimum clearance indicate increasing robustness.
</para>
<para>
If a geometry has a minimum clearance of <varname>e</varname>, it can be said that:
<itemizedlist>
<listitem>
<para>
No two distinct vertices in the geometry are separated by less than <varname>e</varname>.
</para>
</listitem>
<listitem>
<para>
No vertex is closer than <varname>e</varname> to a line segement of which it is not an endpoint.
</para>
</listitem>
</itemizedlist>
</para>
<para>
If no minimum clearance exists for a geometry (for example, a single point, or a multipoint whose points are identical), then
ST_MinimumClearance will return Infinity.
</para>
<para>Availability: 2.3.0 - requires GEOS >= 3.6.0</para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting>
SELECT ST_MinimumClearance('POLYGON ((0 0, 1 0, 1 1, 0.5 3.2e-4, 0 0))');
st_minimumclearance
---------------------
0.00032
</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para>
<xref linkend="ST_MinimumClearanceLine" />
</para>
</refsection>
</refentry>
<refentry id="ST_MinimumClearanceLine">
<refnamediv>
<refname>ST_MinimumClearanceLine</refname>
<refpurpose>Returns the two-point LineString spanning a geometry's minimum clearance.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>Geometry <function>ST_MinimumClearanceLine</function></funcdef>
<paramdef><type>geometry </type>
<parameter>g</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>
Returns the two-point LineString spanning a geometry's minimum clearance. If the geometry does not have a minimum
clearance, <varname>LINESTRING EMPTY</varname> will be returned.
</para>
<para>Availability: 2.3.0 - requires GEOS >= 3.6.0</para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting>
SELECT ST_AsText(ST_MinimumClearanceLine('POLYGON ((0 0, 1 0, 1 1, 0.5 3.2e-4, 0 0))'));
st_astext
-------------------------------
LINESTRING(0.5 0.00032,0.5 0)
</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para>
<xref linkend="ST_MinimumClearance" />
</para>
</refsection>
</refentry>
<refentry id="ST_HausdorffDistance">
<refnamediv>
<refname>ST_HausdorffDistance</refname>
<refpurpose>Returns the Hausdorff distance between two geometries. Basically a measure of how similar or dissimilar 2 geometries are. Units are in the units of the spatial
reference system of the geometries.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>float <function>ST_HausdorffDistance</function></funcdef>
<paramdef><type>geometry </type>
<parameter>g1</parameter></paramdef>
<paramdef><type>geometry </type>
<parameter>g2</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>float <function>ST_HausdorffDistance</function></funcdef>
<paramdef><type>geometry </type>
<parameter>g1</parameter></paramdef>
<paramdef><type>geometry </type>
<parameter>g2</parameter></paramdef>
<paramdef><type>float</type>
<parameter>densifyFrac</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Implements algorithm for computing a distance metric which can be thought of as the "Discrete Hausdorff Distance".
This is the Hausdorff distance restricted to discrete points for one of the geometries. <ulink url="http://en.wikipedia.org/wiki/Hausdorff_distance">Wikipedia article on Hausdorff distance</ulink>
<ulink url="http://lin-ear-th-inking.blogspot.com/2009/01/computing-geometric-similarity.html">Martin Davis note on how Hausdorff Distance calculation was used to prove correctness of the CascadePolygonUnion approach.</ulink></para>
<para>
When densifyFrac is specified, this function performs a segment densification before computing the discrete hausdorff distance. The densifyFrac parameter sets the fraction by which to densify each segment. Each segment will be split into a number of equal-length subsegments, whose fraction of the total length is closest to the given fraction.
</para>
<note>
<para>
The current implementation supports only vertices as the discrete locations. This could be extended to allow an arbitrary density of points to be used.
</para>
</note>
<note>
<para>
This algorithm is NOT equivalent to the standard Hausdorff distance. However, it computes an approximation that is correct for a large subset of useful cases.
One important part of this subset is Linestrings that are roughly parallel to each other, and roughly equal in length. This is a useful metric for line matching.
</para>
</note>
<para>Availability: 1.5.0 - requires GEOS >= 3.2.0</para>
</refsection>
<refsection>
<title>Examples</title>
<para>For each building, find the parcel that best represents it. First we require the parcel intersect with the geometry.
DISTINCT ON guarantees we get each building listed only once, the ORDER BY .. ST_HausdorffDistance gives us a preference of parcel that is most similar to the building.</para>
<programlisting>SELECT DISTINCT ON(buildings.gid) buildings.gid, parcels.parcel_id
FROM buildings INNER JOIN parcels ON ST_Intersects(buildings.geom,parcels.geom)
ORDER BY buildings.gid, ST_HausdorffDistance(buildings.geom, parcels.geom);</programlisting>
<programlisting>postgis=# SELECT ST_HausdorffDistance(
'LINESTRING (0 0, 2 0)'::geometry,
'MULTIPOINT (0 1, 1 0, 2 1)'::geometry);
st_hausdorffdistance
----------------------
1
(1 row)
</programlisting>
<programlisting>postgis=# SELECT st_hausdorffdistance('LINESTRING (130 0, 0 0, 0 150)'::geometry, 'LINESTRING (10 10, 10 150, 130 10)'::geometry, 0.5);
st_hausdorffdistance
----------------------
70
(1 row)
</programlisting>
</refsection>
</refentry>
<refentry id="ST_MaxDistance">
<refnamediv>
<refname>ST_MaxDistance</refname>
<refpurpose>Returns the 2-dimensional largest distance between two geometries in
projected units.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>float <function>ST_MaxDistance</function></funcdef>
<paramdef><type>geometry </type> <parameter>g1</parameter></paramdef>
<paramdef><type>geometry </type> <parameter>g2</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<!-- optionally mention that this function uses indexes if appropriate -->
<note>
<para>Returns the 2-dimensional maximum distance between two geometries in
projected units. If g1 and g2 is the same geometry the function will return the distance between
the two vertices most far from each other in that geometry.</para>
</note>
<para>Availability: 1.5.0</para>
</refsection>
<refsection>
<title>Examples</title>
<para>Basic furthest distance the point is to any part of the line</para>
<programlisting>postgis=# SELECT ST_MaxDistance('POINT(0 0)'::geometry, 'LINESTRING ( 2 0, 0 2 )'::geometry);
st_maxdistance
-----------------
2
(1 row)
postgis=# SELECT ST_MaxDistance('POINT(0 0)'::geometry, 'LINESTRING ( 2 2, 2 2 )'::geometry);
st_maxdistance
------------------
2.82842712474619
(1 row)</programlisting>
</refsection>
<!-- Optionally add a "See Also" section -->
<refsection>
<title>See Also</title>
<para><xref linkend="ST_Distance"/>, <xref linkend="ST_LongestLine"/>, <xref linkend="ST_DFullyWithin" /></para>
</refsection>
</refentry>
<refentry id="ST_DistanceSphere">
<refnamediv>
<refname>ST_DistanceSphere</refname>
<refpurpose>Returns minimum distance in meters between two lon/lat
geometries. Uses a spherical earth and radius derived from the spheroid
defined by the SRID.
Faster than ST_DistanceSpheroid <xref linkend="ST_Distance_Spheroid" />, but less
accurate. PostGIS versions prior to 1.5 only implemented for points.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>float <function>ST_DistanceSphere</function></funcdef>
<paramdef><type>geometry </type> <parameter>geomlonlatA</parameter></paramdef>
<paramdef><type>geometry </type> <parameter>geomlonlatB</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns minimum distance in meters between two lon/lat
points. Uses a spherical earth and radius derived from the spheroid
defined by the SRID.
Faster than <xref linkend="ST_Distance_Spheroid"/>, but less
accurate. PostGIS Versions prior to 1.5 only implemented for points.</para>
<para>Availability: 1.5 - support for other geometry types besides points was introduced. Prior versions only work with points.</para>
<para>Changed: 2.2.0 In prior versions this used to be called ST_Distance_Sphere</para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting>SELECT round(CAST(ST_DistanceSphere(ST_Centroid(the_geom), ST_GeomFromText('POINT(-118 38)',4326)) As numeric),2) As dist_meters,
round(CAST(ST_Distance(ST_Transform(ST_Centroid(the_geom),32611),
ST_Transform(ST_GeomFromText('POINT(-118 38)', 4326),32611)) As numeric),2) As dist_utm11_meters,
round(CAST(ST_Distance(ST_Centroid(the_geom), ST_GeomFromText('POINT(-118 38)', 4326)) As numeric),5) As dist_degrees,
round(CAST(ST_Distance(ST_Transform(the_geom,32611),
ST_Transform(ST_GeomFromText('POINT(-118 38)', 4326),32611)) As numeric),2) As min_dist_line_point_meters
FROM
(SELECT ST_GeomFromText('LINESTRING(-118.584 38.374,-118.583 38.5)', 4326) As the_geom) as foo;
dist_meters | dist_utm11_meters | dist_degrees | min_dist_line_point_meters
-------------+-------------------+--------------+----------------------------
70424.47 | 70438.00 | 0.72900 | 65871.18
</programlisting>
</refsection>
<!-- Optionally add a "See Also" section -->
<refsection>
<title>See Also</title>
<para><xref linkend="ST_Distance" />, <xref linkend="ST_Distance_Spheroid" /></para>
</refsection>
</refentry>
<refentry id="ST_Distance_Spheroid">
<refnamediv>
<refname>ST_DistanceSpheroid</refname>
<refpurpose>Returns the minimum distance between two lon/lat geometries given a
particular spheroid.
PostGIS versions prior to 1.5 only support points.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>float <function>ST_DistanceSpheroid</function></funcdef>
<paramdef><type>geometry </type> <parameter>geomlonlatA</parameter></paramdef>
<paramdef><type>geometry </type> <parameter>geomlonlatB</parameter></paramdef>
<paramdef><type>spheroid </type> <parameter>measurement_spheroid</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns minimum distance in meters between two lon/lat
geometries given a particular spheroid. See the explanation of spheroids given for
<xref linkend="ST_Length_Spheroid" />. PostGIS version prior to 1.5 only support points.</para>
<note>
<para>This function currently does not look at the SRID of a geometry and will always assume its represented in the coordinates of the passed in spheroid. Prior versions of this function only support points.</para>
</note>
<para>Availability: 1.5 - support for other geometry types besides points was introduced. Prior versions only work with points.</para>
<para>Changed: 2.2.0 In prior versions this used to be called ST_Distance_Spheroid</para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting>SELECT round(CAST(
ST_DistanceSpheroid(ST_Centroid(the_geom), ST_GeomFromText('POINT(-118 38)',4326), 'SPHEROID["WGS 84",6378137,298.257223563]')
As numeric),2) As dist_meters_spheroid,
round(CAST(ST_DistanceSphere(ST_Centroid(the_geom), ST_GeomFromText('POINT(-118 38)',4326)) As numeric),2) As dist_meters_sphere,
round(CAST(ST_Distance(ST_Transform(ST_Centroid(the_geom),32611),
ST_Transform(ST_GeomFromText('POINT(-118 38)', 4326),32611)) As numeric),2) As dist_utm11_meters
FROM
(SELECT ST_GeomFromText('LINESTRING(-118.584 38.374,-118.583 38.5)', 4326) As the_geom) as foo;
dist_meters_spheroid | dist_meters_sphere | dist_utm11_meters
----------------------+--------------------+-------------------
70454.92 | 70424.47 | 70438.00
</programlisting>
</refsection>
<!-- Optionally add a "See Also" section -->
<refsection>
<title>See Also</title>
<para><xref linkend="ST_Distance" />, <xref linkend="ST_DistanceSphere" /></para>
</refsection>
</refentry>
<refentry id="ST_DFullyWithin">
<refnamediv>
<refname>ST_DFullyWithin</refname>
<refpurpose>Returns true if all of the geometries are within the specified
distance of one another</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>boolean <function>ST_DFullyWithin</function></funcdef>
<paramdef><type>geometry </type>
<parameter>g1</parameter></paramdef>
<paramdef><type>geometry </type>
<parameter>g2</parameter></paramdef>
<paramdef><type>double precision </type>
<parameter>distance</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns true if the geometries is fully within the specified distance
of one another. The distance is specified in units defined by the
spatial reference system of the geometries. For this function to make
sense, the source geometries must both be of the same coordinate projection,
having the same SRID.</para>
<note>
<para>This function call will automatically include a bounding box
comparison that will make use of any indexes that are available on
the geometries.</para>
</note>
<para>Availability: 1.5.0</para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting>postgis=# SELECT ST_DFullyWithin(geom_a, geom_b, 10) as DFullyWithin10, ST_DWithin(geom_a, geom_b, 10) as DWithin10, ST_DFullyWithin(geom_a, geom_b, 20) as DFullyWithin20 from
(select ST_GeomFromText('POINT(1 1)') as geom_a,ST_GeomFromText('LINESTRING(1 5, 2 7, 1 9, 14 12)') as geom_b) t1;
-----------------
DFullyWithin10 | DWithin10 | DFullyWithin20 |
---------------+----------+---------------+
f | t | t | </programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_MaxDistance"/>, <xref linkend="ST_DWithin"/></para>
</refsection>
</refentry>
<refentry id="ST_DWithin">
<refnamediv>
<refname>ST_DWithin</refname>
<refpurpose>Returns true if the geometries are within the specified
distance of one another. For geometry units are in those of spatial reference and For geography units are in meters and measurement is
defaulted to use_spheroid=true (measure around spheroid), for faster check, use_spheroid=false to measure along sphere.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>boolean <function>ST_DWithin</function></funcdef>
<paramdef><type>geometry </type>
<parameter>g1</parameter></paramdef>
<paramdef><type>geometry </type>
<parameter>g2</parameter></paramdef>
<paramdef><type>double precision </type>
<parameter>distance_of_srid</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>boolean <function>ST_DWithin</function></funcdef>
<paramdef><type>geography </type>
<parameter>gg1</parameter></paramdef>
<paramdef><type>geography </type>
<parameter>gg2</parameter></paramdef>
<paramdef><type>double precision </type>
<parameter>distance_meters</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>boolean <function>ST_DWithin</function></funcdef>
<paramdef><type>geography </type>
<parameter>gg1</parameter></paramdef>
<paramdef><type>geography </type>
<parameter>gg2</parameter></paramdef>
<paramdef><type>double precision </type>
<parameter>distance_meters</parameter></paramdef>
<paramdef><type>boolean </type>
<parameter>use_spheroid</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns true if the geometries are within the specified distance
of one another.</para>
<para>For Geometries: The distance is specified in units defined by the
spatial reference system of the geometries. For this function to make
sense, the source geometries must both be of the same coordinate projection,
having the same SRID.</para>
<para>For geography units are in meters and measurement is
defaulted to use_spheroid=true, for faster check, use_spheroid=false to measure along sphere.
</para>
<note>
<para>This function call will automatically include a bounding box
comparison that will make use of any indexes that are available on
the geometries.</para>
</note>
<note>
<para>Prior to 1.3, ST_Expand was commonly used in conjunction with && and ST_Distance to
achieve the same effect and in pre-1.3.4 this function was basically short-hand for that construct.
From 1.3.4, ST_DWithin uses a more short-circuit distance function which should make it more efficient
than prior versions for larger buffer regions.</para>
</note>
<note><para>Use ST_3DDWithin if you have 3D geometries.</para></note>
<para>&sfs_compliant;</para>
<para>Availability: 1.5.0 support for geography was introduced</para>
<para>Enhanced: 2.1.0 improved speed for geography. See <ulink url="http://blog.opengeo.org/2012/07/12/making-geography-faster/">Making Geography faster</ulink> for details.</para>
<para>Enhanced: 2.1.0 support for curved geometries was introduced.</para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting>
--Find the nearest hospital to each school
--that is within 3000 units of the school.
-- We do an ST_DWithin search to utilize indexes to limit our search list
-- that the non-indexable ST_Distance needs to process
--If the units of the spatial reference is meters then units would be meters
SELECT DISTINCT ON (s.gid) s.gid, s.school_name, s.the_geom, h.hospital_name
FROM schools s
LEFT JOIN hospitals h ON ST_DWithin(s.the_geom, h.the_geom, 3000)
ORDER BY s.gid, ST_Distance(s.the_geom, h.the_geom);
--The schools with no close hospitals
--Find all schools with no hospital within 3000 units
--away from the school. Units is in units of spatial ref (e.g. meters, feet, degrees)
SELECT s.gid, s.school_name
FROM schools s
LEFT JOIN hospitals h ON ST_DWithin(s.the_geom, h.the_geom, 3000)
WHERE h.gid IS NULL;
</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_Distance"/>, <xref linkend="ST_Expand"/></para>
</refsection>
</refentry>
<refentry id="ST_Equals">
<refnamediv>
<refname>ST_Equals</refname>
<refpurpose>Returns true if the given geometries represent the same geometry. Directionality
is ignored.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>boolean <function>ST_Equals</function></funcdef>
<paramdef><type>geometry </type> <parameter>A</parameter></paramdef>
<paramdef><type>geometry </type> <parameter>B</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns TRUE if the given Geometries are "spatially
equal". Use this for a 'better' answer than '='.
Note by spatially equal we mean ST_Within(A,B) = true and ST_Within(B,A) = true and
also mean ordering of points can be different but
represent the same geometry structure. To verify the order of points is consistent, use
ST_OrderingEquals (it must be noted ST_OrderingEquals is a little more stringent than simply verifying order of
points are the same).</para>
<important>
<para>This function will return false if either geometry is invalid except in the case where they are binary equal.</para>
</important>
<para>&sfs_compliant; s2.1.1.2</para>
<para>&sqlmm_compliant; SQL-MM 3: 5.1.24</para>
<para>Changed: 2.2.0 Returns true even for invalid geometries if they are binary equal</para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting>SELECT ST_Equals(ST_GeomFromText('LINESTRING(0 0, 10 10)'),
ST_GeomFromText('LINESTRING(0 0, 5 5, 10 10)'));
st_equals
-----------
t
(1 row)
SELECT ST_Equals(ST_Reverse(ST_GeomFromText('LINESTRING(0 0, 10 10)')),
ST_GeomFromText('LINESTRING(0 0, 5 5, 10 10)'));
st_equals
-----------
t
(1 row)
</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_IsValid"/>, <xref linkend="ST_OrderingEquals"/>, <xref linkend="ST_Reverse"/>, <xref linkend="ST_Within" /></para>
</refsection>
</refentry>
<refentry id="ST_GeometricMedian">
<refnamediv>
<refname>
ST_GeometricMedian
</refname>
<refpurpose>
Returns the geometric median of a MultiPoint.
</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>geometry
<function>
ST_GeometricMedian
</function>
</funcdef>
<paramdef>
<type>
geometry
</type>
<parameter>
g
</parameter>
</paramdef>
<paramdef>
<type>
float8
</type>
<parameter>
tolerance
</parameter>
</paramdef>
<paramdef>
<type>
int
</type>
<parameter>
max_iter
</parameter>
</paramdef>
<paramdef>
<type>
boolean
</type>
<parameter>
fail_if_not_converged
</parameter>
</paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>
Computes the approximate geometric median of a MultiPoint geometry
using the Weiszfeld algorithm. The geometric median provides a
centrality measure that is less sensitive to outlier points than
the centroid.
The algorithm will iterate until the distance change between
successive iterations is less than the supplied <varname>tolerance</varname>
parameter. If this condition has not been met after <varname>max_iterations</varname>
iterations, the function will produce an error and exit, unless <varname>fail_if_not_converged</varname>
is set to false.
If a tolerance value is not provided, a default tolerance value
will be calculated based on the extent of the input geometry.
</para>
<para>Availability: 2.3.0</para>
<para>&Z_support;</para>
</refsection>
<refsection>
<title>Examples</title>
<para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_geometricmedian01.png" />
</imageobject>
<caption>
<para>
Comparison of the centroid (turquoise point) and geometric
median (red point) of a four-point MultiPoint (yellow points).
</para>
</caption>
</mediaobject>
</informalfigure>
</para>
<programlisting>
WITH test AS (
SELECT 'MULTIPOINT((0 0), (1 1), (2 2), (200 200))'::geometry geom)
SELECT
ST_AsText(ST_Centroid(geom)) centroid,
ST_AsText(ST_GeometricMedian(geom)) median
FROM test;
centroid | median
--------------------+----------------------------------------
POINT(50.75 50.75) | POINT(1.9761550281255 1.9761550281255)
(1 row)
</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_Centroid"/></para>
</refsection>
</refentry>
<refentry id="ST_HasArc">
<refnamediv>
<refname>ST_HasArc</refname>
<refpurpose>Returns true if a geometry or geometry collection contains a circular string</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>boolean <function>ST_HasArc</function></funcdef>
<paramdef><type>geometry </type> <parameter>geomA</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns true if a geometry or geometry collection contains a circular string</para>
<para>Availability: 1.2.3?</para>
<para>&Z_support;</para>
<para>&curve_support;</para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting>SELECT ST_HasArc(ST_Collect('LINESTRING(1 2, 3 4, 5 6)', 'CIRCULARSTRING(1 1, 2 3, 4 5, 6 7, 5 6)'));
st_hasarc
--------
t
</programlisting>
</refsection>
<!-- Optionally add a "See Also" section -->
<refsection>
<title>See Also</title>
<para><xref linkend="ST_CurveToLine" />, <xref linkend="ST_LineToCurve" /></para>
</refsection>
</refentry>
<refentry id="ST_Intersects">
<refnamediv>
<refname>ST_Intersects</refname>
<refpurpose>Returns TRUE if the Geometries/Geography "spatially
intersect in 2D" - (share any portion of space) and FALSE if they don't (they are Disjoint).
For geography -- tolerance is 0.00001 meters (so any points that close are considered to intersect)
</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>boolean <function>ST_Intersects</function></funcdef>
<paramdef>
<type>geometry</type>
<parameter>geomA</parameter>
</paramdef>
<paramdef>
<type>geometry</type>
<parameter>geomB</parameter>
</paramdef>
</funcprototype>
<funcprototype>
<funcdef>boolean <function>ST_Intersects</function></funcdef>
<paramdef>
<type>geography</type>
<parameter>geogA</parameter>
</paramdef>
<paramdef>
<type>geography</type>
<parameter>geogB</parameter>
</paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>If a geometry or geography shares any portion of space then they intersect.
For geography -- tolerance is 0.00001 meters (so any points that are close are considered to intersect)</para>
<para>Overlaps, Touches, Within all imply spatial intersection. If any of the aforementioned
returns true, then the geometries also spatially intersect.
Disjoint implies false for spatial intersection.</para>
<important>
<para>Do not call with a <varname>GEOMETRYCOLLECTION</varname> as an argument for geometry version. The geography
version supports GEOMETRYCOLLECTION since its a thin wrapper around distance implementation.</para>
</important>
<para>Enhanced: 2.3.0 Enhancement to PIP short-circuit extended to support MultiPoints with few points. Prior versions only supported point in polygon.</para>
<para>Performed by the GEOS module (for geometry), geography is native</para>
<para>Availability: 1.5 support for geography was introduced.</para>
<note>
<para>This function call will automatically include a bounding box
comparison that will make use of any indexes that are available on the
geometries.</para>
</note>
<note>
<para>For geography, this function has a distance tolerance of about 0.00001 meters and uses the sphere rather
than spheroid calculation.</para>
</note>
<note>
<para>NOTE: this is the "allowable" version that returns a
boolean, not an integer.</para>
</note>
<para>&sfs_compliant; s2.1.1.2 //s2.1.13.3
- ST_Intersects(g1, g2 ) --> Not (ST_Disjoint(g1, g2 ))
</para>
<para>&sqlmm_compliant; SQL-MM 3: 5.1.27</para>
<para>&sfcgal_enhanced;</para>
</refsection>
<refsection>
<title>Geometry Examples</title>
<programlisting>SELECT ST_Intersects('POINT(0 0)'::geometry, 'LINESTRING ( 2 0, 0 2 )'::geometry);
st_intersects
---------------
f
(1 row)
SELECT ST_Intersects('POINT(0 0)'::geometry, 'LINESTRING ( 0 0, 0 2 )'::geometry);
st_intersects
---------------
t
(1 row)
</programlisting>
</refsection>
<refsection>
<title>Geography Examples</title>
<programlisting>SELECT ST_Intersects(
ST_GeographyFromText('SRID=4326;LINESTRING(-43.23456 72.4567,-43.23456 72.4568)'),
ST_GeographyFromText('SRID=4326;POINT(-43.23456 72.4567772)')
);
st_intersects
---------------
t
</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para> <xref linkend="ST_3DIntersects" />, <xref linkend="ST_Disjoint"/></para>
</refsection>
</refentry>
<refentry id="ST_Length">
<refnamediv>
<refname>ST_Length</refname>
<refpurpose>Returns the 2D length of the geometry if it is a LineString or MultiLineString. geometry are in units of spatial reference and geography are in meters (default spheroid)</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>float <function>ST_Length</function></funcdef>
<paramdef><type>geometry </type><parameter>a_2dlinestring</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>float <function>ST_Length</function></funcdef>
<paramdef><type>geography </type><parameter>geog</parameter></paramdef>
<paramdef choice="opt"><type>boolean </type><parameter>use_spheroid=true</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>For geometry: Returns the 2D Cartesian length of the geometry if it is a LineString, MultiLineString, ST_Curve, ST_MultiCurve. 0 is returned for
areal geometries. For areal geometries use <xref linkend="ST_Perimeter" />. For geometry types, units for length measures are specified by the
spatial reference system of the geometry.</para>
<para>For geography types, the calculations are performed using the inverse geodesic problem, where length units are in meters.
If PostGIS is compiled with PROJ version 4.8.0 or later, the spheroid is specified by the SRID, otherwise it is exclusive to WGS84.
If <varname>use_spheroid=false</varname>, then calculations will approximate a sphere instead of a spheroid.</para>
<para>Currently for geometry this is an alias for ST_Length2D, but this may change to support higher dimensions.</para>
<warning><para>Changed: 2.0.0 Breaking change -- in prior versions applying this to a MULTI/POLYGON of type geography would give you the perimeter of the POLYGON/MULTIPOLYGON. In 2.0.0
this was changed to return 0 to be in line with geometry behavior. Please use ST_Perimeter if you want the perimeter of a polygon</para></warning>
<note><para>For geography measurement defaults spheroid measurement. To use the faster less accurate sphere use ST_Length(gg,false);</para></note>
<para>&sfs_compliant; s2.1.5.1</para>
<para>&sqlmm_compliant; SQL-MM 3: 7.1.2, 9.3.4</para>
<para>Availability: 1.5.0 geography support was introduced in 1.5.</para>
<para>&sfcgal_enhanced;</para>
</refsection>
<refsection>
<title>Geometry Examples</title>
<para>Return length in feet for line string. Note this is in feet because EPSG:2249 is
Massachusetts State Plane Feet</para>
<programlisting>
SELECT ST_Length(ST_GeomFromText('LINESTRING(743238 2967416,743238 2967450,743265 2967450,
743265.625 2967416,743238 2967416)',2249));
st_length
---------
122.630744000095
--Transforming WGS 84 LineString to Massachusetts state plane meters
SELECT ST_Length(
ST_Transform(
ST_GeomFromEWKT('SRID=4326;LINESTRING(-72.1260 42.45, -72.1240 42.45666, -72.123 42.1546)'),
26986
)
);
st_length
---------
34309.4563576191
</programlisting>
</refsection>
<refsection>
<title>Geography Examples</title>
<para>Return length of WGS 84 geography line</para>
<programlisting>
-- default calculation is using a sphere rather than spheroid
SELECT ST_Length(the_geog) As length_spheroid, ST_Length(the_geog,false) As length_sphere
FROM (SELECT ST_GeographyFromText(
'SRID=4326;LINESTRING(-72.1260 42.45, -72.1240 42.45666, -72.123 42.1546)') As the_geog)
As foo;
length_spheroid | length_sphere
------------------+------------------
34310.5703627288 | 34346.2060960742
</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_GeographyFromText" />, <xref linkend="ST_GeomFromEWKT" />, <xref linkend="ST_Length_Spheroid" />, <xref linkend="ST_Perimeter" />, <xref linkend="ST_Transform" /></para>
</refsection>
</refentry>
<refentry id="ST_Length2D">
<refnamediv>
<refname>ST_Length2D</refname>
<refpurpose>Returns the 2-dimensional length of the geometry if it is a
linestring or multi-linestring. This is an alias for <varname>ST_Length</varname></refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>float <function>ST_Length2D</function></funcdef>
<paramdef><type>geometry </type> <parameter>a_2dlinestring</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns the 2-dimensional length of the geometry if it is a
linestring or multi-linestring. This is an alias for <varname>ST_Length</varname></para>
</refsection>
<!-- Optionally add a "See Also" section -->
<refsection>
<title>See Also</title>
<para><xref linkend="ST_Length" />, <xref linkend="ST_3DLength" /></para>
</refsection>
</refentry>
<refentry id="ST_3DLength">
<refnamediv>
<refname>ST_3DLength</refname>
<refpurpose>Returns the 3-dimensional or 2-dimensional length of the geometry if it is a
linestring or multi-linestring. </refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>float <function>ST_3DLength</function></funcdef>
<paramdef><type>geometry </type> <parameter>a_3dlinestring</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns the 3-dimensional or 2-dimensional length of the geometry if it is a
linestring or multi-linestring. For 2-d lines it will just return the 2-d length (same as ST_Length and ST_Length2D)</para>
<para>&Z_support;</para>
<para>Changed: 2.0.0 In prior versions this used to be called ST_Length3D</para>
</refsection>
<refsection>
<title>Examples</title>
<para>Return length in feet for a 3D cable. Note this is in feet because EPSG:2249 is
Massachusetts State Plane Feet</para>
<programlisting>
SELECT ST_3DLength(ST_GeomFromText('LINESTRING(743238 2967416 1,743238 2967450 1,743265 2967450 3,
743265.625 2967416 3,743238 2967416 3)',2249));
ST_3DLength
-----------
122.704716741457
</programlisting>
</refsection>
<!-- Optionally add a "See Also" section -->
<refsection>
<title>See Also</title>
<para><xref linkend="ST_Length" />, <xref linkend="ST_Length2D" /></para>
</refsection>
</refentry>
<refentry id="ST_Length_Spheroid">
<refnamediv>
<refname>ST_LengthSpheroid</refname>
<refpurpose>Calculates the 2D or 3D length/perimeter of a geometry on an ellipsoid. This
is useful if the coordinates of the geometry are in
longitude/latitude and a length is desired without reprojection.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>float <function>ST_LengthSpheroid</function></funcdef>
<paramdef><type>geometry </type> <parameter>a_geometry</parameter></paramdef>
<paramdef><type>spheroid </type> <parameter>a_spheroid</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Calculates the length/perimeter of a geometry on an ellipsoid. This
is useful if the coordinates of the geometry are in
longitude/latitude and a length is desired without reprojection.
The ellipsoid is a separate database type and can be constructed
as follows:</para>
<literallayout>SPHEROID[<NAME>,<SEMI-MAJOR AXIS>,<INVERSE FLATTENING>]</literallayout>
<literallayout>SPHEROID["GRS_1980",6378137,298.257222101]</literallayout>
<para>Availability: 1.2.2</para>
<para>Changed: 2.2.0 In prior versions this used to be called ST_Length_Spheroid and used to have a ST_3DLength_Spheroid alias</para>
<para>&Z_support;</para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting>SELECT ST_LengthSpheroid( geometry_column,
'SPHEROID["GRS_1980",6378137,298.257222101]' )
FROM geometry_table;
SELECT ST_LengthSpheroid( the_geom, sph_m ) As tot_len,
ST_LengthSpheroid(ST_GeometryN(the_geom,1), sph_m) As len_line1,
ST_LengthSpheroid(ST_GeometryN(the_geom,2), sph_m) As len_line2
FROM (SELECT ST_GeomFromText('MULTILINESTRING((-118.584 38.374,-118.583 38.5),
(-71.05957 42.3589 , -71.061 43))') As the_geom,
CAST('SPHEROID["GRS_1980",6378137,298.257222101]' As spheroid) As sph_m) as foo;
tot_len | len_line1 | len_line2
------------------+------------------+------------------
85204.5207562955 | 13986.8725229309 | 71217.6482333646
--3D
SELECT ST_LengthSpheroid( the_geom, sph_m ) As tot_len,
ST_LengthSpheroid(ST_GeometryN(the_geom,1), sph_m) As len_line1,
ST_LengthSpheroid(ST_GeometryN(the_geom,2), sph_m) As len_line2
FROM (SELECT ST_GeomFromEWKT('MULTILINESTRING((-118.584 38.374 20,-118.583 38.5 30),
(-71.05957 42.3589 75, -71.061 43 90))') As the_geom,
CAST('SPHEROID["GRS_1980",6378137,298.257222101]' As spheroid) As sph_m) as foo;
tot_len | len_line1 | len_line2
------------------+-----------------+------------------
85204.5259107402 | 13986.876097711 | 71217.6498130292
</programlisting>
</refsection>
<!-- Optionally add a "See Also" section -->
<refsection>
<title>See Also</title>
<para><xref linkend="ST_GeometryN" />, <xref linkend="ST_Length" /></para>
</refsection>
</refentry>
<refentry id="ST_Length2D_Spheroid">
<refnamediv>
<refname>ST_Length2D_Spheroid</refname>
<refpurpose>Calculates the 2D length/perimeter of a geometry on an ellipsoid. This
is useful if the coordinates of the geometry are in
longitude/latitude and a length is desired without reprojection. </refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>float <function>ST_Length2D_Spheroid</function></funcdef>
<paramdef><type>geometry </type> <parameter>a_geometry</parameter></paramdef>
<paramdef><type>spheroid </type> <parameter>a_spheroid</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Calculates the 2D length/perimeter of a geometry on an ellipsoid. This
is useful if the coordinates of the geometry are in
longitude/latitude and a length is desired without reprojection.
The ellipsoid is a separate database type and can be constructed
as follows:</para>
<literallayout>SPHEROID[<NAME>,<SEMI-MAJOR AXIS>,<INVERSE FLATTENING>]</literallayout>
<literallayout>SPHEROID["GRS_1980",6378137,298.257222101]</literallayout>
<note><para>This is much like <xref linkend="ST_Length_Spheroid" /> except it will ignore the Z ordinate in calculations.</para></note>
</refsection>
<refsection>
<title>Examples</title>
<programlisting>SELECT ST_Length2D_Spheroid( geometry_column,
'SPHEROID["GRS_1980",6378137,298.257222101]' )
FROM geometry_table;
SELECT ST_Length2D_Spheroid( the_geom, sph_m ) As tot_len,
ST_Length2D_Spheroid(ST_GeometryN(the_geom,1), sph_m) As len_line1,
ST_Length2D_Spheroid(ST_GeometryN(the_geom,2), sph_m) As len_line2
FROM (SELECT ST_GeomFromText('MULTILINESTRING((-118.584 38.374,-118.583 38.5),
(-71.05957 42.3589 , -71.061 43))') As the_geom,
CAST('SPHEROID["GRS_1980",6378137,298.257222101]' As spheroid) As sph_m) as foo;
tot_len | len_line1 | len_line2
------------------+------------------+------------------
85204.5207562955 | 13986.8725229309 | 71217.6482333646
--3D Observe same answer
SELECT ST_Length2D_Spheroid( the_geom, sph_m ) As tot_len,
ST_Length2D_Spheroid(ST_GeometryN(the_geom,1), sph_m) As len_line1,
ST_Length2D_Spheroid(ST_GeometryN(the_geom,2), sph_m) As len_line2
FROM (SELECT ST_GeomFromEWKT('MULTILINESTRING((-118.584 38.374 20,-118.583 38.5 30),
(-71.05957 42.3589 75, -71.061 43 90))') As the_geom,
CAST('SPHEROID["GRS_1980",6378137,298.257222101]' As spheroid) As sph_m) as foo;
tot_len | len_line1 | len_line2
------------------+------------------+------------------
85204.5207562955 | 13986.8725229309 | 71217.6482333646
</programlisting>
</refsection>
<!-- Optionally add a "See Also" section -->
<refsection>
<title>See Also</title>
<para><xref linkend="ST_GeometryN" />, <xref linkend="ST_Length_Spheroid" /></para>
</refsection>
</refentry>
<refentry id="ST_LongestLine">
<refnamediv>
<refname>ST_LongestLine</refname>
<refpurpose>Returns the 2-dimensional longest line points of two geometries.
The function will only return the first longest line if more than one, that the function finds.
The line returned will always start in g1 and end in g2.
The length of the line this function returns will always be the same as st_maxdistance returns for g1 and g2.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>geometry <function>ST_LongestLine</function></funcdef>
<paramdef><type>geometry </type>
<parameter>g1</parameter></paramdef>
<paramdef><type>geometry </type>
<parameter>g2</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns the 2-dimensional longest line between the points of two geometries.
</para>
<para>Availability: 1.5.0</para>
</refsection>
<refsection>
<title>Examples</title>
<informaltable>
<tgroup cols="2">
<tbody>
<row>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_longestline01.png" />
</imageobject>
<caption><para>Longest line between point and line</para></caption>
</mediaobject>
</informalfigure>
<programlisting>
SELECT ST_AsText(
ST_LongestLine('POINT(100 100)'::geometry,
'LINESTRING (20 80, 98 190, 110 180, 50 75 )'::geometry)
) As lline;
lline
-----------------
LINESTRING(100 100,98 190)
</programlisting>
</para></entry>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_longestline02.png" />
</imageobject>
<caption><para>longest line between polygon and polygon</para></caption>
</mediaobject>
</informalfigure>
<programlisting>
SELECT ST_AsText(
ST_LongestLine(
ST_GeomFromText('POLYGON((175 150, 20 40,
50 60, 125 100, 175 150))'),
ST_Buffer(ST_GeomFromText('POINT(110 170)'), 20)
)
) As llinewkt;
lline
-----------------
LINESTRING(20 40,121.111404660392 186.629392246051)
</programlisting>
</para></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<informaltable>
<tgroup cols="1">
<tbody>
<row>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_longestline03.png" />
</imageobject>
<caption><para>longest straight distance to travel from one part of an elegant city to the other
Note the max distance = to the length of the line.</para></caption>
</mediaobject>
</informalfigure>
<programlisting>
SELECT ST_AsText(ST_LongestLine(c.the_geom, c.the_geom)) As llinewkt,
ST_MaxDistance(c.the_geom,c.the_geom) As max_dist,
ST_Length(ST_LongestLine(c.the_geom, c.the_geom)) As lenll
FROM (SELECT ST_BuildArea(ST_Collect(the_geom)) As the_geom
FROM (SELECT ST_Translate(ST_SnapToGrid(ST_Buffer(ST_Point(50 ,generate_series(50,190, 50)
),40, 'quad_segs=2'),1), x, 0) As the_geom
FROM generate_series(1,100,50) As x) AS foo
) As c;
llinewkt | max_dist | lenll
---------------------------+------------------+------------------
LINESTRING(23 22,129 178) | 188.605408193933 | 188.605408193933
</programlisting>
</para></entry>
</row>
</tbody>
</tgroup>
</informaltable>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_MaxDistance"/>, <xref linkend="ST_ShortestLine"/>, <xref linkend="ST_LongestLine"/></para>
</refsection>
</refentry>
<refentry id="ST_OrderingEquals">
<refnamediv>
<refname>ST_OrderingEquals</refname>
<refpurpose>Returns true if the given geometries represent the same geometry
and points are in the same directional order.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>boolean <function>ST_OrderingEquals</function></funcdef>
<paramdef><type>geometry </type> <parameter>A</parameter></paramdef>
<paramdef><type>geometry </type> <parameter>B</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>ST_OrderingEquals compares two geometries and returns t (TRUE) if the
geometries are equal and the coordinates are in the same order;
otherwise it returns f (FALSE).</para>
<note>
<para>This function is implemented as per the ArcSDE SQL
specification rather than SQL-MM.
http://edndoc.esri.com/arcsde/9.1/sql_api/sqlapi3.htm#ST_OrderingEquals</para>
</note>
<para>&sqlmm_compliant; SQL-MM 3: 5.1.43</para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting>SELECT ST_OrderingEquals(ST_GeomFromText('LINESTRING(0 0, 10 10)'),
ST_GeomFromText('LINESTRING(0 0, 5 5, 10 10)'));
st_orderingequals
-----------
f
(1 row)
SELECT ST_OrderingEquals(ST_GeomFromText('LINESTRING(0 0, 10 10)'),
ST_GeomFromText('LINESTRING(0 0, 0 0, 10 10)'));
st_orderingequals
-----------
t
(1 row)
SELECT ST_OrderingEquals(ST_Reverse(ST_GeomFromText('LINESTRING(0 0, 10 10)')),
ST_GeomFromText('LINESTRING(0 0, 0 0, 10 10)'));
st_orderingequals
-----------
f
(1 row)
</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_Equals"/>, <xref linkend="ST_Reverse"/></para>
</refsection>
</refentry>
<refentry id="ST_Overlaps">
<refnamediv>
<refname>ST_Overlaps</refname>
<refpurpose>Returns TRUE if the Geometries share space, are of the same dimension, but are not completely contained by each other.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>boolean <function>ST_Overlaps</function></funcdef>
<paramdef><type>geometry </type> <parameter>A</parameter></paramdef>
<paramdef><type>geometry </type> <parameter>B</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns TRUE if the Geometries "spatially
overlap". By that we mean they intersect, but one does not completely contain another. </para>
<para>Performed by the GEOS module</para>
<note><para>Do not call with a GeometryCollection as an argument</para></note>
<para>This function call will automatically include a bounding box
comparison that will make use of any indexes that are available on
the geometries. To avoid index use, use the function
_ST_Overlaps.</para>
<para>NOTE: this is the "allowable" version that returns a
boolean, not an integer.</para>
<para>&sfs_compliant; s2.1.1.2 // s2.1.13.3</para>
<para>&sqlmm_compliant; SQL-MM 3: 5.1.32</para>
</refsection>
<refsection>
<title>Examples</title>
<para>The following illustrations all return <varname>TRUE</varname>.</para>
<informaltable>
<tgroup cols="3">
<tbody>
<row>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_overlaps01.png" />
</imageobject>
<caption><para><varname>MULTIPOINT</varname> / <varname>MULTIPOINT</varname></para></caption>
</mediaobject>
</informalfigure></para></entry>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_overlaps02.png" />
</imageobject>
<caption><para><varname>LINESTRING</varname> / <varname>LINESTRING</varname></para></caption>
</mediaobject>
</informalfigure></para></entry>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_overlaps03.png" />
</imageobject>
<caption><para><varname>POLYGON</varname> / <varname>POLYGON</varname></para></caption>
</mediaobject>
</informalfigure></para></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<programlisting>--a point on a line is contained by the line and is of a lower dimension, and therefore does not overlap the line
nor crosses
SELECT ST_Overlaps(a,b) As a_overlap_b,
ST_Crosses(a,b) As a_crosses_b,
ST_Intersects(a, b) As a_intersects_b, ST_Contains(b,a) As b_contains_a
FROM (SELECT ST_GeomFromText('POINT(1 0.5)') As a, ST_GeomFromText('LINESTRING(1 0, 1 1, 3 5)') As b)
As foo
a_overlap_b | a_crosses_b | a_intersects_b | b_contains_a
------------+-------------+----------------+--------------
f | f | t | t
--a line that is partly contained by circle, but not fully is defined as intersecting and crossing,
-- but since of different dimension it does not overlap
SELECT ST_Overlaps(a,b) As a_overlap_b, ST_Crosses(a,b) As a_crosses_b,
ST_Intersects(a, b) As a_intersects_b,
ST_Contains(a,b) As a_contains_b
FROM (SELECT ST_Buffer(ST_GeomFromText('POINT(1 0.5)'), 3) As a, ST_GeomFromText('LINESTRING(1 0, 1 1, 3 5)') As b)
As foo;
a_overlap_b | a_crosses_b | a_intersects_b | a_contains_b
-------------+-------------+----------------+--------------
f | t | t | f
-- a 2-dimensional bent hot dog (aka buffered line string) that intersects a circle,
-- but is not fully contained by the circle is defined as overlapping since they are of the same dimension,
-- but it does not cross, because the intersection of the 2 is of the same dimension
-- as the maximum dimension of the 2
SELECT ST_Overlaps(a,b) As a_overlap_b, ST_Crosses(a,b) As a_crosses_b, ST_Intersects(a, b) As a_intersects_b,
ST_Contains(b,a) As b_contains_a,
ST_Dimension(a) As dim_a, ST_Dimension(b) as dim_b, ST_Dimension(ST_Intersection(a,b)) As dima_intersection_b
FROM (SELECT ST_Buffer(ST_GeomFromText('POINT(1 0.5)'), 3) As a,
ST_Buffer(ST_GeomFromText('LINESTRING(1 0, 1 1, 3 5)'),0.5) As b)
As foo;
a_overlap_b | a_crosses_b | a_intersects_b | b_contains_a | dim_a | dim_b | dima_intersection_b
-------------+-------------+----------------+--------------+-------+-------+---------------------
t | f | t | f | 2 | 2 | 2
</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_Contains"/>, <xref linkend="ST_Crosses"/>, <xref linkend="ST_Dimension"/>, <xref linkend="ST_Intersects"/></para>
</refsection>
</refentry>
<refentry id="ST_Perimeter">
<refnamediv>
<refname>ST_Perimeter</refname>
<refpurpose>Return the length measurement of the boundary of an ST_Surface
or ST_MultiSurface geometry or geography. (Polygon, MultiPolygon). geometry measurement is in units of spatial reference and geography is in meters.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>float <function>ST_Perimeter</function></funcdef>
<paramdef><type>geometry </type><parameter>g1</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>float <function>ST_Perimeter</function></funcdef>
<paramdef><type>geography </type><parameter>geog</parameter></paramdef>
<paramdef choice="opt"><type>boolean </type><parameter>use_spheroid=true</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns the 2D perimeter of the geometry/geography if it is a ST_Surface, ST_MultiSurface (Polygon, MultiPolygon). 0 is returned for
non-areal geometries. For linear geometries use <xref linkend="ST_Length" />. For geometry types, units for perimeter measures are specified by the
spatial reference system of the geometry.</para>
<para>For geography types, the calculations are performed using the inverse geodesic problem, where perimeter units are in meters.
If PostGIS is compiled with PROJ version 4.8.0 or later, the spheroid is specified by the SRID, otherwise it is exclusive to WGS84.
If <varname>use_spheroid=false</varname>, then calculations will approximate a sphere instead of a spheroid.</para>
<para>Currently this is an alias for ST_Perimeter2D, but this may change to support higher dimensions.</para>
<para>&sfs_compliant; s2.1.5.1</para>
<para>&sqlmm_compliant; SQL-MM 3: 8.1.3, 9.5.4</para>
<para>Availability 2.0.0: Support for geography was introduced</para>
</refsection>
<refsection>
<title>Examples: Geometry</title>
<para>Return perimeter in feet for Polygon and MultiPolygon. Note this is in feet because EPSG:2249 is
Massachusetts State Plane Feet</para>
<programlisting>
SELECT ST_Perimeter(ST_GeomFromText('POLYGON((743238 2967416,743238 2967450,743265 2967450,
743265.625 2967416,743238 2967416))', 2249));
st_perimeter
---------
122.630744000095
(1 row)
SELECT ST_Perimeter(ST_GeomFromText('MULTIPOLYGON(((763104.471273676 2949418.44119003,
763104.477769673 2949418.42538203,
763104.189609677 2949418.22343004,763104.471273676 2949418.44119003)),
((763104.471273676 2949418.44119003,763095.804579742 2949436.33850239,
763086.132105649 2949451.46730207,763078.452329651 2949462.11549407,
763075.354136904 2949466.17407812,763064.362142565 2949477.64291974,
763059.953961626 2949481.28983009,762994.637609571 2949532.04103014,
762990.568508415 2949535.06640477,762986.710889563 2949539.61421415,
763117.237897679 2949709.50493431,763235.236617789 2949617.95619822,
763287.718121842 2949562.20592617,763111.553321674 2949423.91664605,
763104.471273676 2949418.44119003)))', 2249));
st_perimeter
---------
845.227713366825
(1 row)
</programlisting>
</refsection>
<refsection>
<title>Examples: Geography</title>
<para>Return perimeter in meters and feet for Polygon and MultiPolygon. Note this is geography (WGS 84 long lat)</para>
<programlisting>
SELECT ST_Perimeter(geog) As per_meters, ST_Perimeter(geog)/0.3048 As per_ft
FROM ST_GeogFromText('POLYGON((-71.1776848522251 42.3902896512902,-71.1776843766326 42.3903829478009,
-71.1775844305465 42.3903826677917,-71.1775825927231 42.3902893647987,-71.1776848522251 42.3902896512902))') As geog;
per_meters | per_ft
-----------------+------------------
37.3790462565251 | 122.634666195949
-- MultiPolygon example --
SELECT ST_Perimeter(geog) As per_meters, ST_Perimeter(geog,false) As per_sphere_meters, ST_Perimeter(geog)/0.3048 As per_ft
FROM ST_GeogFromText('MULTIPOLYGON(((-71.1044543107478 42.340674480411,-71.1044542869917 42.3406744369506,
-71.1044553562977 42.340673886454,-71.1044543107478 42.340674480411)),
((-71.1044543107478 42.340674480411,-71.1044860600303 42.3407237015564,-71.1045215770124 42.3407653385914,
-71.1045498002983 42.3407946553165,-71.1045611902745 42.3408058316308,-71.1046016507427 42.340837442371,
-71.104617893173 42.3408475056957,-71.1048586153981 42.3409875993595,-71.1048736143677 42.3409959528211,
-71.1048878050242 42.3410084812078,-71.1044020965803 42.3414730072048,
-71.1039672113619 42.3412202916693,-71.1037740497748 42.3410666421308,
-71.1044280218456 42.3406894151355,-71.1044543107478 42.340674480411)))') As geog;
per_meters | per_sphere_meters | per_ft
------------------+-------------------+------------------
257.634283683311 | 257.412311446337 | 845.256836231335
</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_GeogFromText" />, <xref linkend="ST_GeomFromText" />, <xref linkend="ST_Length" /></para>
</refsection>
</refentry>
<refentry id="ST_Perimeter2D">
<refnamediv>
<refname>ST_Perimeter2D</refname>
<refpurpose>Returns the 2-dimensional perimeter of the geometry, if it
is a polygon or multi-polygon. This is currently an alias for ST_Perimeter.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>float <function>ST_Perimeter2D</function></funcdef>
<paramdef><type>geometry </type> <parameter>geomA</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns the 2-dimensional perimeter of the geometry, if it
is a polygon or multi-polygon. </para>
<!-- optionally mention that this function uses indexes if appropriate -->
<note>
<para> This is currently an alias for ST_Perimeter. In future versions ST_Perimeter may return the highest dimension perimeter for a geometry. This is still under consideration</para>
</note>
</refsection>
<!-- Optionally add a "See Also" section -->
<refsection>
<title>See Also</title>
<para><xref linkend="ST_Perimeter" /></para>
</refsection>
</refentry>
<refentry id="ST_3DPerimeter">
<refnamediv>
<refname>ST_3DPerimeter</refname>
<refpurpose>Returns the 3-dimensional perimeter of the geometry, if it
is a polygon or multi-polygon.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>float <function>ST_3DPerimeter</function></funcdef>
<paramdef><type>geometry </type> <parameter>geomA</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns the 3-dimensional perimeter of the geometry, if it
is a polygon or multi-polygon. If the geometry is 2-dimensional, then the 2-dimensional perimeter is returned. </para>
<para>&Z_support;</para>
<para>Changed: 2.0.0 In prior versions this used to be called ST_Perimeter3D</para>
</refsection>
<refsection>
<title>Examples</title>
<para>Perimeter of a slightly elevated polygon in the air in Massachusetts state plane feet</para>
<programlisting>SELECT ST_3DPerimeter(the_geom), ST_Perimeter2d(the_geom), ST_Perimeter(the_geom) FROM
(SELECT ST_GeomFromEWKT('SRID=2249;POLYGON((743238 2967416 2,743238 2967450 1,
743265.625 2967416 1,743238 2967416 2))') As the_geom) As foo;
ST_3DPerimeter | st_perimeter2d | st_perimeter
------------------+------------------+------------------
105.465793597674 | 105.432997272188 | 105.432997272188
</programlisting>
</refsection>
<!-- Optionally add a "See Also" section -->
<refsection>
<title>See Also</title>
<para><xref linkend="ST_GeomFromEWKT" />, <xref linkend="ST_Perimeter" />, <xref linkend="ST_Perimeter2D" /></para>
</refsection>
</refentry>
<refentry id="ST_PointOnSurface">
<refnamediv>
<refname>ST_PointOnSurface</refname>
<refpurpose>Returns a <varname>POINT</varname> guaranteed to lie on the surface.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>geometry <function>ST_PointOnSurface</function></funcdef>
<paramdef><type>geometry </type>
<parameter>g1</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns a <varname>POINT</varname> guaranteed to intersect a surface.</para>
<para>&sfs_compliant; s3.2.14.2 // s3.2.18.2</para>
<para>&sqlmm_compliant; SQL-MM 3: 8.1.5, 9.5.6.
According to the specs, ST_PointOnSurface works for surface geometries (POLYGONs, MULTIPOLYGONS, CURVED POLYGONS). So PostGIS seems to be extending what
the spec allows here. Most databases Oracle,DB II, ESRI SDE seem to only support this function for surfaces. SQL Server 2008 like PostGIS supports for all common geometries.</para>
<para>&Z_support;</para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting>SELECT ST_AsText(ST_PointOnSurface('POINT(0 5)'::geometry));
st_astext
------------
POINT(0 5)
(1 row)
SELECT ST_AsText(ST_PointOnSurface('LINESTRING(0 5, 0 10)'::geometry));
st_astext
------------
POINT(0 5)
(1 row)
SELECT ST_AsText(ST_PointOnSurface('POLYGON((0 0, 0 5, 5 5, 5 0, 0 0))'::geometry));
st_astext
----------------
POINT(2.5 2.5)
(1 row)
SELECT ST_AsEWKT(ST_PointOnSurface(ST_GeomFromEWKT('LINESTRING(0 5 1, 0 0 1, 0 10 2)')));
st_asewkt
----------------
POINT(0 0 1)
(1 row)
</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_Centroid" />, <xref linkend="ST_Point_Inside_Circle" /></para>
</refsection>
</refentry>
<refentry id="ST_Project">
<refnamediv>
<refname>ST_Project</refname>
<refpurpose>Returns a <varname>POINT</varname> projected from a start point using a distance in meters and bearing (azimuth) in radians.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>geography <function>ST_Project</function></funcdef>
<paramdef><type>geography </type>
<parameter>g1</parameter></paramdef>
<paramdef><type>float </type>
<parameter>distance</parameter></paramdef>
<paramdef><type>float </type>
<parameter>azimuth</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns a <varname>POINT</varname> projected along a geodesic from a start point using an azimuth (bearing) measured in radians and distance measured in meters. This is also called a direct geodesic problem.</para>
<para>The azimuth is sometimes called the heading or the bearing in navigation. It is measured relative to true north (azimuth zero). East is azimuth 90 (π/2), south is azimuth 180 (π), west is azimuth 270 (3π/2).</para>
<para>The distance is given in meters.</para>
<para>Availability: 2.0.0</para>
</refsection>
<refsection>
<title>Example: Using degrees - projected point 100,000 meters and bearing 45 degrees </title>
<programlisting>SELECT ST_AsText(ST_Project('POINT(0 0)'::geography, 100000, radians(45.0)));
st_astext
--------------------------------------------
POINT(0.635231029125537 0.639472334729198)
(1 row)
</programlisting>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_Azimuth" />, <xref linkend="ST_Distance" />, <ulink url="http://www.postgresql.org/docs/current/interactive/functions-math.html">PostgreSQL Math Functions</ulink></para>
</refsection>
</refentry>
<refentry id="ST_Relate">
<refnamediv>
<refname>ST_Relate</refname>
<refpurpose>Returns true if this Geometry is spatially related to
anotherGeometry, by testing for intersections between the
Interior, Boundary and Exterior of the two geometries as specified
by the values in the intersectionMatrixPattern. If no intersectionMatrixPattern
is passed in, then returns the maximum intersectionMatrixPattern that relates the 2 geometries.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>boolean <function>ST_Relate</function></funcdef>
<paramdef><type>geometry </type> <parameter>geomA</parameter></paramdef>
<paramdef><type>geometry </type> <parameter>geomB</parameter></paramdef>
<paramdef><type>text </type> <parameter>intersectionMatrixPattern</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>text <function>ST_Relate</function></funcdef>
<paramdef><type>geometry </type> <parameter>geomA</parameter></paramdef>
<paramdef><type>geometry </type> <parameter>geomB</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>text <function>ST_Relate</function></funcdef>
<paramdef><type>geometry </type> <parameter>geomA</parameter></paramdef>
<paramdef><type>geometry </type> <parameter>geomB</parameter></paramdef>
<paramdef><type>integer </type> <parameter>BoundaryNodeRule</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Version 1: Takes geomA, geomB, intersectionMatrix and Returns 1 (TRUE) if this Geometry is spatially related to
anotherGeometry, by testing for intersections between the
Interior, Boundary and Exterior of the two geometries as specified
by the values in the <ulink url="http://en.wikipedia.org/wiki/DE-9IM">DE-9IM matrix pattern</ulink>.</para>
<para>This is especially useful for testing compound checks of intersection, crosses, etc in one step.</para>
<para>Do not call with a GeometryCollection as an argument</para>
<note><para>This is the "allowable" version that returns a
boolean, not an integer. This is defined in OGC spec</para></note>
<note><para>This DOES NOT automagically include an index call. The reason for that
is some relationships are anti e.g. Disjoint. If you are
using a relationship pattern that requires intersection, then include the &&
index call.</para></note>
<para>Version 2: Takes geomA and geomB and returns the <xref linkend="DE-9IM" /></para>
<para>Version 3: same as version 2, but allows to specify a boundary node rule (1:OGC/MOD2, 2:Endpoint, 3:MultivalentEndpoint, 4:MonovalentEndpoint)</para>
<note><para>Do not call with a GeometryCollection as an argument</para></note>
<para>not in OGC spec, but implied. see s2.1.13.2</para>
<para>Performed by the GEOS module</para>
<para>&sfs_compliant; s2.1.1.2 // s2.1.13.3</para>
<para>&sqlmm_compliant; SQL-MM 3: 5.1.25</para>
<para>Enhanced: 2.0.0 - added support for specifying boundary node rule (requires GEOS >= 3.0).</para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting>
--Find all compounds that intersect and not touch a poly (interior intersects)
SELECT l.* , b.name As poly_name
FROM polys As b
INNER JOIN compounds As l
ON (p.the_geom && b.the_geom
AND ST_Relate(l.the_geom, b.the_geom,'T********'));
SELECT ST_Relate(ST_GeometryFromText('POINT(1 2)'), ST_Buffer(ST_GeometryFromText('POINT(1 2)'),2));
st_relate
-----------
0FFFFF212
SELECT ST_Relate(ST_GeometryFromText('LINESTRING(1 2, 3 4)'), ST_GeometryFromText('LINESTRING(5 6, 7 8)'));
st_relate
-----------
FF1FF0102
SELECT ST_Relate(ST_GeometryFromText('POINT(1 2)'), ST_Buffer(ST_GeometryFromText('POINT(1 2)'),2), '0FFFFF212');
st_relate
-----------
t
SELECT ST_Relate(ST_GeometryFromText('POINT(1 2)'), ST_Buffer(ST_GeometryFromText('POINT(1 2)'),2), '*FF*FF212');
st_relate
-----------
t
</programlisting>
</refsection>
<!-- Optionally add a "See Also" section -->
<refsection>
<title>See Also</title>
<para><xref linkend="ST_Crosses" />, <xref linkend="DE-9IM" />, <xref linkend="ST_Disjoint" />, <xref linkend="ST_Intersects" />, <xref linkend="ST_Touches" /></para>
</refsection>
</refentry>
<refentry id="ST_RelateMatch">
<refnamediv>
<refname>ST_RelateMatch</refname>
<refpurpose>Returns true if intersectionMattrixPattern1 implies intersectionMatrixPattern2</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>boolean <function>ST_RelateMatch</function></funcdef>
<paramdef><type>text </type> <parameter>intersectionMatrix</parameter></paramdef>
<paramdef><type>text </type> <parameter>intersectionMatrixPattern</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para> Takes intersectionMatrix and intersectionMatrixPattern and Returns true if the intersectionMatrix satisfies
the intersectionMatrixPattern. For more information refer to <xref linkend="DE-9IM" />. </para>
<para>Availability: 2.0.0 - requires GEOS >= 3.3.0. </para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting>
SELECT ST_RelateMatch('101202FFF', 'TTTTTTFFF') ;
-- result --
t
--example of common intersection matrix patterns and example matrices
-- comparing relationships of involving one invalid geometry and ( a line and polygon that intersect at interior and boundary)
SELECT mat.name, pat.name, ST_RelateMatch(mat.val, pat.val) As satisfied
FROM
( VALUES ('Equality', 'T1FF1FFF1'),
('Overlaps', 'T*T***T**'),
('Within', 'T*F**F***'),
('Disjoint', 'FF*FF****') As pat(name,val)
CROSS JOIN
( VALUES ('Self intersections (invalid)', '111111111'),
('IE2_BI1_BB0_BE1_EI1_EE2', 'FF2101102'),
('IB1_IE1_BB0_BE0_EI2_EI1_EE2', 'F11F00212')
) As mat(name,val);
</programlisting>
</refsection>
<!-- Optionally add a "See Also" section -->
<refsection>
<title>See Also</title>
<para><xref linkend="DE-9IM" />, <xref linkend="ST_Relate" /></para>
</refsection>
</refentry>
<refentry id="ST_ShortestLine">
<refnamediv>
<refname>ST_ShortestLine</refname>
<refpurpose>Returns the 2-dimensional shortest line between two geometries</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>geometry <function>ST_ShortestLine</function></funcdef>
<paramdef><type>geometry </type>
<parameter>g1</parameter></paramdef>
<paramdef><type>geometry </type>
<parameter>g2</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns the 2-dimensional shortest line between two geometries. The function will
only return the first shortest line if more than one, that the function finds.
If g1 and g2 intersects in just one point the function will return a line with both start
and end in that intersection-point.
If g1 and g2 are intersecting with more than one point the function will return a line with start
and end in the same point but it can be any of the intersecting points.
The line returned will always start in g1 and end in g2.
The length of the line this function returns will always be the same as ST_Distance returns for g1 and g2.
</para>
<para>Availability: 1.5.0</para>
</refsection>
<refsection>
<title>Examples</title>
<informaltable>
<tgroup cols="2">
<tbody>
<row>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_shortestline01.png" />
</imageobject>
<caption><para>Shortest line between point and linestring</para></caption>
</mediaobject>
</informalfigure>
<programlisting>
SELECT ST_AsText(
ST_ShortestLine('POINT(100 100)'::geometry,
'LINESTRING (20 80, 98 190, 110 180, 50 75 )'::geometry)
) As sline;
sline
-----------------
LINESTRING(100 100,73.0769230769231 115.384615384615)
</programlisting>
</para></entry>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_shortestline02.png" />
</imageobject>
<caption><para>shortest line between polygon and polygon</para></caption>
</mediaobject>
</informalfigure>
<programlisting>
SELECT ST_AsText(
ST_ShortestLine(
ST_GeomFromText('POLYGON((175 150, 20 40, 50 60, 125 100, 175 150))'),
ST_Buffer(ST_GeomFromText('POINT(110 170)'), 20)
)
) As slinewkt;
LINESTRING(140.752120669087 125.695053378061,121.111404660392 153.370607753949)
</programlisting>
</para></entry>
</row>
</tbody>
</tgroup>
</informaltable>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_ClosestPoint"/>, <xref linkend="ST_Distance"/>, <xref linkend="ST_LongestLine"/>, <xref linkend="ST_MaxDistance"/></para>
</refsection>
</refentry>
<refentry id="ST_Touches">
<refnamediv>
<refname>ST_Touches</refname>
<refpurpose>Returns <varname>TRUE</varname> if the geometries have at least one point in common,
but their interiors do not intersect.</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>boolean <function>ST_Touches</function></funcdef>
<paramdef><type>geometry </type>
<parameter>g1</parameter></paramdef>
<paramdef><type>geometry </type>
<parameter>g2</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns <varname>TRUE</varname> if the only points in common between
<parameter>g1</parameter> and <parameter>g2</parameter> lie in the union of the
boundaries of <parameter>g1</parameter> and <parameter>g2</parameter>.
The <function>ST_Touches</function> relation applies
to all Area/Area, Line/Line, Line/Area, Point/Area and Point/Line pairs of relationships,
but <emphasis>not</emphasis> to the Point/Point pair.</para>
<para>In mathematical terms, this predicate is expressed as:</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_touches-math.gif" />
</imageobject>
</mediaobject>
</informalfigure>
<para>The allowable DE-9IM Intersection Matrices for the two geometries are:</para>
<itemizedlist>
<listitem>
<para><markup>FT*******</markup></para>
</listitem>
<listitem>
<para><markup>F**T*****</markup></para>
</listitem>
<listitem>
<para><markup>F***T****</markup></para>
</listitem>
</itemizedlist>
<important>
<para>Do not call with a <varname>GEOMETRYCOLLECTION</varname> as an argument</para>
</important>
<note>
<para>This function call will automatically include a bounding box
comparison that will make use of any indexes that are available on
the geometries. To avoid using an index, use <function>_ST_Touches</function> instead.</para>
</note>
<para>&sfs_compliant; s2.1.1.2 // s2.1.13.3</para>
<para>&sqlmm_compliant; SQL-MM 3: 5.1.28</para>
</refsection>
<refsection>
<title>Examples</title>
<para>The <function>ST_Touches</function> predicate returns <varname>TRUE</varname> in all the following illustrations.</para>
<informaltable>
<tgroup cols="3">
<tbody>
<row>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_touches01.png" />
</imageobject>
<caption><para><varname>POLYGON</varname> / <varname>POLYGON</varname></para></caption>
</mediaobject>
</informalfigure></para></entry>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_touches02.png" />
</imageobject>
<caption><para><varname>POLYGON</varname> / <varname>POLYGON</varname></para></caption>
</mediaobject>
</informalfigure></para></entry>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_touches03.png" />
</imageobject>
<caption><para><varname>POLYGON</varname> / <varname>LINESTRING</varname></para></caption>
</mediaobject>
</informalfigure></para></entry>
</row>
<row>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_touches04.png" />
</imageobject>
<caption><para><varname>LINESTRING</varname> / <varname>LINESTRING</varname></para></caption>
</mediaobject>
</informalfigure></para></entry>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_touches05.png" />
</imageobject>
<caption><para><varname>LINESTRING</varname> / <varname>LINESTRING</varname></para></caption>
</mediaobject>
</informalfigure></para></entry>
<entry><para><informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="images/st_touches06.png" />
</imageobject>
<caption><para><varname>POLYGON</varname> / <varname>POINT</varname></para></caption>
</mediaobject>
</informalfigure></para></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<programlisting>SELECT ST_Touches('LINESTRING(0 0, 1 1, 0 2)'::geometry, 'POINT(1 1)'::geometry);
st_touches
------------
f
(1 row)
SELECT ST_Touches('LINESTRING(0 0, 1 1, 0 2)'::geometry, 'POINT(0 2)'::geometry);
st_touches
------------
t
(1 row)</programlisting>
</refsection>
</refentry>
<refentry id="ST_Within">
<refnamediv>
<refname>ST_Within</refname>
<refpurpose>Returns true if the geometry A is completely inside geometry B</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcprototype>
<funcdef>boolean <function>ST_Within</function></funcdef>
<paramdef><type>geometry </type>
<parameter>A</parameter></paramdef>
<paramdef><type>geometry </type>
<parameter>B</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>Returns TRUE if geometry A is completely inside geometry B. For this function to make
sense, the source geometries must both be of the same coordinate projection,
having the same SRID. It is a given that if ST_Within(A,B) is true and ST_Within(B,A) is true, then
the two geometries are considered spatially equal.</para>
<para>Performed by the GEOS module</para>
<para>Enhanced: 2.3.0 Enhancement to PIP short-circuit for geometry extended to support MultiPoints with few points. Prior versions only supported point in polygon.</para>
<important>
<para>Do not call with a <varname>GEOMETRYCOLLECTION</varname> as an argument</para>
</important>
<important>
<para>Do not use this function with invalid geometries. You will get unexpected results.</para>
</important>
<para>This function call will automatically include a bounding box
comparison that will make use of any indexes that are available on
the geometries. To avoid index use, use the function
_ST_Within.</para>
<para>NOTE: this is the "allowable" version that returns a
boolean, not an integer.</para>
<para>&sfs_compliant; s2.1.1.2 // s2.1.13.3
- a.Relate(b, 'T*F**F***')
</para>
<para>&sqlmm_compliant; SQL-MM 3: 5.1.30</para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting>
--a circle within a circle
SELECT ST_Within(smallc,smallc) As smallinsmall,
ST_Within(smallc, bigc) As smallinbig,
ST_Within(bigc,smallc) As biginsmall,
ST_Within(ST_Union(smallc, bigc), bigc) as unioninbig,
ST_Within(bigc, ST_Union(smallc, bigc)) as biginunion,
ST_Equals(bigc, ST_Union(smallc, bigc)) as bigisunion
FROM
(
SELECT ST_Buffer(ST_GeomFromText('POINT(50 50)'), 20) As smallc,
ST_Buffer(ST_GeomFromText('POINT(50 50)'), 40) As bigc) As foo;
--Result
smallinsmall | smallinbig | biginsmall | unioninbig | biginunion | bigisunion
--------------+------------+------------+------------+------------+------------
t | t | f | t | t | t
(1 row)
</programlisting>
<para><inlinemediaobject>
<imageobject>
<imagedata fileref="images/st_within01.png" />
</imageobject>
</inlinemediaobject> </para>
</refsection>
<refsection>
<title>See Also</title>
<para><xref linkend="ST_Contains"/>, <xref linkend="ST_Equals"/>, <xref linkend="ST_IsValid"/></para>
</refsection>
</refentry>
</sect1>
|