mapobject.c 25.9 KB
Newer Older
1
/******************************************************************************
2
 * $Id$
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Project:  MapServer
 * Purpose:  Functions for operating on a mapObj that don't belong in a
 *           more specific file such as mapfile.c, or mapdraw.c.
 * Author:   Frank Warmerdam, warmerdam@pobox.com
 *
 ******************************************************************************
 * Copyright (c) 2004, Frank Warmerdam
 *
 * 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:
 *
19
 * The above copyright notice and this permission notice shall be included in
20 21 22 23 24 25 26 27 28 29 30 31
 * 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 "mapserver.h"
32
#include "mapows.h"
33 34 35 36 37 38

#ifdef USE_GDAL
#  include "gdal.h"
#  include "cpl_conv.h"
#endif

39

40 41 42 43 44 45 46 47 48 49 50 51 52 53

void freeWeb(webObj *web);
void freeScalebar(scalebarObj *scalebar);
void freeReferenceMap(referenceMapObj *ref);
void freeLegend(legendObj *legend);

/************************************************************************/
/*                            msNewMapObj()                             */
/*                                                                      */
/*      Create a new initialized map object.                            */
/************************************************************************/

mapObj *msNewMapObj()
{
54
  mapObj *map;
55

56 57
  /* create an empty map, no layers etc... */
  map = (mapObj *)calloc(sizeof(mapObj),1);
58

59 60 61 62
  if(!map) {
    msSetError(MS_MEMERR, NULL, "msCreateMap()");
    return NULL;
  }
63

64 65
  if( initMap( map ) == -1 )
    return NULL;
66

67 68
  if( msPostMapParseOutputFormatSetup( map ) == MS_FAILURE )
    return NULL;
69

70
  return map;
71 72 73 74 75 76
}

/************************************************************************/
/*                             msFreeMap()                              */
/************************************************************************/

77 78
void msFreeMap(mapObj *map)
{
79 80 81 82 83
  int i;

  if(!map) return;

  /* printf("msFreeMap(): maybe freeing map at %p count=%d.\n",map, map->refcount); */
84 85 86
  if(MS_REFCNT_DECR_IS_NOT_ZERO(map)) {
    return;
  }
87
  if(map->debug >= MS_DEBUGLEVEL_VV)
88 89
    msDebug("msFreeMap(): freeing map at %p.\n",map);

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  msCloseConnections(map);

  msFree(map->name);
  msFree(map->shapepath);
  msFree(map->mappath);

  msFreeProjection(&(map->projection));
  msFreeProjection(&(map->latlon));

  msFreeLabelCache(&(map->labelcache));

  msFree(map->imagetype);

  msFreeFontSet(&(map->fontset));

  msFreeSymbolSet(&map->symbolset); /* free symbols */
  msFree(map->symbolset.filename);

  freeWeb(&(map->web));

  freeScalebar(&(map->scalebar));
  freeReferenceMap(&(map->reference));
112
  freeLegend(&(map->legend));
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

  for(i=0; i<map->maxlayers; i++) {
    if(GET_LAYER(map, i) != NULL) {
      GET_LAYER(map, i)->map = NULL;
      if(freeLayer((GET_LAYER(map, i))) == MS_SUCCESS)
        free(GET_LAYER(map, i));
    }
  }
  msFree(map->layers);

  if(map->layerorder)
    free(map->layerorder);

  msFree(map->templatepattern);
  msFree(map->datapattern);
  msFreeHashItems(&(map->configoptions));
  if(map->outputformat && map->outputformat->refcount > 0 && --map->outputformat->refcount < 1)
    msFreeOutputFormat(map->outputformat);

  for(i=0; i<map->numoutputformats; i++ ) {
    if(map->outputformatlist[i]->refcount > 0 && --map->outputformatlist[i]->refcount < 1)
      msFreeOutputFormat(map->outputformatlist[i]);
  }
  if(map->outputformatlist != NULL)
    msFree(map->outputformatlist);

  msFreeQuery(&(map->query));

141 142 143 144 145
#ifdef USE_V8_MAPSCRIPT
  if (map->v8context)
    msV8FreeContext(map);
#endif

146 147 148 149 150 151 152 153 154 155
  msFree(map);
}

