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
|
/******************************************************************************
* $Id: gdaltransformer.cpp 26279 2013-08-08 20:16:32Z rouault $
*
* Project: Mapinfo Image Warper
* Purpose: Implementation of one or more GDALTrasformerFunc types, including
* the GenImgProj (general image reprojector) transformer.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 2002, i3 - information integration and imaging
* Fort Collin, CO
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "gdal_priv.h"
#include "gdal_alg.h"
#include "ogr_spatialref.h"
#include "cpl_string.h"
#include "gdal_alg_priv.h"
#include "cpl_list.h"
#include "cpl_multiproc.h"
CPL_CVSID("$Id: gdaltransformer.cpp 26279 2013-08-08 20:16:32Z rouault $");
CPL_C_START
void *GDALDeserializeGCPTransformer( CPLXMLNode *psTree );
void *GDALDeserializeTPSTransformer( CPLXMLNode *psTree );
void *GDALDeserializeGeoLocTransformer( CPLXMLNode *psTree );
void *GDALDeserializeRPCTransformer( CPLXMLNode *psTree );
CPL_C_END
static CPLXMLNode *GDALSerializeReprojectionTransformer( void *pTransformArg );
static void *GDALDeserializeReprojectionTransformer( CPLXMLNode *psTree );
static CPLXMLNode *GDALSerializeGenImgProjTransformer( void *pTransformArg );
static void *GDALDeserializeGenImgProjTransformer( CPLXMLNode *psTree );
static void GDALRefreshGenImgProjTransformer(void* hTransformArg);
/************************************************************************/
/* GDALTransformFunc */
/* */
/* Documentation for GDALTransformFunc typedef. */
/************************************************************************/
/*!
\typedef int GDALTransformerFunc
Generic signature for spatial point transformers.
This function signature is used for a variety of functions that accept
passed in functions used to transform point locations between two coordinate
spaces.
The GDALCreateGenImgProjTransformer(), GDALCreateReprojectionTransformer(),
GDALCreateGCPTransformer() and GDALCreateApproxTransformer() functions can
be used to prepare argument data for some built-in transformers. As well,
applications can implement their own transformers to the following signature.
\code
typedef int
(*GDALTransformerFunc)( void *pTransformerArg,
int bDstToSrc, int nPointCount,
double *x, double *y, double *z, int *panSuccess );
\endcode
@param pTransformerArg application supplied callback data used by the
transformer.
@param bDstToSrc if TRUE the transformation will be from the destination
coordinate space to the source coordinate system, otherwise the transformation
will be from the source coordinate system to the destination coordinate system.
@param nPointCount number of points in the x, y and z arrays.
@param x input X coordinates. Results returned in same array.
@param y input Y coordinates. Results returned in same array.
@param z input Z coordinates. Results returned in same array.
@param panSuccess array of ints in which success (TRUE) or failure (FALSE)
flags are returned for the translation of each point.
@return TRUE if the overall transformation succeeds (though some individual
points may have failed) or FALSE if the overall transformation fails.
*/
/************************************************************************/
/* GDALSuggestedWarpOutput() */
/************************************************************************/
/**
* Suggest output file size.
*
* This function is used to suggest the size, and georeferenced extents
* appropriate given the indicated transformation and input file. It walks
* the edges of the input file (approximately 20 sample points along each
* edge) transforming into output coordinates in order to get an extents box.
*
* Then a resolution is computed with the intent that the length of the
* distance from the top left corner of the output imagery to the bottom right
* corner would represent the same number of pixels as in the source image.
* Note that if the image is somewhat rotated the diagonal taken isnt of the
* whole output bounding rectangle, but instead of the locations where the
* top/left and bottom/right corners transform. The output pixel size is
* always square. This is intended to approximately preserve the resolution
* of the input data in the output file.
*
* The values returned in padfGeoTransformOut, pnPixels and pnLines are
* the suggested number of pixels and lines for the output file, and the
* geotransform relating those pixels to the output georeferenced coordinates.
*
* The trickiest part of using the function is ensuring that the
* transformer created is from source file pixel/line coordinates to
* output file georeferenced coordinates. This can be accomplished with
* GDALCreateGenImgProjTransformer() by passing a NULL for the hDstDS.
*
* @param hSrcDS the input image (it is assumed the whole input images is
* being transformed).
* @param pfnTransformer the transformer function.
* @param pTransformArg the callback data for the transformer function.
* @param padfGeoTransformOut the array of six doubles in which the suggested
* geotransform is returned.
* @param pnPixels int in which the suggest pixel width of output is returned.
* @param pnLines int in which the suggest pixel height of output is returned.
*
* @return CE_None if successful or CE_Failure otherwise.
*/
CPLErr CPL_STDCALL
GDALSuggestedWarpOutput( GDALDatasetH hSrcDS,
GDALTransformerFunc pfnTransformer,
void *pTransformArg,
double *padfGeoTransformOut,
int *pnPixels, int *pnLines )
{
VALIDATE_POINTER1( hSrcDS, "GDALSuggestedWarpOutput", CE_Failure );
double adfExtent[4] = { 0 };
return GDALSuggestedWarpOutput2( hSrcDS, pfnTransformer, pTransformArg,
padfGeoTransformOut, pnPixels, pnLines,
adfExtent, 0 );
}
static int GDALSuggestedWarpOutput2_MustAdjustForRightBorder(
GDALTransformerFunc pfnTransformer, void *pTransformArg,
double* padfExtent, int nPixels, int nLines,
double dfPixelSizeX, double dfPixelSizeY)
{
int nSamplePoints;
double dfRatio;
int bErr;
int nBadCount;
int abSuccess[21] = { 0 };
double adfX[21] = { 0 };
double adfY[21] = { 0 };
double adfZ[21] = { 0 };
//double dfMinXOut = padfExtent[0];
//double dfMinYOut = padfExtent[1];
double dfMaxXOut = padfExtent[2];
double dfMaxYOut = padfExtent[3];
// Take 20 steps
nSamplePoints = 0;
for( dfRatio = 0.0; dfRatio <= 1.01; dfRatio += 0.05 )
{
// Ensure we end exactly at the end.
if( dfRatio > 0.99 )
dfRatio = 1.0;
// Along right
adfX[nSamplePoints] = dfMaxXOut;
adfY[nSamplePoints] = dfMaxYOut - dfPixelSizeY * dfRatio * nLines;
adfZ[nSamplePoints++] = 0.0;
}
bErr = FALSE;
if( !pfnTransformer( pTransformArg, TRUE, nSamplePoints,
adfX, adfY, adfZ, abSuccess ) )
{
bErr = TRUE;
}
if( !bErr && !pfnTransformer( pTransformArg, FALSE, nSamplePoints,
adfX, adfY, adfZ, abSuccess ) )
{
bErr = TRUE;
}
nSamplePoints = 0;
nBadCount = 0;
for( dfRatio = 0.0; !bErr && dfRatio <= 1.01; dfRatio += 0.05 )
{
double expected_x = dfMaxXOut;
double expected_y = dfMaxYOut - dfPixelSizeY * dfRatio * nLines;
if (fabs(adfX[nSamplePoints] - expected_x) > dfPixelSizeX ||
fabs(adfY[nSamplePoints] - expected_y) > dfPixelSizeY)
nBadCount ++;
nSamplePoints ++;
}
return (nBadCount == nSamplePoints);
}
static int GDALSuggestedWarpOutput2_MustAdjustForBottomBorder(
GDALTransformerFunc pfnTransformer, void *pTransformArg,
double* padfExtent, int nPixels, int nLines,
double dfPixelSizeX, double dfPixelSizeY)
{
int nSamplePoints;
double dfRatio;
int bErr;
int nBadCount;
int abSuccess[21] = { 0 };
double adfX[21] = { 0 };
double adfY[21] = { 0 };
double adfZ[21] = { 0 };
double dfMinXOut = padfExtent[0];
double dfMinYOut = padfExtent[1];
//double dfMaxXOut = padfExtent[2];
//double dfMaxYOut = padfExtent[3];
// Take 20 steps
nSamplePoints = 0;
for( dfRatio = 0.0; dfRatio <= 1.01; dfRatio += 0.05 )
{
// Ensure we end exactly at the end.
if( dfRatio > 0.99 )
dfRatio = 1.0;
// Along right
adfX[nSamplePoints] = dfMinXOut + dfPixelSizeX * dfRatio * nPixels;
adfY[nSamplePoints] = dfMinYOut;
adfZ[nSamplePoints++] = 0.0;
}
bErr = FALSE;
if( !pfnTransformer( pTransformArg, TRUE, nSamplePoints,
adfX, adfY, adfZ, abSuccess ) )
{
bErr = TRUE;
}
if( !bErr && !pfnTransformer( pTransformArg, FALSE, nSamplePoints,
adfX, adfY, adfZ, abSuccess ) )
{
bErr = TRUE;
}
nSamplePoints = 0;
nBadCount = 0;
for( dfRatio = 0.0; !bErr && dfRatio <= 1.01; dfRatio += 0.05 )
{
double expected_x = dfMinXOut + dfPixelSizeX * dfRatio * nPixels;
double expected_y = dfMinYOut;
if (fabs(adfX[nSamplePoints] - expected_x) > dfPixelSizeX ||
fabs(adfY[nSamplePoints] - expected_y) > dfPixelSizeY)
nBadCount ++;
nSamplePoints ++;
}
return (nBadCount == nSamplePoints);
}
/************************************************************************/
/* GDALSuggestedWarpOutput2() */
/************************************************************************/
/**
* Suggest output file size.
*
* This function is used to suggest the size, and georeferenced extents
* appropriate given the indicated transformation and input file. It walks
* the edges of the input file (approximately 20 sample points along each
* edge) transforming into output coordinates in order to get an extents box.
*
* Then a resolution is computed with the intent that the length of the
* distance from the top left corner of the output imagery to the bottom right
* corner would represent the same number of pixels as in the source image.
* Note that if the image is somewhat rotated the diagonal taken isnt of the
* whole output bounding rectangle, but instead of the locations where the
* top/left and bottom/right corners transform. The output pixel size is
* always square. This is intended to approximately preserve the resolution
* of the input data in the output file.
*
* The values returned in padfGeoTransformOut, pnPixels and pnLines are
* the suggested number of pixels and lines for the output file, and the
* geotransform relating those pixels to the output georeferenced coordinates.
*
* The trickiest part of using the function is ensuring that the
* transformer created is from source file pixel/line coordinates to
* output file georeferenced coordinates. This can be accomplished with
* GDALCreateGenImgProjTransformer() by passing a NULL for the hDstDS.
*
* @param hSrcDS the input image (it is assumed the whole input images is
* being transformed).
* @param pfnTransformer the transformer function.
* @param pTransformArg the callback data for the transformer function.
* @param padfGeoTransformOut the array of six doubles in which the suggested
* geotransform is returned.
* @param pnPixels int in which the suggest pixel width of output is returned.
* @param pnLines int in which the suggest pixel height of output is returned.
* @param padfExtent Four entry array to return extents as (xmin, ymin, xmax, ymax).
* @param nOptions Options, currently always zero.
*
* @return CE_None if successful or CE_Failure otherwise.
*/
CPLErr CPL_STDCALL
GDALSuggestedWarpOutput2( GDALDatasetH hSrcDS,
GDALTransformerFunc pfnTransformer,
void *pTransformArg,
double *padfGeoTransformOut,
int *pnPixels, int *pnLines,
double *padfExtent, int nOptions )
{
VALIDATE_POINTER1( hSrcDS, "GDALSuggestedWarpOutput2", CE_Failure );
/* -------------------------------------------------------------------- */
/* Setup sample points all around the edge of the input raster. */
/* -------------------------------------------------------------------- */
int nSamplePoints = 0;
int nInXSize = GDALGetRasterXSize( hSrcDS );
int nInYSize = GDALGetRasterYSize( hSrcDS );
if (pfnTransformer == GDALGenImgProjTransform)
{
/* In case CHECK_WITH_INVERT_PROJ has been modified */
GDALRefreshGenImgProjTransformer(pTransformArg);
}
#define N_PIXELSTEP 50
int nSteps = (int) (double(MIN(nInYSize, nInXSize)) / N_PIXELSTEP + .5);
if (nSteps < 20)
nSteps = 20;
nSteps = MIN(nSteps,100);
retry:
int nSampleMax = (nSteps + 1)*(nSteps + 1);
int *pabSuccess = NULL;
double *padfX, *padfY, *padfZ;
double *padfXRevert, *padfYRevert, *padfZRevert;
double dfRatio = 0.0;
double dfStep = 1. / nSteps;
pabSuccess = (int *) VSIMalloc3(sizeof(int), nSteps + 1, nSteps + 1);
padfX = (double *) VSIMalloc3(sizeof(double) * 3, nSteps + 1, nSteps + 1);
padfXRevert = (double *) VSIMalloc3(sizeof(double) * 3, nSteps + 1, nSteps + 1);
if (pabSuccess == NULL || padfX == NULL || padfXRevert == NULL)
{
CPLFree( padfX );
CPLFree( padfXRevert );
CPLFree( pabSuccess );
if (nSteps > 20)
{
nSteps = 20;
goto retry;
}
return CE_Failure;
}
padfY = padfX + nSampleMax;
padfZ = padfX + nSampleMax * 2;
padfYRevert = padfXRevert + nSampleMax;
padfZRevert = padfXRevert + nSampleMax * 2;
// Take N_STEPS steps
int iStep;
for( iStep = 0; iStep <= nSteps; iStep ++ )
{
dfRatio = (iStep == nSteps) ? 1.0 : iStep * dfStep;
// Along top
padfX[nSamplePoints] = dfRatio * nInXSize;
padfY[nSamplePoints] = 0.0;
padfZ[nSamplePoints++] = 0.0;
// Along bottom
padfX[nSamplePoints] = dfRatio * nInXSize;
padfY[nSamplePoints] = nInYSize;
padfZ[nSamplePoints++] = 0.0;
// Along left
padfX[nSamplePoints] = 0.0;
padfY[nSamplePoints] = dfRatio * nInYSize;
padfZ[nSamplePoints++] = 0.0;
// Along right
padfX[nSamplePoints] = nInXSize;
padfY[nSamplePoints] = dfRatio * nInYSize;
padfZ[nSamplePoints++] = 0.0;
}
CPLAssert( nSamplePoints == 4 * (nSteps + 1) );
memset( pabSuccess, 1, sizeof(int) * nSampleMax );
/* -------------------------------------------------------------------- */
/* Transform them to the output coordinate system. */
/* -------------------------------------------------------------------- */
int nFailedCount = 0, i;
if( !pfnTransformer( pTransformArg, FALSE, nSamplePoints,
padfX, padfY, padfZ, pabSuccess ) )
{
CPLError( CE_Failure, CPLE_AppDefined,
"GDALSuggestedWarpOutput() failed because the passed\n"
"transformer failed." );
CPLFree( padfX );
CPLFree( padfXRevert );
CPLFree( pabSuccess );
return CE_Failure;
}
for( i = 0; i < nSamplePoints; i++ )
{
if( !pabSuccess[i] )
nFailedCount++;
}
/* -------------------------------------------------------------------- */
/* Check if the computed target coordinates are revertable. */
/* If not, try the detailed grid sampling. */
/* -------------------------------------------------------------------- */
if (nFailedCount == 0 )
{
memcpy(padfXRevert, padfX, nSamplePoints * sizeof(double));
memcpy(padfYRevert, padfY, nSamplePoints * sizeof(double));
memcpy(padfZRevert, padfZ, nSamplePoints * sizeof(double));
if( !pfnTransformer( pTransformArg, TRUE, nSamplePoints,
padfXRevert, padfYRevert, padfZRevert, pabSuccess ) )
{
nFailedCount = 1;
}
else
{
for( i = 0; nFailedCount == 0 && i < nSamplePoints; i++ )
{
if( !pabSuccess[i] )
nFailedCount++;
dfRatio = 0.0 + (i/4) * dfStep;
if (dfRatio>0.99)
dfRatio = 1.0;
double dfExpectedX, dfExpectedY;
if ((i % 4) == 0)
{
dfExpectedX = dfRatio * nInXSize;
dfExpectedY = 0.0;
}
else if ((i % 4) == 1)
{
dfExpectedX = dfRatio * nInXSize;
dfExpectedY = nInYSize;
}
else if ((i % 4) == 2)
{
dfExpectedX = 0.0;
dfExpectedY = dfRatio * nInYSize;
}
else
{
dfExpectedX = nInXSize;
dfExpectedY = dfRatio * nInYSize;
}
if (fabs(padfXRevert[i] - dfExpectedX) > nInXSize / nSteps ||
fabs(padfYRevert[i] - dfExpectedY) > nInYSize / nSteps)
nFailedCount ++;
}
}
}
/* -------------------------------------------------------------------- */
/* If any of the edge points failed to transform, we need to */
/* build a fairly detailed internal grid of points instead to */
/* help identify the area that is transformable. */
/* -------------------------------------------------------------------- */
if( nFailedCount > 0 )
{
int iStep2;
double dfRatio2;
nSamplePoints = 0;
// Take N_STEPS steps
for( iStep = 0; iStep <= nSteps; iStep ++ )
{
dfRatio = (iStep == nSteps) ? 1.0 : iStep * dfStep;
for( iStep2 = 0; iStep2 <= nSteps; iStep2 ++ )
{
dfRatio2 = (iStep2 == nSteps) ? 1.0 : iStep2 * dfStep;
// Along top
padfX[nSamplePoints] = dfRatio2 * nInXSize;
padfY[nSamplePoints] = dfRatio * nInYSize;
padfZ[nSamplePoints++] = 0.0;
}
}
CPLAssert( nSamplePoints == nSampleMax );
if( !pfnTransformer( pTransformArg, FALSE, nSamplePoints,
padfX, padfY, padfZ, pabSuccess ) )
{
CPLError( CE_Failure, CPLE_AppDefined,
"GDALSuggestedWarpOutput() failed because the passed\n"
"transformer failed." );
CPLFree( padfX );
CPLFree( padfXRevert );
CPLFree( pabSuccess );
return CE_Failure;
}
}
/* -------------------------------------------------------------------- */
/* Collect the bounds, ignoring any failed points. */
/* -------------------------------------------------------------------- */
double dfMinXOut=0, dfMinYOut=0, dfMaxXOut=0, dfMaxYOut=0;
int bGotInitialPoint = FALSE;
nFailedCount = 0;
for( i = 0; i < nSamplePoints; i++ )
{
int x_i = i % (nSteps + 1);
int y_i = i / (nSteps + 1);
if (x_i > 0 && (pabSuccess[i-1] || pabSuccess[i]))
{
double x_out_before = padfX[i-1];
double x_out_after = padfX[i];
int nIter = 0;
double x_in_before = (x_i - 1) * nInXSize * 1.0 / nSteps;
double x_in_after = x_i * nInXSize * 1.0 / nSteps;
int valid_before = pabSuccess[i-1];
int valid_after = pabSuccess[i];
/* Detect discontinuity in target coordinates when the target x coordinates */
/* change sign. This may be a false positive when the targe tx is around 0 */
/* Dichotomic search to reduce the interval to near the discontinuity and */
/* get a better out extent */
while ( (!valid_before || !valid_after ||
x_out_before * x_out_after < 0) && nIter < 16 )
{
double x = (x_in_before + x_in_after) / 2;
double y = y_i * nInYSize * 1.0 / nSteps;
double z= 0;
//fprintf(stderr, "[%d] (%f, %f) -> ", nIter, x, y);
int bSuccess = TRUE;
if( !pfnTransformer( pTransformArg, FALSE, 1,
&x, &y, &z, &bSuccess ) || !bSuccess )
{
//fprintf(stderr, "invalid\n");
if (!valid_before)
{
x_in_before = (x_in_before + x_in_after) / 2;
}
else if (!valid_after)
{
x_in_after = (x_in_before + x_in_after) / 2;
}
else
break;
}
else
{
//fprintf(stderr, "(%f, %f)\n", x, y);
if( !bGotInitialPoint )
{
bGotInitialPoint = TRUE;
dfMinXOut = dfMaxXOut = x;
dfMinYOut = dfMaxYOut = y;
}
else
{
dfMinXOut = MIN(dfMinXOut,x);
dfMinYOut = MIN(dfMinYOut,y);
dfMaxXOut = MAX(dfMaxXOut,x);
dfMaxYOut = MAX(dfMaxYOut,y);
}
if (!valid_before || x_out_before * x < 0)
{
valid_after = TRUE;
x_in_after = (x_in_before + x_in_after) / 2;
x_out_after = x;
}
else
{
valid_before = TRUE;
x_out_before = x;
x_in_before = (x_in_before + x_in_after) / 2;
}
}
nIter ++;
}
}
if( !pabSuccess[i] )
{
nFailedCount++;
continue;
}
if( !bGotInitialPoint )
{
bGotInitialPoint = TRUE;
dfMinXOut = dfMaxXOut = padfX[i];
dfMinYOut = dfMaxYOut = padfY[i];
}
else
{
dfMinXOut = MIN(dfMinXOut, padfX[i]);
dfMinYOut = MIN(dfMinYOut, padfY[i]);
dfMaxXOut = MAX(dfMaxXOut, padfX[i]);
dfMaxYOut = MAX(dfMaxYOut, padfY[i]);
}
}
if( nFailedCount > nSamplePoints - 10 )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Too many points (%d out of %d) failed to transform,\n"
"unable to compute output bounds.",
nFailedCount, nSamplePoints );
CPLFree( padfX );
CPLFree( padfXRevert );
CPLFree( pabSuccess );
return CE_Failure;
}
if( nFailedCount > 0 )
CPLDebug( "GDAL",
"GDALSuggestedWarpOutput(): %d out of %d points failed to transform.",
nFailedCount, nSamplePoints );
/* -------------------------------------------------------------------- */
/* Compute the distance in "georeferenced" units from the top */
/* corner of the transformed input image to the bottom left */
/* corner of the transformed input. Use this distance to */
/* compute an approximate pixel size in the output */
/* georeferenced coordinates. */
/* -------------------------------------------------------------------- */
double dfDiagonalDist, dfDeltaX, dfDeltaY;
if( pabSuccess[0] && pabSuccess[nSamplePoints - 1] )
{
dfDeltaX = padfX[nSamplePoints-1] - padfX[0];
dfDeltaY = padfY[nSamplePoints-1] - padfY[0];
}
else
{
dfDeltaX = dfMaxXOut - dfMinXOut;
dfDeltaY = dfMaxYOut - dfMinYOut;
}
dfDiagonalDist = sqrt( dfDeltaX * dfDeltaX + dfDeltaY * dfDeltaY );
/* -------------------------------------------------------------------- */
/* Compute a pixel size from this. */
/* -------------------------------------------------------------------- */
double dfPixelSize;
dfPixelSize = dfDiagonalDist
/ sqrt(((double)nInXSize)*nInXSize + ((double)nInYSize)*nInYSize);
double dfPixels = (dfMaxXOut - dfMinXOut) / dfPixelSize;
double dfLines = (dfMaxYOut - dfMinYOut) / dfPixelSize;
if( dfPixels > INT_MAX - 1 || dfLines > INT_MAX - 1 )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Computed dimensions are too big : %.0f x %.0f",
dfPixels + 0.5, dfLines + 0.5 );
CPLFree( padfX );
CPLFree( padfXRevert );
CPLFree( pabSuccess );
return CE_Failure;
}
*pnPixels = (int) (dfPixels + 0.5);
*pnLines = (int) (dfLines + 0.5);
double dfPixelSizeX = dfPixelSize;
double dfPixelSizeY = dfPixelSize;
double adfExtent[4];
const double adfRatioArray[] = { 0, 0.001, 0.01, 0.1, 1 };
size_t nRetry;
#define N_ELEMENTS(x) (sizeof(x) / sizeof(x[0]))
/* -------------------------------------------------------------------- */
/* Check that the right border is not completely out of source */
/* image. If so, adjust the x pixel size a bit in the hope it will */
/* fit. */
/* -------------------------------------------------------------------- */
for( nRetry = 0; nRetry < N_ELEMENTS(adfRatioArray); nRetry ++ )
{
double dfTryPixelSizeX =
dfPixelSizeX - dfPixelSizeX * adfRatioArray[nRetry] / *pnPixels;
adfExtent[0] = dfMinXOut;
adfExtent[1] = dfMaxYOut - (*pnLines) * dfPixelSizeY;
adfExtent[2] = dfMinXOut + (*pnPixels) * dfTryPixelSizeX;
adfExtent[3] = dfMaxYOut;
if (!GDALSuggestedWarpOutput2_MustAdjustForRightBorder(
pfnTransformer, pTransformArg,
adfExtent, *pnPixels, *pnLines,
dfTryPixelSizeX, dfPixelSizeY))
{
dfPixelSizeX = dfTryPixelSizeX;
break;
}
}
/* -------------------------------------------------------------------- */
/* Check that the bottom border is not completely out of source */
/* image. If so, adjust the y pixel size a bit in the hope it will */
/* fit. */
/* -------------------------------------------------------------------- */
for( nRetry = 0; nRetry < N_ELEMENTS(adfRatioArray); nRetry ++ )
{
double dfTryPixelSizeY =
dfPixelSizeY - dfPixelSizeY * adfRatioArray[nRetry] / *pnLines;
adfExtent[0] = dfMinXOut;
adfExtent[1] = dfMaxYOut - (*pnLines) * dfTryPixelSizeY;
adfExtent[2] = dfMinXOut + (*pnPixels) * dfPixelSizeX;
adfExtent[3] = dfMaxYOut;
if (!GDALSuggestedWarpOutput2_MustAdjustForBottomBorder(
pfnTransformer, pTransformArg,
adfExtent, *pnPixels, *pnLines,
dfPixelSizeX, dfTryPixelSizeY))
{
dfPixelSizeY = dfTryPixelSizeY;
break;
}
}
/* -------------------------------------------------------------------- */
/* Recompute some bounds so that all return values are consistent */
/* -------------------------------------------------------------------- */
dfMaxXOut = dfMinXOut + (*pnPixels) * dfPixelSizeX;
dfMinYOut = dfMaxYOut - (*pnLines) * dfPixelSizeY;
/* -------------------------------------------------------------------- */
/* Return raw extents. */
/* -------------------------------------------------------------------- */
padfExtent[0] = dfMinXOut;
padfExtent[1] = dfMinYOut;
padfExtent[2] = dfMaxXOut;
padfExtent[3] = dfMaxYOut;
/* -------------------------------------------------------------------- */
/* Set the output geotransform. */
/* -------------------------------------------------------------------- */
padfGeoTransformOut[0] = dfMinXOut;
padfGeoTransformOut[1] = dfPixelSizeX;
padfGeoTransformOut[2] = 0.0;
padfGeoTransformOut[3] = dfMaxYOut;
padfGeoTransformOut[4] = 0.0;
padfGeoTransformOut[5] = - dfPixelSizeY;
CPLFree( padfX );
CPLFree( padfXRevert );
CPLFree( pabSuccess );
return CE_None;
}
/************************************************************************/
/* ==================================================================== */
/* GDALGenImgProjTransformer */
/* ==================================================================== */
/************************************************************************/
typedef struct {
GDALTransformerInfo sTI;
double adfSrcGeoTransform[6];
double adfSrcInvGeoTransform[6];
void *pSrcGCPTransformArg;
void *pSrcRPCTransformArg;
void *pSrcTPSTransformArg;
void *pSrcGeoLocTransformArg;
void *pReprojectArg;
double adfDstGeoTransform[6];
double adfDstInvGeoTransform[6];
void *pDstGCPTransformArg;
} GDALGenImgProjTransformInfo;
/************************************************************************/
/* GDALCloneGenImgProjTransformer() */
/************************************************************************/
void* GDALCloneGenImgProjTransformer( void *hTransformArg )
{
VALIDATE_POINTER1( hTransformArg, "GDALCloneGenImgProjTransformer", NULL );
GDALGenImgProjTransformInfo *psInfo =
(GDALGenImgProjTransformInfo *) hTransformArg;
GDALGenImgProjTransformInfo *psClonedInfo = (GDALGenImgProjTransformInfo *)
CPLMalloc(sizeof(GDALGenImgProjTransformInfo));
memcpy(psClonedInfo, psInfo, sizeof(GDALGenImgProjTransformInfo));
if( psClonedInfo->pSrcGCPTransformArg )
psClonedInfo->pSrcGCPTransformArg = GDALCloneTransformer( psInfo->pSrcGCPTransformArg );
if( psClonedInfo->pSrcRPCTransformArg )
psClonedInfo->pSrcRPCTransformArg = GDALCloneTransformer( psInfo->pSrcRPCTransformArg );
if( psClonedInfo->pSrcTPSTransformArg )
psClonedInfo->pSrcTPSTransformArg = GDALCloneTransformer( psInfo->pSrcTPSTransformArg );
if( psClonedInfo->pSrcGeoLocTransformArg )
psClonedInfo->pSrcGeoLocTransformArg = GDALCloneTransformer( psInfo->pSrcGeoLocTransformArg );
if( psClonedInfo->pReprojectArg )
psClonedInfo->pReprojectArg = GDALCloneTransformer( psInfo->pReprojectArg );
if( psClonedInfo->pDstGCPTransformArg )
psClonedInfo->pDstGCPTransformArg = GDALCloneTransformer( psInfo->pDstGCPTransformArg );
return psClonedInfo;
}
/************************************************************************/
/* GDALCreateGenImgProjTransformer() */
/************************************************************************/
/**
* Create image to image transformer.
*
* This function creates a transformation object that maps from pixel/line
* coordinates on one image to pixel/line coordinates on another image. The
* images may potentially be georeferenced in different coordinate systems,
* and may used GCPs to map between their pixel/line coordinates and
* georeferenced coordinates (as opposed to the default assumption that their
* geotransform should be used).
*
* This transformer potentially performs three concatenated transformations.
*
* The first stage is from source image pixel/line coordinates to source
* image georeferenced coordinates, and may be done using the geotransform,
* or if not defined using a polynomial model derived from GCPs. If GCPs
* are used this stage is accomplished using GDALGCPTransform().
*
* The second stage is to change projections from the source coordinate system
* to the destination coordinate system, assuming they differ. This is
* accomplished internally using GDALReprojectionTransform().
*
* The third stage is converting from destination image georeferenced
* coordinates to destination image coordinates. This is done using the
* destination image geotransform, or if not available, using a polynomial
* model derived from GCPs. If GCPs are used this stage is accomplished using
* GDALGCPTransform(). This stage is skipped if hDstDS is NULL when the
* transformation is created.
*
* @param hSrcDS source dataset, or NULL.
* @param pszSrcWKT the coordinate system for the source dataset. If NULL,
* it will be read from the dataset itself.
* @param hDstDS destination dataset (or NULL).
* @param pszDstWKT the coordinate system for the destination dataset. If
* NULL, and hDstDS not NULL, it will be read from the destination dataset.
* @param bGCPUseOK TRUE if GCPs should be used if the geotransform is not
* available on the source dataset (not destination).
* @param dfGCPErrorThreshold ignored/deprecated.
* @param nOrder the maximum order to use for GCP derived polynomials if
* possible. Use 0 to autoselect, or -1 for thin plate splines.
*
* @return handle suitable for use GDALGenImgProjTransform(), and to be
* deallocated with GDALDestroyGenImgProjTransformer().
*/
void *
GDALCreateGenImgProjTransformer( GDALDatasetH hSrcDS, const char *pszSrcWKT,
GDALDatasetH hDstDS, const char *pszDstWKT,
int bGCPUseOK, double dfGCPErrorThreshold,
int nOrder )
{
char **papszOptions = NULL;
void *pRet;
if( pszSrcWKT != NULL )
papszOptions = CSLSetNameValue( papszOptions, "SRC_SRS", pszSrcWKT );
if( pszDstWKT != NULL )
papszOptions = CSLSetNameValue( papszOptions, "DST_SRS", pszDstWKT );
if( !bGCPUseOK )
papszOptions = CSLSetNameValue( papszOptions, "GCPS_OK", "FALSE" );
if( nOrder != 0 )
papszOptions = CSLSetNameValue( papszOptions, "MAX_GCP_ORDER",
CPLString().Printf("%d",nOrder) );
pRet = GDALCreateGenImgProjTransformer2( hSrcDS, hDstDS, papszOptions );
CSLDestroy( papszOptions );
return pRet;
}
/************************************************************************/
/* InsertCenterLong() */
/* */
/* Insert a CENTER_LONG Extension entry on a GEOGCS to indicate */
/* the center longitude of the dataset for wrapping purposes. */
/************************************************************************/
static CPLString InsertCenterLong( GDALDatasetH hDS, CPLString osWKT )
{
if( !EQUALN(osWKT.c_str(), "GEOGCS[", 7) )
return osWKT;
if( strstr(osWKT,"EXTENSION[\"CENTER_LONG") != NULL )
return osWKT;
/* -------------------------------------------------------------------- */
/* For now we only do this if we have a geotransform since */
/* other forms require a bunch of extra work. */
/* -------------------------------------------------------------------- */
double adfGeoTransform[6];
if( GDALGetGeoTransform( hDS, adfGeoTransform ) != CE_None )
return osWKT;
/* -------------------------------------------------------------------- */
/* Compute min/max longitude based on testing the four corners. */
/* -------------------------------------------------------------------- */
double dfMinLong, dfMaxLong;
int nXSize = GDALGetRasterXSize( hDS );
int nYSize = GDALGetRasterYSize( hDS );
dfMinLong =
MIN(MIN(adfGeoTransform[0] + 0 * adfGeoTransform[1]
+ 0 * adfGeoTransform[2],
adfGeoTransform[0] + nXSize * adfGeoTransform[1]
+ 0 * adfGeoTransform[2]),
MIN(adfGeoTransform[0] + 0 * adfGeoTransform[1]
+ nYSize * adfGeoTransform[2],
adfGeoTransform[0] + nXSize * adfGeoTransform[1]
+ nYSize * adfGeoTransform[2]));
dfMaxLong =
MAX(MAX(adfGeoTransform[0] + 0 * adfGeoTransform[1]
+ 0 * adfGeoTransform[2],
adfGeoTransform[0] + nXSize * adfGeoTransform[1]
+ 0 * adfGeoTransform[2]),
MAX(adfGeoTransform[0] + 0 * adfGeoTransform[1]
+ nYSize * adfGeoTransform[2],
adfGeoTransform[0] + nXSize * adfGeoTransform[1]
+ nYSize * adfGeoTransform[2]));
if( dfMaxLong - dfMinLong > 360.0 )
return osWKT;
/* -------------------------------------------------------------------- */
/* Insert center long. */
/* -------------------------------------------------------------------- */
OGRSpatialReference oSRS( osWKT );
double dfCenterLong = (dfMaxLong + dfMinLong) / 2.0;
OGR_SRSNode *poExt;
poExt = new OGR_SRSNode( "EXTENSION" );
poExt->AddChild( new OGR_SRSNode( "CENTER_LONG" ) );
poExt->AddChild( new OGR_SRSNode( CPLString().Printf("%g",dfCenterLong) ));
oSRS.GetRoot()->AddChild( poExt->Clone() );
delete poExt;
/* -------------------------------------------------------------------- */
/* Convert back to wkt. */
/* -------------------------------------------------------------------- */
char *pszWKT = NULL;
oSRS.exportToWkt( &pszWKT );
osWKT = pszWKT;
CPLFree( pszWKT );
return osWKT;
}
/************************************************************************/
/* GDALCreateGenImgProjTransformer2() */
/************************************************************************/
/**
* Create image to image transformer.
*
* This function creates a transformation object that maps from pixel/line
* coordinates on one image to pixel/line coordinates on another image. The
* images may potentially be georeferenced in different coordinate systems,
* and may used GCPs to map between their pixel/line coordinates and
* georeferenced coordinates (as opposed to the default assumption that their
* geotransform should be used).
*
* This transformer potentially performs three concatenated transformations.
*
* The first stage is from source image pixel/line coordinates to source
* image georeferenced coordinates, and may be done using the geotransform,
* or if not defined using a polynomial model derived from GCPs. If GCPs
* are used this stage is accomplished using GDALGCPTransform().
*
* The second stage is to change projections from the source coordinate system
* to the destination coordinate system, assuming they differ. This is
* accomplished internally using GDALReprojectionTransform().
*
* The third stage is converting from destination image georeferenced
* coordinates to destination image coordinates. This is done using the
* destination image geotransform, or if not available, using a polynomial
* model derived from GCPs. If GCPs are used this stage is accomplished using
* GDALGCPTransform(). This stage is skipped if hDstDS is NULL when the
* transformation is created.
*
* Supported Options:
* <ul>
* <li> SRC_SRS: WKT SRS to be used as an override for hSrcDS.
* <li> DST_SRS: WKT SRS to be used as an override for hDstDS.
* <li> GCPS_OK: If false, GCPs will not be used, default is TRUE.
* <li> REFINE_MINIMUM_GCPS: The minimum amount of GCPs that should be available after the refinement.
* <li> REFINE_TOLERANCE: The tolernace that specifies when a GCP will be eliminated.
* <li> MAX_GCP_ORDER: the maximum order to use for GCP derived polynomials if
* possible. The default is to autoselect based on the number of GCPs.
* A value of -1 triggers use of Thin Plate Spline instead of polynomials.
* <li> METHOD: may have a value which is one of GEOTRANSFORM, GCP_POLYNOMIAL,
* GCP_TPS, GEOLOC_ARRAY, RPC to force only one geolocation method to be
* considered on the source dataset.
* <li> RPC_HEIGHT: A fixed height to be used with RPC calculations.
* <li> RPC_DEM: The name of a DEM file to be used with RPC calculations.
* <li> INSERT_CENTER_LONG: May be set to FALSE to disable setting up a
* CENTER_LONG value on the coordinate system to rewrap things around the
* center of the image.
* </ul>
*
* @param hSrcDS source dataset, or NULL.
* @param hDstDS destination dataset (or NULL).
* @param papszOptions NULL-terminated list of string options (or NULL).
*
* @return handle suitable for use GDALGenImgProjTransform(), and to be
* deallocated with GDALDestroyGenImgProjTransformer() or NULL on failure.
*/
void *
GDALCreateGenImgProjTransformer2( GDALDatasetH hSrcDS, GDALDatasetH hDstDS,
char **papszOptions )
{
GDALGenImgProjTransformInfo *psInfo;
char **papszMD;
GDALRPCInfo sRPCInfo;
const char *pszMethod = CSLFetchNameValue( papszOptions, "METHOD" );
const char *pszValue;
int nOrder = 0, bGCPUseOK = TRUE, nMinimumGcps = -1, bRefine = FALSE;
double dfTolerance = 0.0;
const char *pszSrcWKT = CSLFetchNameValue( papszOptions, "SRC_SRS" );
const char *pszDstWKT = CSLFetchNameValue( papszOptions, "DST_SRS" );
pszValue = CSLFetchNameValue( papszOptions, "MAX_GCP_ORDER" );
if( pszValue )
nOrder = atoi(pszValue);
pszValue = CSLFetchNameValue( papszOptions, "GCPS_OK" );
if( pszValue )
bGCPUseOK = CSLTestBoolean(pszValue);
pszValue = CSLFetchNameValue( papszOptions, "REFINE_MINIMUM_GCPS" );
if( pszValue )
{
if( atoi(pszValue) != -1)
nMinimumGcps = atoi(pszValue);
}
pszValue = CSLFetchNameValue( papszOptions, "REFINE_TOLERANCE" );
if( pszValue )
{
dfTolerance = atof(pszValue);
bRefine = TRUE;
}
/* -------------------------------------------------------------------- */
/* Initialize the transform info. */
/* -------------------------------------------------------------------- */
psInfo = (GDALGenImgProjTransformInfo *)
CPLCalloc(sizeof(GDALGenImgProjTransformInfo),1);
strcpy( psInfo->sTI.szSignature, "GTI" );
psInfo->sTI.pszClassName = "GDALGenImgProjTransformer";
psInfo->sTI.pfnTransform = GDALGenImgProjTransform;
psInfo->sTI.pfnCleanup = GDALDestroyGenImgProjTransformer;
psInfo->sTI.pfnSerialize = GDALSerializeGenImgProjTransformer;
/* -------------------------------------------------------------------- */
/* Get forward and inverse geotransform for the source image. */
/* -------------------------------------------------------------------- */
if( hSrcDS == NULL )
{
psInfo->adfSrcGeoTransform[0] = 0.0;
psInfo->adfSrcGeoTransform[1] = 1.0;
psInfo->adfSrcGeoTransform[2] = 0.0;
psInfo->adfSrcGeoTransform[3] = 0.0;
psInfo->adfSrcGeoTransform[4] = 0.0;
psInfo->adfSrcGeoTransform[5] = 1.0;
memcpy( psInfo->adfSrcInvGeoTransform, psInfo->adfSrcGeoTransform,
sizeof(double) * 6 );
}
else if( (pszMethod == NULL || EQUAL(pszMethod,"GEOTRANSFORM"))
&& GDALGetGeoTransform( hSrcDS, psInfo->adfSrcGeoTransform )
== CE_None
&& (psInfo->adfSrcGeoTransform[0] != 0.0
|| psInfo->adfSrcGeoTransform[1] != 1.0
|| psInfo->adfSrcGeoTransform[2] != 0.0
|| psInfo->adfSrcGeoTransform[3] != 0.0
|| psInfo->adfSrcGeoTransform[4] != 0.0
|| ABS(psInfo->adfSrcGeoTransform[5]) != 1.0) )
{
if( !GDALInvGeoTransform( psInfo->adfSrcGeoTransform,
psInfo->adfSrcInvGeoTransform ) )
{
CPLError(CE_Failure, CPLE_AppDefined, "Cannot invert geotransform");
GDALDestroyGenImgProjTransformer( psInfo );
return NULL;
}
if( pszSrcWKT == NULL )
pszSrcWKT = GDALGetProjectionRef( hSrcDS );
}
else if( bGCPUseOK
&& (pszMethod == NULL || EQUAL(pszMethod,"GCP_POLYNOMIAL") )
&& GDALGetGCPCount( hSrcDS ) > 0 && nOrder >= 0 )
{
if(bRefine)
{
psInfo->pSrcGCPTransformArg =
GDALCreateGCPRefineTransformer( GDALGetGCPCount( hSrcDS ),
GDALGetGCPs( hSrcDS ), nOrder,
FALSE, dfTolerance, nMinimumGcps );
}
else
{
psInfo->pSrcGCPTransformArg =
GDALCreateGCPTransformer( GDALGetGCPCount( hSrcDS ),
GDALGetGCPs( hSrcDS ), nOrder,
FALSE );
}
if( psInfo->pSrcGCPTransformArg == NULL )
{
GDALDestroyGenImgProjTransformer( psInfo );
return NULL;
}
if( pszSrcWKT == NULL )
pszSrcWKT = GDALGetGCPProjection( hSrcDS );
}
else if( bGCPUseOK
&& GDALGetGCPCount( hSrcDS ) > 0
&& nOrder <= 0
&& (pszMethod == NULL || EQUAL(pszMethod,"GCP_TPS")) )
{
psInfo->pSrcTPSTransformArg =
GDALCreateTPSTransformer( GDALGetGCPCount( hSrcDS ),
GDALGetGCPs( hSrcDS ), FALSE );
if( psInfo->pSrcTPSTransformArg == NULL )
{
GDALDestroyGenImgProjTransformer( psInfo );
return NULL;
}
if( pszSrcWKT == NULL )
pszSrcWKT = GDALGetGCPProjection( hSrcDS );
}
else if( (pszMethod == NULL || EQUAL(pszMethod,"RPC"))
&& (papszMD = GDALGetMetadata( hSrcDS, "RPC" )) != NULL
&& GDALExtractRPCInfo( papszMD, &sRPCInfo ) )
{
psInfo->pSrcRPCTransformArg =
GDALCreateRPCTransformer( &sRPCInfo, FALSE, 0.1, papszOptions );
if( psInfo->pSrcRPCTransformArg == NULL )
{
GDALDestroyGenImgProjTransformer( psInfo );
return NULL;
}
if( pszSrcWKT == NULL )
pszSrcWKT = SRS_WKT_WGS84;
}
else if( (pszMethod == NULL || EQUAL(pszMethod,"GEOLOC_ARRAY"))
&& (papszMD = GDALGetMetadata( hSrcDS, "GEOLOCATION" )) != NULL )
{
psInfo->pSrcGeoLocTransformArg =
GDALCreateGeoLocTransformer( hSrcDS, papszMD, FALSE );
if( psInfo->pSrcGeoLocTransformArg == NULL )
{
GDALDestroyGenImgProjTransformer( psInfo );
return NULL;
}
if( pszSrcWKT == NULL )
pszSrcWKT = CSLFetchNameValue( papszMD, "SRS" );
}
else if( pszMethod != NULL )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Unable to compute a %s based transformation between pixel/line\n"
"and georeferenced coordinates for %s.\n",
pszMethod, GDALGetDescription( hSrcDS ) );
GDALDestroyGenImgProjTransformer( psInfo );
return NULL;
}
else
{
CPLError( CE_Failure, CPLE_AppDefined,
"Unable to compute a transformation between pixel/line\n"
"and georeferenced coordinates for %s.\n"
"There is no affine transformation and no GCPs.",
GDALGetDescription( hSrcDS ) );
GDALDestroyGenImgProjTransformer( psInfo );
return NULL;
}
/* -------------------------------------------------------------------- */
/* Setup reprojection. */
/* -------------------------------------------------------------------- */
if( pszDstWKT == NULL && hDstDS != NULL )
pszDstWKT = GDALGetProjectionRef( hDstDS );
if( pszSrcWKT != NULL && strlen(pszSrcWKT) > 0
&& pszDstWKT != NULL && strlen(pszDstWKT) > 0
&& !EQUAL(pszSrcWKT,pszDstWKT) )
{
CPLString osSrcWKT = pszSrcWKT;
if (hSrcDS
&& CSLFetchBoolean( papszOptions, "INSERT_CENTER_LONG", TRUE ) )
osSrcWKT = InsertCenterLong( hSrcDS, osSrcWKT );
psInfo->pReprojectArg =
GDALCreateReprojectionTransformer( osSrcWKT.c_str(), pszDstWKT );
if( psInfo->pReprojectArg == NULL )
{
GDALDestroyGenImgProjTransformer( psInfo );
return NULL;
}
}
/* -------------------------------------------------------------------- */
/* Get forward and inverse geotransform for destination image. */
/* If we have no destination use a unit transform. */
/* -------------------------------------------------------------------- */
if( hDstDS )
{
GDALGetGeoTransform( hDstDS, psInfo->adfDstGeoTransform );
if( !GDALInvGeoTransform( psInfo->adfDstGeoTransform,
psInfo->adfDstInvGeoTransform ) )
{
CPLError(CE_Failure, CPLE_AppDefined, "Cannot invert geotransform");
GDALDestroyGenImgProjTransformer( psInfo );
return NULL;
}
}
else
{
psInfo->adfDstGeoTransform[0] = 0.0;
psInfo->adfDstGeoTransform[1] = 1.0;
psInfo->adfDstGeoTransform[2] = 0.0;
psInfo->adfDstGeoTransform[3] = 0.0;
psInfo->adfDstGeoTransform[4] = 0.0;
psInfo->adfDstGeoTransform[5] = 1.0;
memcpy( psInfo->adfDstInvGeoTransform, psInfo->adfDstGeoTransform,
sizeof(double) * 6 );
}
return psInfo;
}
/************************************************************************/
/* GDALRefreshGenImgProjTransformer() */
/************************************************************************/
void GDALRefreshGenImgProjTransformer(void* hTransformArg)
{
GDALGenImgProjTransformInfo *psInfo =
static_cast<GDALGenImgProjTransformInfo *>( hTransformArg );
if (psInfo->pReprojectArg)
{
CPLXMLNode* psXML = GDALSerializeReprojectionTransformer(psInfo->pReprojectArg);
GDALDestroyReprojectionTransformer(psInfo->pReprojectArg);
psInfo->pReprojectArg = GDALDeserializeReprojectionTransformer(psXML);
CPLDestroyXMLNode(psXML);
}
}
/************************************************************************/
/* GDALCreateGenImgProjTransformer3() */
/************************************************************************/
/**
* Create image to image transformer.
*
* This function creates a transformation object that maps from pixel/line
* coordinates on one image to pixel/line coordinates on another image. The
* images may potentially be georeferenced in different coordinate systems,
* and may used GCPs to map between their pixel/line coordinates and
* georeferenced coordinates (as opposed to the default assumption that their
* geotransform should be used).
*
* This transformer potentially performs three concatenated transformations.
*
* The first stage is from source image pixel/line coordinates to source
* image georeferenced coordinates, and may be done using the geotransform,
* or if not defined using a polynomial model derived from GCPs. If GCPs
* are used this stage is accomplished using GDALGCPTransform().
*
* The second stage is to change projections from the source coordinate system
* to the destination coordinate system, assuming they differ. This is
* accomplished internally using GDALReprojectionTransform().
*
* The third stage is converting from destination image georeferenced
* coordinates to destination image coordinates. This is done using the
* destination image geotransform, or if not available, using a polynomial
* model derived from GCPs. If GCPs are used this stage is accomplished using
* GDALGCPTransform(). This stage is skipped if hDstDS is NULL when the
* transformation is created.
*
* @param pszSrcWKT source WKT (or NULL).
* @param padfSrcGeoTransform source geotransform (or NULL).
* @param pszDstWKT destination WKT (or NULL).
* @param padfDstGeoTransform destination geotransform (or NULL).
*
* @return handle suitable for use GDALGenImgProjTransform(), and to be
* deallocated with GDALDestroyGenImgProjTransformer() or NULL on failure.
*/
void *
GDALCreateGenImgProjTransformer3( const char *pszSrcWKT,
const double *padfSrcGeoTransform,
const char *pszDstWKT,
const double *padfDstGeoTransform )
{
GDALGenImgProjTransformInfo *psInfo;
/* -------------------------------------------------------------------- */
/* Initialize the transform info. */
/* -------------------------------------------------------------------- */
psInfo = (GDALGenImgProjTransformInfo *)
CPLCalloc(sizeof(GDALGenImgProjTransformInfo),1);
strcpy( psInfo->sTI.szSignature, "GTI" );
psInfo->sTI.pszClassName = "GDALGenImgProjTransformer";
psInfo->sTI.pfnTransform = GDALGenImgProjTransform;
psInfo->sTI.pfnCleanup = GDALDestroyGenImgProjTransformer;
psInfo->sTI.pfnSerialize = GDALSerializeGenImgProjTransformer;
/* -------------------------------------------------------------------- */
/* Get forward and inverse geotransform for the source image. */
/* -------------------------------------------------------------------- */
if( padfSrcGeoTransform )
{
memcpy( psInfo->adfSrcGeoTransform, padfSrcGeoTransform,
sizeof(psInfo->adfSrcGeoTransform) );
if( !GDALInvGeoTransform( psInfo->adfSrcGeoTransform,
psInfo->adfSrcInvGeoTransform ) )
{
CPLError(CE_Failure, CPLE_AppDefined, "Cannot invert geotransform");
GDALDestroyGenImgProjTransformer( psInfo );
return NULL;
}
}
else
{
psInfo->adfSrcGeoTransform[0] = 0.0;
psInfo->adfSrcGeoTransform[1] = 1.0;
psInfo->adfSrcGeoTransform[2] = 0.0;
psInfo->adfSrcGeoTransform[3] = 0.0;
psInfo->adfSrcGeoTransform[4] = 0.0;
psInfo->adfSrcGeoTransform[5] = 1.0;
memcpy( psInfo->adfSrcInvGeoTransform, psInfo->adfSrcGeoTransform,
sizeof(double) * 6 );
}
/* -------------------------------------------------------------------- */
/* Setup reprojection. */
/* -------------------------------------------------------------------- */
if( pszSrcWKT != NULL && strlen(pszSrcWKT) > 0
&& pszDstWKT != NULL && strlen(pszDstWKT) > 0
&& !EQUAL(pszSrcWKT, pszDstWKT) )
{
psInfo->pReprojectArg =
GDALCreateReprojectionTransformer( pszSrcWKT, pszDstWKT );
if( psInfo->pReprojectArg == NULL )
{
GDALDestroyGenImgProjTransformer( psInfo );
return NULL;
}
}
/* -------------------------------------------------------------------- */
/* Get forward and inverse geotransform for destination image. */
/* If we have no destination matrix use a unit transform. */
/* -------------------------------------------------------------------- */
if( padfDstGeoTransform )
{
memcpy( psInfo->adfDstGeoTransform, padfDstGeoTransform,
sizeof(psInfo->adfDstGeoTransform) );
if( !GDALInvGeoTransform( psInfo->adfDstGeoTransform,
psInfo->adfDstInvGeoTransform ) )
{
CPLError(CE_Failure, CPLE_AppDefined, "Cannot invert geotransform");
GDALDestroyGenImgProjTransformer( psInfo );
return NULL;
}
}
else
{
psInfo->adfDstGeoTransform[0] = 0.0;
psInfo->adfDstGeoTransform[1] = 1.0;
psInfo->adfDstGeoTransform[2] = 0.0;
psInfo->adfDstGeoTransform[3] = 0.0;
psInfo->adfDstGeoTransform[4] = 0.0;
psInfo->adfDstGeoTransform[5] = 1.0;
memcpy( psInfo->adfDstInvGeoTransform, psInfo->adfDstGeoTransform,
sizeof(double) * 6 );
}
return psInfo;
}
/************************************************************************/
/* GDALSetGenImgProjTransformerDstGeoTransform() */
/************************************************************************/
/**
* Set GenImgProj output geotransform.
*
* Normally the "destination geotransform", or transformation between
* georeferenced output coordinates and pixel/line coordinates on the
* destination file is extracted from the destination file by
* GDALCreateGenImgProjTransformer() and stored in the GenImgProj private
* info. However, sometimes it is inconvenient to have an output file
* handle with appropriate geotransform information when creating the
* transformation. For these cases, this function can be used to apply
* the destination geotransform.
*
* @param hTransformArg the handle to update.
* @param padfGeoTransform the destination geotransform to apply (six doubles).
*/
void GDALSetGenImgProjTransformerDstGeoTransform(
void *hTransformArg, const double *padfGeoTransform )
{
VALIDATE_POINTER0( hTransformArg, "GDALSetGenImgProjTransformerDstGeoTransform" );
GDALGenImgProjTransformInfo *psInfo =
static_cast<GDALGenImgProjTransformInfo *>( hTransformArg );
memcpy( psInfo->adfDstGeoTransform, padfGeoTransform, sizeof(double) * 6 );
if( !GDALInvGeoTransform( psInfo->adfDstGeoTransform,
psInfo->adfDstInvGeoTransform ) )
{
CPLError(CE_Failure, CPLE_AppDefined, "Cannot invert geotransform");
}
}
/************************************************************************/
/* GDALDestroyGenImgProjTransformer() */
/************************************************************************/
/**
* GenImgProjTransformer deallocator.
*
* This function is used to deallocate the handle created with
* GDALCreateGenImgProjTransformer().
*
* @param hTransformArg the handle to deallocate.
*/
void GDALDestroyGenImgProjTransformer( void *hTransformArg )
{
VALIDATE_POINTER0( hTransformArg, "GDALDestroyGenImgProjTransformer" );
GDALGenImgProjTransformInfo *psInfo =
(GDALGenImgProjTransformInfo *) hTransformArg;
if( psInfo->pSrcGCPTransformArg != NULL )
GDALDestroyGCPTransformer( psInfo->pSrcGCPTransformArg );
if( psInfo->pSrcTPSTransformArg != NULL )
GDALDestroyTPSTransformer( psInfo->pSrcTPSTransformArg );
if( psInfo->pSrcRPCTransformArg != NULL )
GDALDestroyRPCTransformer( psInfo->pSrcRPCTransformArg );
if( psInfo->pSrcGeoLocTransformArg != NULL )
GDALDestroyGeoLocTransformer( psInfo->pSrcGeoLocTransformArg );
if( psInfo->pDstGCPTransformArg != NULL )
GDALDestroyGCPTransformer( psInfo->pDstGCPTransformArg );
if( psInfo->pReprojectArg != NULL )
GDALDestroyReprojectionTransformer( psInfo->pReprojectArg );
CPLFree( psInfo );
}
/************************************************************************/
/* GDALGenImgProjTransform() */
/************************************************************************/
/**
* Perform general image reprojection transformation.
*
* Actually performs the transformation setup in
* GDALCreateGenImgProjTransformer(). This function matches the signature
* required by the GDALTransformerFunc(), and more details on the arguments
* can be found in that topic.
*/
int GDALGenImgProjTransform( void *pTransformArg, int bDstToSrc,
int nPointCount,
double *padfX, double *padfY, double *padfZ,
int *panSuccess )
{
GDALGenImgProjTransformInfo *psInfo =
(GDALGenImgProjTransformInfo *) pTransformArg;
int i;
double *padfGeoTransform;
void *pGCPTransformArg;
void *pRPCTransformArg;
void *pTPSTransformArg;
void *pGeoLocTransformArg;
/* -------------------------------------------------------------------- */
/* Convert from src (dst) pixel/line to src (dst) */
/* georeferenced coordinates. */
/* -------------------------------------------------------------------- */
if( bDstToSrc )
{
padfGeoTransform = psInfo->adfDstGeoTransform;
pGCPTransformArg = psInfo->pDstGCPTransformArg;
pRPCTransformArg = NULL;
pTPSTransformArg = NULL;
pGeoLocTransformArg = NULL;
}
else
{
padfGeoTransform = psInfo->adfSrcGeoTransform;
pGCPTransformArg = psInfo->pSrcGCPTransformArg;
pRPCTransformArg = psInfo->pSrcRPCTransformArg;
pTPSTransformArg = psInfo->pSrcTPSTransformArg;
pGeoLocTransformArg = psInfo->pSrcGeoLocTransformArg;
}
if( pGCPTransformArg != NULL )
{
if( !GDALGCPTransform( pGCPTransformArg, FALSE,
nPointCount, padfX, padfY, padfZ,
panSuccess ) )
return FALSE;
}
else if( pTPSTransformArg != NULL )
{
if( !GDALTPSTransform( pTPSTransformArg, FALSE,
nPointCount, padfX, padfY, padfZ,
panSuccess ) )
return FALSE;
}
else if( pRPCTransformArg != NULL )
{
if( !GDALRPCTransform( pRPCTransformArg, FALSE,
nPointCount, padfX, padfY, padfZ,
panSuccess ) )
return FALSE;
}
else if( pGeoLocTransformArg != NULL )
{
if( !GDALGeoLocTransform( pGeoLocTransformArg, FALSE,
nPointCount, padfX, padfY, padfZ,
panSuccess ) )
return FALSE;
}
else
{
for( i = 0; i < nPointCount; i++ )
{
double dfNewX, dfNewY;
if( padfX[i] == HUGE_VAL || padfY[i] == HUGE_VAL )
{
panSuccess[i] = FALSE;
continue;
}
dfNewX = padfGeoTransform[0]
+ padfX[i] * padfGeoTransform[1]
+ padfY[i] * padfGeoTransform[2];
dfNewY = padfGeoTransform[3]
+ padfX[i] * padfGeoTransform[4]
+ padfY[i] * padfGeoTransform[5];
padfX[i] = dfNewX;
padfY[i] = dfNewY;
}
}
/* -------------------------------------------------------------------- */
/* Reproject if needed. */
/* -------------------------------------------------------------------- */
if( psInfo->pReprojectArg )
{
if( !GDALReprojectionTransform( psInfo->pReprojectArg, bDstToSrc,
nPointCount, padfX, padfY, padfZ,
panSuccess ) )
return FALSE;
}
else
{
for( i = 0; i < nPointCount; i++ )
panSuccess[i] = 1;
}
/* -------------------------------------------------------------------- */
/* Convert dst (src) georef coordinates back to pixel/line. */
/* -------------------------------------------------------------------- */
if( bDstToSrc )
{
padfGeoTransform = psInfo->adfSrcInvGeoTransform;
pGCPTransformArg = psInfo->pSrcGCPTransformArg;
pRPCTransformArg = psInfo->pSrcRPCTransformArg;
pTPSTransformArg = psInfo->pSrcTPSTransformArg;
pGeoLocTransformArg = psInfo->pSrcGeoLocTransformArg;
}
else
{
padfGeoTransform = psInfo->adfDstInvGeoTransform;
pGCPTransformArg = psInfo->pDstGCPTransformArg;
pRPCTransformArg = NULL;
pTPSTransformArg = NULL;
pGeoLocTransformArg = NULL;
}
if( pGCPTransformArg != NULL )
{
if( !GDALGCPTransform( pGCPTransformArg, TRUE,
nPointCount, padfX, padfY, padfZ,
panSuccess ) )
return FALSE;
}
else if( pTPSTransformArg != NULL )
{
if( !GDALTPSTransform( pTPSTransformArg, TRUE,
nPointCount, padfX, padfY, padfZ,
panSuccess ) )
return FALSE;
}
else if( pRPCTransformArg != NULL )
{
if( !GDALRPCTransform( pRPCTransformArg, TRUE,
nPointCount, padfX, padfY, padfZ,
panSuccess ) )
return FALSE;
}
else if( pGeoLocTransformArg != NULL )
{
if( !GDALGeoLocTransform( pGeoLocTransformArg, TRUE,
nPointCount, padfX, padfY, padfZ,
panSuccess ) )
return FALSE;
}
else
{
for( i = 0; i < nPointCount; i++ )
{
double dfNewX, dfNewY;
if( !panSuccess[i] )
continue;
dfNewX = padfGeoTransform[0]
+ padfX[i] * padfGeoTransform[1]
+ padfY[i] * padfGeoTransform[2];
dfNewY = padfGeoTransform[3]
+ padfX[i] * padfGeoTransform[4]
+ padfY[i] * padfGeoTransform[5];
padfX[i] = dfNewX;
padfY[i] = dfNewY;
}
}
return TRUE;
}
/************************************************************************/
/* GDALSerializeGenImgProjTransformer() */
/************************************************************************/
static CPLXMLNode *
GDALSerializeGenImgProjTransformer( void *pTransformArg )
{
char szWork[200];
CPLXMLNode *psTree;
GDALGenImgProjTransformInfo *psInfo =
(GDALGenImgProjTransformInfo *) pTransformArg;
psTree = CPLCreateXMLNode( NULL, CXT_Element, "GenImgProjTransformer" );
/* -------------------------------------------------------------------- */
/* Handle GCP transformation. */
/* -------------------------------------------------------------------- */
if( psInfo->pSrcGCPTransformArg != NULL )
{
CPLXMLNode *psTransformerContainer;
CPLXMLNode *psTransformer;
psTransformerContainer =
CPLCreateXMLNode( psTree, CXT_Element, "SrcGCPTransformer" );
psTransformer = GDALSerializeTransformer( GDALGCPTransform,
psInfo->pSrcGCPTransformArg);
if( psTransformer != NULL )
CPLAddXMLChild( psTransformerContainer, psTransformer );
}
/* -------------------------------------------------------------------- */
/* Handle TPS transformation. */
/* -------------------------------------------------------------------- */
else if( psInfo->pSrcTPSTransformArg != NULL )
{
CPLXMLNode *psTransformerContainer;
CPLXMLNode *psTransformer;
psTransformerContainer =
CPLCreateXMLNode( psTree, CXT_Element, "SrcTPSTransformer" );
psTransformer =
GDALSerializeTransformer( NULL, psInfo->pSrcTPSTransformArg);
if( psTransformer != NULL )
CPLAddXMLChild( psTransformerContainer, psTransformer );
}
/* -------------------------------------------------------------------- */
/* Handle GeoLoc transformation. */
/* -------------------------------------------------------------------- */
else if( psInfo->pSrcGeoLocTransformArg != NULL )
{
CPLXMLNode *psTransformerContainer;
CPLXMLNode *psTransformer;
psTransformerContainer =
CPLCreateXMLNode( psTree, CXT_Element, "SrcGeoLocTransformer" );
psTransformer =
GDALSerializeTransformer( NULL, psInfo->pSrcGeoLocTransformArg);
if( psTransformer != NULL )
CPLAddXMLChild( psTransformerContainer, psTransformer );
}
/* -------------------------------------------------------------------- */
/* Handle RPC transformation. */
/* -------------------------------------------------------------------- */
else if( psInfo->pSrcRPCTransformArg != NULL )
{
CPLXMLNode *psTransformerContainer;
CPLXMLNode *psTransformer;
psTransformerContainer =
CPLCreateXMLNode( psTree, CXT_Element, "SrcRPCTransformer" );
psTransformer =
GDALSerializeTransformer( NULL, psInfo->pSrcRPCTransformArg);
if( psTransformer != NULL )
CPLAddXMLChild( psTransformerContainer, psTransformer );
}
/* -------------------------------------------------------------------- */
/* Handle source geotransforms. */
/* -------------------------------------------------------------------- */
else
{
sprintf( szWork, "%.18g,%.18g,%.18g,%.18g,%.18g,%.18g",
psInfo->adfSrcGeoTransform[0],
psInfo->adfSrcGeoTransform[1],
psInfo->adfSrcGeoTransform[2],
psInfo->adfSrcGeoTransform[3],
psInfo->adfSrcGeoTransform[4],
psInfo->adfSrcGeoTransform[5] );
CPLCreateXMLElementAndValue( psTree, "SrcGeoTransform", szWork );
sprintf( szWork, "%.18g,%.18g,%.18g,%.18g,%.18g,%.18g",
psInfo->adfSrcInvGeoTransform[0],
psInfo->adfSrcInvGeoTransform[1],
psInfo->adfSrcInvGeoTransform[2],
psInfo->adfSrcInvGeoTransform[3],
psInfo->adfSrcInvGeoTransform[4],
psInfo->adfSrcInvGeoTransform[5] );
CPLCreateXMLElementAndValue( psTree, "SrcInvGeoTransform", szWork );
}
/* -------------------------------------------------------------------- */
/* Handle destination geotransforms. */
/* -------------------------------------------------------------------- */
sprintf( szWork, "%.18g,%.18g,%.18g,%.18g,%.18g,%.18g",
psInfo->adfDstGeoTransform[0],
psInfo->adfDstGeoTransform[1],
psInfo->adfDstGeoTransform[2],
psInfo->adfDstGeoTransform[3],
psInfo->adfDstGeoTransform[4],
psInfo->adfDstGeoTransform[5] );
CPLCreateXMLElementAndValue( psTree, "DstGeoTransform", szWork );
sprintf( szWork, "%.18g,%.18g,%.18g,%.18g,%.18g,%.18g",
psInfo->adfDstInvGeoTransform[0],
psInfo->adfDstInvGeoTransform[1],
psInfo->adfDstInvGeoTransform[2],
psInfo->adfDstInvGeoTransform[3],
psInfo->adfDstInvGeoTransform[4],
psInfo->adfDstInvGeoTransform[5] );
CPLCreateXMLElementAndValue( psTree, "DstInvGeoTransform", szWork );
/* -------------------------------------------------------------------- */
/* Do we have a reprojection transformer? */
/* -------------------------------------------------------------------- */
if( psInfo->pReprojectArg != NULL )
{
CPLXMLNode *psTransformerContainer;
CPLXMLNode *psTransformer;
psTransformerContainer =
CPLCreateXMLNode( psTree, CXT_Element, "ReprojectTransformer" );
psTransformer = GDALSerializeTransformer( GDALReprojectionTransform,
psInfo->pReprojectArg );
if( psTransformer != NULL )
CPLAddXMLChild( psTransformerContainer, psTransformer );
}
return psTree;
}
/************************************************************************/
/* GDALDeserializeGenImgProjTransformer() */
/************************************************************************/
void *GDALDeserializeGenImgProjTransformer( CPLXMLNode *psTree )
{
GDALGenImgProjTransformInfo *psInfo;
CPLXMLNode *psSubtree;
/* -------------------------------------------------------------------- */
/* Initialize the transform info. */
/* -------------------------------------------------------------------- */
psInfo = (GDALGenImgProjTransformInfo *)
CPLCalloc(sizeof(GDALGenImgProjTransformInfo),1);
strcpy( psInfo->sTI.szSignature, "GTI" );
psInfo->sTI.pszClassName = "GDALGenImgProjTransformer";
psInfo->sTI.pfnTransform = GDALGenImgProjTransform;
psInfo->sTI.pfnCleanup = GDALDestroyGenImgProjTransformer;
psInfo->sTI.pfnSerialize = GDALSerializeGenImgProjTransformer;
/* -------------------------------------------------------------------- */
/* SrcGeotransform */
/* -------------------------------------------------------------------- */
if( CPLGetXMLNode( psTree, "SrcGeoTransform" ) != NULL )
{
sscanf( CPLGetXMLValue( psTree, "SrcGeoTransform", "" ),
"%lg,%lg,%lg,%lg,%lg,%lg",
psInfo->adfSrcGeoTransform + 0,
psInfo->adfSrcGeoTransform + 1,
psInfo->adfSrcGeoTransform + 2,
psInfo->adfSrcGeoTransform + 3,
psInfo->adfSrcGeoTransform + 4,
psInfo->adfSrcGeoTransform + 5 );
if( CPLGetXMLNode( psTree, "SrcInvGeoTransform" ) != NULL )
{
sscanf( CPLGetXMLValue( psTree, "SrcInvGeoTransform", "" ),
"%lg,%lg,%lg,%lg,%lg,%lg",
psInfo->adfSrcInvGeoTransform + 0,
psInfo->adfSrcInvGeoTransform + 1,
psInfo->adfSrcInvGeoTransform + 2,
psInfo->adfSrcInvGeoTransform + 3,
psInfo->adfSrcInvGeoTransform + 4,
psInfo->adfSrcInvGeoTransform + 5 );
}
else
{
if( !GDALInvGeoTransform( psInfo->adfSrcGeoTransform,
psInfo->adfSrcInvGeoTransform ) )
{
CPLError(CE_Failure, CPLE_AppDefined, "Cannot invert geotransform");
}
}
}
/* -------------------------------------------------------------------- */
/* Src GCP Transform */
/* -------------------------------------------------------------------- */
psSubtree = CPLGetXMLNode( psTree, "SrcGCPTransformer" );
if( psSubtree != NULL && psSubtree->psChild != NULL )
{
psInfo->pSrcGCPTransformArg =
GDALDeserializeGCPTransformer( psSubtree->psChild );
}
/* -------------------------------------------------------------------- */
/* Src TPS Transform */
/* -------------------------------------------------------------------- */
psSubtree = CPLGetXMLNode( psTree, "SrcTPSTransformer" );
if( psSubtree != NULL && psSubtree->psChild != NULL )
{
psInfo->pSrcTPSTransformArg =
GDALDeserializeTPSTransformer( psSubtree->psChild );
}
/* -------------------------------------------------------------------- */
/* Src GeoLoc Transform */
/* -------------------------------------------------------------------- */
psSubtree = CPLGetXMLNode( psTree, "SrcGeoLocTransformer" );
if( psSubtree != NULL && psSubtree->psChild != NULL )
{
psInfo->pSrcGeoLocTransformArg =
GDALDeserializeGeoLocTransformer( psSubtree->psChild );
}
/* -------------------------------------------------------------------- */
/* Src RPC Transform */
/* -------------------------------------------------------------------- */
psSubtree = CPLGetXMLNode( psTree, "SrcRPCTransformer" );
if( psSubtree != NULL && psSubtree->psChild != NULL )
{
psInfo->pSrcRPCTransformArg =
GDALDeserializeRPCTransformer( psSubtree->psChild );
}
/* -------------------------------------------------------------------- */
/* DstGeotransform */
/* -------------------------------------------------------------------- */
if( CPLGetXMLNode( psTree, "DstGeoTransform" ) != NULL )
{
sscanf( CPLGetXMLValue( psTree, "DstGeoTransform", "" ),
"%lg,%lg,%lg,%lg,%lg,%lg",
psInfo->adfDstGeoTransform + 0,
psInfo->adfDstGeoTransform + 1,
psInfo->adfDstGeoTransform + 2,
psInfo->adfDstGeoTransform + 3,
psInfo->adfDstGeoTransform + 4,
psInfo->adfDstGeoTransform + 5 );
if( CPLGetXMLNode( psTree, "DstInvGeoTransform" ) != NULL )
{
sscanf( CPLGetXMLValue( psTree, "DstInvGeoTransform", "" ),
"%lg,%lg,%lg,%lg,%lg,%lg",
psInfo->adfDstInvGeoTransform + 0,
psInfo->adfDstInvGeoTransform + 1,
psInfo->adfDstInvGeoTransform + 2,
psInfo->adfDstInvGeoTransform + 3,
psInfo->adfDstInvGeoTransform + 4,
psInfo->adfDstInvGeoTransform + 5 );
}
else
{
if( !GDALInvGeoTransform( psInfo->adfDstGeoTransform,
psInfo->adfDstInvGeoTransform ) )
{
CPLError(CE_Failure, CPLE_AppDefined, "Cannot invert geotransform");
}
}
}
/* -------------------------------------------------------------------- */
/* Reproject transformer */
/* -------------------------------------------------------------------- */
psSubtree = CPLGetXMLNode( psTree, "ReprojectTransformer" );
if( psSubtree != NULL && psSubtree->psChild != NULL )
{
psInfo->pReprojectArg =
GDALDeserializeReprojectionTransformer( psSubtree->psChild );
}
return psInfo;
}
/************************************************************************/
/* ==================================================================== */
/* GDALReprojectionTransformer */
/* ==================================================================== */
/************************************************************************/
typedef struct {
GDALTransformerInfo sTI;
OGRCoordinateTransformation *poForwardTransform;
OGRCoordinateTransformation *poReverseTransform;
} GDALReprojectionTransformInfo;
/************************************************************************/
/* GDALCreateReprojectionTransformer() */
/************************************************************************/
/**
* Create reprojection transformer.
*
* Creates a callback data structure suitable for use with
* GDALReprojectionTransformation() to represent a transformation from
* one geographic or projected coordinate system to another. On input
* the coordinate systems are described in OpenGIS WKT format.
*
* Internally the OGRCoordinateTransformation object is used to implement
* the reprojection.
*
* @param pszSrcWKT the coordinate system for the source coordinate system.
* @param pszDstWKT the coordinate system for the destination coordinate
* system.
*
* @return Handle for use with GDALReprojectionTransform(), or NULL if the
* system fails to initialize the reprojection.
**/
void *GDALCreateReprojectionTransformer( const char *pszSrcWKT,
const char *pszDstWKT )
{
OGRSpatialReference oSrcSRS, oDstSRS;
OGRCoordinateTransformation *poForwardTransform;
/* -------------------------------------------------------------------- */
/* Ingest the SRS definitions. */
/* -------------------------------------------------------------------- */
if( oSrcSRS.importFromWkt( (char **) &pszSrcWKT ) != OGRERR_NONE )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Failed to import coordinate system `%s'.",
pszSrcWKT );
return NULL;
}
if( oDstSRS.importFromWkt( (char **) &pszDstWKT ) != OGRERR_NONE )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Failed to import coordinate system `%s'.",
pszSrcWKT );
return NULL;
}
/* -------------------------------------------------------------------- */
/* Build the forward coordinate transformation. */
/* -------------------------------------------------------------------- */
poForwardTransform = OGRCreateCoordinateTransformation(&oSrcSRS,&oDstSRS);
if( poForwardTransform == NULL )
// OGRCreateCoordinateTransformation() will report errors on its own.
return NULL;
/* -------------------------------------------------------------------- */
/* Create a structure to hold the transform info, and also */
/* build reverse transform. We assume that if the forward */
/* transform can be created, then so can the reverse one. */
/* -------------------------------------------------------------------- */
GDALReprojectionTransformInfo *psInfo;
psInfo = (GDALReprojectionTransformInfo *)
CPLCalloc(sizeof(GDALReprojectionTransformInfo),1);
psInfo->poForwardTransform = poForwardTransform;
psInfo->poReverseTransform =
OGRCreateCoordinateTransformation(&oDstSRS,&oSrcSRS);
strcpy( psInfo->sTI.szSignature, "GTI" );
psInfo->sTI.pszClassName = "GDALReprojectionTransformer";
psInfo->sTI.pfnTransform = GDALReprojectionTransform;
psInfo->sTI.pfnCleanup = GDALDestroyReprojectionTransformer;
psInfo->sTI.pfnSerialize = GDALSerializeReprojectionTransformer;
return psInfo;
}
/************************************************************************/
/* GDALDestroyReprojectionTransformer() */
/************************************************************************/
/**
* Destroy reprojection transformation.
*
* @param pTransformArg the transformation handle returned by
* GDALCreateReprojectionTransformer().
*/
void GDALDestroyReprojectionTransformer( void *pTransformArg )
{
VALIDATE_POINTER0( pTransformArg, "GDALDestroyReprojectionTransformer" );
GDALReprojectionTransformInfo *psInfo =
(GDALReprojectionTransformInfo *) pTransformArg;
if( psInfo->poForwardTransform )
delete psInfo->poForwardTransform;
if( psInfo->poReverseTransform )
delete psInfo->poReverseTransform;
CPLFree( psInfo );
}
/************************************************************************/
/* GDALReprojectionTransform() */
/************************************************************************/
/**
* Perform reprojection transformation.
*
* Actually performs the reprojection transformation described in
* GDALCreateReprojectionTransformer(). This function matches the
* GDALTransformerFunc() signature. Details of the arguments are described
* there.
*/
int GDALReprojectionTransform( void *pTransformArg, int bDstToSrc,
int nPointCount,
double *padfX, double *padfY, double *padfZ,
int *panSuccess )
{
GDALReprojectionTransformInfo *psInfo =
(GDALReprojectionTransformInfo *) pTransformArg;
int bSuccess;
if( bDstToSrc )
bSuccess = psInfo->poReverseTransform->TransformEx(
nPointCount, padfX, padfY, padfZ, panSuccess );
else
bSuccess = psInfo->poForwardTransform->TransformEx(
nPointCount, padfX, padfY, padfZ, panSuccess );
return bSuccess;
}
/************************************************************************/
/* GDALSerializeReprojectionTransformer() */
/************************************************************************/
static CPLXMLNode *
GDALSerializeReprojectionTransformer( void *pTransformArg )
{
CPLXMLNode *psTree;
GDALReprojectionTransformInfo *psInfo =
(GDALReprojectionTransformInfo *) pTransformArg;
psTree = CPLCreateXMLNode( NULL, CXT_Element, "ReprojectionTransformer" );
/* -------------------------------------------------------------------- */
/* Handle SourceCS. */
/* -------------------------------------------------------------------- */
OGRSpatialReference *poSRS;
char *pszWKT = NULL;
poSRS = psInfo->poForwardTransform->GetSourceCS();
poSRS->exportToWkt( &pszWKT );
CPLCreateXMLElementAndValue( psTree, "SourceSRS", pszWKT );
CPLFree( pszWKT );
/* -------------------------------------------------------------------- */
/* Handle DestinationCS. */
/* -------------------------------------------------------------------- */
poSRS = psInfo->poForwardTransform->GetTargetCS();
poSRS->exportToWkt( &pszWKT );
CPLCreateXMLElementAndValue( psTree, "TargetSRS", pszWKT );
CPLFree( pszWKT );
return psTree;
}
/************************************************************************/
/* GDALDeserializeReprojectionTransformer() */
/************************************************************************/
static void *
GDALDeserializeReprojectionTransformer( CPLXMLNode *psTree )
{
const char *pszSourceSRS = CPLGetXMLValue( psTree, "SourceSRS", NULL );
const char *pszTargetSRS= CPLGetXMLValue( psTree, "TargetSRS", NULL );
char *pszSourceWKT = NULL, *pszTargetWKT = NULL;
void *pResult = NULL;
if( pszSourceSRS != NULL )
{
OGRSpatialReference oSRS;
if( oSRS.SetFromUserInput( pszSourceSRS ) == OGRERR_NONE )
oSRS.exportToWkt( &pszSourceWKT );
}
if( pszTargetSRS != NULL )
{
OGRSpatialReference oSRS;
if( oSRS.SetFromUserInput( pszTargetSRS ) == OGRERR_NONE )
oSRS.exportToWkt( &pszTargetWKT );
}
if( pszSourceWKT != NULL && pszTargetWKT != NULL )
{
pResult = GDALCreateReprojectionTransformer( pszSourceWKT,
pszTargetWKT );
}
else
{
CPLError( CE_Failure, CPLE_AppDefined,
"ReprojectionTransformer definition missing either\n"
"SourceSRS or TargetSRS definition." );
}
CPLFree( pszSourceWKT );
CPLFree( pszTargetWKT );
return pResult;
}
/************************************************************************/
/* ==================================================================== */
/* Approximate transformer. */
/* ==================================================================== */
/************************************************************************/
typedef struct
{
GDALTransformerInfo sTI;
GDALTransformerFunc pfnBaseTransformer;
void *pBaseCBData;
double dfMaxError;
int bOwnSubtransformer;
} ApproxTransformInfo;
/************************************************************************/
/* GDALCloneApproxTransformer() */
/************************************************************************/
void* GDALCloneApproxTransformer( void *hTransformArg )
{
VALIDATE_POINTER1( hTransformArg, "GDALCloneApproxTransformer", NULL );
ApproxTransformInfo *psInfo =
(ApproxTransformInfo *) hTransformArg;
ApproxTransformInfo *psClonedInfo = (ApproxTransformInfo *)
CPLMalloc(sizeof(ApproxTransformInfo));
memcpy(psClonedInfo, psInfo, sizeof(ApproxTransformInfo));
if( psClonedInfo->pBaseCBData )
{
psClonedInfo->pBaseCBData = GDALCloneTransformer( psInfo->pBaseCBData );
if( psClonedInfo->pBaseCBData == NULL )
{
CPLFree(psClonedInfo);
return NULL;
}
}
psClonedInfo->bOwnSubtransformer = TRUE;
return psClonedInfo;
}
/************************************************************************/
/* GDALSerializeApproxTransformer() */
/************************************************************************/
static CPLXMLNode *
GDALSerializeApproxTransformer( void *pTransformArg )
{
CPLXMLNode *psTree;
ApproxTransformInfo *psInfo = (ApproxTransformInfo *) pTransformArg;
psTree = CPLCreateXMLNode( NULL, CXT_Element, "ApproxTransformer" );
/* -------------------------------------------------------------------- */
/* Attach max error. */
/* -------------------------------------------------------------------- */
CPLCreateXMLElementAndValue( psTree, "MaxError",
CPLString().Printf("%g",psInfo->dfMaxError) );
/* -------------------------------------------------------------------- */
/* Capture underlying transformer. */
/* -------------------------------------------------------------------- */
CPLXMLNode *psTransformerContainer;
CPLXMLNode *psTransformer;
psTransformerContainer =
CPLCreateXMLNode( psTree, CXT_Element, "BaseTransformer" );
psTransformer = GDALSerializeTransformer( psInfo->pfnBaseTransformer,
psInfo->pBaseCBData );
if( psTransformer != NULL )
CPLAddXMLChild( psTransformerContainer, psTransformer );
return psTree;
}
/************************************************************************/
/* GDALCreateApproxTransformer() */
/************************************************************************/
/**
* Create an approximating transformer.
*
* This function creates a context for an approximated transformer. Basically
* a high precision transformer is supplied as input and internally linear
* approximations are computed to generate results to within a defined
* precision.
*
* The approximation is actually done at the point where GDALApproxTransform()
* calls are made, and depend on the assumption that the roughly linear. The
* first and last point passed in must be the extreme values and the
* intermediate values should describe a curve between the end points. The
* approximator transforms and center using the approximate transformer, and
* then compares the true middle transformed value to a linear approximation
* based on the end points. If the error is within the supplied threshold
* then the end points are used to linearly approximate all the values
* otherwise the inputs points are split into two smaller sets, and the
* function recursively called till a sufficiently small set of points if found
* that the linear approximation is OK, or that all the points are exactly
* computed.
*
* This function is very suitable for approximating transformation results
* from output pixel/line space to input coordinates for warpers that operate
* on one input scanline at a time. Care should be taken using it in other
* circumstances as little internal validation is done, in order to keep things
* fast.
*
* @param pfnBaseTransformer the high precision transformer which should be
* approximated.
* @param pBaseTransformArg the callback argument for the high precision
* transformer.
* @param dfMaxError the maximum cartesian error in the "output" space that
* is to be accepted in the linear approximation.
*
* @return callback pointer suitable for use with GDALApproxTransform(). It
* should be deallocated with GDALDestroyApproxTransformer().
*/
void *GDALCreateApproxTransformer( GDALTransformerFunc pfnBaseTransformer,
void *pBaseTransformArg, double dfMaxError)
{
ApproxTransformInfo *psATInfo;
psATInfo = (ApproxTransformInfo*) CPLMalloc(sizeof(ApproxTransformInfo));
psATInfo->pfnBaseTransformer = pfnBaseTransformer;
psATInfo->pBaseCBData = pBaseTransformArg;
psATInfo->dfMaxError = dfMaxError;
psATInfo->bOwnSubtransformer = FALSE;
strcpy( psATInfo->sTI.szSignature, "GTI" );
psATInfo->sTI.pszClassName = "GDALApproxTransformer";
psATInfo->sTI.pfnTransform = GDALApproxTransform;
psATInfo->sTI.pfnCleanup = GDALDestroyApproxTransformer;
psATInfo->sTI.pfnSerialize = GDALSerializeApproxTransformer;
return psATInfo;
}
/************************************************************************/
/* GDALApproxTransformerOwnsSubtransformer() */
/************************************************************************/
void GDALApproxTransformerOwnsSubtransformer( void *pCBData, int bOwnFlag )
{
ApproxTransformInfo *psATInfo = (ApproxTransformInfo *) pCBData;
psATInfo->bOwnSubtransformer = bOwnFlag;
}
/************************************************************************/
/* GDALDestroyApproxTransformer() */
/************************************************************************/
/**
* Cleanup approximate transformer.
*
* Deallocates the resources allocated by GDALCreateApproxTransformer().
*
* @param pCBData callback data originally returned by
* GDALCreateApproxTransformer().
*/
void GDALDestroyApproxTransformer( void * pCBData )
{
VALIDATE_POINTER0( pCBData, "GDALDestroyApproxTransformer" );
ApproxTransformInfo *psATInfo = (ApproxTransformInfo *) pCBData;
if( psATInfo->bOwnSubtransformer )
GDALDestroyTransformer( psATInfo->pBaseCBData );
CPLFree( pCBData );
}
/************************************************************************/
/* GDALApproxTransform() */
/************************************************************************/
/**
* Perform approximate transformation.
*
* Actually performs the approximate transformation described in
* GDALCreateApproxTransformer(). This function matches the
* GDALTransformerFunc() signature. Details of the arguments are described
* there.
*/
int GDALApproxTransform( void *pCBData, int bDstToSrc, int nPoints,
double *x, double *y, double *z, int *panSuccess )
{
ApproxTransformInfo *psATInfo = (ApproxTransformInfo *) pCBData;
double x2[3], y2[3], z2[3], dfDeltaX, dfDeltaY, dfError, dfDist, dfDeltaZ;
int nMiddle, anSuccess2[3], i, bSuccess;
nMiddle = (nPoints-1)/2;
/* -------------------------------------------------------------------- */
/* Bail if our preconditions are not met, or if error is not */
/* acceptable. */
/* -------------------------------------------------------------------- */
if( y[0] != y[nPoints-1] || y[0] != y[nMiddle]
|| x[0] == x[nPoints-1] || x[0] == x[nMiddle]
|| psATInfo->dfMaxError == 0.0 || nPoints <= 5 )
{
return psATInfo->pfnBaseTransformer( psATInfo->pBaseCBData, bDstToSrc,
nPoints, x, y, z, panSuccess );
}
/* -------------------------------------------------------------------- */
/* Transform first, last and middle point. */
/* -------------------------------------------------------------------- */
x2[0] = x[0];
y2[0] = y[0];
z2[0] = z[0];
x2[1] = x[nMiddle];
y2[1] = y[nMiddle];
z2[1] = z[nMiddle];
x2[2] = x[nPoints-1];
y2[2] = y[nPoints-1];
z2[2] = z[nPoints-1];
bSuccess =
psATInfo->pfnBaseTransformer( psATInfo->pBaseCBData, bDstToSrc, 3,
x2, y2, z2, anSuccess2 );
if( !bSuccess || !anSuccess2[0] || !anSuccess2[1] || !anSuccess2[2] )
return psATInfo->pfnBaseTransformer( psATInfo->pBaseCBData, bDstToSrc,
nPoints, x, y, z, panSuccess );
/* -------------------------------------------------------------------- */
/* Is the error at the middle acceptable relative to an */
/* interpolation of the middle position? */
/* -------------------------------------------------------------------- */
dfDeltaX = (x2[2] - x2[0]) / (x[nPoints-1] - x[0]);
dfDeltaY = (y2[2] - y2[0]) / (x[nPoints-1] - x[0]);
dfDeltaZ = (z2[2] - z2[0]) / (x[nPoints-1] - x[0]);
dfError = fabs((x2[0] + dfDeltaX * (x[nMiddle] - x[0])) - x2[1])
+ fabs((y2[0] + dfDeltaY * (x[nMiddle] - x[0])) - y2[1]);
if( dfError > psATInfo->dfMaxError )
{
#ifdef notdef
CPLDebug( "GDAL", "ApproxTransformer - "
"error %g over threshold %g, subdivide %d points.",
dfError, psATInfo->dfMaxError, nPoints );
#endif
bSuccess =
GDALApproxTransform( psATInfo, bDstToSrc, nMiddle,
x, y, z, panSuccess );
if( !bSuccess )
return FALSE;
bSuccess =
GDALApproxTransform( psATInfo, bDstToSrc, nPoints - nMiddle,
x+nMiddle, y+nMiddle, z+nMiddle,
panSuccess+nMiddle );
if( !bSuccess )
return FALSE;
return TRUE;
}
/* -------------------------------------------------------------------- */
/* Error is OK since this is just used to compute output bounds */
/* of newly created file for gdalwarper. So just use affine */
/* approximation of the reverse transform. Eventually we */
/* should implement iterative searching to find a result within */
/* our error threshold. */
/* -------------------------------------------------------------------- */
for( i = nPoints-1; i >= 0; i-- )
{
dfDist = (x[i] - x[0]);
y[i] = y2[0] + dfDeltaY * dfDist;
x[i] = x2[0] + dfDeltaX * dfDist;
z[i] = z2[0] + dfDeltaZ * dfDist;
panSuccess[i] = TRUE;
}
return TRUE;
}
/************************************************************************/
/* GDALDeserializeApproxTransformer() */
/************************************************************************/
static void *
GDALDeserializeApproxTransformer( CPLXMLNode *psTree )
{
double dfMaxError = atof(CPLGetXMLValue( psTree, "MaxError", "0.25" ));
CPLXMLNode *psContainer;
GDALTransformerFunc pfnBaseTransform = NULL;
void *pBaseCBData = NULL;
psContainer = CPLGetXMLNode( psTree, "BaseTransformer" );
if( psContainer != NULL && psContainer->psChild != NULL )
{
GDALDeserializeTransformer( psContainer->psChild,
&pfnBaseTransform,
&pBaseCBData );
}
if( pfnBaseTransform == NULL )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Cannot get base transform for approx transformer." );
return NULL;
}
else
{
void *pApproxCBData = GDALCreateApproxTransformer( pfnBaseTransform,
pBaseCBData,
dfMaxError );
GDALApproxTransformerOwnsSubtransformer( pApproxCBData, TRUE );
return pApproxCBData;
}
}
/************************************************************************/
/* GDALApplyGeoTransform() */
/************************************************************************/
/**
* Apply GeoTransform to x/y coordinate.
*
* Applies the following computation, converting a (pixel,line) coordinate
* into a georeferenced (geo_x,geo_y) location.
*
* *pdfGeoX = padfGeoTransform[0] + dfPixel * padfGeoTransform[1]
* + dfLine * padfGeoTransform[2];
* *pdfGeoY = padfGeoTransform[3] + dfPixel * padfGeoTransform[4]
* + dfLine * padfGeoTransform[5];
*
* @param padfGeoTransform Six coefficient GeoTransform to apply.
* @param dfPixel Input pixel position.
* @param dfLine Input line position.
* @param pdfGeoX output location where geo_x (easting/longitude) location is placed.
* @param pdfGeoY output location where geo_y (northing/latitude) location is placed.
*/
void CPL_STDCALL GDALApplyGeoTransform( double *padfGeoTransform,
double dfPixel, double dfLine,
double *pdfGeoX, double *pdfGeoY )
{
*pdfGeoX = padfGeoTransform[0] + dfPixel * padfGeoTransform[1]
+ dfLine * padfGeoTransform[2];
*pdfGeoY = padfGeoTransform[3] + dfPixel * padfGeoTransform[4]
+ dfLine * padfGeoTransform[5];
}
/************************************************************************/
/* GDALInvGeoTransform() */
/************************************************************************/
/**
* Invert Geotransform.
*
* This function will invert a standard 3x2 set of GeoTransform coefficients.
* This converts the equation from being pixel to geo to being geo to pixel.
*
* @param gt_in Input geotransform (six doubles - unaltered).
* @param gt_out Output geotransform (six doubles - updated).
*
* @return TRUE on success or FALSE if the equation is uninvertable.
*/
int CPL_STDCALL GDALInvGeoTransform( double *gt_in, double *gt_out )
{
double det, inv_det;
/* Special case - no rotation - to avoid computing determinate */
/* and potential precision issues. */
if( gt_in[2] == 0.0 && gt_in[4] == 0.0 &&
gt_in[1] != 0.0 && gt_in[5] != 0.0 )
{
/*X = gt_in[0] + x * gt_in[1]
Y = gt_in[3] + y * gt_in[5]
-->
x = -gt_in[0] / gt_in[1] + (1 / gt_in[1]) * X
y = -gt_in[3] / gt_in[5] + (1 / gt_in[5]) * Y
*/
gt_out[0] = -gt_in[0] / gt_in[1];
gt_out[1] = 1.0 / gt_in[1];
gt_out[2] = 0.0;
gt_out[3] = -gt_in[3] / gt_in[5];
gt_out[4] = 0.0;
gt_out[5] = 1.0 / gt_in[5];
return 1;
}
/* we assume a 3rd row that is [1 0 0] */
/* Compute determinate */
det = gt_in[1] * gt_in[5] - gt_in[2] * gt_in[4];
if( fabs(det) < 0.000000000000001 )
return 0;
inv_det = 1.0 / det;
/* compute adjoint, and devide by determinate */
gt_out[1] = gt_in[5] * inv_det;
gt_out[4] = -gt_in[4] * inv_det;
gt_out[2] = -gt_in[2] * inv_det;
gt_out[5] = gt_in[1] * inv_det;
gt_out[0] = ( gt_in[2] * gt_in[3] - gt_in[0] * gt_in[5]) * inv_det;
gt_out[3] = (-gt_in[1] * gt_in[3] + gt_in[0] * gt_in[4]) * inv_det;
return 1;
}
/************************************************************************/
/* GDALSerializeTransformer() */
/************************************************************************/
CPLXMLNode *GDALSerializeTransformer( GDALTransformerFunc pfnFunc,
void *pTransformArg )
{
VALIDATE_POINTER1( pTransformArg, "GDALSerializeTransformer", NULL );
GDALTransformerInfo *psInfo = static_cast<GDALTransformerInfo *>(pTransformArg);
if( psInfo == NULL || !EQUAL(psInfo->szSignature,"GTI") )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Attempt to serialize non-GTI transformer." );
return NULL;
}
else if ( psInfo->pfnSerialize == NULL )
{
CPLError( CE_Failure, CPLE_AppDefined,
"No serialization function available for this transformer." );
return NULL;
}
else
{
return psInfo->pfnSerialize( pTransformArg );
}
}
/************************************************************************/
/* GDALRegisterTransformDeserializer() */
/************************************************************************/
static CPLList* psListDeserializer = NULL;
static void* hDeserializerMutex = NULL;
typedef struct
{
char* pszTransformName;
GDALTransformerFunc pfnTransformerFunc;
GDALTransformDeserializeFunc pfnDeserializeFunc;
} TransformDeserializerInfo;
void* GDALRegisterTransformDeserializer(const char* pszTransformName,
GDALTransformerFunc pfnTransformerFunc,
GDALTransformDeserializeFunc pfnDeserializeFunc)
{
TransformDeserializerInfo* psInfo =
(TransformDeserializerInfo*)CPLMalloc(sizeof(TransformDeserializerInfo));
psInfo->pszTransformName = CPLStrdup(pszTransformName);
psInfo->pfnTransformerFunc = pfnTransformerFunc;
psInfo->pfnDeserializeFunc = pfnDeserializeFunc;
CPLMutexHolderD(&hDeserializerMutex);
psListDeserializer = CPLListInsert(psListDeserializer, psInfo, 0);
return psInfo;
}
/************************************************************************/
/* GDALUnregisterTransformDeserializer() */
/************************************************************************/
void GDALUnregisterTransformDeserializer(void* pData)
{
CPLMutexHolderD(&hDeserializerMutex);
CPLList* psList = psListDeserializer;
CPLList* psLast = NULL;
while(psList)
{
if (psList->pData == pData)
{
TransformDeserializerInfo* psInfo =
(TransformDeserializerInfo*)pData;
CPLFree(psInfo->pszTransformName);
CPLFree(pData);
if (psLast)
psLast->psNext = psList->psNext;
else
psListDeserializer = NULL;
CPLFree(psList);
break;
}
psLast = psList;
psList = psList->psNext;
}
}
/************************************************************************/
/* GDALUnregisterTransformDeserializer() */
/************************************************************************/
void GDALCleanupTransformDeserializerMutex()
{
if( hDeserializerMutex != NULL )
{
CPLDestroyMutex(hDeserializerMutex);
hDeserializerMutex = NULL;
}
}
/************************************************************************/
/* GDALDeserializeTransformer() */
/************************************************************************/
CPLErr GDALDeserializeTransformer( CPLXMLNode *psTree,
GDALTransformerFunc *ppfnFunc,
void **ppTransformArg )
{
*ppfnFunc = NULL;
*ppTransformArg = NULL;
CPLErrorReset();
if( psTree == NULL || psTree->eType != CXT_Element )
CPLError( CE_Failure, CPLE_AppDefined,
"Malformed element in GDALDeserializeTransformer" );
else if( EQUAL(psTree->pszValue,"GenImgProjTransformer") )
{
*ppfnFunc = GDALGenImgProjTransform;
*ppTransformArg = GDALDeserializeGenImgProjTransformer( psTree );
}
else if( EQUAL(psTree->pszValue,"ReprojectionTransformer") )
{
*ppfnFunc = GDALReprojectionTransform;
*ppTransformArg = GDALDeserializeReprojectionTransformer( psTree );
}
else if( EQUAL(psTree->pszValue,"GCPTransformer") )
{
*ppfnFunc = GDALGCPTransform;
*ppTransformArg = GDALDeserializeGCPTransformer( psTree );
}
else if( EQUAL(psTree->pszValue,"TPSTransformer") )
{
*ppfnFunc = GDALTPSTransform;
*ppTransformArg = GDALDeserializeTPSTransformer( psTree );
}
else if( EQUAL(psTree->pszValue,"GeoLocTransformer") )
{
*ppfnFunc = GDALGeoLocTransform;
*ppTransformArg = GDALDeserializeGeoLocTransformer( psTree );
}
else if( EQUAL(psTree->pszValue,"RPCTransformer") )
{
*ppfnFunc = GDALRPCTransform;
*ppTransformArg = GDALDeserializeRPCTransformer( psTree );
}
else if( EQUAL(psTree->pszValue,"ApproxTransformer") )
{
*ppfnFunc = GDALApproxTransform;
*ppTransformArg = GDALDeserializeApproxTransformer( psTree );
}
else
{
GDALTransformDeserializeFunc pfnDeserializeFunc = NULL;
{
CPLMutexHolderD(&hDeserializerMutex);
CPLList* psList = psListDeserializer;
while(psList)
{
TransformDeserializerInfo* psInfo =
(TransformDeserializerInfo*)psList->pData;
if (strcmp(psInfo->pszTransformName, psTree->pszValue) == 0)
{
*ppfnFunc = psInfo->pfnTransformerFunc;
pfnDeserializeFunc = psInfo->pfnDeserializeFunc;
break;
}
psList = psList->psNext;
}
}
if (pfnDeserializeFunc != NULL)
{
*ppTransformArg = pfnDeserializeFunc( psTree );
}
else
{
CPLError( CE_Failure, CPLE_AppDefined,
"Unrecognised element '%s' GDALDeserializeTransformer",
psTree->pszValue );
}
}
return CPLGetLastErrorType();
}
/************************************************************************/
/* GDALDestroyTransformer() */
/************************************************************************/
void GDALDestroyTransformer( void *pTransformArg )
{
GDALTransformerInfo *psInfo = (GDALTransformerInfo *) pTransformArg;
if( psInfo == NULL || !EQUAL(psInfo->szSignature,"GTI") )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Attempt to destroy non-GTI transformer." );
}
else
psInfo->pfnCleanup( pTransformArg );
}
/************************************************************************/
/* GDALUseTransformer() */
/************************************************************************/
int GDALUseTransformer( void *pTransformArg,
int bDstToSrc, int nPointCount,
double *x, double *y, double *z,
int *panSuccess )
{
GDALTransformerInfo *psInfo = (GDALTransformerInfo *) pTransformArg;
if( psInfo == NULL || !EQUAL(psInfo->szSignature,"GTI") )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Attempt to use non-GTI transformer." );
return FALSE;
}
else
return psInfo->pfnTransform( pTransformArg, bDstToSrc, nPointCount,
x, y, z, panSuccess );
}
/************************************************************************/
/* GDALCloneTransformer() */
/************************************************************************/
/* TODO GDAL 2.0 : use a void* (*pfnClone) (void *) member */
void *GDALCloneTransformer( void *pTransformArg )
{
VALIDATE_POINTER1( pTransformArg, "GDALCloneTransformer", NULL );
GDALTransformerInfo *psInfo = static_cast<GDALTransformerInfo *>(pTransformArg);
if( psInfo == NULL || !EQUAL(psInfo->szSignature,"GTI") )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Attempt to clone non-GTI transformer." );
return NULL;
}
void* (*pfnClone)(void*) = NULL;
if( EQUAL(psInfo->pszClassName,"GDALTPSTransformer") )
{
pfnClone = GDALCloneTPSTransformer;
}
/* TODO
else if( EQUAL(psInfo->pszClassName,"GDALGeoLocTransformer") )
{
pfnClone = GDALCloneGeoLocTransformer;
}
else if( EQUAL(psInfo->pszClassName,"GDALRPCTransformer") )
{
pfnClone = GDALCloneRPCTransformer;
}
*/
else if( EQUAL(psInfo->pszClassName,"GDALGenImgProjTransformer") )
{
pfnClone = GDALCloneGenImgProjTransformer;
}
/* Useless : serialization/deserialization is fine */
/*
else if( EQUAL(psInfo->pszClassName,"GDALReprojectionTransformer") )
{
pfnClone = GDALCloneReprojectionTransformer;
}
*/
else if( EQUAL(psInfo->pszClassName,"GDALApproxTransformer") )
{
pfnClone = GDALCloneApproxTransformer;
}
if( pfnClone != NULL )
{
void* pRet = pfnClone(pTransformArg);
if( pRet != NULL )
return pRet;
}
if ( psInfo->pfnSerialize == NULL )
{
CPLError( CE_Failure, CPLE_AppDefined,
"No serialization function available for this transformer." );
return NULL;
}
else
{
CPLXMLNode* pSerialized = psInfo->pfnSerialize( pTransformArg );
if( pSerialized == NULL )
return NULL;
GDALTransformerFunc pfnTransformer = NULL;
void *pClonedTransformArg = NULL;
if( GDALDeserializeTransformer( pSerialized, &pfnTransformer, &pClonedTransformArg ) !=
CE_None )
{
CPLDestroyXMLNode(pSerialized);
return NULL;
}
CPLDestroyXMLNode(pSerialized);
return pClonedTransformArg;
}
}
|