layerobject.c 6.67 KB
Newer Older
1
/******************************************************************************
2
 * $Id: layerobject.c 11373 2011-03-29 23:37:29Z dmorissette $
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
 *
 * Project:  MapServer
 * Purpose:  Functions for operating on a layerObj that don't belong in a
 *           more specific file such as mapfile.c.  
 *           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:
 *
 * 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 "mapserver.h"

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

39
MS_CVSID("$Id: layerobject.c 11373 2011-03-29 23:37:29Z dmorissette $")
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

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

   Returns the index at which the class was inserted.
   ======================================================================== */
 
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 */
#ifndef __cplusplus
        layer->class[layer->numclasses]=classobj;
#else
        layer->_class[layer->numclasses]=classobj;
#endif
	/* 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 */

#ifndef __cplusplus
        for (i=layer->numclasses-1; i>=nIndex; i--)
            layer->class[i+1] = layer->class[i];
        layer->class[nIndex]=classobj;
#else
        for (i=layer->numclasses-1; i>=nIndex; i--)
            layer->_class[i+1] = layer->_class[i];
        layer->_class[nIndex]=classobj;
#endif

	/* 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;
    }
}

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

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

classObj *msRemoveClass(layerObj *layer, int nIndex) 
{
    int i;
    classObj *classobj;
    
    if (nIndex < 0 || nIndex >= layer->numclasses)
    {
        msSetError(MS_CHILDERR, "Cannot remove class, invalid index %d",
                   "removeClass()", nIndex);
        return NULL;
    }
    else 
    {
#ifndef __cplusplus
        classobj=layer->class[nIndex];
#else
        classobj=layer->_class[nIndex];
#endif
	classobj->layer=NULL;
	MS_REFCNT_DECR(classobj);

        /* Iteratively copy the higher index classes down one index */
        for (i=nIndex; i<layer->numclasses-1; i++)
        {
#ifndef __cplusplus
            layer->class[i]=layer->class[i+1];
#else
            layer->_class[i]=layer->_class[i+1];
#endif
        }
#ifndef __cplusplus
        layer->class[i]=NULL;
#else
        layer->_class[i]=NULL;
#endif
        
        /* decrement number of layers and return copy of removed layer */
        layer->numclasses--;
        return classobj;
    }
}

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

        layer->class[nClassIndex] = layer->class[nClassIndex-1];
        
        layer->class[nClassIndex-1] = psTmpClass;

        return(MS_SUCCESS);
    }
    msSetError(MS_CHILDERR, "Invalid index: %d", "msMoveClassUp()",
               nClassIndex);
    return (MS_FAILURE);
}


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

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

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

        return(MS_SUCCESS);
    }
    msSetError(MS_CHILDERR, "Invalid index: %d", "msMoveClassDown()",
               nClassIndex);
    return (MS_FAILURE);
}

/**
 * Set the extent of a layer.
 */  

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)
      return(MS_SUCCESS);
      
    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);
}