/************************************************************************/
/*                         msGetConfigOption()                          */
/************************************************************************/

const char *msGetConfigOption( mapObj *map, const char *key)

{
156
  return msLookupHashTable( &(map->configoptions), key );
157 158 159 160 161 162 163 164 165
}

/************************************************************************/
/*                         msSetConfigOption()                          */
/************************************************************************/

int msSetConfigOption( mapObj *map, const char *key, const char *value)

{
166 167 168 169 170 171
  /* We have special "early" handling of this so that it will be */
  /* in effect when the projection blocks are parsed and pj_init is called. */
  if( strcasecmp(key,"PROJ_LIB") == 0 ) {
    /* value may be relative to map path */
    msSetPROJ_LIB( value, map->mappath );
  }
172

173 174 175 176 177 178 179 180
  /* Same for MS_ERRORFILE, we want it to kick in as early as possible
   * to catch parsing errors.
   * Value can be relative to mapfile, unless it's already absolute
   */
  if( strcasecmp(key,"MS_ERRORFILE") == 0 ) {
    if (msSetErrorFile( value, map->mappath ) != MS_SUCCESS)
      return MS_FAILURE;
  }
181

182 183 184
  if( msLookupHashTable( &(map->configoptions), key ) != NULL )
    msRemoveHashTable( &(map->configoptions), key );
  msInsertHashTable( &(map->configoptions), key, value );
185

186
  return MS_SUCCESS;
187 188 189 190 191 192 193 194 195
}

/************************************************************************/
/*                         msTestConfigOption()                         */
/************************************************************************/

int msTestConfigOption( mapObj *map, const char *key, int default_result )

{
196 197 198 199 200 201 202 203 204 205 206
  const char *result = msGetConfigOption( map, key );

  if( result == NULL )
    return default_result;

  if( strcasecmp(result,"YES") == 0
      || strcasecmp(result,"ON") == 0
      || strcasecmp(result,"TRUE") == 0 )
    return MS_TRUE;
  else
    return MS_FALSE;
207 208 209 210 211 212 213 214 215
}

/************************************************************************/
/*                      msApplyMapConfigOptions()                       */
/************************************************************************/

void msApplyMapConfigOptions( mapObj *map )

{
216 217 218 219 220 221 222 223 224 225 226
  const char *key;

  for( key = msFirstKeyFromHashTable( &(map->configoptions) );
       key != NULL;
       key = msNextKeyFromHashTable( &(map->configoptions), key ) ) {
    const char *value = msLookupHashTable( &(map->configoptions), key );
    if( strcasecmp(key,"PROJ_LIB") == 0 ) {
      msSetPROJ_LIB( value, map->mappath );
    } else if( strcasecmp(key,"MS_ERRORFILE") == 0 ) {
      msSetErrorFile( value, map->mappath );
    } else {
227 228

#if defined(USE_GDAL) && GDAL_RELEASE_DATE > 20030601
229
      CPLSetConfigOption( key, value );
230 231
#endif
    }
232
  }
233 234 235 236 237 238 239 240
}

/************************************************************************/
/*                         msMapIgnoreMissingData()                               */
/************************************************************************/

int msMapIgnoreMissingData( mapObj *map )
{
241 242
  const char *result = msGetConfigOption( map, "ON_MISSING_DATA" );
  const int default_result =
243
#ifndef IGNORE_MISSING_DATA
244
    MS_MISSING_DATA_FAIL;
245
#else
246
    MS_MISSING_DATA_LOG;
247
#endif
248 249 250 251 252 253 254 255 256 257 258 259

  if( result == NULL )
    return default_result;

  if( strcasecmp(result,"FAIL") == 0 )
    return MS_MISSING_DATA_FAIL;
  else if( strcasecmp(result,"LOG") == 0 )
    return MS_MISSING_DATA_LOG;
  else if( strcasecmp(result,"IGNORE") == 0 )
    return MS_MISSING_DATA_IGNORE;

  return default_result;
260 261 262 263 264 265
}

