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

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

39

40 41 42 43 44 45 46

/* ===========================================================================
   msInsertClass

   Returns the index at which the class was inserted.
   ======================================================================== */

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
int msInsertClass(layerObj *layer, classObj *classobj, int nIndex)
{
  int i;

  if (!classobj) {
    msSetError(MS_CHILDERR, "Cannot insert NULL class", "msInsertClass()");
    return -1;
  }

  /* Ensure there is room for a new class */
  if (msGrowLayerClasses(layer) == NULL) {
    return -1;
  }
  /* Catch attempt to insert past end of styles array */
  else if (nIndex >= layer->numclasses) {
    msSetError(MS_CHILDERR, "Cannot insert class beyond index %d",
               "msInsertClass()", layer->numclasses-1);
    return -1;
  } else if (nIndex < 0) { /* Insert at the end by default */
66
#ifndef __cplusplus
67
    layer->class[layer->numclasses]=classobj;
68
#else
69
    layer->_class[layer->numclasses]=classobj;
70
#endif
71 72 73 74 75 76 77 78 79
    /* set parent pointer */
    classobj->layer=layer;
    MS_REFCNT_INCR(classobj);
    layer->numclasses++;
    return layer->numclasses-1;
  } else if (nIndex >= 0 && nIndex < layer->numclasses) {

    /* Copy classes existing at the specified nIndex or greater */
    /* to an index one higher */
80 81

#ifndef __cplusplus
82 83 84
    for (i=layer->numclasses-1; i>=nIndex; i--)
      layer->class[i+1] = layer->class[i];
    layer->class[nIndex]=classobj;
85
#else
86 87 88
    for (i=layer->numclasses-1; i>=nIndex; i--)
      layer->_class[i+1] = layer->_class[i];
    layer->_class[nIndex]=classobj;
89 90
#endif

91 92 93 94 95 96 97 98 99 100
    /* set parent pointer */
    classobj->layer=layer;
    MS_REFCNT_INCR(classobj);
    /* increment number of classes and return */
    layer->numclasses++;
    return nIndex;
  } else {
    msSetError(MS_CHILDERR, "Invalid index", "msInsertClass()");
    return -1;
  }
101 102 103 104 105 106 107 108
}

/* ===========================================================================
   msRemoveClass

   remove the class at an index from a layer, returning a copy
   ======================================================================== */

109
classObj *msRemoveClass(layerObj *layer, int nIndex)
110
{
111 112 113 114 115 116 117 118
  int i;
  classObj *classobj;

  if (nIndex < 0 || nIndex >= layer->numclasses) {
    msSetError(MS_CHILDERR, "Cannot remove class, invalid index %d",
               "removeClass()", nIndex);
    return NULL;
  } else {
119
#ifndef __cplusplus
120
    classobj=layer->class[nIndex];
121
#else
122
    classobj=layer->_class[nIndex];
123
#endif
124 125
    classobj->layer=NULL;
    MS_REFCNT_DECR(classobj);
126

127 128
    /* Iteratively copy the higher index classes down one index */
    for (i=nIndex; i<layer->numclasses-1; i++) {
129
#ifndef __cplusplus
130
      layer->class[i]=layer->class[i+1];
131
#else
132
      layer->_class[i]=layer->_class[i+1];
133
#endif
134
    }
135
#ifndef __cplusplus
136
    layer->class[i]=NULL;
137
#else
138
    layer->_class[i]=NULL;
139
#endif
140 141 142 143 144

    /* decrement number of layers and return copy of removed layer */
    layer->numclasses--;
    return classobj;
  }
145 146 147 148
}

/**
 * Move the class up inside the array of classes.
149
 */
150 151
int msMoveClassUp(layerObj *layer, int nClassIndex)
{
152 153 154
  classObj *psTmpClass = NULL;
  if (layer && nClassIndex < layer->numclasses && nClassIndex >0) {
    psTmpClass=layer->class[nClassIndex];
155

156
    layer->class[nClassIndex] = layer->class[nClassIndex-1];
157

158 159 160 161 162 163 164
    layer->class[nClassIndex-1] = psTmpClass;

    return(MS_SUCCESS);
  }
  msSetError(MS_CHILDERR, "Invalid index: %d", "msMoveClassUp()",
             nClassIndex);
  return (MS_FAILURE);
165 166 167 168 169
}


/**
 * Move the class down inside the array of classes.
170
 */
171 172
int msMoveClassDown(layerObj *layer, int nClassIndex)
{
173 174 175
  classObj *psTmpClass = NULL;
  if (layer && nClassIndex < layer->numclasses-1 && nClassIndex >=0) {
    psTmpClass=layer->class[nClassIndex];
176

177
    layer->class[nClassIndex] = layer->class[nClassIndex+1];
178

179
    layer->class[nClassIndex+1] = psTmpClass;
180

181 182 183 184 185
    return(MS_SUCCESS);
  }
  msSetError(MS_CHILDERR, "Invalid index: %d", "msMoveClassDown()",
             nClassIndex);
  return (MS_FAILURE);
186 187 188 189
}

/**
 * Set the extent of a layer.
190 191 192 193 194 195 196 197 198 199 200 201
 */

int msLayerSetExtent( layerObj *layer,
                      double minx, double miny, double maxx, double maxy)
{

  layer->extent.minx = minx;
  layer->extent.miny = miny;
  layer->extent.maxx = maxx;
  layer->extent.maxy = maxy;

  if (minx == -1.0 && miny == -1.0 && maxx == -1.0 && maxy == -1.0)
202
    return(MS_SUCCESS);
203 204 205 206 207 208 209

  if (!MS_VALID_EXTENT(layer->extent)) {
    msSetError(MS_MISCERR, "Given layer extent is invalid. minx=%lf, miny=%lf, maxx=%lf, maxy=%lf.", "msLayerSetExtent()", layer->extent.minx, layer->extent.miny, layer->extent.maxx, layer->extent.maxy);
    return(MS_FAILURE);
  }

  return(MS_SUCCESS);
210
}