1 /******************************************************************************
2  * $Id$
3  *
4  * Project:  MapServer
5  * Purpose:  Functions for operating on a layerObj that don't belong in a
6  *           more specific file such as mapfile.c.
7  *           Adapted from mapobject.c.
8  * Author:   Sean Gillies, sgillies@frii.com
9  *
10  ******************************************************************************
11  * Copyright (c) 2004, Sean Gillies
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a
14  * copy of this software and associated documentation files (the "Software"),
15  * to deal in the Software without restriction, including without limitation
16  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  * and/or sell copies of the Software, and to permit persons to whom the
18  * Software is furnished to do so, subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included in
21  * all copies of this Software or works derived from this Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29  * DEALINGS IN THE SOFTWARE.
30  ****************************************************************************/
31 
32 #include "mapserver.h"
33 
34 
35 /* ===========================================================================
36    msInsertClass
37 
38    Returns the index at which the class was inserted.
39    ======================================================================== */
40 
msInsertClass(layerObj * layer,classObj * classobj,int nIndex)41 int msInsertClass(layerObj *layer, classObj *classobj, int nIndex)
42 {
43   int i;
44 
45   if (!classobj) {
46     msSetError(MS_CHILDERR, "Cannot insert NULL class", "msInsertClass()");
47     return -1;
48   }
49 
50   /* Ensure there is room for a new class */
51   if (msGrowLayerClasses(layer) == NULL) {
52     return -1;
53   }
54   /* Catch attempt to insert past end of styles array */
55   else if (nIndex >= layer->numclasses) {
56     msSetError(MS_CHILDERR, "Cannot insert class beyond index %d",
57                "msInsertClass()", layer->numclasses-1);
58     return -1;
59   } else if (nIndex < 0) { /* Insert at the end by default */
60 #ifndef __cplusplus
61     layer->class[layer->numclasses]=classobj;
62 #else
63     layer->_class[layer->numclasses]=classobj;
64 #endif
65     /* set parent pointer */
66     classobj->layer=layer;
67     MS_REFCNT_INCR(classobj);
68     layer->numclasses++;
69     return layer->numclasses-1;
70   } else if (nIndex >= 0 && nIndex < layer->numclasses) {
71 
72     /* Copy classes existing at the specified nIndex or greater */
73     /* to an index one higher */
74 
75 #ifndef __cplusplus
76     for (i=layer->numclasses-1; i>=nIndex; i--)
77       layer->class[i+1] = layer->class[i];
78     layer->class[nIndex]=classobj;
79 #else
80     for (i=layer->numclasses-1; i>=nIndex; i--)
81       layer->_class[i+1] = layer->_class[i];
82     layer->_class[nIndex]=classobj;
83 #endif
84 
85     /* set parent pointer */
86     classobj->layer=layer;
87     MS_REFCNT_INCR(classobj);
88     /* increment number of classes and return */
89     layer->numclasses++;
90     return nIndex;
91   } else {
92     msSetError(MS_CHILDERR, "Invalid index", "msInsertClass()");
93     return -1;
94   }
95 }
96 
97 /* ===========================================================================
98    msRemoveClass
99 
100    remove the class at an index from a layer, returning a copy
101    ======================================================================== */
102 
msRemoveClass(layerObj * layer,int nIndex)103 classObj *msRemoveClass(layerObj *layer, int nIndex)
104 {
105   int i;
106   classObj *classobj;
107 
108   if (nIndex < 0 || nIndex >= layer->numclasses) {
109     msSetError(MS_CHILDERR, "Cannot remove class, invalid index %d",
110                "removeClass()", nIndex);
111     return NULL;
112   } else {
113 #ifndef __cplusplus
114     classobj=layer->class[nIndex];
115 #else
116     classobj=layer->_class[nIndex];
117 #endif
118     classobj->layer=NULL;
119     MS_REFCNT_DECR(classobj);
120 
121     /* Iteratively copy the higher index classes down one index */
122     for (i=nIndex; i<layer->numclasses-1; i++) {
123 #ifndef __cplusplus
124       layer->class[i]=layer->class[i+1];
125 #else
126       layer->_class[i]=layer->_class[i+1];
127 #endif
128     }
129 #ifndef __cplusplus
130     layer->class[i]=NULL;
131 #else
132     layer->_class[i]=NULL;
133 #endif
134 
135     /* decrement number of layers and return copy of removed layer */
136     layer->numclasses--;
137     return classobj;
138   }
139 }
140 
141 /**
142  * Move the class up inside the array of classes.
143  */
msMoveClassUp(layerObj * layer,int nClassIndex)144 int msMoveClassUp(layerObj *layer, int nClassIndex)
145 {
146   classObj *psTmpClass = NULL;
147   if (layer && nClassIndex < layer->numclasses && nClassIndex >0) {
148     psTmpClass=layer->class[nClassIndex];
149 
150     layer->class[nClassIndex] = layer->class[nClassIndex-1];
151 
152     layer->class[nClassIndex-1] = psTmpClass;
153 
154     return(MS_SUCCESS);
155   }
156   msSetError(MS_CHILDERR, "Invalid index: %d", "msMoveClassUp()",
157              nClassIndex);
158   return (MS_FAILURE);
159 }
160 
161 
162 /**
163  * Move the class down inside the array of classes.
164  */
msMoveClassDown(layerObj * layer,int nClassIndex)165 int msMoveClassDown(layerObj *layer, int nClassIndex)
166 {
167   classObj *psTmpClass = NULL;
168   if (layer && nClassIndex < layer->numclasses-1 && nClassIndex >=0) {
169     psTmpClass=layer->class[nClassIndex];
170 
171     layer->class[nClassIndex] = layer->class[nClassIndex+1];
172 
173     layer->class[nClassIndex+1] = psTmpClass;
174 
175     return(MS_SUCCESS);
176   }
177   msSetError(MS_CHILDERR, "Invalid index: %d", "msMoveClassDown()",
178              nClassIndex);
179   return (MS_FAILURE);
180 }
181 
182 /**
183  * Set the extent of a layer.
184  */
185 
msLayerSetExtent(layerObj * layer,double minx,double miny,double maxx,double maxy)186 int msLayerSetExtent( layerObj *layer,
187                       double minx, double miny, double maxx, double maxy)
188 {
189 
190   layer->extent.minx = minx;
191   layer->extent.miny = miny;
192   layer->extent.maxx = maxx;
193   layer->extent.maxy = maxy;
194 
195   if (minx == -1.0 && miny == -1.0 && maxx == -1.0 && maxy == -1.0)
196     return(MS_SUCCESS);
197 
198   if (!MS_VALID_EXTENT(layer->extent)) {
199     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);
200     return(MS_FAILURE);
201   }
202 
203   return(MS_SUCCESS);
204 }
205