/************************************************************************/
/*                           msMapSetExtent()                           */
/************************************************************************/

266 267 268
int msMapSetExtent( mapObj *map,
                    double minx, double miny, double maxx, double maxy)
{
269

270 271 272 273 274 275 276 277 278 279
  map->extent.minx = minx;
  map->extent.miny = miny;
  map->extent.maxx = maxx;
  map->extent.maxy = maxy;

  if (!MS_VALID_EXTENT(map->extent)) {
    msSetError(MS_MISCERR, "Given map extent is invalid. Check that it " \
               "is in the form: minx, miny, maxx, maxy", "setExtent()");
    return MS_FAILURE;
  }
280

281 282
  map->cellsize = msAdjustExtent(&(map->extent), map->width,
                                 map->height);
283

284 285 286 287 288 289
  /* if the map size is also set, recompute scale, ignore errors? */
  if( map->width != -1 || map->height != -1 )
    msCalculateScale(map->extent, map->units, map->width, map->height,
                     map->resolution, &(map->scaledenom));

  return msMapComputeGeotransform( map );
290 291 292 293 294 295
}

/************************************************************************/
/*                           msMapOffsetExtent()                        */
/************************************************************************/

296 297 298 299 300
int msMapOffsetExtent( mapObj *map, double x, double y)
{
  return msMapSetExtent( map,
                         map->extent.minx + x, map->extent.miny + y,
                         map->extent.maxx + x, map->extent.maxy + y);
301 302 303 304 305 306
}

/************************************************************************/
/*                           msMapScaleExtent()                         */
/************************************************************************/

307 308 309 310
int msMapScaleExtent( mapObj *map, double zoomfactor,
                      double minscaledenom, double maxscaledenom)
{
  double geo_width, geo_height, center_x, center_y, md;
311

312 313 314
  if (zoomfactor <= 0.0) {
    msSetError(MS_MISCERR, "The given zoomfactor is invalid", "msMapScaleExtent()");
  }
315

316 317
  geo_width = map->extent.maxx - map->extent.minx;
  geo_height = map->extent.maxy - map->extent.miny;
318

319 320
  center_x = map->extent.minx + geo_width * 0.5;
  center_y = map->extent.miny + geo_height * 0.5;
321

322
  geo_width *= zoomfactor;
323

324 325 326 327 328 329 330 331
  if (minscaledenom > 0 || maxscaledenom > 0) {
    /* ensure we are within the valid scale domain */
    md = (map->width-1)/(map->resolution * msInchesPerUnit(map->units, center_y));
    if (minscaledenom > 0 && geo_width < minscaledenom * md)
      geo_width = minscaledenom * md;
    if (maxscaledenom > 0 && geo_width > maxscaledenom * md)
      geo_width = maxscaledenom * md;
  }
332

333 334 335 336 337 338
  geo_width *= 0.5;
  geo_height = geo_width * map->height / map->width;

  return msMapSetExtent( map,
                         center_x - geo_width, center_y - geo_height,
                         center_x + geo_width, center_y + geo_height);
339 340 341 342 343 344
}

/************************************************************************/
/*                           msMapSetCenter()                           */
/************************************************************************/

345 346 347 348
int msMapSetCenter( mapObj *map, pointObj *center)
{
  return msMapOffsetExtent(map, center->x - (map->extent.minx + map->extent.maxx) * 0.5,
                           center->y - (map->extent.miny + map->extent.maxy) * 0.5);
349 350 351 352 353 354 355 356 357
}

/************************************************************************/
/*                           msMapSetRotation()                         */
/************************************************************************/

int msMapSetRotation( mapObj *map, double rotation_angle )

{
358 359 360 361 362
  map->gt.rotation_angle = rotation_angle;
  if( map->gt.rotation_angle != 0.0 )
    map->gt.need_geotransform = MS_TRUE;
  else
    map->gt.need_geotransform = MS_FALSE;
363

364
  return msMapComputeGeotransform( map );
365 366 367 368 369 370 371 372 373
}

