root/branches/branch-5-0/mapserver/layerobject.c

Revision 6428, 6.5 kB (checked in by dmorissette, 1 year ago)

Added missing line at end of license text

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