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
|
/******************************************************************************
* $Id: mapshape.c 11881 2011-07-07 19:55:43Z sdlime $
*
* Project: MapServer
* Purpose: Implements support for shapefile access.
* Authors: Steve Lime and Frank Warmerdam
*
* Note:
* This code is entirely based on the previous work of Frank Warmerdam. It is
* essentially shapelib 1.1.5. However, there were enough changes that it was
* incorporated into the MapServer source to avoid confusion. Relicensed with
* permission of Frank Warmerdam (shapelib author). See the README
* for licence details.
*
******************************************************************************
* Copyright (c) 1996-2005 Regents of the University of Minnesota.
*
* 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 of this Software or works derived from this 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 <limits.h>
#include <assert.h>
#include "mapserver.h"
MS_CVSID("$Id: mapshape.c 11881 2011-07-07 19:55:43Z sdlime $")
/* Only use this macro on 32-bit integers! */
#define SWAP_FOUR_BYTES(data) \
( ((data >> 24) & 0x000000FF) | ((data >> 8) & 0x0000FF00) | \
((data << 8) & 0x00FF0000) | ((data << 24) & 0xFF000000) )
#define ByteCopy( a, b, c ) memcpy( b, a, c )
static int bBigEndian;
/************************************************************************/
/* SwapWord() */
/* */
/* Swap a 2, 4 or 8 byte word. */
/************************************************************************/
static void SwapWord( int length, void * wordP )
{
int i;
uchar temp;
for( i=0; i < length/2; i++ ) {
temp = ((uchar *) wordP)[i];
((uchar *)wordP)[i] = ((uchar *) wordP)[length-i-1];
((uchar *) wordP)[length-i-1] = temp;
}
}
/************************************************************************/
/* SfRealloc() */
/* */
/* A realloc cover function that will access a NULL pointer as */
/* a valid input. */
/************************************************************************/
static void * SfRealloc( void * pMem, int nNewSize )
{
if( pMem == NULL )
return( (void *) malloc(nNewSize) );
else
return( (void *) realloc(pMem,nNewSize) );
}
/************************************************************************/
/* writeHeader() */
/* */
/* Write out a header for the .shp and .shx files as well as the */
/* contents of the index (.shx) file. */
/************************************************************************/
static void writeHeader( SHPHandle psSHP )
{
uchar abyHeader[100];
int i;
ms_int32 i32;
double dValue;
ms_int32 *panSHX;
/* -------------------------------------------------------------------- */
/* Prepare header block for .shp file. */
/* -------------------------------------------------------------------- */
for( i = 0; i < 100; i++ )
abyHeader[i] = 0;
abyHeader[2] = 0x27; /* magic cookie */
abyHeader[3] = 0x0a;
i32 = psSHP->nFileSize/2; /* file size */
ByteCopy( &i32, abyHeader+24, 4 );
if( !bBigEndian ) SwapWord( 4, abyHeader+24 );
i32 = 1000; /* version */
ByteCopy( &i32, abyHeader+28, 4 );
if( bBigEndian ) SwapWord( 4, abyHeader+28 );
i32 = psSHP->nShapeType; /* shape type */
ByteCopy( &i32, abyHeader+32, 4 );
if( bBigEndian ) SwapWord( 4, abyHeader+32 );
dValue = psSHP->adBoundsMin[0]; /* set bounds */
ByteCopy( &dValue, abyHeader+36, 8 );
if( bBigEndian ) SwapWord( 8, abyHeader+36 );
dValue = psSHP->adBoundsMin[1];
ByteCopy( &dValue, abyHeader+44, 8 );
if( bBigEndian ) SwapWord( 8, abyHeader+44 );
dValue = psSHP->adBoundsMax[0];
ByteCopy( &dValue, abyHeader+52, 8 );
if( bBigEndian ) SwapWord( 8, abyHeader+52 );
dValue = psSHP->adBoundsMax[1];
ByteCopy( &dValue, abyHeader+60, 8 );
if( bBigEndian ) SwapWord( 8, abyHeader+60 );
dValue = psSHP->adBoundsMin[2]; /* z */
ByteCopy( &dValue, abyHeader+68, 8 );
if( bBigEndian ) SwapWord( 8, abyHeader+68 );
dValue = psSHP->adBoundsMax[2];
ByteCopy( &dValue, abyHeader+76, 8 );
if( bBigEndian ) SwapWord( 8, abyHeader+76 );
dValue = psSHP->adBoundsMin[3]; /* m */
ByteCopy( &dValue, abyHeader+84, 8 );
if( bBigEndian ) SwapWord( 8, abyHeader+84 );
dValue = psSHP->adBoundsMax[3];
ByteCopy( &dValue, abyHeader+92, 8 );
if( bBigEndian ) SwapWord( 8, abyHeader+92 );
/* -------------------------------------------------------------------- */
/* Write .shp file header. */
/* -------------------------------------------------------------------- */
fseek( psSHP->fpSHP, 0, 0 );
fwrite( abyHeader, 100, 1, psSHP->fpSHP );
/* -------------------------------------------------------------------- */
/* Prepare, and write .shx file header. */
/* -------------------------------------------------------------------- */
i32 = (psSHP->nRecords * 2 * sizeof(ms_int32) + 100)/2; /* file size */
ByteCopy( &i32, abyHeader+24, 4 );
if( !bBigEndian ) SwapWord( 4, abyHeader+24 );
fseek( psSHP->fpSHX, 0, 0 );
fwrite( abyHeader, 100, 1, psSHP->fpSHX );
/* -------------------------------------------------------------------- */
/* Write out the .shx contents. */
/* -------------------------------------------------------------------- */
panSHX = (ms_int32 *) msSmallMalloc(sizeof(ms_int32) * 2 * psSHP->nRecords);
for( i = 0; i < psSHP->nRecords; i++ ) {
panSHX[i*2 ] = psSHP->panRecOffset[i]/2;
panSHX[i*2+1] = psSHP->panRecSize[i]/2;
if( !bBigEndian ) {
*(panSHX+i*2) = SWAP_FOUR_BYTES(*(panSHX+i*2));
*(panSHX+i*2+1) = SWAP_FOUR_BYTES(*(panSHX+i*2+1));
}
}
fwrite( panSHX, sizeof(ms_int32) * 2, psSHP->nRecords, psSHP->fpSHX );
free( panSHX );
}
/************************************************************************/
/* msSHPOpen() */
/* */
/* Open the .shp and .shx files based on the basename of the */
/* files or either file name. */
/************************************************************************/
SHPHandle msSHPOpen( const char * pszLayer, const char * pszAccess )
{
char *pszFullname, *pszBasename;
SHPHandle psSHP;
uchar *pabyBuf;
int i;
double dValue;
/* -------------------------------------------------------------------- */
/* Ensure the access string is one of the legal ones. We */
/* ensure the result string indicates binary to avoid common */
/* problems on Windows. */
/* -------------------------------------------------------------------- */
if( strcmp(pszAccess,"rb+") == 0 || strcmp(pszAccess,"r+b") == 0 || strcmp(pszAccess,"r+") == 0 )
pszAccess = "r+b";
else
pszAccess = "rb";
/* -------------------------------------------------------------------- */
/* Establish the byte order on this machine. */
/* -------------------------------------------------------------------- */
i = 1;
if( *((uchar *) &i) == 1 )
bBigEndian = MS_FALSE;
else
bBigEndian = MS_TRUE;
/* -------------------------------------------------------------------- */
/* Initialize the info structure. */
/* -------------------------------------------------------------------- */
psSHP = (SHPHandle) msSmallMalloc(sizeof(SHPInfo));
psSHP->bUpdated = MS_FALSE;
psSHP->pabyRec = NULL;
psSHP->panParts = NULL;
psSHP->nBufSize = psSHP->nPartMax = 0;
/* -------------------------------------------------------------------- */
/* Compute the base (layer) name. If there is any extension */
/* on the passed in filename we will strip it off. */
/* -------------------------------------------------------------------- */
pszBasename = (char *) msSmallMalloc(strlen(pszLayer)+5);
strcpy( pszBasename, pszLayer );
for( i = strlen(pszBasename)-1;
i > 0 && pszBasename[i] != '.' && pszBasename[i] != '/' && pszBasename[i] != '\\';
i-- ) {}
if( pszBasename[i] == '.' )
pszBasename[i] = '\0';
/* -------------------------------------------------------------------- */
/* Open the .shp and .shx files. Note that files pulled from */
/* a PC to Unix with upper case filenames won't work! */
/* -------------------------------------------------------------------- */
pszFullname = (char *) msSmallMalloc(strlen(pszBasename) + 5);
sprintf( pszFullname, "%s.shp", pszBasename );
psSHP->fpSHP = fopen(pszFullname, pszAccess );
if( psSHP->fpSHP == NULL ) {
msFree(pszBasename);
msFree(pszFullname);
msFree(psSHP);
return( NULL );
}
sprintf( pszFullname, "%s.shx", pszBasename );
psSHP->fpSHX = fopen(pszFullname, pszAccess );
if( psSHP->fpSHX == NULL ) {
msFree(pszBasename);
msFree(pszFullname);
msFree(psSHP);
return( NULL );
}
free( pszFullname );
free( pszBasename );
/* -------------------------------------------------------------------- */
/* Read the file size from the SHP file. */
/* -------------------------------------------------------------------- */
pabyBuf = (uchar *) msSmallMalloc(100);
fread( pabyBuf, 100, 1, psSHP->fpSHP );
psSHP->nFileSize = (pabyBuf[24] * 256 * 256 * 256
+ pabyBuf[25] * 256 * 256
+ pabyBuf[26] * 256
+ pabyBuf[27]) * 2;
/* -------------------------------------------------------------------- */
/* Read SHX file Header info */
/* -------------------------------------------------------------------- */
fread( pabyBuf, 100, 1, psSHP->fpSHX );
if( pabyBuf[0] != 0 || pabyBuf[1] != 0 || pabyBuf[2] != 0x27 || (pabyBuf[3] != 0x0a && pabyBuf[3] != 0x0d) ) {
fclose( psSHP->fpSHP );
fclose( psSHP->fpSHX );
free( psSHP );
return( NULL );
}
psSHP->nRecords = pabyBuf[27] + pabyBuf[26] * 256 + pabyBuf[25] * 256 * 256 + pabyBuf[24] * 256 * 256 * 256;
if (psSHP->nRecords != 0)
psSHP->nRecords = (psSHP->nRecords*2 - 100) / 8;
if( psSHP->nRecords < 0 || psSHP->nRecords > 256000000 )
{
msSetError(MS_SHPERR, "Corrupted .shp file : nRecords = %d.", "msSHPOpen()",
psSHP->nRecords);
fclose( psSHP->fpSHP );
fclose( psSHP->fpSHX );
free( psSHP );
return( NULL );
}
psSHP->nShapeType = pabyBuf[32];
if( bBigEndian ) SwapWord( 8, pabyBuf+36 );
memcpy( &dValue, pabyBuf+36, 8 );
psSHP->adBoundsMin[0] = dValue;
if( bBigEndian ) SwapWord( 8, pabyBuf+44 );
memcpy( &dValue, pabyBuf+44, 8 );
psSHP->adBoundsMin[1] = dValue;
if( bBigEndian ) SwapWord( 8, pabyBuf+52 );
memcpy( &dValue, pabyBuf+52, 8 );
psSHP->adBoundsMax[0] = dValue;
if( bBigEndian ) SwapWord( 8, pabyBuf+60 );
memcpy( &dValue, pabyBuf+60, 8 );
psSHP->adBoundsMax[1] = dValue;
if( bBigEndian ) SwapWord( 8, pabyBuf+68 ); /* z */
memcpy( &dValue, pabyBuf+68, 8 );
psSHP->adBoundsMin[2] = dValue;
if( bBigEndian ) SwapWord( 8, pabyBuf+76 );
memcpy( &dValue, pabyBuf+76, 8 );
psSHP->adBoundsMax[2] = dValue;
if( bBigEndian ) SwapWord( 8, pabyBuf+84 ); /* m */
memcpy( &dValue, pabyBuf+84, 8 );
psSHP->adBoundsMin[3] = dValue;
if( bBigEndian ) SwapWord( 8, pabyBuf+92 );
memcpy( &dValue, pabyBuf+92, 8 );
psSHP->adBoundsMax[3] = dValue;
free( pabyBuf );
/* -------------------------------------------------------------------- */
/* Read the .shx file to get the offsets to each record in */
/* the .shp file. */
/* -------------------------------------------------------------------- */
psSHP->nMaxRecords = psSHP->nRecords;
/* Our in-memory cache of offset information */
psSHP->panRecOffset = (int *) malloc(sizeof(int) * psSHP->nMaxRecords );
/* Our in-memory cache of size information */
psSHP->panRecSize = (int *) malloc(sizeof(int) * psSHP->nMaxRecords );
/* The completeness information for our in-memory cache */
psSHP->panRecLoaded = msAllocBitArray( 1 + (psSHP->nMaxRecords / SHX_BUFFER_PAGE) ) ;
/* Is our in-memory cache completely populated? */
psSHP->panRecAllLoaded = 0;
/* malloc failed? clean up and shut down */
if (psSHP->panRecOffset == NULL ||
psSHP->panRecSize == NULL ||
psSHP->panRecLoaded == NULL)
{
free(psSHP->panRecOffset);
free(psSHP->panRecSize);
free(psSHP->panRecLoaded);
fclose( psSHP->fpSHP );
fclose( psSHP->fpSHX );
free( psSHP );
msSetError(MS_MEMERR, "Out of memory", "msSHPOpen()");
return( NULL );
}
return( psSHP );
}
/************************************************************************/
/* msSHPClose() */
/* */
/* Close the .shp and .shx files. */
/************************************************************************/
void msSHPClose(SHPHandle psSHP )
{
/* -------------------------------------------------------------------- */
/* Update the header if we have modified anything. */
/* -------------------------------------------------------------------- */
if( psSHP->bUpdated )
writeHeader( psSHP );
/* -------------------------------------------------------------------- */
/* Free all resources, and close files. */
/* -------------------------------------------------------------------- */
free( psSHP->panRecOffset );
free( psSHP->panRecSize );
free( psSHP->panRecLoaded );
if(psSHP->pabyRec) free(psSHP->pabyRec);
if(psSHP->panParts) free(psSHP->panParts);
fclose( psSHP->fpSHX );
fclose( psSHP->fpSHP );
free( psSHP );
}
/************************************************************************/
/* msSHPGetInfo() */
/* */
/* Fetch general information about the shape file. */
/************************************************************************/
void msSHPGetInfo(SHPHandle psSHP, int * pnEntities, int * pnShapeType )
{
if( pnEntities )
*pnEntities = psSHP->nRecords;
if( pnShapeType )
*pnShapeType = psSHP->nShapeType;
}
/************************************************************************/
/* msSHPCreate() */
/* */
/* Create a new shape file and return a handle to the open */
/* shape file with read/write access. */
/************************************************************************/
SHPHandle msSHPCreate( const char * pszLayer, int nShapeType )
{
char *pszBasename, *pszFullname;
int i;
FILE *fpSHP, *fpSHX;
uchar abyHeader[100];
ms_int32 i32;
double dValue;
#ifndef USE_POINT_Z_M
if( nShapeType == SHP_POLYGONZ
|| nShapeType == SHP_POLYGONM
|| nShapeType == SHP_ARCZ
|| nShapeType == SHP_ARCM
|| nShapeType == SHP_POINTZ
|| nShapeType == SHP_POINTM
|| nShapeType == SHP_MULTIPOINTZ
|| nShapeType == SHP_MULTIPOINTM )
{
msSetError( MS_SHPERR,
"Attempt to create M/Z shapefile but without having enabled Z/M support.",
"msSHPCreate()" );
return NULL;
}
#endif
/* -------------------------------------------------------------------- */
/* Establish the byte order on this system. */
/* -------------------------------------------------------------------- */
i = 1;
if( *((uchar *) &i) == 1 )
bBigEndian = MS_FALSE;
else
bBigEndian = MS_TRUE;
/* -------------------------------------------------------------------- */
/* Compute the base (layer) name. If there is any extension */
/* on the passed in filename we will strip it off. */
/* -------------------------------------------------------------------- */
pszBasename = (char *) msSmallMalloc(strlen(pszLayer)+5);
strcpy( pszBasename, pszLayer );
for( i = strlen(pszBasename)-1;
i > 0 && pszBasename[i] != '.' && pszBasename[i] != '/' && pszBasename[i] != '\\';
i-- ) {}
if( pszBasename[i] == '.' )
pszBasename[i] = '\0';
/* -------------------------------------------------------------------- */
/* Open the two files so we can write their headers. */
/* -------------------------------------------------------------------- */
pszFullname = (char *) msSmallMalloc(strlen(pszBasename) + 5);
sprintf( pszFullname, "%s.shp", pszBasename );
fpSHP = fopen(pszFullname, "wb" );
if( fpSHP == NULL )
return( NULL );
sprintf( pszFullname, "%s.shx", pszBasename );
fpSHX = fopen(pszFullname, "wb" );
if( fpSHX == NULL )
return( NULL );
free( pszFullname );
/* -------------------------------------------------------------------- */
/* Prepare header block for .shp file. */
/* -------------------------------------------------------------------- */
for( i = 0; i < 100; i++ )
abyHeader[i] = 0;
abyHeader[2] = 0x27; /* magic cookie */
abyHeader[3] = 0x0a;
i32 = 50; /* file size */
ByteCopy( &i32, abyHeader+24, 4 );
if( !bBigEndian ) SwapWord( 4, abyHeader+24 );
i32 = 1000; /* version */
ByteCopy( &i32, abyHeader+28, 4 );
if( bBigEndian ) SwapWord( 4, abyHeader+28 );
i32 = nShapeType; /* shape type */
ByteCopy( &i32, abyHeader+32, 4 );
if( bBigEndian ) SwapWord( 4, abyHeader+32 );
dValue = 0.0; /* set bounds */
ByteCopy( &dValue, abyHeader+36, 8 );
ByteCopy( &dValue, abyHeader+44, 8 );
ByteCopy( &dValue, abyHeader+52, 8 );
ByteCopy( &dValue, abyHeader+60, 8 );
/* -------------------------------------------------------------------- */
/* Write .shp file header. */
/* -------------------------------------------------------------------- */
fwrite( abyHeader, 100, 1, fpSHP );
/* -------------------------------------------------------------------- */
/* Prepare, and write .shx file header. */
/* -------------------------------------------------------------------- */
i32 = 50; /* file size */
ByteCopy( &i32, abyHeader+24, 4 );
if( !bBigEndian ) SwapWord( 4, abyHeader+24 );
fwrite( abyHeader, 100, 1, fpSHX );
/* -------------------------------------------------------------------- */
/* Close the files, and then open them as regular existing files. */
/* -------------------------------------------------------------------- */
fclose( fpSHP );
fclose( fpSHX );
return( msSHPOpen( pszLayer, "rb+" ) );
}
/************************************************************************/
/* writeBounds() */
/* */
/* Compute a bounds rectangle for a shape, and set it into the */
/* indicated location in the record. */
/************************************************************************/
static void writeBounds( uchar * pabyRec, shapeObj *shape, int nVCount )
{
double dXMin, dXMax, dYMin, dYMax;
int i, j;
if( nVCount == 0 ) {
dXMin = dYMin = dXMax = dYMax = 0.0;
} else {
dXMin = dXMax = shape->line[0].point[0].x;
dYMin = dYMax = shape->line[0].point[0].y;
for( i=0; i<shape->numlines; i++ ) {
for( j=0; j<shape->line[i].numpoints; j++ ) {
dXMin = MS_MIN(dXMin, shape->line[i].point[j].x);
dXMax = MS_MAX(dXMax, shape->line[i].point[j].x);
dYMin = MS_MIN(dYMin, shape->line[i].point[j].y);
dYMax = MS_MAX(dYMax, shape->line[i].point[j].y);
}
}
}
if( bBigEndian ) {
SwapWord( 8, &dXMin );
SwapWord( 8, &dYMin );
SwapWord( 8, &dXMax );
SwapWord( 8, &dYMax );
}
ByteCopy( &dXMin, pabyRec + 0, 8 );
ByteCopy( &dYMin, pabyRec + 8, 8 );
ByteCopy( &dXMax, pabyRec + 16, 8 );
ByteCopy( &dYMax, pabyRec + 24, 8 );
}
int msSHPWritePoint(SHPHandle psSHP, pointObj *point )
{
int nRecordOffset, nRecordSize=0;
uchar *pabyRec;
ms_int32 i32, nPoints, nParts;
if( psSHP->nShapeType != SHP_POINT) return(-1);
psSHP->bUpdated = MS_TRUE;
/* Fill the SHX buffer if it is not already full. */
if( ! psSHP->panRecAllLoaded ) msSHXLoadAll( psSHP );
/* -------------------------------------------------------------------- */
/* Add the new entity to the in memory index. */
/* -------------------------------------------------------------------- */
psSHP->nRecords++;
if( psSHP->nRecords > psSHP->nMaxRecords ) {
psSHP->nMaxRecords = (int) (psSHP->nMaxRecords * 1.3 + 100);
psSHP->panRecOffset = (int *) SfRealloc(psSHP->panRecOffset,sizeof(int) * psSHP->nMaxRecords );
psSHP->panRecSize = (int *) SfRealloc(psSHP->panRecSize,sizeof(int) * psSHP->nMaxRecords );
}
/* -------------------------------------------------------------------- */
/* Compute a few things. */
/* -------------------------------------------------------------------- */
nPoints = 1;
nParts = 1;
/* -------------------------------------------------------------------- */
/* Initialize record. */
/* -------------------------------------------------------------------- */
psSHP->panRecOffset[psSHP->nRecords-1] = nRecordOffset = psSHP->nFileSize;
pabyRec = (uchar *) msSmallMalloc(nPoints * 2 * sizeof(double) + nParts * 4 + 128);
/* -------------------------------------------------------------------- */
/* Write vertices for a point. */
/* -------------------------------------------------------------------- */
ByteCopy( &(point->x), pabyRec + 12, 8 );
ByteCopy( &(point->y), pabyRec + 20, 8 );
if( bBigEndian ) {
SwapWord( 8, pabyRec + 12 );
SwapWord( 8, pabyRec + 20 );
}
nRecordSize = 20;
/* -------------------------------------------------------------------- */
/* Set the shape type, record number, and record size. */
/* -------------------------------------------------------------------- */
i32 = psSHP->nRecords-1+1; /* record # */
if( !bBigEndian ) i32 = SWAP_FOUR_BYTES(i32);
ByteCopy( &i32, pabyRec, 4 );
i32 = nRecordSize/2; /* record size */
if( !bBigEndian ) i32 = SWAP_FOUR_BYTES(i32);
ByteCopy( &i32, pabyRec + 4, 4 );
i32 = psSHP->nShapeType; /* shape type */
if( bBigEndian ) i32 = SWAP_FOUR_BYTES(i32);
ByteCopy( &i32, pabyRec + 8, 4 );
/* -------------------------------------------------------------------- */
/* Write out record. */
/* -------------------------------------------------------------------- */
fseek( psSHP->fpSHP, nRecordOffset, 0 );
fwrite( pabyRec, nRecordSize+8, 1, psSHP->fpSHP );
free( pabyRec );
psSHP->panRecSize[psSHP->nRecords-1] = nRecordSize;
psSHP->nFileSize += nRecordSize + 8;
/* -------------------------------------------------------------------- */
/* Expand file wide bounds based on this shape. */
/* -------------------------------------------------------------------- */
if( psSHP->nRecords == 1 ) {
psSHP->adBoundsMin[0] = psSHP->adBoundsMax[0] = point->x;
psSHP->adBoundsMin[1] = psSHP->adBoundsMax[1] = point->y;
} else {
psSHP->adBoundsMin[0] = MS_MIN(psSHP->adBoundsMin[0], point->x);
psSHP->adBoundsMin[1] = MS_MIN(psSHP->adBoundsMin[1], point->y);
psSHP->adBoundsMax[0] = MS_MAX(psSHP->adBoundsMax[0], point->x);
psSHP->adBoundsMax[1] = MS_MAX(psSHP->adBoundsMax[1], point->y);
}
return( psSHP->nRecords - 1 );
}
int msSHPWriteShape(SHPHandle psSHP, shapeObj *shape )
{
int nRecordOffset, i, j, k, nRecordSize=0;
uchar *pabyRec;
int nShapeType;
ms_int32 i32, nPoints, nParts;
#ifdef USE_POINT_Z_M
double dfMMin, dfMMax = 0;
#endif
psSHP->bUpdated = MS_TRUE;
/* Fill the SHX buffer if it is not already full. */
if( ! psSHP->panRecAllLoaded ) msSHXLoadAll( psSHP );
/* -------------------------------------------------------------------- */
/* Add the new entity to the in memory index. */
/* -------------------------------------------------------------------- */
psSHP->nRecords++;
if( psSHP->nRecords > psSHP->nMaxRecords ) {
psSHP->nMaxRecords = (int) (psSHP->nMaxRecords * 1.3 + 100);
psSHP->panRecOffset = (int *) SfRealloc(psSHP->panRecOffset,sizeof(int) * psSHP->nMaxRecords );
psSHP->panRecSize = (int *) SfRealloc(psSHP->panRecSize,sizeof(int) * psSHP->nMaxRecords );
}
/* -------------------------------------------------------------------- */
/* Compute a few things. */
/* -------------------------------------------------------------------- */
nPoints = 0;
for(i=0; i<shape->numlines; i++)
nPoints += shape->line[i].numpoints;
nParts = shape->numlines;
/* -------------------------------------------------------------------- */
/* Initialize record. */
/* -------------------------------------------------------------------- */
psSHP->panRecOffset[psSHP->nRecords-1] = nRecordOffset = psSHP->nFileSize;
pabyRec = (uchar *) msSmallMalloc(nPoints * 4 * sizeof(double) + nParts * 8 + 128);
nShapeType = psSHP->nShapeType;
if (shape->type == MS_SHAPE_NULL) {
nShapeType = 0;
nRecordSize = 12;
}
/* -------------------------------------------------------------------- */
/* Write vertices for a Polygon or Arc. */
/* -------------------------------------------------------------------- */
else if(psSHP->nShapeType == SHP_POLYGON || psSHP->nShapeType == SHP_ARC ||
psSHP->nShapeType == SHP_POLYGONM || psSHP->nShapeType == SHP_ARCM ||
psSHP->nShapeType == SHP_ARCZ || psSHP->nShapeType == SHP_POLYGONZ) {
ms_int32 t_nParts, t_nPoints, partSize;
t_nParts = nParts;
t_nPoints = nPoints;
writeBounds( pabyRec + 12, shape, t_nPoints );
if( bBigEndian ) {
nPoints = SWAP_FOUR_BYTES(nPoints);
nParts = SWAP_FOUR_BYTES(nParts);
}
ByteCopy( &nPoints, pabyRec + 40 + 8, 4 );
ByteCopy( &nParts, pabyRec + 36 + 8, 4 );
partSize = 0; /* first part always starts at 0 */
ByteCopy( &partSize, pabyRec + 44 + 8 + 4*0, 4 );
if( bBigEndian ) SwapWord( 4, pabyRec + 44 + 8 + 4*0);
for( i = 1; i < t_nParts; i++ ) {
partSize += shape->line[i-1].numpoints;
ByteCopy( &partSize, pabyRec + 44 + 8 + 4*i, 4 );
if( bBigEndian ) SwapWord( 4, pabyRec + 44 + 8 + 4*i);
}
k = 0; /* overall point counter */
for( i = 0; i < shape->numlines; i++ ) {
for( j = 0; j < shape->line[i].numpoints; j++ ) {
ByteCopy( &(shape->line[i].point[j].x), pabyRec + 44 + 4*t_nParts + 8 + k * 16, 8 );
ByteCopy( &(shape->line[i].point[j].y), pabyRec + 44 + 4*t_nParts + 8 + k * 16 + 8, 8 );
if( bBigEndian ) {
SwapWord( 8, pabyRec + 44+4*t_nParts+8+k*16 );
SwapWord( 8, pabyRec + 44+4*t_nParts+8+k*16+8 );
}
k++;
}
}
nRecordSize = 44 + 4*t_nParts + 16 * t_nPoints;
#ifdef USE_POINT_Z_M
/* -------------------------------------------------------------------- */
/* measured shape : polygon and arc. */
/* -------------------------------------------------------------------- */
if(psSHP->nShapeType == SHP_POLYGONM || psSHP->nShapeType == SHP_ARCM) {
dfMMin = shape->line[0].point[0].m;
dfMMax = shape->line[shape->numlines-1].point[shape->line[shape->numlines-1].numpoints-1].m;
nRecordSize = 44 + 4*t_nParts + 8 + (t_nPoints* 16);
ByteCopy( &(dfMMin), pabyRec + nRecordSize, 8 );
if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
nRecordSize += 8;
ByteCopy( &(dfMMax), pabyRec + nRecordSize, 8 );
if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
nRecordSize += 8;
for( i = 0; i < shape->numlines; i++ ) {
for( j = 0; j < shape->line[i].numpoints; j++ ) {
ByteCopy( &(shape->line[i].point[j].m), pabyRec + nRecordSize, 8 );
if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
nRecordSize += 8;
}
}
}
/* -------------------------------------------------------------------- */
/* Polygon. Arc with Z */
/* -------------------------------------------------------------------- */
if (psSHP->nShapeType == SHP_POLYGONZ || psSHP->nShapeType == SHP_ARCZ
|| psSHP->nShapeType == SHP_POLYGONM || psSHP->nShapeType == SHP_ARCM) {
dfMMin = shape->line[0].point[0].z;
dfMMax = shape->line[shape->numlines-1].point[shape->line[shape->numlines-1].numpoints-1].z;
nRecordSize = 44 + 4*t_nParts + 8 + (t_nPoints* 16);
ByteCopy( &(dfMMin), pabyRec + nRecordSize, 8 );
if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
nRecordSize += 8;
ByteCopy( &(dfMMax), pabyRec + nRecordSize, 8 );
if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
nRecordSize += 8;
for( i = 0; i < shape->numlines; i++ ) {
for( j = 0; j < shape->line[i].numpoints; j++ ) {
ByteCopy( &(shape->line[i].point[j].z), pabyRec + nRecordSize, 8 );
if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
nRecordSize += 8;
}
}
}
#endif /* def USE_POINT_Z_M */
}
/* -------------------------------------------------------------------- */
/* Write vertices for a MultiPoint. */
/* -------------------------------------------------------------------- */
else if( psSHP->nShapeType == SHP_MULTIPOINT ||
psSHP->nShapeType == SHP_MULTIPOINTM ||
psSHP->nShapeType == SHP_MULTIPOINTZ) {
ms_int32 t_nPoints;
t_nPoints = nPoints;
writeBounds( pabyRec + 12, shape, nPoints );
if( bBigEndian ) nPoints = SWAP_FOUR_BYTES(nPoints);
ByteCopy( &nPoints, pabyRec + 44, 4 );
for( i = 0; i < shape->line[0].numpoints; i++ ) {
ByteCopy( &(shape->line[0].point[i].x), pabyRec + 48 + i*16, 8 );
ByteCopy( &(shape->line[0].point[i].y), pabyRec + 48 + i*16 + 8, 8 );
if( bBigEndian ) {
SwapWord( 8, pabyRec + 48 + i*16 );
SwapWord( 8, pabyRec + 48 + i*16 + 8 );
}
}
nRecordSize = 40 + 16 * t_nPoints;
#ifdef USE_POINT_Z_M
if (psSHP->nShapeType == SHP_MULTIPOINTM) {
dfMMin = shape->line[0].point[0].m;
dfMMax = shape->line[0].point[shape->line[0].numpoints-1].m;
ByteCopy( &(dfMMin), pabyRec + nRecordSize, 8 );
if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
nRecordSize += 8;
ByteCopy( &(dfMMax), pabyRec + nRecordSize, 8 );
if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
nRecordSize += 8;
for( i = 0; i < shape->line[0].numpoints; i++ ) {
ByteCopy( &(shape->line[0].point[i].m), pabyRec + nRecordSize, 8 );
if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
nRecordSize += 8;
}
}
if (psSHP->nShapeType == SHP_MULTIPOINTZ) {
dfMMin = shape->line[0].point[0].z;
dfMMax = shape->line[0].point[shape->line[0].numpoints-1].z;
ByteCopy( &(dfMMin), pabyRec + nRecordSize, 8 );
if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
nRecordSize += 8;
ByteCopy( &(dfMMax), pabyRec + nRecordSize, 8 );
if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
nRecordSize += 8;
for( i = 0; i < shape->line[0].numpoints; i++ ) {
ByteCopy( &(shape->line[0].point[i].z), pabyRec + nRecordSize, 8 );
if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
nRecordSize += 8;
}
}
#endif /* USE_POINT_Z_M */
}
/* -------------------------------------------------------------------- */
/* Write vertices for a point. */
/* -------------------------------------------------------------------- */
else if( psSHP->nShapeType == SHP_POINT || psSHP->nShapeType == SHP_POINTM ||
psSHP->nShapeType == SHP_POINTZ) {
ByteCopy( &(shape->line[0].point[0].x), pabyRec + 12, 8 );
ByteCopy( &(shape->line[0].point[0].y), pabyRec + 20, 8 );
if( bBigEndian ) {
SwapWord( 8, pabyRec + 12 );
SwapWord( 8, pabyRec + 20 );
}
nRecordSize = 20;
#ifdef USE_POINT_Z_M
if (psSHP->nShapeType == SHP_POINTM) {
ByteCopy( &(shape->line[0].point[0].m), pabyRec + nRecordSize, 8 );
if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
nRecordSize += 8;
}
if (psSHP->nShapeType == SHP_POINTZ) {
ByteCopy( &(shape->line[0].point[0].z), pabyRec + nRecordSize, 8 );
if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
nRecordSize += 8;
}
#endif /* USE_POINT_Z_M */
}
/* -------------------------------------------------------------------- */
/* Set the shape type, record number, and record size. */
/* -------------------------------------------------------------------- */
i32 = psSHP->nRecords-1+1; /* record # */
if( !bBigEndian ) i32 = SWAP_FOUR_BYTES(i32);
ByteCopy( &i32, pabyRec, 4 );
i32 = nRecordSize/2; /* record size */
if( !bBigEndian ) i32 = SWAP_FOUR_BYTES(i32);
ByteCopy( &i32, pabyRec + 4, 4 );
i32 = nShapeType; /* shape type */
if( bBigEndian ) i32 = SWAP_FOUR_BYTES(i32);
ByteCopy( &i32, pabyRec + 8, 4 );
/* -------------------------------------------------------------------- */
/* Write out record. */
/* -------------------------------------------------------------------- */
fseek( psSHP->fpSHP, nRecordOffset, 0 );
fwrite( pabyRec, nRecordSize+8, 1, psSHP->fpSHP );
free( pabyRec );
psSHP->panRecSize[psSHP->nRecords-1] = nRecordSize;
psSHP->nFileSize += nRecordSize + 8;
/* -------------------------------------------------------------------- */
/* Expand file wide bounds based on this shape. */
/* -------------------------------------------------------------------- */
if( psSHP->nRecords == 1 ) {
psSHP->adBoundsMin[0] = psSHP->adBoundsMax[0] = shape->line[0].point[0].x;
psSHP->adBoundsMin[1] = psSHP->adBoundsMax[1] = shape->line[0].point[0].y;
#ifdef USE_POINT_Z_M
psSHP->adBoundsMin[2] = psSHP->adBoundsMax[2] = shape->line[0].point[0].z;
psSHP->adBoundsMin[3] = psSHP->adBoundsMax[3] = shape->line[0].point[0].m;
#endif
}
for( i=0; i<shape->numlines; i++ ) {
for( j=0; j<shape->line[i].numpoints; j++ ) {
psSHP->adBoundsMin[0] = MS_MIN(psSHP->adBoundsMin[0], shape->line[i].point[j].x);
psSHP->adBoundsMin[1] = MS_MIN(psSHP->adBoundsMin[1], shape->line[i].point[j].y);
#ifdef USE_POINT_Z_M
psSHP->adBoundsMin[2] = MS_MIN(psSHP->adBoundsMin[2], shape->line[i].point[j].z);
psSHP->adBoundsMin[3] = MS_MIN(psSHP->adBoundsMin[3], shape->line[i].point[j].m);
#endif
psSHP->adBoundsMax[0] = MS_MAX(psSHP->adBoundsMax[0], shape->line[i].point[j].x);
psSHP->adBoundsMax[1] = MS_MAX(psSHP->adBoundsMax[1], shape->line[i].point[j].y);
#ifdef USE_POINT_Z_M
psSHP->adBoundsMax[2] = MS_MAX(psSHP->adBoundsMax[2], shape->line[i].point[j].z);
psSHP->adBoundsMax[3] = MS_MAX(psSHP->adBoundsMax[3], shape->line[i].point[j].m);
#endif
}
}
return( psSHP->nRecords - 1 );
}
/*
** msSHPReadAllocateBuffer() - Ensure our record buffer is large enough.
*/
static int msSHPReadAllocateBuffer( SHPHandle psSHP, int hEntity, const char* pszCallingFunction)
{
int nEntitySize = msSHXReadSize(psSHP, hEntity) + 8;
/* -------------------------------------------------------------------- */
/* Ensure our record buffer is large enough. */
/* -------------------------------------------------------------------- */
if( nEntitySize > psSHP->nBufSize ) {
psSHP->pabyRec = (uchar *) SfRealloc(psSHP->pabyRec,nEntitySize);
if (psSHP->pabyRec == NULL)
{
/* Reallocate previous successfull size for following features */
psSHP->pabyRec = msSmallMalloc(psSHP->nBufSize);
msSetError(MS_MEMERR, "Out of memory. Cannot allocate %d bytes. Probably broken shapefile at feature %d",
pszCallingFunction, nEntitySize, hEntity);
return(MS_FAILURE);
}
psSHP->nBufSize = nEntitySize;
}
if (psSHP->pabyRec == NULL)
{
msSetError(MS_MEMERR, "Out of memory", pszCallingFunction);
return(MS_FAILURE);
}
return MS_SUCCESS;
}
/*
** msSHPReadPoint() - Reads a single point from a POINT shape file.
*/
int msSHPReadPoint( SHPHandle psSHP, int hEntity, pointObj *point )
{
int nEntitySize;
/* -------------------------------------------------------------------- */
/* Only valid for point shapefiles */
/* -------------------------------------------------------------------- */
if( psSHP->nShapeType != SHP_POINT ) {
msSetError(MS_SHPERR, "msSHPReadPoint only operates on point shapefiles.", "msSHPReadPoint()");
return(MS_FAILURE);
}
/* -------------------------------------------------------------------- */
/* Validate the record/entity number. */
/* -------------------------------------------------------------------- */
if( hEntity < 0 || hEntity >= psSHP->nRecords ) {
msSetError(MS_SHPERR, "Record index out of bounds.", "msSHPReadPoint()");
return(MS_FAILURE);
}
nEntitySize = msSHXReadSize( psSHP, hEntity) + 8;
if( msSHXReadSize( psSHP, hEntity) == 4 ) {
msSetError(MS_SHPERR, "NULL feature encountered.", "msSHPReadPoint()");
return(MS_FAILURE);
}
else if ( nEntitySize < 28 ) {
msSetError(MS_SHPERR, "Corrupted feature encountered. hEntity=%d, nEntitySize=%d", "msSHPReadPoint()",
hEntity, nEntitySize);
return(MS_FAILURE);
}
if (msSHPReadAllocateBuffer(psSHP, hEntity, "msSHPReadPoint()") == MS_FAILURE)
{
return MS_FAILURE;
}
/* -------------------------------------------------------------------- */
/* Read the record. */
/* -------------------------------------------------------------------- */
fseek( psSHP->fpSHP, msSHXReadOffset( psSHP, hEntity), 0 );
fread( psSHP->pabyRec, nEntitySize, 1, psSHP->fpSHP );
memcpy( &(point->x), psSHP->pabyRec + 12, 8 );
memcpy( &(point->y), psSHP->pabyRec + 20, 8 );
if( bBigEndian ) {
SwapWord( 8, &(point->x));
SwapWord( 8, &(point->y));
}
return(MS_SUCCESS);
}
/*
** msSHXLoadPage()
**
** The SHX tells us what the byte offsets of the shapes in the SHP file are.
** We read the SHX file in ~8K pages and store those pages in memory for
** successive accesses during the reading cycle (first bounds are read,
** then entire shapes). Each time we read a page, we mark it as read.
*/
int msSHXLoadPage( SHPHandle psSHP, int shxBufferPage )
{
int i;
/* Each SHX record is 8 bytes long (two ints), hence our buffer size. */
char buffer[SHX_BUFFER_PAGE * 8];
/* Validate the page number. */
if( shxBufferPage < 0 )
return(MS_FAILURE);
/* The SHX file starts with 100 bytes of header, skip that. */
fseek( psSHP->fpSHX, 100 + shxBufferPage * SHX_BUFFER_PAGE * 8, 0 );
fread( buffer, 8, SHX_BUFFER_PAGE, psSHP->fpSHX );
/* Copy the buffer contents out into the working arrays. */
for( i = 0; i < SHX_BUFFER_PAGE; i++ ) {
int tmpOffset, tmpSize;
/* Don't write information past the end of the arrays, please. */
if(psSHP->nRecords <= (shxBufferPage * SHX_BUFFER_PAGE + i) )
break;
memcpy( &tmpOffset, (buffer + (8*i)), 4);
memcpy( &tmpSize, (buffer + (8*i) + 4), 4);
/* SHX uses big endian numbers for the offsets, so we have to flip them */
/* if we are a little endian machine. */
if( !bBigEndian ) {
tmpOffset = SWAP_FOUR_BYTES(tmpOffset);
tmpSize = SWAP_FOUR_BYTES(tmpSize);
}
/* SHX stores the offsets in 2 byte units, so we double them to get */
/* an offset in bytes. */
tmpOffset = tmpOffset * 2;
tmpSize = tmpSize * 2;
/* Write the answer into the working arrays on the SHPHandle */
psSHP->panRecOffset[shxBufferPage * SHX_BUFFER_PAGE + i] = tmpOffset;
psSHP->panRecSize[shxBufferPage * SHX_BUFFER_PAGE + i] = tmpSize;
}
msSetBit(psSHP->panRecLoaded, shxBufferPage, 1);
return(MS_SUCCESS);
}
int msSHXLoadAll( SHPHandle psSHP ) {
int i;
uchar *pabyBuf;
pabyBuf = (uchar *) msSmallMalloc(8 * psSHP->nRecords );
fread( pabyBuf, 8, psSHP->nRecords, psSHP->fpSHX );
for( i = 0; i < psSHP->nRecords; i++ ) {
ms_int32 nOffset, nLength;
memcpy( &nOffset, pabyBuf + i * 8, 4 );
memcpy( &nLength, pabyBuf + i * 8 + 4, 4 );
if( !bBigEndian ) {
nOffset = SWAP_FOUR_BYTES( nOffset );
nLength = SWAP_FOUR_BYTES( nLength );
}
psSHP->panRecOffset[i] = nOffset*2;
psSHP->panRecSize[i] = nLength*2;
}
free(pabyBuf);
psSHP->panRecAllLoaded = 1;
return(MS_SUCCESS);
}
int msSHXReadOffset( SHPHandle psSHP, int hEntity ) {
int shxBufferPage = hEntity / SHX_BUFFER_PAGE;
/* Validate the record/entity number. */
if( hEntity < 0 || hEntity >= psSHP->nRecords )
return(MS_FAILURE);
if( ! (psSHP->panRecAllLoaded || msGetBit(psSHP->panRecLoaded, shxBufferPage)) ) {
msSHXLoadPage( psSHP, shxBufferPage );
}
return psSHP->panRecOffset[hEntity];
}
int msSHXReadSize( SHPHandle psSHP, int hEntity ) {
int shxBufferPage = hEntity / SHX_BUFFER_PAGE;
/* Validate the record/entity number. */
if( hEntity < 0 || hEntity >= psSHP->nRecords )
return(MS_FAILURE);
if( ! (psSHP->panRecAllLoaded || msGetBit(psSHP->panRecLoaded, shxBufferPage)) ) {
msSHXLoadPage( psSHP, shxBufferPage );
}
return psSHP->panRecSize[hEntity];
}
/*
** msSHPReadShape() - Reads the vertices for one shape from a shape file.
*/
void msSHPReadShape( SHPHandle psSHP, int hEntity, shapeObj *shape )
{
int i, j, k;
#ifdef USE_POINT_Z_M
int nOffset = 0;
#endif
int nEntitySize, nRequiredSize;
msInitShape(shape); /* initialize the shape */
/* -------------------------------------------------------------------- */
/* Validate the record/entity number. */
/* -------------------------------------------------------------------- */
if( hEntity < 0 || hEntity >= psSHP->nRecords )
return;
if( msSHXReadSize(psSHP, hEntity) == 4 ) {
shape->type = MS_SHAPE_NULL;
return;
}
nEntitySize = msSHXReadSize(psSHP, hEntity) + 8;
if (msSHPReadAllocateBuffer(psSHP, hEntity, "msSHPReadShape()") == MS_FAILURE)
{
shape->type = MS_SHAPE_NULL;
return;
}
/* -------------------------------------------------------------------- */
/* Read the record. */
/* -------------------------------------------------------------------- */
fseek( psSHP->fpSHP, msSHXReadOffset(psSHP, hEntity), 0 );
fread( psSHP->pabyRec, nEntitySize, 1, psSHP->fpSHP );
/* -------------------------------------------------------------------- */
/* Extract vertices for a Polygon or Arc. */
/* -------------------------------------------------------------------- */
if( psSHP->nShapeType == SHP_POLYGON || psSHP->nShapeType == SHP_ARC ||
psSHP->nShapeType == SHP_POLYGONM || psSHP->nShapeType == SHP_ARCM ||
psSHP->nShapeType == SHP_POLYGONZ || psSHP->nShapeType == SHP_ARCZ)
{
ms_int32 nPoints, nParts;
if (nEntitySize < 40 + 8 + 4)
{
shape->type = MS_SHAPE_NULL;
msSetError(MS_SHPERR, "Corrupted feature encountered. hEntity = %d, nEntitySize=%d", "msSHPReadShape()",
hEntity, nEntitySize);
return;
}
/* copy the bounding box */
memcpy( &shape->bounds.minx, psSHP->pabyRec + 8 + 4, 8 );
memcpy( &shape->bounds.miny, psSHP->pabyRec + 8 + 12, 8 );
memcpy( &shape->bounds.maxx, psSHP->pabyRec + 8 + 20, 8 );
memcpy( &shape->bounds.maxy, psSHP->pabyRec + 8 + 28, 8 );
if( bBigEndian ) {
SwapWord( 8, &shape->bounds.minx);
SwapWord( 8, &shape->bounds.miny);
SwapWord( 8, &shape->bounds.maxx);
SwapWord( 8, &shape->bounds.maxy);
}
memcpy( &nPoints, psSHP->pabyRec + 40 + 8, 4 );
memcpy( &nParts, psSHP->pabyRec + 36 + 8, 4 );
if( bBigEndian ) {
nPoints = SWAP_FOUR_BYTES(nPoints);
nParts = SWAP_FOUR_BYTES(nParts);
}
if (nPoints < 0 || nParts < 0 ||
nPoints > 50 * 1000 * 1000 || nParts > 10 * 1000 * 1000)
{
shape->type = MS_SHAPE_NULL;
msSetError(MS_SHPERR, "Corrupted feature encountered. hEntity = %d, nPoints =%d, nParts = %d", "msSHPReadShape()",
hEntity, nPoints, nParts);
return;
}
/* -------------------------------------------------------------------- */
/* Copy out the part array from the record. */
/* -------------------------------------------------------------------- */
if( psSHP->nPartMax < nParts ) {
psSHP->panParts = (int *) SfRealloc(psSHP->panParts, nParts * sizeof(int) );
if (psSHP->panParts == NULL)
{
/* Reallocate previous successfull size for following features */
psSHP->panParts = (int *) msSmallMalloc(psSHP->nPartMax * sizeof(int) );
shape->type = MS_SHAPE_NULL;
msSetError(MS_MEMERR, "Out of memory. Cannot allocate %d bytes. Probably broken shapefile at feature %d",
"msSHPReadShape()", nParts * sizeof(int), hEntity);
return;
}
psSHP->nPartMax = nParts;
}
if (psSHP->panParts == NULL)
{
shape->type = MS_SHAPE_NULL;
msSetError(MS_MEMERR, "Out of memory", "msSHPReadShape()");
return;
}
/* With the previous checks on nPoints and nParts, */
/* we should not overflow here and after */
/* since 50 M * (16 + 8 + 8) = 1 600 MB */
if (44 + 8 + 4 * nParts + 16 * nPoints > nEntitySize)
{
shape->type = MS_SHAPE_NULL;
msSetError(MS_SHPERR, "Corrupted .shp file : shape %d, nPoints=%d, nParts=%d.",
"msSHPReadShape()", hEntity, nPoints, nParts);
return;
}
memcpy( psSHP->panParts, psSHP->pabyRec + 44 + 8, 4 * nParts );
if( bBigEndian ) {
for( i = 0; i < nParts; i++ ) {
*(psSHP->panParts+i) = SWAP_FOUR_BYTES(*(psSHP->panParts+i));
}
}
/* -------------------------------------------------------------------- */
/* Fill the shape structure. */
/* -------------------------------------------------------------------- */
shape->line = (lineObj *)malloc(sizeof(lineObj)*nParts);
MS_CHECK_ALLOC_NO_RET(shape->line, sizeof(lineObj)*nParts);
shape->numlines = nParts;
k = 0; /* overall point counter */
for( i = 0; i < nParts; i++) {
if( i == nParts-1)
shape->line[i].numpoints = nPoints - psSHP->panParts[i];
else
shape->line[i].numpoints = psSHP->panParts[i+1] - psSHP->panParts[i];
if (shape->line[i].numpoints <= 0)
{
msSetError(MS_SHPERR, "Corrupted .shp file : shape %d, shape->line[%d].numpoints=%d", "msSHPReadShape()",
hEntity, i, shape->line[i].numpoints);
while(--i >= 0)
free(shape->line[i].point);
free(shape->line);
shape->line = NULL;
shape->numlines = 0;
shape->type = MS_SHAPE_NULL;
return;
}
if( (shape->line[i].point = (pointObj *)malloc(sizeof(pointObj)*shape->line[i].numpoints)) == NULL ) {
while(--i >= 0)
free(shape->line[i].point);
free(shape->line);
shape->numlines = 0;
shape->type = MS_SHAPE_NULL;
msSetError(MS_MEMERR, "Out of memory", "msSHPReadShape()");
return;
}
/* nOffset = 44 + 8 + 4*nParts; */
for( j = 0; j < shape->line[i].numpoints; j++ ) {
memcpy(&(shape->line[i].point[j].x), psSHP->pabyRec + 44 + 4*nParts + 8 + k * 16, 8 );
memcpy(&(shape->line[i].point[j].y), psSHP->pabyRec + 44 + 4*nParts + 8 + k * 16 + 8, 8 );
if( bBigEndian ) {
SwapWord( 8, &(shape->line[i].point[j].x) );
SwapWord( 8, &(shape->line[i].point[j].y) );
}
#ifdef USE_POINT_Z_M
/* -------------------------------------------------------------------- */
/* Polygon, Arc with Z values. */
/* -------------------------------------------------------------------- */
shape->line[i].point[j].z = 0.0; /* initialize */
if (psSHP->nShapeType == SHP_POLYGONZ || psSHP->nShapeType == SHP_ARCZ) {
nOffset = 44 + 8 + (4*nParts) + (16*nPoints) ;
if( nEntitySize >= nOffset + 16 + 8*nPoints ) {
memcpy(&(shape->line[i].point[j].z), psSHP->pabyRec + nOffset + 16 + k*8, 8 );
if( bBigEndian ) SwapWord( 8, &(shape->line[i].point[j].z) );
}
}
/* -------------------------------------------------------------------- */
/* Measured arc and polygon support. */
/* -------------------------------------------------------------------- */
shape->line[i].point[j].m = 0; /* initialize */
if (psSHP->nShapeType == SHP_POLYGONM || psSHP->nShapeType == SHP_ARCM) {
nOffset = 44 + 8 + (4*nParts) + (16*nPoints) ;
if( nEntitySize >= nOffset + 16 + 8*nPoints ) {
memcpy(&(shape->line[i].point[j].m), psSHP->pabyRec + nOffset + 16 + k*8, 8 );
if( bBigEndian ) SwapWord( 8, &(shape->line[i].point[j].m) );
}
}
#endif /* USE_POINT_Z_M */
k++;
}
}
if(psSHP->nShapeType == SHP_POLYGON
|| psSHP->nShapeType == SHP_POLYGONZ
|| psSHP->nShapeType == SHP_POLYGONM)
shape->type = MS_SHAPE_POLYGON;
else
shape->type = MS_SHAPE_LINE;
}
/* -------------------------------------------------------------------- */
/* Extract a MultiPoint. */
/* -------------------------------------------------------------------- */
else if( psSHP->nShapeType == SHP_MULTIPOINT || psSHP->nShapeType == SHP_MULTIPOINTM ||
psSHP->nShapeType == SHP_MULTIPOINTZ) {
ms_int32 nPoints;
if (nEntitySize < 44 + 4)
{
shape->type = MS_SHAPE_NULL;
msSetError(MS_SHPERR, "Corrupted feature encountered. recSize of feature %d=%d", "msSHPReadShape()",
hEntity, msSHXReadSize(psSHP, hEntity));
return;
}
/* copy the bounding box */
memcpy( &shape->bounds.minx, psSHP->pabyRec + 8 + 4, 8 );
memcpy( &shape->bounds.miny, psSHP->pabyRec + 8 + 12, 8 );
memcpy( &shape->bounds.maxx, psSHP->pabyRec + 8 + 20, 8 );
memcpy( &shape->bounds.maxy, psSHP->pabyRec + 8 + 28, 8 );
if( bBigEndian ) {
SwapWord( 8, &shape->bounds.minx);
SwapWord( 8, &shape->bounds.miny);
SwapWord( 8, &shape->bounds.maxx);
SwapWord( 8, &shape->bounds.maxy);
}
memcpy( &nPoints, psSHP->pabyRec + 44, 4 );
if( bBigEndian ) nPoints = SWAP_FOUR_BYTES(nPoints);
/* -------------------------------------------------------------------- */
/* Fill the shape structure. */
/* -------------------------------------------------------------------- */
if( (shape->line = (lineObj *)malloc(sizeof(lineObj))) == NULL ) {
shape->type = MS_SHAPE_NULL;
msSetError(MS_MEMERR, "Out of memory", "msSHPReadShape()");
return;
}
if (nPoints < 0 || nPoints > 50 * 1000 * 1000)
{
free(shape->line);
shape->type = MS_SHAPE_NULL;
msSetError(MS_SHPERR, "Corrupted .shp file : shape %d, nPoints=%d.",
"msSHPReadShape()", hEntity, nPoints);
return;
}
nRequiredSize = 48 + nPoints * 16;
if (psSHP->nShapeType == SHP_MULTIPOINTZ || psSHP->nShapeType == SHP_MULTIPOINTM)
nRequiredSize += 16 + nPoints * 8;
if (nRequiredSize > nEntitySize)
{
free(shape->line);
shape->type = MS_SHAPE_NULL;
msSetError(MS_SHPERR, "Corrupted .shp file : shape %d : nPoints = %d, nEntitySize = %d",
"msSHPReadShape()", hEntity, nPoints, nEntitySize);
return;
}
shape->numlines = 1;
shape->line[0].numpoints = nPoints;
shape->line[0].point = (pointObj *) malloc( nPoints * sizeof(pointObj) );
if (shape->line[0].point == NULL)
{
free(shape->line);
shape->numlines = 0;
shape->type = MS_SHAPE_NULL;
msSetError(MS_MEMERR, "Out of memory", "msSHPReadShape()");
return;
}
for( i = 0; i < nPoints; i++ ) {
memcpy(&(shape->line[0].point[i].x), psSHP->pabyRec + 48 + 16 * i, 8 );
memcpy(&(shape->line[0].point[i].y), psSHP->pabyRec + 48 + 16 * i + 8, 8 );
if( bBigEndian ) {
SwapWord( 8, &(shape->line[0].point[i].x) );
SwapWord( 8, &(shape->line[0].point[i].y) );
}
#ifdef USE_POINT_Z_M
/* -------------------------------------------------------------------- */
/* MulipointZ */
/* -------------------------------------------------------------------- */
shape->line[0].point[i].z = 0; /* initialize */
if (psSHP->nShapeType == SHP_MULTIPOINTZ) {
nOffset = 48 + 16*nPoints;
memcpy(&(shape->line[0].point[i].z), psSHP->pabyRec + nOffset + 16 + i*8, 8 );
if( bBigEndian ) SwapWord( 8, &(shape->line[0].point[i].z));
}
/* -------------------------------------------------------------------- */
/* Measured shape : multipont. */
/* -------------------------------------------------------------------- */
shape->line[0].point[i].m = 0; /* initialize */
if (psSHP->nShapeType == SHP_MULTIPOINTM) {
nOffset = 48 + 16*nPoints;
memcpy(&(shape->line[0].point[i].m), psSHP->pabyRec + nOffset + 16 + i*8, 8 );
if( bBigEndian ) SwapWord( 8, &(shape->line[0].point[i].m));
}
#endif /* USE_POINT_Z_M */
}
shape->type = MS_SHAPE_POINT;
}
/* -------------------------------------------------------------------- */
/* Extract a Point. */
/* -------------------------------------------------------------------- */
else if(psSHP->nShapeType == SHP_POINT || psSHP->nShapeType == SHP_POINTM ||
psSHP->nShapeType == SHP_POINTZ) {
if (nEntitySize < 20 + 8)
{
shape->type = MS_SHAPE_NULL;
msSetError(MS_SHPERR, "Corrupted feature encountered. recSize of feature %d=%d", "msSHPReadShape()",
hEntity, msSHXReadSize(psSHP, hEntity));
return;
}
/* -------------------------------------------------------------------- */
/* Fill the shape structure. */
/* -------------------------------------------------------------------- */
shape->line = (lineObj *)malloc(sizeof(lineObj));
MS_CHECK_ALLOC_NO_RET(shape->line, sizeof(lineObj));
shape->numlines = 1;
shape->line[0].numpoints = 1;
shape->line[0].point = (pointObj *) msSmallMalloc(sizeof(pointObj));
memcpy( &(shape->line[0].point[0].x), psSHP->pabyRec + 12, 8 );
memcpy( &(shape->line[0].point[0].y), psSHP->pabyRec + 20, 8 );
if( bBigEndian ) {
SwapWord( 8, &(shape->line[0].point[0].x));
SwapWord( 8, &(shape->line[0].point[0].y));
}
#ifdef USE_POINT_Z_M
/* -------------------------------------------------------------------- */
/* PointZ */
/* -------------------------------------------------------------------- */
shape->line[0].point[0].z = 0; /* initialize */
if (psSHP->nShapeType == SHP_POINTZ) {
nOffset = 20 + 8;
if( nEntitySize >= nOffset + 8 ) {
memcpy(&(shape->line[0].point[0].z), psSHP->pabyRec + nOffset, 8 );
if( bBigEndian ) SwapWord( 8, &(shape->line[0].point[0].z));
}
}
/* -------------------------------------------------------------------- */
/* Measured support : point. */
/* -------------------------------------------------------------------- */
shape->line[0].point[0].m = 0; /* initialize */
if (psSHP->nShapeType == SHP_POINTM) {
nOffset = 20 + 8;
if( nEntitySize >= nOffset + 8 ) {
memcpy(&(shape->line[0].point[0].m), psSHP->pabyRec + nOffset, 8 );
if( bBigEndian ) SwapWord( 8, &(shape->line[0].point[0].m));
}
}
#endif /* USE_POINT_Z_M */
/* set the bounding box to the point */
shape->bounds.minx = shape->bounds.maxx = shape->line[0].point[0].x;
shape->bounds.miny = shape->bounds.maxy = shape->line[0].point[0].y;
shape->type = MS_SHAPE_POINT;
}
shape->index = hEntity;
return;
}
int msSHPReadBounds( SHPHandle psSHP, int hEntity, rectObj *padBounds)
{
/* -------------------------------------------------------------------- */
/* Validate the record/entity number. */
/* -------------------------------------------------------------------- */
if( psSHP->nRecords <= 0 || hEntity < -1 || hEntity >= psSHP->nRecords ) {
padBounds->minx = padBounds->miny = padBounds->maxx = padBounds->maxy = 0.0;
return MS_FAILURE;
}
/* -------------------------------------------------------------------- */
/* If the entity is -1 we fetch the bounds for the whole file. */
/* -------------------------------------------------------------------- */
if( hEntity == -1 ) {
padBounds->minx = psSHP->adBoundsMin[0];
padBounds->miny = psSHP->adBoundsMin[1];
padBounds->maxx = psSHP->adBoundsMax[0];
padBounds->maxy = psSHP->adBoundsMax[1];
} else {
if( msSHXReadSize(psSHP, hEntity) == 4 ) { /* NULL shape */
padBounds->minx = padBounds->miny = padBounds->maxx = padBounds->maxy = 0.0;
return MS_FAILURE;
}
if( psSHP->nShapeType != SHP_POINT && psSHP->nShapeType != SHP_POINTZ && psSHP->nShapeType != SHP_POINTM) {
fseek( psSHP->fpSHP, msSHXReadOffset(psSHP, hEntity) + 12, 0 );
fread( padBounds, sizeof(double)*4, 1, psSHP->fpSHP );
if( bBigEndian ) {
SwapWord( 8, &(padBounds->minx) );
SwapWord( 8, &(padBounds->miny) );
SwapWord( 8, &(padBounds->maxx) );
SwapWord( 8, &(padBounds->maxy) );
}
if(msIsNan(padBounds->minx)) { /* empty shape */
padBounds->minx = padBounds->miny = padBounds->maxx = padBounds->maxy = 0.0;
return MS_FAILURE;
}
} else {
/* -------------------------------------------------------------------- */
/* For points we fetch the point, and duplicate it as the */
/* minimum and maximum bound. */
/* -------------------------------------------------------------------- */
fseek( psSHP->fpSHP, msSHXReadOffset(psSHP, hEntity) + 12, 0 );
fread( padBounds, sizeof(double)*2, 1, psSHP->fpSHP );
if( bBigEndian ) {
SwapWord( 8, &(padBounds->minx) );
SwapWord( 8, &(padBounds->miny) );
}
padBounds->maxx = padBounds->minx;
padBounds->maxy = padBounds->miny;
}
}
return MS_SUCCESS;
}
int msShapefileOpen(shapefileObj *shpfile, char *mode, char *filename, int log_failures)
{
int i;
char *dbfFilename;
size_t bufferSize = 0;
if(!filename) {
if( log_failures )
msSetError(MS_IOERR, "No (NULL) filename provided.", "msShapefileOpen()");
return(-1);
}
/* initialize a few things */
shpfile->status = NULL;
shpfile->lastshape = -1;
shpfile->isopen = MS_FALSE;
/* open the shapefile file (appending ok) and get basic info */
if(!mode)
shpfile->hSHP = msSHPOpen( filename, "rb");
else
shpfile->hSHP = msSHPOpen( filename, mode);
if(!shpfile->hSHP) {
if( log_failures )
msSetError(MS_IOERR, "(%s)", "msShapefileOpen()", filename);
return(-1);
}
strlcpy(shpfile->source, filename, sizeof(shpfile->source));
/* load some information about this shapefile */
msSHPGetInfo( shpfile->hSHP, &shpfile->numshapes, &shpfile->type);
msSHPReadBounds( shpfile->hSHP, -1, &(shpfile->bounds));
bufferSize = strlen(filename)+5;
dbfFilename = (char *)msSmallMalloc(bufferSize);
dbfFilename[0] = '\0';
strcpy(dbfFilename, filename);
/* clean off any extention the filename might have */
for (i = strlen(dbfFilename) - 1;
i > 0 && dbfFilename[i] != '.' && dbfFilename[i] != '/' && dbfFilename[i] != '\\';
i-- ) {}
if( dbfFilename[i] == '.' )
dbfFilename[i] = '\0';
strlcat(dbfFilename, ".dbf", bufferSize);
shpfile->hDBF = msDBFOpen(dbfFilename, "rb");
if(!shpfile->hDBF) {
if( log_failures )
msSetError(MS_IOERR, "(%s)", "msShapefileOpen()", dbfFilename);
free(dbfFilename);
return(-1);
}
free(dbfFilename);
shpfile->isopen = MS_TRUE;
return(0); /* all o.k. */
}
/* Creates a new shapefile */
int msShapefileCreate(shapefileObj *shpfile, char *filename, int type)
{
if(type != SHP_POINT && type != SHP_MULTIPOINT && type != SHP_ARC &&
type != SHP_POLYGON &&
type != SHP_POINTM && type != SHP_MULTIPOINTM &&
type != SHP_ARCM && type != SHP_POLYGONM &&
type != SHP_POINTZ && type != SHP_MULTIPOINTZ &&
type != SHP_ARCZ && type != SHP_POLYGONZ) {
msSetError(MS_SHPERR, "Invalid shape type.", "msNewSHPFile()");
return(-1);
}
/* create the spatial portion */
shpfile->hSHP = msSHPCreate(filename, type);
if(!shpfile->hSHP) {
msSetError(MS_IOERR, "(%s)", "msNewSHPFile()",filename);
return(-1);
}
/* retrieve a few things about this shapefile */
msSHPGetInfo( shpfile->hSHP, &shpfile->numshapes, &shpfile->type);
msSHPReadBounds( shpfile->hSHP, -1, &(shpfile->bounds));
/* initialize a few other things */
shpfile->status = NULL;
shpfile->lastshape = -1;
shpfile->isopen = MS_TRUE;
shpfile->hDBF = NULL; /* XBase file is NOT created here... */
return(0);
}
void msShapefileClose(shapefileObj *shpfile)
{
if (shpfile && shpfile->isopen == MS_TRUE) { /* Silently return if called with NULL shpfile by freeLayer() */
if(shpfile->hSHP) msSHPClose(shpfile->hSHP);
if(shpfile->hDBF) msDBFClose(shpfile->hDBF);
if(shpfile->status) free(shpfile->status);
shpfile->isopen = MS_FALSE;
}
}
/* status array lives in the shpfile, can return MS_SUCCESS/MS_FAILURE/MS_DONE */
int msShapefileWhichShapes(shapefileObj *shpfile, rectObj rect, int debug)
{
int i;
rectObj shaperect;
char *filename;
char *sourcename = 0; /* shape file source string from map file */
char *s = 0; /* pointer to start of '.shp' in source string */
if(shpfile->status) {
free(shpfile->status);
shpfile->status = NULL;
}
shpfile->statusbounds = rect; /* save the search extent */
/* rect and shapefile DON'T overlap... */
if(msRectOverlap(&shpfile->bounds, &rect) != MS_TRUE)
return(MS_DONE);
if(msRectContained(&shpfile->bounds, &rect) == MS_TRUE) {
shpfile->status = msAllocBitArray(shpfile->numshapes);
if(!shpfile->status) {
msSetError(MS_MEMERR, NULL, "msShapefileWhichShapes()");
return(MS_FAILURE);
}
msSetAllBits(shpfile->status, shpfile->numshapes, 1);
}
else {
/* deal with case where sourcename is of the form 'file.shp' */
sourcename = msStrdup(shpfile->source);
/* TODO: need to add case-insensitive handling! */
s = strstr(sourcename, ".shp");
if( s ) *s = '\0';
filename = (char *)malloc(strlen(sourcename)+strlen(MS_INDEX_EXTENSION)+1);
MS_CHECK_ALLOC(filename, strlen(sourcename)+strlen(MS_INDEX_EXTENSION)+1, MS_FAILURE);
sprintf(filename, "%s%s", sourcename, MS_INDEX_EXTENSION);
shpfile->status = msSearchDiskTree(filename, rect, debug);
free(filename);
free(sourcename);
if(shpfile->status) { /* index */
msFilterTreeSearch(shpfile, shpfile->status, rect);
}
else { /* no index */
shpfile->status = msAllocBitArray(shpfile->numshapes);
if(!shpfile->status) {
msSetError(MS_MEMERR, NULL, "msShapefileWhichShapes()");
return(MS_FAILURE);
}
for(i=0;i<shpfile->numshapes;i++) {
if(msSHPReadBounds(shpfile->hSHP, i, &shaperect) == MS_SUCCESS)
if(msRectOverlap(&shaperect, &rect) == MS_TRUE) msSetBit(shpfile->status, i, 1);
}
}
}
shpfile->lastshape = -1;
return(MS_SUCCESS); /* success */
}
/* Return the absolute path to the given layer's tileindex file's directory */
void msTileIndexAbsoluteDir(char *tiFileAbsDir, layerObj *layer)
{
char tiFileAbsPath[MS_MAXPATHLEN];
char *tiFileAbsDirTmp=NULL;
msBuildPath(tiFileAbsPath, layer->map->mappath, layer->tileindex); /* absolute path to tileindex file */
tiFileAbsDirTmp = msGetPath(tiFileAbsPath); /* tileindex file's directory */
strlcpy(tiFileAbsDir, tiFileAbsDirTmp, MS_MAXPATHLEN);
free(tiFileAbsDirTmp);
}
/*
** Build possible paths we might find the tile file at:
** map dir + shape path + filename?
** tile dir + shape path + filename?
** map dir + filename?
**
** Returns
** MS_SUCCESS - found a file
** MS_FAILURE - no file, and map is configured to fail on missing
** MS_DONE - no file, and map is configured to continue on missing
*/
int msTiledSHPTryOpen(shapefileObj *shpfile, layerObj *layer, char *tiFileAbsDir, char *filename) {
char szPath[MS_MAXPATHLEN];
int ignore_missing = msMapIgnoreMissingData(layer->map);
int log_failures = MS_TRUE;
if( ignore_missing == MS_MISSING_DATA_IGNORE )
log_failures = MS_FALSE;
if(msShapefileOpen(shpfile, "rb", msBuildPath3(szPath, layer->map->mappath, layer->map->shapepath, filename), log_failures) == -1) {
if(msShapefileOpen(shpfile, "rb", msBuildPath3(szPath, tiFileAbsDir, layer->map->shapepath, filename), log_failures) == -1) {
if(msShapefileOpen(shpfile, "rb", msBuildPath(szPath, layer->map->mappath, filename), log_failures) == -1) {
if(ignore_missing == MS_MISSING_DATA_FAIL) {
msSetError(MS_IOERR, "Unable to open shapefile '%s' for layer '%s' ... fatal error.", "msTiledSHPTryOpen()", filename, layer->name);
return(MS_FAILURE);
}
else if( ignore_missing == MS_MISSING_DATA_LOG ) {
if( layer->debug || layer->map->debug ) {
msDebug( "Unable to open shapefile '%s' for layer '%s' ... ignoring this missing data.\n", szPath, layer->name );
}
return(MS_DONE);
}
else if( ignore_missing == MS_MISSING_DATA_IGNORE ) {
return(MS_DONE);
}
else {
/* never get here */
msSetError(MS_IOERR, "msIgnoreMissingData returned unexpected value.", "msTiledSHPTryOpen()");
return(MS_FAILURE);
}
}
}
}
return(MS_SUCCESS);
}
int msTiledSHPOpenFile(layerObj *layer)
{
int i;
char *filename, tilename[MS_MAXPATHLEN], szPath[MS_MAXPATHLEN];
char tiFileAbsDir[MS_MAXPATHLEN];
msTiledSHPLayerInfo *tSHP=NULL;
if ( msCheckParentPointer(layer->map,"map")==MS_FAILURE )
return MS_FAILURE;
/* allocate space for a shapefileObj using layer->layerinfo */
tSHP = (msTiledSHPLayerInfo *) malloc(sizeof(msTiledSHPLayerInfo));
MS_CHECK_ALLOC(tSHP, sizeof(msTiledSHPLayerInfo), MS_FAILURE);
tSHP->shpfile = (shapefileObj *) malloc(sizeof(shapefileObj));
if (tSHP->shpfile == NULL)
{
msSetError(MS_MEMERR, "%s: %d: Out of memory allocating %u bytes.\n", "msTiledSHPOpenFile()",
__FILE__, __LINE__, sizeof(shapefileObj));
free(tSHP);
return MS_FAILURE;
}
tSHP->tileshpfile = NULL; /* may need this if not using a tile layer, look for malloc later */
layer->layerinfo = tSHP;
tSHP->tilelayerindex = msGetLayerIndex(layer->map, layer->tileindex);
if(tSHP->tilelayerindex != -1) { /* does the tileindex reference another layer */
int status;
layerObj *tlp;
tlp = (GET_LAYER(layer->map, tSHP->tilelayerindex));
if(tlp->connectiontype != MS_SHAPEFILE) {
msSetError(MS_SDEERR, "Tileindex layer must be a shapefile.", "msTiledSHPOpenFile()");
return(MS_FAILURE);
}
status = msLayerOpen(tlp);
if(status != MS_SUCCESS) return(MS_FAILURE);
/* build item list */
status = msLayerWhichItems(tlp, MS_FALSE, NULL);
if(status != MS_SUCCESS) return(MS_FAILURE);
tSHP->tileshpfile = (shapefileObj *) tlp->layerinfo; /* shapefiles use layerinfo to point to a shapefileObj */
} else { /* or reference a shapefile directly */
/* we need tSHP->tileshpfile if we're not working with a layer */
tSHP->tileshpfile = (shapefileObj *) malloc(sizeof(shapefileObj));
if (tSHP->tileshpfile == NULL)
{
msSetError(MS_MEMERR, "%s: %d: Out of memory allocating %u bytes.\n", "msTiledSHPOpenFile()",
__FILE__, __LINE__, sizeof(shapefileObj));
free(tSHP->shpfile);
free(tSHP);
return MS_FAILURE;
}
if(msShapefileOpen(tSHP->tileshpfile, "rb", msBuildPath3(szPath, layer->map->mappath, layer->map->shapepath, layer->tileindex), MS_TRUE) == -1)
if(msShapefileOpen(tSHP->tileshpfile, "rb", msBuildPath(szPath, layer->map->mappath, layer->tileindex), MS_TRUE) == -1)
return(MS_FAILURE);
}
if((layer->tileitemindex = msDBFGetItemIndex(tSHP->tileshpfile->hDBF, layer->tileitem)) == -1) return(MS_FAILURE);
msTileIndexAbsoluteDir(tiFileAbsDir, layer);
/* position the source at the FIRST tile to use as a template, this is so the functions that fill the iteminfo array have something to work from */
for(i=0; i<tSHP->tileshpfile->numshapes; i++) {
int try_open;
if(!layer->data) /* assume whole filename is in attribute field */
filename = (char*) msDBFReadStringAttribute(tSHP->tileshpfile->hDBF, i, layer->tileitemindex);
else {
snprintf(tilename, sizeof(tilename), "%s/%s", msDBFReadStringAttribute(tSHP->tileshpfile->hDBF, i, layer->tileitemindex) , layer->data);
filename = tilename;
}
if(strlen(filename) == 0) continue; /* check again */
try_open = msTiledSHPTryOpen(tSHP->shpfile, layer, tiFileAbsDir, filename);
if( try_open == MS_DONE )
continue;
else if (try_open == MS_FAILURE )
return(MS_FAILURE);
return(MS_SUCCESS); /* found a template, ok to proceed */
}
msSetError(MS_SHPERR, "Unable to open a single tile to use as a template in layer %s.", "msTiledSHPOpenFile()", layer->name?layer->name:"(null)");
return(MS_FAILURE);
}
int msTiledSHPWhichShapes(layerObj *layer, rectObj rect, int isQuery)
{
int i, status;
char *filename, tilename[MS_MAXPATHLEN];
char tiFileAbsDir[MS_MAXPATHLEN];
msTiledSHPLayerInfo *tSHP=NULL;
if ( msCheckParentPointer(layer->map,"map")==MS_FAILURE )
return MS_FAILURE;
tSHP = layer->layerinfo;
if(!tSHP) {
msSetError(MS_SHPERR, "Tiled shapefile layer has not been opened.", "msTiledSHPWhichShapes()");
return(MS_FAILURE);
}
msShapefileClose(tSHP->shpfile); /* close previously opened files */
if(tSHP->tilelayerindex != -1) { /* does the tileindex reference another layer */
layerObj *tlp;
shapeObj tshape;
tlp = (GET_LAYER(layer->map, tSHP->tilelayerindex));
status= msLayerWhichShapes(tlp, rect, isQuery);
if(status != MS_SUCCESS) return(status); /* could be MS_DONE or MS_FAILURE */
msTileIndexAbsoluteDir(tiFileAbsDir, layer);
msInitShape(&tshape);
while((status = msLayerNextShape(tlp, &tshape)) == MS_SUCCESS) {
int try_open;
/* TODO: seems stupid to read the tileitem seperately from the shape, need to fix msTiledSHPOpenFile */
if(!layer->data) /* assume whole filename is in attribute field */
filename = (char *) msDBFReadStringAttribute(tSHP->tileshpfile->hDBF, tshape.index, layer->tileitemindex);
else {
snprintf(tilename, sizeof(tilename), "%s/%s", msDBFReadStringAttribute(tSHP->tileshpfile->hDBF, tshape.index, layer->tileitemindex) , layer->data);
filename = tilename;
}
if(strlen(filename) == 0) continue; /* check again */
try_open = msTiledSHPTryOpen(tSHP->shpfile, layer, tiFileAbsDir, filename);
if( try_open == MS_DONE )
continue;
else if (try_open == MS_FAILURE )
return(MS_FAILURE);
status = msShapefileWhichShapes(tSHP->shpfile, rect, layer->debug);
if(status == MS_DONE) {
/* Close and continue to next tile */
msShapefileClose(tSHP->shpfile);
continue;
}
else if(status != MS_SUCCESS) {
msShapefileClose(tSHP->shpfile);
return(MS_FAILURE);
}
/* the layer functions keeps track of this */
/* tSHP->tileshpfile->lastshape = tshape.index; */
break;
}
return(status); /* if we reach here we either 1) ran out of tiles or 2) had an error reading a tile */
} else { /* or reference a shapefile directly */
int try_open;
status = msShapefileWhichShapes(tSHP->tileshpfile, rect, layer->debug);
if(status != MS_SUCCESS) return(status); /* could be MS_DONE or MS_FAILURE */
msTileIndexAbsoluteDir(tiFileAbsDir, layer);
/* position the source at the FIRST shapefile */
for(i=0; i<tSHP->tileshpfile->numshapes; i++) {
if(msGetBit(tSHP->tileshpfile->status,i)) {
if(!layer->data) /* assume whole filename is in attribute field */
filename = (char *) msDBFReadStringAttribute(tSHP->tileshpfile->hDBF, i, layer->tileitemindex);
else {
snprintf(tilename, sizeof(tilename), "%s/%s", msDBFReadStringAttribute(tSHP->tileshpfile->hDBF, i, layer->tileitemindex) , layer->data);
filename = tilename;
}
if(strlen(filename) == 0) continue; /* check again */
try_open = msTiledSHPTryOpen(tSHP->shpfile, layer, tiFileAbsDir, filename);
if( try_open == MS_DONE )
continue;
else if (try_open == MS_FAILURE )
return(MS_FAILURE);
status = msShapefileWhichShapes(tSHP->shpfile, rect, layer->debug);
if(status == MS_DONE) {
/* Close and continue to next tile */
msShapefileClose(tSHP->shpfile);
continue;
}
else if(status != MS_SUCCESS) {
msShapefileClose(tSHP->shpfile);
return(MS_FAILURE);
}
tSHP->tileshpfile->lastshape = i;
break;
}
}
if(i == tSHP->tileshpfile->numshapes)
return(MS_DONE); /* no more tiles */
else
return(MS_SUCCESS);
}
return(MS_FAILURE); /* should *never* get here */
}
int msTiledSHPNextShape(layerObj *layer, shapeObj *shape)
{
int i, status, filter_passed = MS_FALSE;
char *filename, tilename[MS_MAXPATHLEN];
char tiFileAbsDir[MS_MAXPATHLEN];
msTiledSHPLayerInfo *tSHP=NULL;
if ( msCheckParentPointer(layer->map,"map")==MS_FAILURE )
return MS_FAILURE;
tSHP = layer->layerinfo;
if(!tSHP) {
msSetError(MS_SHPERR, "Tiled shapefile layer has not been opened.", "msTiledSHPNextShape()");
return(MS_FAILURE);
}
msTileIndexAbsoluteDir(tiFileAbsDir, layer);
do {
i = tSHP->shpfile->lastshape + 1;
while(i<tSHP->shpfile->numshapes && !msGetBit(tSHP->shpfile->status,i)) i++; /* next "in" shape */
if(i == tSHP->shpfile->numshapes) { /* done with this tile, need a new one */
msShapefileClose(tSHP->shpfile); /* clean up */
/* position the source to the NEXT shapefile based on the tileindex */
if(tSHP->tilelayerindex != -1) { /* does the tileindex reference another layer */
layerObj *tlp;
shapeObj tshape;
int try_open;
tlp = (GET_LAYER(layer->map, tSHP->tilelayerindex));
msInitShape(&tshape);
while((status = msLayerNextShape(tlp, &tshape)) == MS_SUCCESS) {
/* TODO: seems stupid to read the tileitem seperately from the shape, need to fix msTiledSHPOpenFile */
if(!layer->data) /* assume whole filename is in attribute field */
filename = (char *) msDBFReadStringAttribute(tSHP->tileshpfile->hDBF, tshape.index, layer->tileitemindex);
else {
snprintf(tilename, sizeof(tilename),"%s/%s", msDBFReadStringAttribute(tSHP->tileshpfile->hDBF, tshape.index, layer->tileitemindex) , layer->data);
filename = tilename;
}
if(strlen(filename) == 0) continue; /* check again */
try_open = msTiledSHPTryOpen(tSHP->shpfile, layer, tiFileAbsDir, filename);
if( try_open == MS_DONE )
continue;
else if (try_open == MS_FAILURE )
return(MS_FAILURE);
status = msShapefileWhichShapes(tSHP->shpfile, tSHP->tileshpfile->statusbounds, layer->debug);
if(status == MS_DONE) {
/* Close and continue to next tile */
msShapefileClose(tSHP->shpfile);
continue;
} else if(status != MS_SUCCESS) {
msShapefileClose(tSHP->shpfile);
return(MS_FAILURE);
}
/* the layer functions keeps track of this */
/* tSHP->tileshpfile->lastshape = tshape.index; */
break;
}
if(status == MS_DONE) return(MS_DONE); /* no more tiles */
else {
msFreeShape(&tshape);
continue; /* we've got shapes */
}
} else { /* or reference a shapefile directly */
for(i=(tSHP->tileshpfile->lastshape + 1); i<tSHP->tileshpfile->numshapes; i++) {
if(msGetBit(tSHP->tileshpfile->status,i)) {
int try_open;
if(!layer->data) /* assume whole filename is in attribute field */
filename = (char*)msDBFReadStringAttribute(tSHP->tileshpfile->hDBF, i, layer->tileitemindex);
else {
snprintf(tilename, sizeof(tilename),"%s/%s", msDBFReadStringAttribute(tSHP->tileshpfile->hDBF, i, layer->tileitemindex) , layer->data);
filename = tilename;
}
if(strlen(filename) == 0) continue; /* check again */
try_open = msTiledSHPTryOpen(tSHP->shpfile, layer, tiFileAbsDir, filename);
if( try_open == MS_DONE )
continue;
else if (try_open == MS_FAILURE )
return(MS_FAILURE);
status = msShapefileWhichShapes(tSHP->shpfile, tSHP->tileshpfile->statusbounds, layer->debug);
if(status == MS_DONE) {
/* Close and continue to next tile */
msShapefileClose(tSHP->shpfile);
continue;
} else if(status != MS_SUCCESS) {
msShapefileClose(tSHP->shpfile);
return(MS_FAILURE);
}
tSHP->tileshpfile->lastshape = i;
break;
}
} /* end for loop */
if(i == tSHP->tileshpfile->numshapes) return(MS_DONE); /* no more tiles */
else continue; /* we've got shapes */
}
}
tSHP->shpfile->lastshape = i;
msSHPReadShape(tSHP->shpfile->hSHP, i, shape);
if(shape->type == MS_SHAPE_NULL) {
msFreeShape(shape);
continue; /* skip NULL shapes */
}
shape->tileindex = tSHP->tileshpfile->lastshape;
shape->values = msDBFGetValueList(tSHP->shpfile->hDBF, i, layer->iteminfo, layer->numitems);
shape->numvalues = layer->numitems;
filter_passed = MS_TRUE; /* By default accept ANY shape */
if(layer->numitems > 0 && layer->iteminfo) {
filter_passed = msEvalExpression(layer, shape, &(layer->filter), layer->filteritemindex);
}
if(!filter_passed) msFreeShape(shape); /* free's values as well */
} while(!filter_passed); /* Loop until both spatial and attribute filters match */
return(MS_SUCCESS);
}
int msTiledSHPGetShape(layerObj *layer, shapeObj *shape, resultObj *record)
{
char *filename, tilename[MS_MAXPATHLEN], szPath[MS_MAXPATHLEN];
msTiledSHPLayerInfo *tSHP=NULL;
char tiFileAbsDir[MS_MAXPATHLEN];
long shapeindex = record->shapeindex;
int tileindex = record->tileindex;
if ( msCheckParentPointer(layer->map,"map")==MS_FAILURE )
return MS_FAILURE;
tSHP = layer->layerinfo;
if(!tSHP) {
msSetError(MS_SHPERR, "Tiled shapefile layer has not been opened.", "msTiledSHPGetShape()");
return(MS_FAILURE);
}
if((tileindex < 0) || (tileindex >= tSHP->tileshpfile->numshapes)) return(MS_FAILURE); /* invalid tile id */
if(tileindex != tSHP->tileshpfile->lastshape) { /* correct tile is not currenly open so open the correct tile */
msShapefileClose(tSHP->shpfile); /* close current tile */
if(!layer->data) /* assume whole filename is in attribute field */
filename = (char*) msDBFReadStringAttribute(tSHP->tileshpfile->hDBF, tileindex, layer->tileitemindex);
else {
snprintf(tilename, sizeof(tilename), "%s/%s", msDBFReadStringAttribute(tSHP->tileshpfile->hDBF, tileindex, layer->tileitemindex) , layer->data);
filename = tilename;
}
/* open the shapefile, since a specific tile was request an error should be generated if that tile does not exist */
if(strlen(filename) == 0) return(MS_FAILURE);
if(msShapefileOpen(tSHP->shpfile, "rb", msBuildPath3(szPath, tiFileAbsDir, layer->map->shapepath, filename), MS_TRUE) == -1) {
if(msShapefileOpen(tSHP->shpfile, "rb", msBuildPath3(szPath, layer->map->mappath, layer->map->shapepath, filename), MS_TRUE) == -1) {
if(msShapefileOpen(tSHP->shpfile, "rb", msBuildPath(szPath, layer->map->mappath, filename), MS_TRUE) == -1) {
return(MS_FAILURE);
}
}
}
}
if((shapeindex < 0) || (shapeindex >= tSHP->shpfile->numshapes)) return(MS_FAILURE);
msSHPReadShape(tSHP->shpfile->hSHP, shapeindex, shape);
tSHP->shpfile->lastshape = shapeindex;
if(layer->numitems > 0 && layer->iteminfo) {
shape->numvalues = layer->numitems;
shape->values = msDBFGetValueList(tSHP->shpfile->hDBF, shapeindex, layer->iteminfo, layer->numitems);
if(!shape->values) return(MS_FAILURE);
}
shape->tileindex = tileindex;
return(MS_SUCCESS);
}
void msTiledSHPClose(layerObj *layer)
{
msTiledSHPLayerInfo *tSHP=NULL;
tSHP = layer->layerinfo;
if(tSHP) {
msShapefileClose(tSHP->shpfile);
free(tSHP->shpfile);
if(tSHP->tilelayerindex != -1) {
layerObj *tlp;
if ( msCheckParentPointer(layer->map,"map")==MS_FAILURE )
return;
tlp = (GET_LAYER(layer->map, tSHP->tilelayerindex));
msLayerClose(tlp);
} else {
msShapefileClose(tSHP->tileshpfile);
free(tSHP->tileshpfile);
}
free(tSHP);
}
layer->layerinfo = NULL;
}
/************************************************************************/
/* msTiledSHPClose() */
/* Overloaded version of msTiledSHPClose for virtual table architecture */
/************************************************************************/
int msTiledSHPCloseVT(layerObj *layer)
{
msTiledSHPClose(layer);
return MS_SUCCESS;
}
void msTiledSHPLayerFreeItemInfo(layerObj *layer)
{
if(layer->iteminfo) {
free(layer->iteminfo);
layer->iteminfo = NULL;
}
}
int msTiledSHPLayerInitItemInfo(layerObj *layer)
{
msTiledSHPLayerInfo *tSHP=NULL;
tSHP = layer->layerinfo;
if(!tSHP) {
msSetError(MS_SHPERR, "Tiled shapefile layer has not been opened.", "msTiledSHPLayerInitItemInfo()");
return MS_FAILURE;
}
msTiledSHPLayerFreeItemInfo(layer);
layer->iteminfo = (int *) msDBFGetItemIndexes(tSHP->shpfile->hDBF, layer->items, layer->numitems);
if(!layer->iteminfo) return(MS_FAILURE);
return MS_SUCCESS;
}
static void msSHPPassThroughFieldDefinitions( layerObj *layer, DBFHandle hDBF )
{
int numitems, i;
numitems = msDBFGetFieldCount( hDBF );
for(i=0;i<numitems;i++)
{
char item[16];
int nWidth=0, nPrecision=0;
char md_item_name[64];
char gml_width[32], gml_precision[32];
DBFFieldType eType;
const char *gml_type = NULL;
eType = msDBFGetFieldInfo( hDBF, i, item, &nWidth, &nPrecision );
gml_width[0] = '\0';
gml_precision[0] = '\0';
switch( eType )
{
case FTInteger:
gml_type = "Integer";
sprintf( gml_width, "%d", nWidth );
break;
case FTDouble:
gml_type = "Real";
sprintf( gml_width, "%d", nWidth );
sprintf( gml_precision, "%d", nPrecision );
break;
case FTString:
default:
gml_type = "Character";
sprintf( gml_width, "%d", nWidth );
break;
}
snprintf( md_item_name, sizeof(md_item_name), "gml_%s_type", item );
if( msOWSLookupMetadata(&(layer->metadata), "G", "type") == NULL )
msInsertHashTable(&(layer->metadata), md_item_name, gml_type );
snprintf( md_item_name, sizeof(md_item_name), "gml_%s_width", item );
if( strlen(gml_width) > 0
&& msOWSLookupMetadata(&(layer->metadata), "G", "width") == NULL )
msInsertHashTable(&(layer->metadata), md_item_name, gml_width );
snprintf( md_item_name, sizeof(md_item_name), "gml_%s_precision",item );
if( strlen(gml_precision) > 0
&& msOWSLookupMetadata(&(layer->metadata), "G", "precision")==NULL )
msInsertHashTable(&(layer->metadata), md_item_name, gml_precision );
}
}
int msTiledSHPLayerGetItems(layerObj *layer)
{
msTiledSHPLayerInfo *tSHP=NULL;
const char *value;
tSHP = layer->layerinfo;
if(!tSHP) {
msSetError(MS_SHPERR, "Tiled shapefile layer has not been opened.", "msTiledSHPLayerGetItems()");
return MS_FAILURE;
}
layer->numitems = msDBFGetFieldCount(tSHP->shpfile->hDBF);
layer->items = msDBFGetItems(tSHP->shpfile->hDBF);
if(!layer->items) return MS_FAILURE;
/* -------------------------------------------------------------------- */
/* consider populating the field definitions in metadata. */
/* -------------------------------------------------------------------- */
if((value = msOWSLookupMetadata(&(layer->metadata), "G", "types")) != NULL
&& strcasecmp(value,"auto") == 0 )
msSHPPassThroughFieldDefinitions( layer, tSHP->shpfile->hDBF );
return msTiledSHPLayerInitItemInfo(layer);
}
int msTiledSHPLayerGetExtent(layerObj *layer, rectObj *extent)
{
msTiledSHPLayerInfo *tSHP=NULL;
tSHP = layer->layerinfo;
if(!tSHP) {
msSetError(MS_SHPERR, "Tiled shapefile layer has not been opened.", "msTiledSHPLayerGetExtent()");
return MS_FAILURE;
}
*extent = tSHP->tileshpfile->bounds;
return MS_SUCCESS;
}
int msTiledSHPLayerIsOpen(layerObj *layer)
{
if(layer->layerinfo)
return MS_TRUE;
else
return MS_FALSE;
}
int msTiledSHPLayerSupportsCommonFilters(layerObj *layer)
{
return MS_TRUE;
}
int msTiledSHPLayerInitializeVirtualTable(layerObj *layer)
{
assert(layer != NULL);
assert(layer->vtable != NULL);
layer->vtable->LayerSupportsCommonFilters = msTiledSHPLayerSupportsCommonFilters;
layer->vtable->LayerInitItemInfo = msTiledSHPLayerInitItemInfo;
layer->vtable->LayerFreeItemInfo = msTiledSHPLayerFreeItemInfo;
layer->vtable->LayerOpen = msTiledSHPOpenFile;
layer->vtable->LayerIsOpen = msTiledSHPLayerIsOpen;
layer->vtable->LayerWhichShapes = msTiledSHPWhichShapes;
layer->vtable->LayerNextShape = msTiledSHPNextShape;
// layer->vtable->LayerResultsGetShape = msTiledSHPGetShape; /* no special version, use ...GetShape() */
layer->vtable->LayerGetShape = msTiledSHPGetShape;
layer->vtable->LayerClose = msTiledSHPCloseVT;
layer->vtable->LayerGetItems = msTiledSHPLayerGetItems;
layer->vtable->LayerGetExtent = msTiledSHPLayerGetExtent;
/* layer->vtable->LayerApplyFilterToLayer, use default */
/* layer->vtable->LayerGetAutoStyle, use default */
/* layer->vtable->LayerCloseConnection, use default */;
layer->vtable->LayerSetTimeFilter = msLayerMakeBackticsTimeFilter;
/* layer->vtable->LayerCreateItems, use default */
/* layer->vtable->LayerGetNumFeatures, use default */
/* layer->vtable->LayerGetAutoProjection, use defaut*/
return MS_SUCCESS;
}
/* SHAPEFILE Layer virtual table functions */
void msSHPLayerFreeItemInfo(layerObj *layer)
{
if(layer->iteminfo) {
free(layer->iteminfo);
layer->iteminfo = NULL;
}
}
int msSHPLayerInitItemInfo(layerObj *layer)
{
shapefileObj *shpfile = shpfile = layer->layerinfo;
if( ! shpfile) {
msSetError(MS_SHPERR, "Shapefile layer has not been opened.", "msSHPLayerInitItemInfo()");
return MS_FAILURE;
}
/* iteminfo needs to be a bit more complex, a list of indexes plus the length of the list */
msSHPLayerFreeItemInfo(layer);
layer->iteminfo = (int *) msDBFGetItemIndexes(shpfile->hDBF, layer->items, layer->numitems);
if( ! layer->iteminfo) {
return MS_FAILURE;
}
return MS_SUCCESS;
}
int msSHPLayerOpen(layerObj *layer)
{
char szPath[MS_MAXPATHLEN];
shapefileObj *shpfile;
if(layer->layerinfo) return MS_SUCCESS; /* layer already open */
/* allocate space for a shapefileObj using layer->layerinfo */
shpfile = (shapefileObj *) malloc(sizeof(shapefileObj));
MS_CHECK_ALLOC(shpfile, sizeof(shapefileObj), MS_FAILURE);
if ( msCheckParentPointer(layer->map,"map")==MS_FAILURE )
return MS_FAILURE;
layer->layerinfo = shpfile;
if(msShapefileOpen(shpfile, "rb", msBuildPath3(szPath, layer->map->mappath, layer->map->shapepath, layer->data), MS_TRUE) == -1) {
if(msShapefileOpen(shpfile, "rb", msBuildPath(szPath, layer->map->mappath, layer->data), MS_TRUE) == -1) {
layer->layerinfo = NULL;
free(shpfile);
return MS_FAILURE;
}
}
return MS_SUCCESS;
}
int msSHPLayerIsOpen(layerObj *layer)
{
if(layer->layerinfo)
return MS_TRUE;
else
return MS_FALSE;
}
int msSHPLayerWhichShapes(layerObj *layer, rectObj rect, int isQuery)
{
int i, n1=0, n2=0;
int status;
shapefileObj *shpfile;
shpfile = layer->layerinfo;
if(!shpfile) {
msSetError(MS_SHPERR, "Shapefile layer has not been opened.", "msSHPLayerWhichShapes()");
return MS_FAILURE;
}
status = msShapefileWhichShapes(shpfile, rect, layer->debug);
if(status != MS_SUCCESS) {
return status;
}
/* now apply the maxshapes criteria (NOTE: this ignores the filter so you could get less than maxfeatures) */
if(layer->maxfeatures > 0) {
for( i = (shpfile->numshapes - 1); i >= 0; i-- ) {
n2 = msGetBit(shpfile->status, i);
n1 += n2;
if( n2 && n1 > layer->maxfeatures ) {
msSetBit(shpfile->status, i, 0);
}
}
}
return MS_SUCCESS;
}
int msSHPLayerNextShape(layerObj *layer, shapeObj *shape)
{
int i, filter_passed=MS_FALSE;
shapefileObj *shpfile;
shpfile = layer->layerinfo;
if(!shpfile) {
msSetError(MS_SHPERR, "Shapefile layer has not been opened.", "msSHPLayerNextShape()");
return MS_FAILURE;
}
do {
i = msGetNextBit(shpfile->status, shpfile->lastshape + 1, shpfile->numshapes);
shpfile->lastshape = i;
if(i == -1) return(MS_DONE); /* nothing else to read */
msSHPReadShape(shpfile->hSHP, i, shape);
if(shape->type == MS_SHAPE_NULL) {
msFreeShape(shape);
continue; /* skip NULL shapes */
}
shape->values = msDBFGetValueList(shpfile->hDBF, i, layer->iteminfo, layer->numitems);
shape->numvalues = layer->numitems;
filter_passed = MS_TRUE; /* By default accept ANY shape */
if(layer->numitems > 0 && layer->iteminfo) {
filter_passed = msEvalExpression(layer, shape, &(layer->filter), layer->filteritemindex);
}
if(!filter_passed) msFreeShape(shape);
} while(!filter_passed); /* Loop until both spatial and attribute filters match */
return MS_SUCCESS;
}
int msSHPLayerGetShape(layerObj *layer, shapeObj *shape, resultObj *record)
{
shapefileObj *shpfile;
long shapeindex;
shpfile = layer->layerinfo;
shapeindex = record->shapeindex;
if(!shpfile) {
msSetError(MS_SHPERR, "Shapefile layer has not been opened.", "msSHPLayerGetShape()");
return MS_FAILURE;
}
/* msSHPReadShape *should* return success or failure so we don't have to test here */
if(shapeindex < 0 || shapeindex >= shpfile->numshapes) {
msSetError(MS_MISCERR, "Invalid feature id.", "msSHPLayerGetShape()");
return MS_FAILURE;
}
msSHPReadShape(shpfile->hSHP, shapeindex, shape);
if(layer->numitems > 0 && layer->iteminfo) {
shape->numvalues = layer->numitems;
shape->values = msDBFGetValueList(shpfile->hDBF, shapeindex, layer->iteminfo, layer->numitems);
if(!shape->values) return MS_FAILURE;
}
return MS_SUCCESS;
}
int msSHPLayerClose(layerObj *layer)
{
shapefileObj *shpfile;
shpfile = layer->layerinfo;
if(!shpfile) return MS_SUCCESS; /* nothing to do */
msShapefileClose(shpfile);
free(layer->layerinfo);
layer->layerinfo = NULL;
return MS_SUCCESS;
}
int msSHPLayerGetItems(layerObj *layer)
{
shapefileObj *shpfile;
const char *value;
shpfile = layer->layerinfo;
if(!shpfile) {
msSetError(MS_SHPERR, "Shapefile layer has not been opened.", "msSHPLayerGetItems()");
return MS_FAILURE;
}
layer->numitems = msDBFGetFieldCount(shpfile->hDBF);
layer->items = msDBFGetItems(shpfile->hDBF);
if(layer->numitems == 0) return MS_SUCCESS; /* No items is a valid case (#3147) */
if(!layer->items) return MS_FAILURE;
/* -------------------------------------------------------------------- */
/* consider populating the field definitions in metadata. */
/* -------------------------------------------------------------------- */
if((value = msOWSLookupMetadata(&(layer->metadata), "G", "types")) != NULL
&& strcasecmp(value,"auto") == 0 )
msSHPPassThroughFieldDefinitions( layer, shpfile->hDBF );
return msLayerInitItemInfo(layer);
}
int msSHPLayerGetExtent(layerObj *layer, rectObj *extent)
{
*extent = ((shapefileObj*)layer->layerinfo)->bounds;
return MS_SUCCESS;
}
int msSHPLayerSupportsCommonFilters(layerObj *layer)
{
return MS_TRUE;
}
int msSHPLayerInitializeVirtualTable(layerObj *layer)
{
assert(layer != NULL);
assert(layer->vtable != NULL);
layer->vtable->LayerSupportsCommonFilters = msSHPLayerSupportsCommonFilters;
layer->vtable->LayerInitItemInfo = msSHPLayerInitItemInfo;
layer->vtable->LayerFreeItemInfo = msSHPLayerFreeItemInfo;
layer->vtable->LayerOpen = msSHPLayerOpen;
layer->vtable->LayerIsOpen = msSHPLayerIsOpen;
layer->vtable->LayerWhichShapes = msSHPLayerWhichShapes;
layer->vtable->LayerNextShape = msSHPLayerNextShape;
layer->vtable->LayerGetShape = msSHPLayerGetShape;
layer->vtable->LayerClose = msSHPLayerClose;
layer->vtable->LayerGetItems = msSHPLayerGetItems;
layer->vtable->LayerGetExtent = msSHPLayerGetExtent;
/* layer->vtable->LayerGetAutoStyle, use default */
/* layer->vtable->LayerCloseConnection, use default */
layer->vtable->LayerSetTimeFilter = msLayerMakeBackticsTimeFilter;
/* layer->vtable->LayerApplyFilterToLayer, use default */
/* layer->vtable->LayerCreateItems, use default */
/* layer->vtable->LayerGetNumFeatures, use default */
return MS_SUCCESS;
}
|