/************************************************************************/
/*                             msMapSetSize()                           */
/************************************************************************/

int msMapSetSize( mapObj *map, int width, int height )

{
374 375
  map->width = width;
  map->height = height;
376

377
  return msMapComputeGeotransform( map ); /* like SetRotation -- sean */
378 379 380 381 382 383 384 385 386 387 388
}

/************************************************************************/
/*                      msMapComputeGeotransform()                      */
/************************************************************************/

extern int InvGeoTransform( double *gt_in, double *gt_out );

int msMapComputeGeotransform( mapObj * map )

{
389 390
  double rot_angle;
  double geo_width, geo_height, center_x, center_y;
391

392
  map->saved_extent = map->extent;
393

394 395 396 397
  /* Do we have all required parameters? */
  if( map->extent.minx == map->extent.maxx
      || map->width == 0 || map->height == 0 )
    return MS_FAILURE;
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
  rot_angle = map->gt.rotation_angle * MS_PI / 180.0;

  geo_width = map->extent.maxx - map->extent.minx;
  geo_height = map->extent.maxy - map->extent.miny;

  center_x = map->extent.minx + geo_width*0.5;
  center_y = map->extent.miny + geo_height*0.5;

  /*
  ** Per bug 1916 we have to adjust for the fact that map extents
  ** are based on the center of the edge pixels, not the outer
  ** edges as is expected in a geotransform.
  */
  map->gt.geotransform[1] =
    cos(rot_angle) * geo_width / (map->width-1);
  map->gt.geotransform[2] =
    sin(rot_angle) * geo_height / (map->height-1);
  map->gt.geotransform[0] = center_x
                            - (map->width * 0.5) * map->gt.geotransform[1]
                            - (map->height * 0.5) * map->gt.geotransform[2];

  map->gt.geotransform[4] =
    sin(rot_angle) * geo_width / (map->width-1);
  map->gt.geotransform[5] =
    - cos(rot_angle) * geo_height / (map->height-1);
  map->gt.geotransform[3] = center_y
                            - (map->width * 0.5) * map->gt.geotransform[4]
                            - (map->height * 0.5) * map->gt.geotransform[5];

  if( InvGeoTransform( map->gt.geotransform,
                       map->gt.invgeotransform ) )
    return MS_SUCCESS;
  else
    return MS_FAILURE;
433 434 435 436 437 438 439 440 441
}

/************************************************************************/
/*                         msMapPixelToGeoref()                         */
/************************************************************************/

void msMapPixelToGeoref( mapObj *map, double *x, double *y )

{
442
  msSetError(MS_MISCERR, NULL, "msMapPixelToGeoref() not yet implemented");
443 444 445 446 447 448 449 450 451
}

/************************************************************************/
/*                         msMapGeorefToPixel()                         */
/************************************************************************/

void msMapGeorefToPixel( mapObj *map, double *x, double *y )

{
452
  msSetError(MS_MISCERR, NULL, "msMapGeorefToPixel() not yet implemented");
453 454 455 456 457 458 459 460 461
}

/************************************************************************/
/*                        msMapSetFakedExtent()                         */
/************************************************************************/

int msMapSetFakedExtent( mapObj *map )

