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
|
/******************************************************************************
* $Id: maputil.c 10305 2010-07-08 20:04:54Z dmorissette $
*
* Project: MapServer
* Purpose: Various utility functions ... a real hodgepodge.
* Author: Steve Lime and the MapServer team.
*
* Notes: Some code (notably msAlphaBlend()) are directly derived from GD. See
* the mapserver/GD-COPYING file for the GD license. Use of this code in this
* manner is compatible with the MapServer license.
*
******************************************************************************
* 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 <time.h>
#include "mapserver.h"
#include "maptime.h"
#include "mapparser.h"
#include "mapthread.h"
#include "mapfile.h"
#include "mapcopy.h"
#ifdef _WIN32
#include <fcntl.h>
#include <io.h>
#endif
#if defined(_WIN32) && !defined(__CYGWIN__)
#include <process.h>
#endif
MS_CVSID("$Id: maputil.c 10305 2010-07-08 20:04:54Z dmorissette $")
extern int msyyparse(void);
extern int msyylex(void);
extern int msyylex_destroy(void);
extern char *msyytext;
extern int msyyresult; /* result of parsing, true/false */
extern int msyystate;
extern char *msyystring;
/*
** Helper functions to convert from strings to other types or objects.
*/
static int bindIntegerAttribute(int *attribute, char *value)
{
if(!value || strlen(value) == 0) return MS_FAILURE;
*attribute = MS_NINT(atof(value)); /*use atof instead of atoi as a fix for bug 2394*/
return MS_SUCCESS;
}
static int bindDoubleAttribute(double *attribute, char *value)
{
if(!value || strlen(value) == 0) return MS_FAILURE;
*attribute = atof(value);
return MS_SUCCESS;
}
static int bindColorAttribute(colorObj *attribute, char *value)
{
if(!value || strlen(value) == 0) return MS_FAILURE;
if(value[0] == '#' && strlen(value) == 7) { /* got a hex color */
char hex[2];
hex[0] = value[1];
hex[1] = value[2];
attribute->red = msHexToInt(hex);
hex[0] = value[3];
hex[1] = value[4];
attribute->green = msHexToInt(hex);
hex[0] = value[5];
hex[1] = value[6];
attribute->blue = msHexToInt(hex);
return MS_SUCCESS;
} else { /* try a space delimited string */
char **tokens=NULL;
int numtokens=0;
tokens = msStringSplit(value, ' ', &numtokens);
if(tokens==NULL || numtokens != 3) {
msFreeCharArray(tokens, numtokens);
return MS_FAILURE; /* punt */
}
attribute->red = atoi(tokens[0]);
attribute->green = atoi(tokens[1]);
attribute->blue = atoi(tokens[2]);
msFreeCharArray(tokens, numtokens);
return MS_SUCCESS;
}
return MS_FAILURE; /* shouldn't get here */
}
/*
** Function to bind various layer properties to shape attributes.
*/
int msBindLayerToShape(layerObj *layer, shapeObj *shape, int querymapMode)
{
int i, j;
labelObj *label; /* for brevity */
styleObj *style;
if(!layer || !shape) return MS_FAILURE;
for(i=0; i<layer->numclasses; i++) {
/* check the styleObj's */
for(j=0; j<layer->class[i]->numstyles; j++) {
style = layer->class[i]->styles[j];
if(style->numbindings > 0) {
if(style->bindings[MS_STYLE_BINDING_SYMBOL].index != -1) {
style->symbol = msGetSymbolIndex(&(layer->map->symbolset), shape->values[style->bindings[MS_STYLE_BINDING_SYMBOL].index], MS_TRUE);
if(style->symbol == -1) style->symbol = 0; /* a reasonable default (perhaps should throw an error?) */
}
if(style->bindings[MS_STYLE_BINDING_ANGLE].index != -1) {
style->angle = 360.0;
bindDoubleAttribute(&style->angle, shape->values[style->bindings[MS_STYLE_BINDING_ANGLE].index]);
}
if(style->bindings[MS_STYLE_BINDING_SIZE].index != -1) {
style->size = 1;
bindDoubleAttribute(&style->size, shape->values[style->bindings[MS_STYLE_BINDING_SIZE].index]);
}
if(style->bindings[MS_STYLE_BINDING_WIDTH].index != -1) {
style->width = 1;
bindDoubleAttribute(&style->width, shape->values[style->bindings[MS_STYLE_BINDING_WIDTH].index]);
}
if(style->bindings[MS_STYLE_BINDING_COLOR].index != -1 && (querymapMode != MS_TRUE)) {
MS_INIT_COLOR(style->color, -1,-1,-1);
bindColorAttribute(&style->color, shape->values[style->bindings[MS_STYLE_BINDING_COLOR].index]);
}
if(style->bindings[MS_STYLE_BINDING_OUTLINECOLOR].index != -1 && (querymapMode != MS_TRUE)) {
MS_INIT_COLOR(style->outlinecolor, -1,-1,-1);
bindColorAttribute(&style->outlinecolor, shape->values[style->bindings[MS_STYLE_BINDING_OUTLINECOLOR].index]);
}
if(style->bindings[MS_STYLE_BINDING_OUTLINEWIDTH].index != -1) {
style->outlinewidth = 1;
bindDoubleAttribute(&style->outlinewidth, shape->values[style->bindings[MS_STYLE_BINDING_OUTLINEWIDTH].index]);
}
if(style->bindings[MS_STYLE_BINDING_OPACITY].index != -1) {
style->opacity = 100;
bindIntegerAttribute(&style->opacity, shape->values[style->bindings[MS_STYLE_BINDING_OPACITY].index]);
}
}
} /* next styleObj */
/* check the labelObj */
label = &(layer->class[i]->label);
if(label->numbindings > 0) {
if(label->bindings[MS_LABEL_BINDING_ANGLE].index != -1) {
label->angle = 0.0;
bindDoubleAttribute(&label->angle, shape->values[label->bindings[MS_LABEL_BINDING_ANGLE].index]);
}
if(label->bindings[MS_LABEL_BINDING_SIZE].index != -1) {
label->size = 1;
bindDoubleAttribute(&label->size, shape->values[label->bindings[MS_LABEL_BINDING_SIZE].index]);
}
if(label->bindings[MS_LABEL_BINDING_COLOR].index != -1) {
MS_INIT_COLOR(label->color, -1,-1,-1);
bindColorAttribute(&label->color, shape->values[label->bindings[MS_LABEL_BINDING_COLOR].index]);
}
if(label->bindings[MS_LABEL_BINDING_OUTLINECOLOR].index != -1) {
MS_INIT_COLOR(label->outlinecolor, -1,-1,-1);
bindColorAttribute(&label->outlinecolor, shape->values[label->bindings[MS_LABEL_BINDING_OUTLINECOLOR].index]);
}
if(label->bindings[MS_LABEL_BINDING_FONT].index != -1) {
msFree(label->font);
label->font = strdup(shape->values[label->bindings[MS_LABEL_BINDING_FONT].index]);
}
if(label->bindings[MS_LABEL_BINDING_PRIORITY].index != -1) {
label->priority = MS_DEFAULT_LABEL_PRIORITY;
bindIntegerAttribute(&label->priority, shape->values[label->bindings[MS_LABEL_BINDING_PRIORITY].index]);
}
}
} /* next classObj */
return MS_SUCCESS;
}
/*
* Used to get red, green, blue integers separately based upon the color index
*/
int getRgbColor(mapObj *map,int i,int *r,int *g,int *b) {
/* check index range */
int status=1;
*r=*g=*b=-1;
if ((i > 0 ) && (i <= map->palette.numcolors) ) {
*r=map->palette.colors[i-1].red;
*g=map->palette.colors[i-1].green;
*b=map->palette.colors[i-1].blue;
status=0;
}
return status;
}
static int searchContextForTag(mapObj *map, char **ltags, char *tag, char *context, int requires)
{
int i;
if(!context) return MS_FAILURE;
/* printf("\tin searchContextForTag, searching %s for %s\n", context, tag); */
if(strstr(context, tag) != NULL) return MS_SUCCESS; /* found the tag */
/* check referenced layers for the tag too */
for(i=0; i<map->numlayers; i++) {
if(strstr(context, ltags[i]) != NULL) { /* need to check this layer */
if(requires == MS_TRUE) {
if(searchContextForTag(map, ltags, tag, GET_LAYER(map, i)->requires, MS_TRUE) == MS_SUCCESS) return MS_SUCCESS;
} else {
if(searchContextForTag(map, ltags, tag, GET_LAYER(map, i)->labelrequires, MS_FALSE) == MS_SUCCESS) return MS_SUCCESS;
}
}
}
return MS_FAILURE;
}
/*
** Function to take a look at all layers with REQUIRES/LABELREQUIRES set to make sure there are no
** recursive context requirements set (e.g. layer1 requires layer2 and layer2 requires layer1). This
** is bug 1059.
*/
int msValidateContexts(mapObj *map)
{
int i;
char **ltags;
int status = MS_SUCCESS;
ltags = (char **) malloc(map->numlayers*sizeof(char *));
for(i=0; i<map->numlayers; i++) {
if(GET_LAYER(map, i)->name == NULL) {
ltags[i] = strdup("[NULL]");
} else {
ltags[i] = (char *) malloc(sizeof(char)*strlen(GET_LAYER(map, i)->name) + 3);
sprintf(ltags[i], "[%s]", GET_LAYER(map, i)->name);
}
}
/* check each layer's REQUIRES and LABELREQUIRES parameters */
for(i=0; i<map->numlayers; i++) {
/* printf("working on layer %s, looking for references to %s\n", GET_LAYER(map, i)->name, ltags[i]); */
if(searchContextForTag(map, ltags, ltags[i], GET_LAYER(map, i)->requires, MS_TRUE) == MS_SUCCESS) {
msSetError(MS_PARSEERR, "Recursion error found for REQUIRES parameter for layer %s.", "msValidateContexts", GET_LAYER(map, i)->name);
status = MS_FAILURE;
break;
}
if(searchContextForTag(map, ltags, ltags[i], GET_LAYER(map, i)->labelrequires, MS_FALSE) == MS_SUCCESS) {
msSetError(MS_PARSEERR, "Recursion error found for LABELREQUIRES parameter for layer %s.", "msValidateContexts", GET_LAYER(map, i)->name);
status = MS_FAILURE;
break;
}
/* printf("done layer %s\n", GET_LAYER(map, i)->name); */
}
/* clean up */
msFreeCharArray(ltags, map->numlayers);
return status;
}
int msEvalContext(mapObj *map, layerObj *layer, char *context)
{
int i, status;
char *tmpstr1=NULL, *tmpstr2=NULL;
int result; /* result of expression parsing operation */
if(!context) return(MS_TRUE); /* no context requirements */
tmpstr1 = strdup(context);
for(i=0; i<map->numlayers; i++) { /* step through all the layers */
if(layer->index == i) continue; /* skip the layer in question */
if (GET_LAYER(map, i)->name == NULL) continue; /* Layer without name cannot be used in contexts */
tmpstr2 = (char *)malloc(sizeof(char)*strlen(GET_LAYER(map, i)->name) + 3);
sprintf(tmpstr2, "[%s]", GET_LAYER(map, i)->name);
if(strstr(tmpstr1, tmpstr2)) {
if(msLayerIsVisible(map, (GET_LAYER(map, i))))
tmpstr1 = msReplaceSubstring(tmpstr1, tmpstr2, "1");
else
tmpstr1 = msReplaceSubstring(tmpstr1, tmpstr2, "0");
}
free(tmpstr2);
}
msAcquireLock( TLOCK_PARSER );
msyystate = MS_TOKENIZE_EXPRESSION; msyystring = tmpstr1;
status = msyyparse();
result = msyyresult;
msReleaseLock( TLOCK_PARSER );
free(tmpstr1);
if (status != 0) {
msSetError(MS_PARSEERR, "Failed to parse context", "msEvalContext");
return MS_FALSE; /* error in parse */
}
return result;
}
/* msEvalExpression()
*
* Evaluates a mapserver expression for a given set of attribute values and
* returns the result of the expression (MS_TRUE or MS_FALSE)
* May also return MS_FALSE in case of parsing errors or invalid expressions
* (check the error stack if you care)
*
* Parser mutex added for type MS_EXPRESSION -- SG
*/
int msEvalExpression(expressionObj *expression, int itemindex, char **items, int numitems)
{
int i;
char *tmpstr=NULL, *tmpstr2=NULL;
int status;
int expresult; /* result of logical expression parsing operation */
if(!expression->string) return(MS_TRUE); /* empty expressions are ALWAYS true */
switch(expression->type) {
case(MS_STRING):
if(itemindex == -1) {
msSetError(MS_MISCERR, "Cannot evaluate expression, no item index defined.", "msEvalExpression()");
return(MS_FALSE);
}
if(itemindex >= numitems) {
msSetError(MS_MISCERR, "Invalid item index.", "msEvalExpression()");
return(MS_FALSE);
}
if(expression->flags & MS_EXP_INSENSITIVE) {
if(strcasecmp(expression->string, items[itemindex]) == 0) return(MS_TRUE); /* got a match */
} else {
if(strcmp(expression->string, items[itemindex]) == 0) return(MS_TRUE); /* got a match */
}
break;
case(MS_EXPRESSION):
tmpstr = strdup(expression->string);
for(i=0; i<expression->numitems; i++) {
tmpstr2 = strdup(items[expression->indexes[i]]);
tmpstr2 = msReplaceSubstring(tmpstr2, "\'", "\\\'");
tmpstr2 = msReplaceSubstring(tmpstr2, "\"", "\\\"");
tmpstr = msReplaceSubstring(tmpstr, expression->items[i], tmpstr2);
free(tmpstr2);
}
// fprintf(stderr, "exp; %s\n", tmpstr);
msAcquireLock( TLOCK_PARSER );
msyystate = MS_TOKENIZE_EXPRESSION;
msyystring = tmpstr; /* set lexer state to EXPRESSION_STRING */
status = msyyparse();
expresult = msyyresult;
msReleaseLock( TLOCK_PARSER );
if (status != 0) {
msSetError(MS_PARSEERR, "Failed to parse expression: %s", "msEvalExpression", tmpstr);
free(tmpstr);
return MS_FALSE;
}
free(tmpstr);
return expresult;
break;
case(MS_REGEX):
if(itemindex == -1) {
msSetError(MS_MISCERR, "Cannot evaluate expression, no item index defined.", "msEvalExpression()");
return(MS_FALSE);
}
if(itemindex >= numitems) {
msSetError(MS_MISCERR, "Invalid item index.", "msEvalExpression()");
return(MS_FALSE);
}
if(!expression->compiled) {
if(expression->flags & MS_EXP_INSENSITIVE)
{
if(ms_regcomp(&(expression->regex), expression->string, MS_REG_EXTENDED|MS_REG_NOSUB|MS_REG_ICASE) != 0) { /* compile the expression */
msSetError(MS_REGEXERR, "Invalid regular expression.", "msEvalExpression()");
return(MS_FALSE);
}
}
else
{
if(ms_regcomp(&(expression->regex), expression->string, MS_REG_EXTENDED|MS_REG_NOSUB) != 0) { /* compile the expression */
msSetError(MS_REGEXERR, "Invalid regular expression.", "msEvalExpression()");
return(MS_FALSE);
}
}
expression->compiled = MS_TRUE;
}
if(ms_regexec(&(expression->regex), items[itemindex], 0, NULL, 0) == 0) return(MS_TRUE); /* got a match */
break;
}
return(MS_FALSE);
}
/*
* int msShapeGetClass(layerObj *layer, shapeObj *shape)
* {
* int i;
*
* for(i=0; i<layer->numclasses; i++) {
* if(layer->class[i]->status != MS_DELETE && msEvalExpression(&(layer->class[i]->expression), layer->classitemindex, shape->values, layer->numitems) == MS_TRUE)
* return(i);
* }
*
* return(-1); // no match
* }
*/
int *msAllocateValidClassGroups(layerObj *lp, int *nclasses)
{
int *classgroup = NULL;
int nvalidclass = 0, i=0;
if (!lp || !lp->classgroup || lp->numclasses <=0 || !nclasses)
return NULL;
classgroup = (int *)malloc(sizeof(int)*lp->numclasses);
nvalidclass = 0;
for (i=0; i<lp->numclasses; i++)
{
if (lp->class[i]->group && strcasecmp(lp->class[i]->group, lp->classgroup) == 0)
{
classgroup[nvalidclass] = i;
nvalidclass++;
}
}
if (nvalidclass > 0)
{
classgroup = (int *)realloc(classgroup, sizeof(int)*nvalidclass);
*nclasses = nvalidclass;
return classgroup;
}
if (classgroup)
msFree(classgroup);
return NULL;
}
int msShapeGetClass(layerObj *layer, shapeObj *shape, double scaledenom, int *classgroup, int numclasses)
{
int i, iclass;
/* INLINE features do not work with expressions, allow the classindex */
/* value set prior to calling this function to carry through. */
if(layer->connectiontype == MS_INLINE) {
if(shape->classindex < 0 || shape->classindex >= layer->numclasses) return(-1);
if(scaledenom > 0) { /* verify scaledenom here */
if((layer->class[shape->classindex]->maxscaledenom > 0) && (scaledenom > layer->class[shape->classindex]->maxscaledenom))
return(-1); /* can skip this feature */
if((layer->class[shape->classindex]->minscaledenom > 0) && (scaledenom <= layer->class[shape->classindex]->minscaledenom))
return(-1); /* can skip this feature */
}
return(shape->classindex);
}
if (layer->numclasses > 0)
{
if (classgroup == NULL || numclasses <=0)
numclasses = layer->numclasses;
for(i=0; i<numclasses; i++)
{
if (classgroup)
iclass = classgroup[i];
else
iclass = i;
if (iclass < 0 || iclass >= layer->numclasses)
continue; /*this should never happen but just in case*/
if(scaledenom > 0) { /* verify scaledenom here */
if((layer->class[iclass]->maxscaledenom > 0) && (scaledenom > layer->class[iclass]->maxscaledenom))
continue; /* can skip this one, next class */
if((layer->class[iclass]->minscaledenom > 0) && (scaledenom <= layer->class[iclass]->minscaledenom))
continue; /* can skip this one, next class */
}
if(layer->class[iclass]->status != MS_DELETE &&
msEvalExpression(&(layer->class[iclass]->expression),
layer->classitemindex, shape->values, layer->numitems) == MS_TRUE)
{
return(iclass);
}
}
}
return(-1); /* no match */
}
char *msShapeGetAnnotation(layerObj *layer, shapeObj *shape)
{
int i;
char *tmpstr=NULL;
if(layer->class[shape->classindex]->text.string) { /* test for global label first */
tmpstr = strdup(layer->class[shape->classindex]->text.string);
switch(layer->class[shape->classindex]->text.type) {
case(MS_STRING):
break;
case(MS_EXPRESSION):
tmpstr = strdup(layer->class[shape->classindex]->text.string);
for(i=0; i<layer->class[shape->classindex]->text.numitems; i++)
tmpstr = msReplaceSubstring(tmpstr, layer->class[shape->classindex]->text.items[i], shape->values[layer->class[shape->classindex]->text.indexes[i]]);
break;
}
} else {
if (shape->values && layer->labelitemindex >= 0)
tmpstr = strdup(shape->values[layer->labelitemindex]);
}
return(tmpstr);
}
/*
** Adjusts an image size in one direction to fit an extent.
*/
int msAdjustImage(rectObj rect, int *width, int *height)
{
if(*width == -1 && *height == -1) {
msSetError(MS_MISCERR, "Cannot calculate both image height and width.", "msAdjustImage()");
return(-1);
}
if(*width > 0)
*height = MS_NINT((rect.maxy - rect.miny)/((rect.maxx - rect.minx)/(*width)));
else
*width = MS_NINT((rect.maxx - rect.minx)/((rect.maxy - rect.miny)/(*height)));
return(0);
}
/*
** Make sure extent fits image window to be created. Returns cellsize of output image.
*/
double msAdjustExtent(rectObj *rect, int width, int height)
{
double cellsize, ox, oy;
cellsize = MS_MAX(MS_CELLSIZE(rect->minx, rect->maxx, width), MS_CELLSIZE(rect->miny, rect->maxy, height));
if(cellsize <= 0) /* avoid division by zero errors */
return(0);
ox = MS_MAX(((width-1) - (rect->maxx - rect->minx)/cellsize)/2,0); /* these were width-1 and height-1 */
oy = MS_MAX(((height-1) - (rect->maxy - rect->miny)/cellsize)/2,0);
rect->minx = rect->minx - ox*cellsize;
rect->miny = rect->miny - oy*cellsize;
rect->maxx = rect->maxx + ox*cellsize;
rect->maxy = rect->maxy + oy*cellsize;
return(cellsize);
}
/*
** Rect must always contain a portion of bounds. If not, rect is
** shifted to overlap by overlay percent. The dimensions of rect do
** not change but placement relative to bounds can.
*/
int msConstrainExtent(rectObj *bounds, rectObj *rect, double overlay)
{
double offset=0;
/* check left edge, and if necessary the right edge of bounds */
if(rect->maxx <= bounds->minx) {
offset = overlay*(rect->maxx - rect->minx);
rect->minx += offset; /* shift right */
rect->maxx += offset;
} else if(rect->minx >= bounds->maxx) {
offset = overlay*(rect->maxx - rect->minx);
rect->minx -= offset; /* shift left */
rect->maxx -= offset;
}
/* check top edge, and if necessary the bottom edge of bounds */
if(rect->maxy <= bounds->miny) {
offset = overlay*(rect->maxy - rect->miny);
rect->miny -= offset; /* shift down */
rect->maxy -= offset;
} else if(rect->miny >= bounds->maxy) {
offset = overlay*(rect->maxy - rect->miny);
rect->miny += offset; /* shift up */
rect->maxy += offset;
}
return(MS_SUCCESS);
}
/*
** Generic function to save an image to a file.
**
** Note that map may be NULL. If it is set, then it is used for two things:
** - Deal with relative imagepaths (compute absolute path relative to map path)
** - Extract the georeferenced extents and coordinate system
** of the map for writing out with the image when appropriate
** (primarily this means via msSaveImageGDAL() to something like GeoTIFF).
**
** The filename is NULL when the image is supposed to be written to stdout.
*/
int msSaveImage(mapObj *map, imageObj *img, char *filename)
{
int nReturnVal = -1;
char szPath[MS_MAXPATHLEN];
struct mstimeval starttime, endtime;
if(map && map->debug >= MS_DEBUGLEVEL_TUNING)
msGettimeofday(&starttime, NULL);
if (img)
{
if (MS_RENDERER_PLUGIN(img->format)) {
rendererVTableObj *renderer = img->format->vtable;
FILE *stream;
if(filename)
stream = fopen(msBuildPath(szPath, map->mappath, filename),"wb");
else {
if ( msIO_needBinaryStdout() == MS_FAILURE )
return MS_FAILURE;
stream = stdout;
}
if(!stream)
return MS_FAILURE;
if(renderer->supports_pixel_buffer) {
rasterBufferObj data;
renderer->getRasterBuffer(img,&data);
msSaveRasterBuffer(&data,stream,img->format );
} else {
renderer->saveImage(img, stream, img->format);
}
fclose(stream);
return MS_SUCCESS;
}
else if( MS_DRIVER_GD(img->format) )
{
if(map != NULL && filename != NULL )
nReturnVal = msSaveImageGD(img,
msBuildPath(szPath, map->mappath,
filename),
img->format );
else
nReturnVal = msSaveImageGD(img, filename, img->format);
}
#ifdef USE_AGG
else if( MS_DRIVER_AGG(img->format) )
{
if(map != NULL && filename != NULL )
nReturnVal = msSaveImageAGG(img,
msBuildPath(szPath, map->mappath,
filename),
img->format );
else
nReturnVal = msSaveImageAGG(img, filename, img->format);
}
#endif
else if( MS_DRIVER_IMAGEMAP(img->format) )
nReturnVal = msSaveImageIM(img, filename, img->format);
#ifdef USE_GDAL
else if( MS_DRIVER_GDAL(img->format) )
{
if (map != NULL && filename != NULL )
nReturnVal = msSaveImageGDAL(map, img,
msBuildPath(szPath, map->mappath,
filename));
else
nReturnVal = msSaveImageGDAL(map, img, filename);
}
#endif
#ifdef USE_MING_FLASH
else if(MS_DRIVER_SWF(img->format) )
{
if (map != NULL && filename != NULL )
nReturnVal = msSaveImageSWF(img,
msBuildPath(szPath, map->mappath,
filename));
else
nReturnVal = msSaveImageSWF(img, filename);
}
#endif
#ifdef USE_PDF
else if( MS_RENDERER_PDF(img->format) )
{
if (map != NULL && filename != NULL )
nReturnVal = msSaveImagePDF(img,
msBuildPath(szPath, map->mappath,
filename));
else
nReturnVal = msSaveImagePDF(img, filename);
}
#endif
else if(MS_DRIVER_SVG(img->format) )
{
if (map != NULL && filename != NULL )
nReturnVal = msSaveImageSVG(img,
msBuildPath(szPath, map->mappath,
filename));
else
nReturnVal = msSaveImageSVG(img, filename);
}
else
msSetError(MS_MISCERR, "Unknown image type",
"msSaveImage()");
}
if(map && map->debug >= MS_DEBUGLEVEL_TUNING) {
msGettimeofday(&endtime, NULL);
msDebug("msSaveImage() total time: %.3fs\n",
(endtime.tv_sec+endtime.tv_usec/1.0e6)-
(starttime.tv_sec+starttime.tv_usec/1.0e6) );
}
return nReturnVal;
}
/*
** Generic function to save an image to a byte array.
** - the return value is the pointer to the byte array
** - size_ptr contains the number of bytes returned
** - format: the desired output format
**
** The caller is responsible to free the returned array
** The function returns NULL if the output format is not supported.
*/
unsigned char *msSaveImageBuffer(imageObj* image, int *size_ptr, outputFormatObj *format)
{
*size_ptr = 0;
if( MS_RENDERER_PLUGIN(image->format)){
rasterBufferObj data;
rendererVTableObj *renderer = image->format->vtable;
if(renderer->supports_pixel_buffer) {
bufferObj buffer;
msBufferInit(&buffer);
renderer->getRasterBuffer(image,&data);
msSaveRasterBufferToBuffer(&data,&buffer,format);
return buffer.data;
//don't free the bufferObj as we don't own the bytes anymore
} else {
msSetError(MS_MISCERR, "Unsupported image type", "msSaveImageBuffer()");
return NULL;
}
}
else if( MS_DRIVER_GD(image->format) )
{
return msSaveImageBufferGD(image, size_ptr, format);
}
#ifdef USE_AGG
else if( MS_DRIVER_AGG(image->format) )
{
return msSaveImageBufferAGG(image, size_ptr, format);
}
#endif
msSetError(MS_MISCERR, "Unsupported image type", "msSaveImage()");
return NULL;
}
/**
* Generic function to free the imageObj
*/
void msFreeImage(imageObj *image)
{
if (image)
{
if(MS_RENDERER_PLUGIN(image->format)) {
rendererVTableObj *renderer = image->format->vtable;
if(renderer->supports_imagecache) {
tileCacheObj *next,*cur = image->tilecache;
while(cur) {
renderer->freeTile(cur->data);
next = cur->next;
free(cur);
cur = next;
}
image->ntiles = 0;
}
renderer->freeImage(image);
}
else if( MS_RENDERER_GD(image->format) ) {
if( image->img.gd != NULL )
msFreeImageGD(image);
#ifdef USE_AGG
} else if( MS_RENDERER_AGG(image->format) ) {
msFreeImageAGG(image);
#endif
} else if( MS_RENDERER_IMAGEMAP(image->format) )
msFreeImageIM(image);
else if( MS_RENDERER_RAWDATA(image->format) )
msFree(image->img.raw_16bit);
#ifdef USE_MING_FLASH
else if( MS_RENDERER_SWF(image->format) )
msFreeImageSWF(image);
#endif
#ifdef USE_PDF
else if( MS_RENDERER_PDF(image->format) )
msFreeImagePDF(image);
#endif
else if( MS_RENDERER_SVG(image->format) )
msFreeImageSVG(image);
else
msSetError(MS_MISCERR, "Unknown image type",
"msFreeImage()");
if (image->imagepath)
free(image->imagepath);
if (image->imageurl)
free(image->imageurl);
if( --image->format->refcount < 1 )
msFreeOutputFormat( image->format );
image->imagepath = NULL;
image->imageurl = NULL;
msFree( image );
}
}
/*
** Return an array containing all the layer's index given a group name.
** If nothing is found, NULL is returned. The nCount is initalized
** to the number of elements in the returned array.
** Note : the caller of the function should free the array.
*/
int *msGetLayersIndexByGroup(mapObj *map, char *groupname, int *pnCount)
{
int i;
int iLayer = 0;
int *aiIndex;
if(!groupname || !map || !pnCount)
{
return NULL;
}
aiIndex = (int *)malloc(sizeof(int) * map->numlayers);
for(i=0;i<map->numlayers; i++)
{
if(!GET_LAYER(map, i)->group) /* skip it */
continue;
if(strcmp(groupname, GET_LAYER(map, i)->group) == 0)
{
aiIndex[iLayer] = i;
iLayer++;
}
}
if (iLayer == 0)
{
free(aiIndex);
aiIndex = NULL;
*pnCount = 0;
}
else
{
aiIndex = (int *)realloc(aiIndex, sizeof(int)* iLayer);
*pnCount = iLayer;
}
return aiIndex;
}
/* ==================================================================== */
/* Measured shape utility functions. */
/* ==================================================================== */
/************************************************************************/
/* pointObj *msGetPointUsingMeasure(shapeObj *shape, double m) */
/* */
/* Using a measured value get the XY location it corresonds */
/* to. */
/* */
/************************************************************************/
pointObj *msGetPointUsingMeasure(shapeObj *shape, double m)
{
#ifdef USE_POINT_Z_M
pointObj *point = NULL;
lineObj line;
double dfMin = 0;
double dfMax = 0;
int i,j = 0;
int bFound = 0;
double dfFirstPointX = 0;
double dfFirstPointY = 0;
double dfFirstPointM = 0;
double dfSecondPointX = 0;
double dfSecondPointY = 0;
double dfSecondPointM = 0;
double dfCurrentM = 0;
double dfFactor = 0;
if (shape && shape->numlines > 0)
{
/* -------------------------------------------------------------------- */
/* check fir the first value (min) and the last value(max) to */
/* see if the m is contained between these min and max. */
/* -------------------------------------------------------------------- */
line = shape->line[0];
dfMin = line.point[0].m;
line = shape->line[shape->numlines-1];
dfMax = line.point[line.numpoints-1].m;
if (m >= dfMin && m <= dfMax)
{
for (i=0; i<shape->numlines; i++)
{
line = shape->line[i];
for (j=0; j<line.numpoints; j++)
{
dfCurrentM = line.point[j].m;
if (dfCurrentM > m)
{
bFound = 1;
dfSecondPointX = line.point[j].x;
dfSecondPointY = line.point[j].y;
dfSecondPointM = line.point[j].m;
/* -------------------------------------------------------------------- */
/* get the previous node xy values. */
/* -------------------------------------------------------------------- */
if (j > 0) /* not the first point of the line */
{
dfFirstPointX = line.point[j-1].x;
dfFirstPointY = line.point[j-1].y;
dfFirstPointM = line.point[j-1].m;
}
else /* get last point of previous line */
{
dfFirstPointX = shape->line[i-1].point[0].x;
dfFirstPointY = shape->line[i-1].point[0].y;
dfFirstPointM = shape->line[i-1].point[0].m;
}
break;
}
}
}
}
if (!bFound)
return NULL;
/* -------------------------------------------------------------------- */
/* extrapolate the m value to get t he xy coordinate. */
/* -------------------------------------------------------------------- */
if (dfFirstPointM != dfSecondPointM)
dfFactor = (m-dfFirstPointM)/(dfSecondPointM - dfFirstPointM);
else
dfFactor = 0;
point = (pointObj *)malloc(sizeof(pointObj));
point->x = dfFirstPointX + (dfFactor * (dfSecondPointX - dfFirstPointX));
point->y = dfFirstPointY +
(dfFactor * (dfSecondPointY - dfFirstPointY));
point->m = dfFirstPointM +
(dfFactor * (dfSecondPointM - dfFirstPointM));
return point;
}
return NULL;
#else
msSetError(MS_MISCERR,
"The \"m\" parameter for points is unavailable in your build.",
"msGetPointUsingMeasure()");
return NULL;
#endif /* USE_POINT_Z_M */
}
/************************************************************************/
/* IntersectionPointLinepointObj *p, pointObj *a, pointObj *b) */
/* */
/* Retunrs a point object corresponding to the intersection of */
/* point p and a line formed of 2 points : a and b. */
/* */
/* Alorith base on : */
/* http://www.faqs.org/faqs/graphics/algorithms-faq/ */
/* */
/* Subject 1.02:How do I find the distance from a point to a line? */
/* */
/* Let the point be C (Cx,Cy) and the line be AB (Ax,Ay) to (Bx,By).*/
/* Let P be the point of perpendicular projection of C on AB. The parameter*/
/* r, which indicates P's position along AB, is computed by the dot product */
/* of AC and AB divided by the square of the length of AB: */
/* */
/* (1) AC dot AB */
/* r = --------- */
/* ||AB||^2 */
/* */
/* r has the following meaning: */
/* */
/* r=0 P = A */
/* r=1 P = B */
/* r<0 P is on the backward extension of AB */
/* r>1 P is on the forward extension of AB */
/* 0<r<1 P is interior to AB */
/* */
/* The length of a line segment in d dimensions, AB is computed by:*/
/* */
/* L = sqrt( (Bx-Ax)^2 + (By-Ay)^2 + ... + (Bd-Ad)^2) */
/* */
/* so in 2D: */
/* */
/* L = sqrt( (Bx-Ax)^2 + (By-Ay)^2 ) */
/* */
/* and the dot product of two vectors in d dimensions, U dot V is computed:*/
/* */
/* D = (Ux * Vx) + (Uy * Vy) + ... + (Ud * Vd) */
/* */
/* so in 2D: */
/* */
/* D = (Ux * Vx) + (Uy * Vy) */
/* */
/* So (1) expands to: */
/* */
/* (Cx-Ax)(Bx-Ax) + (Cy-Ay)(By-Ay) */
/* r = ------------------------------- */
/* L^2 */
/* */
/* The point P can then be found: */
/* */
/* Px = Ax + r(Bx-Ax) */
/* Py = Ay + r(By-Ay) */
/* */
/* And the distance from A to P = r*L. */
/* */
/* Use another parameter s to indicate the location along PC, with the */
/* following meaning: */
/* s<0 C is left of AB */
/* s>0 C is right of AB */
/* s=0 C is on AB */
/* */
/* Compute s as follows: */
/* */
/* (Ay-Cy)(Bx-Ax)-(Ax-Cx)(By-Ay) */
/* s = ----------------------------- */
/* L^2 */
/* */
/* */
/* Then the distance from C to P = |s|*L. */
/* */
/************************************************************************/
pointObj *msIntersectionPointLine(pointObj *p, pointObj *a, pointObj *b)
{
double r = 0;
double L = 0;
pointObj *result = NULL;
if (p && a && b)
{
L = sqrt(((b->x - a->x)*(b->x - a->x)) +
((b->y - a->y)*(b->y - a->y)));
if (L != 0)
r = ((p->x - a->x)*(b->x - a->x) + (p->y - a->y)*(b->y - a->y))/(L*L);
else
r = 0;
result = (pointObj *)malloc(sizeof(pointObj));
/* -------------------------------------------------------------------- */
/* We want to make sure that the point returned is on the line */
/* */
/* r=0 P = A */
/* r=1 P = B */
/* r<0 P is on the backward extension of AB */
/* r>1 P is on the forward extension of AB */
/* 0<r<1 P is interior to AB */
/* -------------------------------------------------------------------- */
if (r < 0)
{
result->x = a->x;
result->y = a->y;
}
else if (r > 1)
{
result->x = b->x;
result->y = b->y;
}
else
{
result->x = a->x + r*(b->x - a->x);
result->y = a->y + r*(b->y - a->y);
}
#ifdef USE_POINT_Z_M
result->m = 0;
#endif
}
return result;
}
/************************************************************************/
/* pointObj *msGetMeasureUsingPoint(shapeObj *shape, pointObj */
/* *point) */
/* */
/* Calculate the intersection point betwwen the point and the */
/* shape and return the Measured value at the intersection. */
/************************************************************************/
pointObj *msGetMeasureUsingPoint(shapeObj *shape, pointObj *point)
{
double dfMinDist = 1e35;
double dfDist = 0;
pointObj oFirst;
pointObj oSecond;
int i, j = 0;
lineObj line;
pointObj *poIntersectionPt = NULL;
double dfFactor = 0;
double dfDistTotal, dfDistToIntersection = 0;
if (shape && point)
{
for (i=0; i<shape->numlines; i++)
{
line = shape->line[i];
/* -------------------------------------------------------------------- */
/* for each line (2 consecutive lines) get the distance between */
/* the line and the point and determine which line segment is */
/* the closeset to the point. */
/* -------------------------------------------------------------------- */
for (j=0; j<line.numpoints-1; j++)
{
dfDist = msDistancePointToSegment(point, &line.point[j], &line.point[j+1]);
if (dfDist < dfMinDist)
{
oFirst.x = line.point[j].x;
oFirst.y = line.point[j].y;
#ifdef USE_POINT_Z_M
oFirst.m = line.point[j].m;
#endif
oSecond.x = line.point[j+1].x;
oSecond.y = line.point[j+1].y;
#ifdef USE_POINT_Z_M
oSecond.m = line.point[j+1].m;
#endif
dfMinDist = dfDist;
}
}
}
/* -------------------------------------------------------------------- */
/* once we have the nearest segment, look for the x,y location */
/* which is the nearest intersection between the line and the */
/* point. */
/* -------------------------------------------------------------------- */
poIntersectionPt = msIntersectionPointLine(point, &oFirst, &oSecond);
if (poIntersectionPt)
{
dfDistTotal = sqrt(((oSecond.x - oFirst.x)*(oSecond.x - oFirst.x)) +
((oSecond.y - oFirst.y)*(oSecond.y - oFirst.y)));
dfDistToIntersection = sqrt(((poIntersectionPt->x - oFirst.x)*
(poIntersectionPt->x - oFirst.x)) +
((poIntersectionPt->y - oFirst.y)*
(poIntersectionPt->y - oFirst.y)));
dfFactor = dfDistToIntersection / dfDistTotal;
#ifdef USE_POINT_Z_M
poIntersectionPt->m = oFirst.m + (oSecond.m - oFirst.m)*dfFactor;
#endif
return poIntersectionPt;
}
}
return NULL;
}
/* ==================================================================== */
/* End Measured shape utility functions. */
/* ==================================================================== */
char **msGetAllGroupNames(mapObj *map, int *numTok)
{
char **papszGroups = NULL;
int bFound = 0;
int nCount = 0;
int i = 0, j = 0;
*numTok = 0;
if (!map->layerorder)
{
map->layerorder = (int*)malloc(map->numlayers * sizeof(int));
/*
* Initiate to default order
*/
for (i=0; i<map->numlayers; i++)
map->layerorder[i] = i;
}
if (map != NULL && map->numlayers > 0)
{
nCount = map->numlayers;
papszGroups = (char **)malloc(sizeof(char *)*nCount);
for (i=0; i<nCount; i++)
papszGroups[i] = NULL;
for (i=0; i<nCount; i++)
{
layerObj *lp;
lp = (GET_LAYER(map, map->layerorder[i]));
bFound = 0;
if (lp->group && lp->status != MS_DELETE)
{
for (j=0; j<*numTok; j++)
{
if (papszGroups[j] &&
strcmp(lp->group, papszGroups[j]) == 0)
{
bFound = 1;
break;
}
}
if (!bFound)
{
/* New group... add to the list of groups found */
papszGroups[(*numTok)] = strdup(lp->group);
(*numTok)++;
}
}
}
}
return papszGroups;
}
/************************************************************************/
/* msForceTmpFileBase() */
/************************************************************************/
static int tmpCount = 0;
static char *ForcedTmpBase = NULL;
void msForceTmpFileBase( const char *new_base )
{
/* -------------------------------------------------------------------- */
/* Clear previous setting, if any. */
/* -------------------------------------------------------------------- */
if( ForcedTmpBase != NULL )
{
free( ForcedTmpBase );
ForcedTmpBase = NULL;
}
tmpCount = -1;
if( new_base == NULL )
return;
/* -------------------------------------------------------------------- */
/* Record new base. */
/* -------------------------------------------------------------------- */
ForcedTmpBase = strdup( new_base );
tmpCount = 0;
}
/**********************************************************************
* msTmpFile()
*
* Generate a Unique temporary filename using:
*
* PID + timestamp + sequence# + extension
*
* If msForceTmpFileBase() has been called to control the temporary filename
* then the filename will be:
*
* TmpBase + sequence# + extension
*
* Returns char* which must be freed by caller.
**********************************************************************/
char *msTmpFile(const char *mappath, const char *tmppath, const char *ext)
{
char *tmpFname;
char szPath[MS_MAXPATHLEN];
const char *fullFname;
char tmpId[128]; /* big enough for time + pid + ext */
const char *tmpBase = NULL;
if( ForcedTmpBase != NULL )
{
tmpBase = ForcedTmpBase;
}
else
{
/* We'll use tmpId and tmpCount to generate unique filenames */
sprintf(tmpId, "%lx_%x",(long)time(NULL),(int)getpid());
tmpBase = tmpId;
}
if (ext == NULL) ext = "";
tmpFname = (char*)malloc(strlen(tmpBase) + 10 + strlen(ext) + 1);
msAcquireLock( TLOCK_TMPFILE );
sprintf(tmpFname, "%s_%x.%s", tmpBase, tmpCount++, ext);
msReleaseLock( TLOCK_TMPFILE );
fullFname = msBuildPath3(szPath, mappath, tmppath, tmpFname);
free(tmpFname);
if (fullFname)
return strdup(fullFname);
return NULL;
}
/**
* Generic function to Initalize an image object.
*/
imageObj *msImageCreate(int width, int height, outputFormatObj *format,
char *imagepath, char *imageurl, mapObj *map)
{
imageObj *image = NULL;
if( MS_RENDERER_GD(format) )
{
image = msImageCreateGD(width, height, format,
imagepath, imageurl, map->resolution, map->defresolution);
if( image != NULL && map) msImageInitGD( image, &map->imagecolor );
}
else if(MS_RENDERER_PLUGIN(format)) {
image = format->vtable->createImage(width,height,format,&map->imagecolor);
image->format = format;
format->refcount++;
image->width = width;
image->height = height;
image->imagepath = NULL;
image->imageurl = NULL;
image->tilecache = NULL;
image->ntiles = 0;
image->resolution = map->resolution;
image->resolutionfactor = map->resolution/map->defresolution;
if (imagepath)
image->imagepath = strdup(imagepath);
if (imageurl)
image->imageurl = strdup(imageurl);
return image;
}
#ifdef USE_AGG
else if( MS_RENDERER_AGG(format) )
{
image = msImageCreateAGG(width, height, format,
imagepath, imageurl, map->resolution, map->defresolution);
if( image != NULL && map) msImageInitAGG( image, &map->imagecolor );
}
#endif
else if( MS_RENDERER_RAWDATA(format) )
{
if( format->imagemode != MS_IMAGEMODE_INT16
&& format->imagemode != MS_IMAGEMODE_FLOAT32
&& format->imagemode != MS_IMAGEMODE_BYTE )
{
msSetError(MS_IMGERR,
"Attempt to use illegal imagemode with rawdata renderer.",
"msImageCreate()" );
return NULL;
}
image = (imageObj *)calloc(1,sizeof(imageObj));
if( format->imagemode == MS_IMAGEMODE_INT16 )
image->img.raw_16bit = (short *)
calloc(sizeof(short),width*height*format->bands);
else if( format->imagemode == MS_IMAGEMODE_FLOAT32 )
image->img.raw_float = (float *)
calloc(sizeof(float),width*height*format->bands);
else if( format->imagemode == MS_IMAGEMODE_BYTE )
image->img.raw_byte = (unsigned char *)
calloc(sizeof(unsigned char),width*height*format->bands);
if( image->img.raw_16bit == NULL )
{
msFree( image );
msSetError(MS_IMGERR,
"Attempt to allocate raw image failed, out of memory.",
"msImageCreate()" );
return NULL;
}
image->format = format;
format->refcount++;
image->width = width;
image->height = height;
image->imagepath = NULL;
image->imageurl = NULL;
image->resolution = map->resolution;
image->resolutionfactor = map->resolution/map->defresolution;
if (imagepath)
image->imagepath = strdup(imagepath);
if (imageurl)
image->imageurl = strdup(imageurl);
return image;
}
else if( MS_RENDERER_IMAGEMAP(format) )
{
image = msImageCreateIM(width, height, format,
imagepath, imageurl, map->resolution, map->defresolution);
if( image != NULL ) msImageInitIM( image );
}
#ifdef USE_MING_FLASH
else if( MS_RENDERER_SWF(format) && map )
{
image = msImageCreateSWF(width, height, format,
imagepath, imageurl, map);
}
#endif
#ifdef USE_PDF
else if( MS_RENDERER_PDF(format) && map)
{
image = msImageCreatePDF(width, height, format,
imagepath, imageurl, map);
}
#endif
else
{
msSetError(MS_MISCERR,
"Unsupported renderer requested, unable to initialize image.",
"msImageCreate()");
return NULL;
}
if(!image)
msSetError(MS_GDERR, "Unable to initialize image.", "msImageCreate()");
return image;
}
/**
* Generic function to transorm the shape coordinates to output coordinates
*/
void msTransformShape(shapeObj *shape, rectObj extent, double cellsize,
imageObj *image)
{
if (image != NULL && MS_RENDERER_PLUGIN(image->format)) {
image->format->vtable->transformShape(shape, extent, cellsize);
return;
}
#ifdef USE_MING_FLASH
if (image != NULL && MS_RENDERER_SWF(image->format) )
{
if (strcasecmp(msGetOutputFormatOption(image->format, "FULL_RESOLUTION",""),
"FALSE") == 0)
msTransformShapeToPixel(shape, extent, cellsize);
else
msTransformShapeSWF(shape, extent, cellsize);
return;
}
#endif
#ifdef USE_PDF
if (image != NULL && MS_RENDERER_PDF(image->format) )
{
if (strcasecmp(msGetOutputFormatOption(image->format, "FULL_RESOLUTION",""),
"FALSE") == 0)
msTransformShapeToPixel(shape, extent, cellsize);
else
msTransformShapePDF(shape, extent, cellsize);
return;
}
#endif
if (image != NULL && MS_RENDERER_SVG(image->format) )
{
msTransformShapeSVG(shape, extent, cellsize, image);
return;
}
#ifdef USE_AGG
if (image != NULL && MS_RENDERER_AGG(image->format) )
{
msTransformShapeAGG(shape, extent, cellsize);
return;
}
#endif
msTransformShapeToPixel(shape, extent, cellsize);
}
/*
** Helper functions supplied as part of bug #2868 solution. Consider moving these to
** mapprimitive.c for more general use.
*/
/* vector difference */
static pointObj point_diff(const pointObj a, const pointObj b) {
pointObj retv = {a.x-b.x,a.y-b.y
#ifdef USE_POINT_Z_M
,a.z-b.z,a.m-b.m
#endif
};
return retv;
}
/* vector sum */
static pointObj point_sum(const pointObj a, const pointObj b) {
pointObj retv = {a.x+b.x,a.y+b.y
#ifdef USE_POINT_Z_M
,a.z+b.z,a.m+b.m
#endif
};
return retv;
}
/* vector multiply */
static pointObj point_mul(const pointObj a, double b) {
pointObj retv= {a.x*b,a.y*b
#ifdef USE_POINT_Z_M
,a.z*b,a.m*b
#endif
};
return retv;
}
/* vector ??? */
static double point_abs2(const pointObj a) {
#ifdef USE_POINT_Z_M
return a.x*a.x+a.y*a.y+a.z*a.z+a.m*a.m;
#else
return a.x*a.x+a.y*a.y;
#endif
}
/* vector normal */
static pointObj point_norm(const pointObj a) {
double lenmul;
pointObj retv;
#ifdef USE_POINT_Z_M
if (a.x==0 && a.y==0 && a.z==0 && a.m==0)
#else
if (a.x==0 && a.y==0)
#endif
return a;
lenmul=1.0/sqrt(point_abs2(a)); /* this seems to be the costly operation */
retv.x = a.x*lenmul;
retv.y = a.y*lenmul;
#ifdef USE_POINT_Z_M
retv.z = a.z*lenmul;
retv.m = a.m*lenmul;
#endif
return retv;
}
/* rotate a vector 90 degrees */
static pointObj point_rotz90(const pointObj a) {
double nx=-1.0*a.y, ny=a.x;
pointObj retv=a;
retv.x=nx; retv.y=ny;
return retv;
}
/* vector cross product (warning: z and m dimensions are ignored!) */
static double point_cross(const pointObj a, const pointObj b) {
return a.x*b.y-a.y*b.x;
}
/*
** For offset corner point calculation 1/sin() is used
** to avoid 1/0 division (and long spikes) we define a
** limit for sin().
*/
#define CURVE_SIN_LIMIT 0.3
shapeObj *msOffsetPolyline(shapeObj *p, double offsetx, double offsety) {
int i, j, first,idx;
shapeObj *ret = (shapeObj*)malloc(sizeof(shapeObj));
msInitShape(ret);
ret->numlines = p->numlines;
ret->line=(lineObj*)malloc(sizeof(lineObj)*ret->numlines);
for(i=0;i<ret->numlines;i++) {
ret->line[i].numpoints=p->line[i].numpoints;
ret->line[i].point=(pointObj*)malloc(sizeof(pointObj)*ret->line[i].numpoints);
}
if(offsety == -99) { /* complex calculations */
for (i = 0; i < p->numlines; i++) {
pointObj old_pt, old_diffdir, old_offdir;
idx=0;
first = 1;
/* saved metrics of the last processed point */
if (p->line[i].numpoints>0)
old_pt=p->line[i].point[0];
for(j=1; j<p->line[i].numpoints; j++) {
const pointObj pt = p->line[i].point[j]; /* place of the point */
const pointObj diffdir = point_norm(point_diff(pt,old_pt)); /* direction of the line */
const pointObj offdir = point_rotz90(diffdir); /* direction where the distance between the line and the offset is measured */
pointObj offpt; /* this will be the corner point of the offset line */
/* offset line points computation */
if(first == 1) { /* first point */
first = 0;
offpt = point_sum(old_pt,point_mul(offdir,offsetx));
} else { /* middle points */
/* curve is the angle of the last and the current line's direction (supplementary angle of the shape's inner angle) */
double sin_curve = point_cross(diffdir,old_diffdir);
double cos_curve = point_cross(old_offdir,diffdir);
if ((-1.0)*CURVE_SIN_LIMIT < sin_curve && sin_curve < CURVE_SIN_LIMIT) {
/* do not calculate 1/sin, instead use a corner point approximation: average of the last and current offset direction and length */
/*
** TODO: fair for obtuse inner angles, however, positive and negative
** acute inner angles would need special handling - similar to LINECAP
** to avoid drawing of long spikes
*/
offpt = point_sum(old_pt, point_mul(point_sum(offdir, old_offdir),0.5*offsetx));
} else {
double base_shift = -1.0*(1.0+cos_curve)/sin_curve;
offpt = point_sum(old_pt, point_mul(point_sum(point_mul(diffdir,base_shift),offdir), offsetx));
}
}
ret->line[i].point[idx]=offpt;
idx++;
old_pt=pt; old_diffdir=diffdir; old_offdir=offdir;
}
/* last point */
if(first == 0) {
pointObj offpt=point_sum(old_pt,point_mul(old_offdir,offsetx));
ret->line[i].point[idx]=offpt;
idx++;
}
if(idx != p->line[i].numpoints) {
/* printf("shouldn't happen :(\n"); */
ret->line[i].numpoints=idx;
ret->line=realloc(ret->line,ret->line[i].numpoints*sizeof(pointObj));
}
}
} else { /* normal offset (eg. drop shadow) */
for (i = 0; i < p->numlines; i++) {
for(j=0; j<p->line[i].numpoints; j++) {
ret->line[i].point[j].x=p->line[i].point[j].x+offsetx;
ret->line[i].point[j].y=p->line[i].point[j].y+offsety;
}
}
}
return ret;
}
/*
-------------------------------------------------------------------------------
msSetup()
Contributed by Jerry Pisk in bug 1203. Heads off potential race condition
in initializing GD font cache with multiple threads. Should be called from
mapscript module initialization code.
-------------------------------------------------------------------------------
*/
int msSetup()
{
#ifdef USE_THREAD
msThreadInit();
#endif
/* Use MS_ERRORFILE and MS_DEBUGLEVEL env vars if set */
if (msDebugInitFromEnv() != MS_SUCCESS)
return MS_FAILURE;
#ifdef USE_GD_FT
if (gdFontCacheSetup() != 0) {
return MS_FAILURE;
}
#endif
#ifdef USE_GEOS
msGEOSSetup();
#endif
return MS_SUCCESS;
}
/* This is intended to be a function to cleanup anything that "hangs around"
when all maps are destroyed, like Registered GDAL drivers, and so forth. */
void msCleanup()
{
msForceTmpFileBase( NULL );
msConnPoolFinalCleanup();
msyylex_destroy();
#ifdef USE_OGR
msOGRCleanup();
#endif
#ifdef USE_GDAL
msGDALCleanup();
#endif
#ifdef USE_PROJ
pj_deallocate_grids();
msSetPROJ_LIB( NULL );
#endif
#if defined(USE_WMS_LYR) || defined(USE_WFS_LYR)
msHTTPCleanup();
#endif
#ifdef USE_GD_FT
gdFontCacheShutdown();
#endif
#ifdef USE_GEOS
msGEOSCleanup();
#endif
msIO_Cleanup();
msResetErrorList();
/* Close/cleanup log/debug output. Keep this at the very end. */
msDebugCleanup();
}
/************************************************************************/
/* msAlphaBlend() */
/* */
/* MapServer variation on gdAlphaBlend() that, I think, does a */
/* better job of merging a non-opaque color into an opaque */
/* color. In particular from gd 2.0.12 on the GD */
/* gdAlphaBlend() will give a transparent "dst" color influence */
/* in the result if the overlay is non-opaque. */
/* */
/* Note that "src" is layered over "dst". */
/************************************************************************/
int msAlphaBlend (int dst, int src)
{
int src_alpha = gdTrueColorGetAlpha(src);
int dst_alpha, alpha, red, green, blue;
int src_weight, dst_weight, tot_weight;
/* -------------------------------------------------------------------- */
/* Simple cases we want to handle fast. */
/* -------------------------------------------------------------------- */
if( src_alpha == gdAlphaOpaque )
return src;
dst_alpha = gdTrueColorGetAlpha(dst);
if( src_alpha == gdAlphaTransparent )
return dst;
if( dst_alpha == gdAlphaTransparent )
return src;
/* -------------------------------------------------------------------- */
/* What will the source and destination alphas be? Note that */
/* the destination weighting is substantially reduced as the */
/* overlay becomes quite opaque. */
/* -------------------------------------------------------------------- */
src_weight = gdAlphaTransparent - src_alpha;
dst_weight = (gdAlphaTransparent - dst_alpha) * src_alpha / gdAlphaMax;
tot_weight = src_weight + dst_weight;
/* -------------------------------------------------------------------- */
/* What red, green and blue result values will we use? */
/* -------------------------------------------------------------------- */
alpha = src_alpha * dst_alpha / gdAlphaMax;
red = (gdTrueColorGetRed(src) * src_weight
+ gdTrueColorGetRed(dst) * dst_weight) / tot_weight;
green = (gdTrueColorGetGreen(src) * src_weight
+ gdTrueColorGetGreen(dst) * dst_weight) / tot_weight;
blue = (gdTrueColorGetBlue(src) * src_weight
+ gdTrueColorGetBlue(dst) * dst_weight) / tot_weight;
/* -------------------------------------------------------------------- */
/* Return merged result. */
/* -------------------------------------------------------------------- */
return ((alpha << 24) + (red << 16) + (green << 8) + blue);
}
/*
RFC 24: check if the parent pointer is NULL and raise an error otherwise
*/
int msCheckParentPointer(void* p, char *objname) {
char* fmt="The %s parent object is null";
char* msg=NULL;
if (p == NULL) {
if(objname != NULL) {
msg=malloc( sizeof(char) * ( ( strlen(fmt)+strlen(objname) ) ) );
if(msg == NULL) {
msg="A required parent object is null";
} else {
sprintf(msg, "The %s parent object is null", objname);
}
} else {
msg="A required parent object is null";
}
msSetError(MS_NULLPARENTERR, msg, "");
return MS_FAILURE;
}
return MS_SUCCESS;
}
inline void msBufferInit(bufferObj *buffer) {
buffer->data=NULL;
buffer->size=0;
buffer->available=0;
buffer->_next_allocation_size = MS_DEFAULT_BUFFER_ALLOC;
}
inline void msBufferResize(bufferObj *buffer, size_t target_size){
while(buffer->available <= target_size) {
buffer->data = realloc(buffer->data,buffer->available+buffer->_next_allocation_size);
buffer->available += buffer->_next_allocation_size;
buffer->_next_allocation_size *= 2;
}
}
inline void msBufferAppend(bufferObj *buffer, void *data, size_t length) {
if(buffer->available < buffer->size+length) {
msBufferResize(buffer,buffer->size+length);
}
memcpy(&(buffer->data[buffer->size]),data,length);
buffer->size += length;
}
inline void msBufferFree(bufferObj *buffer) {
if(buffer->available>0)
free(buffer->data);
}
void msFreeRasterBuffer(rasterBufferObj *b) {
msFree(b->pixelbuffer);
}
/*
** Issue #3043: Layer extent comparison short circuit.
**
** msExtentsOverlap()
**
** Returns MS_TRUE if map extent and layer extent overlap,
** MS_FALSE if they are disjoint, and MS_UNKNOWN if there is
** not enough info to calculate a deterministic answer.
**
*/
int msExtentsOverlap(mapObj *map, layerObj *layer)
{
rectObj map_extent;
rectObj layer_extent;
/* No extent info? Nothing we can do, return MS_UNKNOWN. */
if( (map->extent.minx == -1) && (map->extent.miny == -1) && (map->extent.maxx == -1 ) && (map->extent.maxy == -1) ) return MS_UNKNOWN;
if( (layer->extent.minx == -1) && (layer->extent.miny == -1) && (layer->extent.maxx == -1 ) && (layer->extent.maxy == -1) ) return MS_UNKNOWN;
#ifdef USE_PROJ
/* No map projection? Let someone else sort this out. */
if( ! (map->projection.numargs > 0) )
return MS_UNKNOWN;
/* No layer projection? Perform naive comparison, because they are
** in the same projection. */
if( ! (layer->projection.numargs > 0) )
return msRectOverlap( &(map->extent), &(layer->extent) );
/* We need to transform our rectangles for comparison,
** so we will work with copies and leave the originals intact. */
MS_COPYRECT(&map_extent, &(map->extent) );
MS_COPYRECT(&layer_extent, &(layer->extent) );
/* Transform map extents into geographics for comparison. */
if( msProjectRect(&(map->projection), &(map->latlon), &map_extent) )
return MS_UNKNOWN;
/* Transform layer extents into geographics for comparison. */
if( msProjectRect(&(layer->projection), &(map->latlon), &layer_extent) )
return MS_UNKNOWN;
/* Simple case? Return simple answer. */
if ( map_extent.minx < map_extent.maxx && layer_extent.minx < layer_extent.maxx )
return msRectOverlap( &(map_extent), &(layer_extent) );
/* Uh oh, one of the rects crosses the dateline!
** Let someone else handle it. */
return MS_UNKNOWN;
#else
/* No proj? Naive comparison. */
if( msRectOverlap( &(map->extent), &(layer->extent) ) ) return MS_TRUE;
return MS_FALSE;
#endif
}
|