{
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
  int i;
  /* -------------------------------------------------------------------- */
  /*      Remember the original map extents so we can restore them        */
  /*      later.                                                          */
  /* -------------------------------------------------------------------- */
  map->saved_extent = map->extent;

  /* -------------------------------------------------------------------- */
  /*      Set extents such that the bottom left corner is 0,0 and the     */
  /*      top right is width,height.  Note this is upside down from       */
  /*      the normal sense of pixel/line coordiantes, but we do this      */
  /*      so that the normal "extent" concept of coordinates              */
  /*      increasing to the right, and up is maintained (like in          */
  /*      georeferenced coordinate systems).                              */
  /* -------------------------------------------------------------------- */
  map->extent.minx = 0;
  map->extent.maxx = map->width;
  map->extent.miny = 0;
  map->extent.maxy = map->height;
  map->cellsize = 1.0;

  /* -------------------------------------------------------------------- */
  /*      When we copy the geotransform into the projection object we     */
  /*      have to flip it to account for the preceeding upside-down       */
  /*      coordinate system.                                              */
  /* -------------------------------------------------------------------- */
  map->projection.gt = map->gt;

  map->projection.gt.geotransform[0]
  += map->height * map->gt.geotransform[2];
  map->projection.gt.geotransform[3]
  += map->height * map->gt.geotransform[5];

  map->projection.gt.geotransform[2] *= -1;
  map->projection.gt.geotransform[5] *= -1;

  for(i=0; i<map->numlayers; i++)
    GET_LAYER(map, i)->project = MS_TRUE;

  return InvGeoTransform( map->projection.gt.geotransform,
                          map->projection.gt.invgeotransform );
503
}
504

505 506 507 508 509 510 511
/************************************************************************/
/*                      msMapRestoreRealExtent()                        */
/************************************************************************/

int msMapRestoreRealExtent( mapObj *map )

{
512 513 514
  map->projection.gt.need_geotransform = MS_FALSE;
  map->extent = map->saved_extent;
  map->cellsize = msAdjustExtent(&(map->extent), map->width, map->height);
515

516
  return MS_SUCCESS;
517 518 519 520 521 522 523
}

/************************************************************************/
/*                      msInsertLayer()                                 */
/************************************************************************/
/* Returns the index at which the layer was inserted
 */
524 525

int msInsertLayer(mapObj *map, layerObj *layer, int nIndex)
526
{
527 528 529 530
  if (!layer) {
    msSetError(MS_CHILDERR, "Can't insert a NULL Layer", "msInsertLayer()");
    return -1;
  }
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
  /* Ensure there is room for a new layer */
  if (map->numlayers == map->maxlayers) {
    if (msGrowMapLayers(map) == NULL)
      return -1;
  }

  /* msGrowMapLayers allocates the new layer which we don't need to do since we have 1 that we are inserting
                  not sure if it is possible for this to be non null otherwise, but better to check since this function
                  replaces the value */
  if (map->layers[map->numlayers]!=NULL)
    free(map->layers[map->numlayers]);

  /* Catch attempt to insert past end of layers array */
  if (nIndex >= map->numlayers) {
    msSetError(MS_CHILDERR, "Cannot insert layer beyond index %d",
               "msInsertLayer()", map->numlayers-1);
    return -1;
  } else if (nIndex < 0) { /* Insert at the end by default */
    map->layerorder[map->numlayers] = map->numlayers;
    GET_LAYER(map, map->numlayers) = layer;
    GET_LAYER(map, map->numlayers)->index = map->numlayers;
    GET_LAYER(map, map->numlayers)->map = map;
    MS_REFCNT_INCR(layer);
    map->numlayers++;
    return map->numlayers-1;
  } else if (nIndex >= 0 && nIndex < map->numlayers) {
    /* Move existing layers at the specified nIndex or greater */
    /* to an index one higher */
    int i;
    for (i=map->numlayers; i>nIndex; i--) {
      GET_LAYER(map, i)=GET_LAYER(map, i-1);
      GET_LAYER(map, i)->index = i;
564 565
    }

566 567 568 569
    /* assign new layer to specified index */
    GET_LAYER(map, nIndex)=layer;
    GET_LAYER(map, nIndex)->index = nIndex;
    GET_LAYER(map, nIndex)->map = map;
570

571 572 573 574
    /* adjust layers drawing order */
    for (i=map->numlayers; i>nIndex; i--) {
      map->layerorder[i] = map->layerorder[i-1];
      if (map->layerorder[i] >= nIndex) map->layerorder[i]++;
575
    }
576 577
    for (i=0; i<nIndex; i++) {
      if (map->layerorder[i] >= nIndex) map->layerorder[i]++;
578
    }
579 580 581 582 583 584 585 586 587 588
    map->layerorder[nIndex] = nIndex;

    /* increment number of layers and return */
    MS_REFCNT_INCR(layer);
    map->numlayers++;
    return nIndex;
  } else {
    msSetError(MS_CHILDERR, "Invalid index", "msInsertLayer()");
    return -1;
  }
589 590 591 592 593
}

/************************************************************************/
/*                           msRemoveLayer()                            */
/************************************************************************/
594
layerObj *msRemoveLayer(mapObj *map, int nIndex)
595
{
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
  int i;
  int order_index;
  layerObj *layer;

  if (nIndex < 0 || nIndex >= map->numlayers) {
    msSetError(MS_CHILDERR, "Cannot remove Layer, invalid index %d",
               "msRemoveLayer()", nIndex);
    return NULL;
  } else {
    layer=GET_LAYER(map, nIndex);
    /* msCopyLayer(layer, (GET_LAYER(map, nIndex))); */

    /* Iteratively copy the higher index layers down one index */
    for (i=nIndex; i<map->numlayers-1; i++) {
      /* freeLayer((GET_LAYER(map, i))); */
      /* initLayer((GET_LAYER(map, i)), map); */
      /* msCopyLayer(GET_LAYER(map, i), GET_LAYER(map, i+1)); */
      GET_LAYER(map, i)=GET_LAYER(map, i+1);
      GET_LAYER(map, i)->index = i;
615
    }
616 617 618 619 620 621 622 623 624 625 626 627
    /* Free the extra layer at the end */
    /* freeLayer((GET_LAYER(map, map->numlayers-1))); */
    GET_LAYER(map, map->numlayers-1)=NULL;

    /* Adjust drawing order */
    order_index = 0;
    for (i=0; i<map->numlayers; i++) {
      if (map->layerorder[i] > nIndex) map->layerorder[i]--;
      if (map->layerorder[i] == nIndex) {
        order_index = i;
        break;
      }
628
    }
629 630 631 632 633 634 635 636 637 638 639
    for (i=order_index; i<map->numlayers-1; i++) {
      map->layerorder[i] = map->layerorder[i+1];
      if (map->layerorder[i] > nIndex) map->layerorder[i]--;
    }

    /* decrement number of layers and return copy of removed layer */
    map->numlayers--;
    layer->map=NULL;
    MS_REFCNT_DECR(layer);
    return layer;
  }
640 641 642 643
}

/*
** Move the layer's order for drawing purpose. Moving it up here
644
** will have the effect of drawing the layer earlier.
645 646 647
*/
int msMoveLayerUp(mapObj *map, int nLayerIndex)
{
648 649 650 651 652 653 654 655 656 657 658 659 660
  int iCurrentIndex = -1;
  int i = 0;
  if (map && nLayerIndex < map->numlayers && nLayerIndex >=0) {
    for (i=0; i<map->numlayers; i++) {
      if ( map->layerorder[i] == nLayerIndex) {
        iCurrentIndex = i;
        break;
      }
    }
    if (iCurrentIndex >=0) {
      /* we do not need to promote if it is the first one. */
      if (iCurrentIndex == 0)
        return MS_FAILURE;
661

662 663 664
      map->layerorder[iCurrentIndex] =
        map->layerorder[iCurrentIndex-1];
      map->layerorder[iCurrentIndex-1] = nLayerIndex;
665

666
      return MS_SUCCESS;
667
    }
668 669 670 671
  }
  msSetError(MS_CHILDERR, "Invalid index: %d", "msMoveLayerUp()",
             nLayerIndex);
  return MS_FAILURE;
672 673 674 675
}

/*
** Move the layer's order for drawing purpose. Moving it down here
676
** will have the effect of drawing the layer later.
677 678 679
*/
int msMoveLayerDown(mapObj *map, int nLayerIndex)
{
680 681 682 683 684 685 686 687 688 689 690 691 692
  int iCurrentIndex = -1;
  int i = 0;
  if (map && nLayerIndex < map->numlayers && nLayerIndex >=0) {
    for (i=0; i<map->numlayers; i++) {
      if ( map->layerorder[i] == nLayerIndex) {
        iCurrentIndex = i;
        break;
      }
    }
    if (iCurrentIndex >=0) {
      /* we do not need to demote if it is the last one. */
      if (iCurrentIndex == map->numlayers-1)
        return MS_FAILURE;
693

694 695 696
      map->layerorder[iCurrentIndex] =
        map->layerorder[iCurrentIndex+1];
      map->layerorder[iCurrentIndex+1] = nLayerIndex;
697

698
      return MS_SUCCESS;
699
    }
700 701 702 703
  }
  msSetError(MS_CHILDERR, "Invalid index: %d", "msMoveLayerDown()",
             nLayerIndex);
  return MS_FAILURE;
704 705 706 707 708 709
}


/*
** Set the array used for the drawing order. The array passed must contain
** all the layer's index ordered by the drawing priority.
710
** Ex : for 3 layers in the map file, if
711 712 713 714 715 716 717 718 719 720 721 722
**                          panIndexes[0] = 2
**                          panIndexes[1] = 0
**                          panIndexes[2] = 1
**                          will set the darwing order to layer 2, layer 0,
**                          and then layer 1.
**
** Note : It is assumed that the index panIndexes has the same number of
**        of elements as the number of layers in the map.
** Return TRUE on success else FALSE.
*/
int msSetLayersdrawingOrder(mapObj *self, int *panIndexes)
{
723 724 725 726 727 728 729 730 731 732 733 734
  int nElements = 0;
  int i, j = 0;
  int bFound = 0;

  if (self && panIndexes) {
    nElements = self->numlayers;
    for (i=0; i<nElements; i++) {
      bFound = 0;
      for (j=0; j<nElements; j++) {
        if (panIndexes[j] == i) {
          bFound = 1;
          break;
735
        }
736 737 738 739 740 741 742 743 744
      }
      if (!bFound)
        return 0;
    }
    /* -------------------------------------------------------------------- */
    /*    At this point the array is valid so update the layers order array.*/
    /* -------------------------------------------------------------------- */
    for (i=0; i<nElements; i++) {
      self->layerorder[i] = panIndexes[i];
745
    }
746 747 748
    return 1;
  }
  return 0;
749 750 751 752 753
}


/* =========================================================================
   msMapLoadOWSParameters
754

755 756 757 758 759 760 761
   Function to support mapscript mapObj::loadOWSParameters
   ========================================================================= */

int msMapLoadOWSParameters(mapObj *map, cgiRequestObj *request,
                           const char *wmtver)
{
#ifdef USE_WMS_SVR
762 763 764 765 766
  int version;
  char *wms_exception_format = NULL;
  const char *wms_request= NULL;
  int result, i = 0;
  owsRequestObj ows_request;
767

768 769
  ows_request.numlayers = 0;
  ows_request.enabled_layers = NULL;
770

771

772 773 774 775 776 777
  version = msOWSParseVersionString(wmtver);
  for(i=0; i<request->NumParams; i++) {
    if (strcasecmp(request->ParamNames[i], "EXCEPTIONS") == 0)
      wms_exception_format = request->ParamValues[i];
    else if (strcasecmp(request->ParamNames[i], "REQUEST") == 0)
      wms_request = request->ParamValues[i];
778

779
  }
780

781
  msOWSRequestLayersEnabled(map, "M", wms_request, &ows_request);
782

783 784 785
  result = msWMSLoadGetMapParams(map, version, request->ParamNames,
                                 request->ParamValues, request->NumParams,  wms_exception_format,
                                 wms_request, &ows_request);
786

787 788
  if (ows_request.numlayers > 0)
    msFree(ows_request.enabled_layers);
789

790
  return result;
791

792
#else
793 794 795
  msSetError(MS_WMSERR, "WMS server support is not available.",
             "msMapLoadOWSParameters()");
  return MS_FAILURE;
796 797 798
#endif
}