Coverage Report

Created: 2025-06-13 06:29

/src/MapServer/src/layerobject.c
Line
Count
Source (jump to first uncovered line)
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
   msInsertClass
36
37
   Returns the index at which the class was inserted.
38
   ======================================================================== */
39
40
0
int msInsertClass(layerObj *layer, classObj *classobj, int nIndex) {
41
0
  int i;
42
43
0
  if (!classobj) {
44
0
    msSetError(MS_CHILDERR, "Cannot insert NULL class", "msInsertClass()");
45
0
    return -1;
46
0
  }
47
48
  /* Ensure there is room for a new class */
49
0
  if (msGrowLayerClasses(layer) == NULL) {
50
0
    return -1;
51
0
  }
52
  /* Catch attempt to insert past end of styles array */
53
0
  else if (nIndex >= layer->numclasses) {
54
0
    msSetError(MS_CHILDERR, "Cannot insert class beyond index %d",
55
0
               "msInsertClass()", layer->numclasses - 1);
56
0
    return -1;
57
0
  } else if (nIndex < 0) { /* Insert at the end by default */
58
0
#ifndef __cplusplus
59
0
    layer->class[layer->numclasses] = classobj;
60
#else
61
    layer->_class[layer->numclasses] = classobj;
62
#endif
63
    /* set parent pointer */
64
0
    classobj->layer = layer;
65
0
    MS_REFCNT_INCR(classobj);
66
0
    layer->numclasses++;
67
0
    return layer->numclasses - 1;
68
0
  } else {
69
70
    /* Copy classes existing at the specified nIndex or greater */
71
    /* to an index one higher */
72
73
0
#ifndef __cplusplus
74
0
    for (i = layer->numclasses - 1; i >= nIndex; i--)
75
0
      layer->class[i + 1] = layer->class[i];
76
0
    layer->class[nIndex] = classobj;
77
#else
78
    for (i = layer->numclasses - 1; i >= nIndex; i--)
79
      layer->_class[i + 1] = layer->_class[i];
80
    layer->_class[nIndex] = classobj;
81
#endif
82
83
    /* set parent pointer */
84
0
    classobj->layer = layer;
85
0
    MS_REFCNT_INCR(classobj);
86
    /* increment number of classes and return */
87
0
    layer->numclasses++;
88
0
    return nIndex;
89
0
  }
90
0
}
91
92
/* ===========================================================================
93
   msRemoveClass
94
95
   remove the class at an index from a layer, returning a copy
96
   ======================================================================== */
97
98
0
classObj *msRemoveClass(layerObj *layer, int nIndex) {
99
0
  int i;
100
0
  classObj *classobj;
101
102
0
  if (nIndex < 0 || nIndex >= layer->numclasses) {
103
0
    msSetError(MS_CHILDERR, "Cannot remove class, invalid index %d",
104
0
               "removeClass()", nIndex);
105
0
    return NULL;
106
0
  } else {
107
0
#ifndef __cplusplus
108
0
    classobj = layer->class[nIndex];
109
#else
110
    classobj = layer->_class[nIndex];
111
#endif
112
0
    classobj->layer = NULL;
113
0
    MS_REFCNT_DECR(classobj);
114
115
    /* Iteratively copy the higher index classes down one index */
116
0
    for (i = nIndex; i < layer->numclasses - 1; i++) {
117
0
#ifndef __cplusplus
118
0
      layer->class[i] = layer->class[i + 1];
119
#else
120
      layer->_class[i] = layer->_class[i + 1];
121
#endif
122
0
    }
123
0
#ifndef __cplusplus
124
0
    layer->class[i] = NULL;
125
#else
126
    layer->_class[i] = NULL;
127
#endif
128
129
    /* decrement number of layers and return copy of removed layer */
130
0
    layer->numclasses--;
131
0
    return classobj;
132
0
  }
133
0
}
134
135
/**
136
 * Move the class up inside the array of classes.
137
 */
138
0
int msMoveClassUp(layerObj *layer, int nClassIndex) {
139
0
  classObj *psTmpClass = NULL;
140
0
  if (layer && nClassIndex < layer->numclasses && nClassIndex > 0) {
141
0
    psTmpClass = layer->class[nClassIndex];
142
143
0
    layer->class[nClassIndex] = layer->class[nClassIndex - 1];
144
145
0
    layer->class[nClassIndex - 1] = psTmpClass;
146
147
0
    return (MS_SUCCESS);
148
0
  }
149
0
  msSetError(MS_CHILDERR, "Invalid index: %d", "msMoveClassUp()", nClassIndex);
150
0
  return (MS_FAILURE);
151
0
}
152
153
/**
154
 * Move the class down inside the array of classes.
155
 */
156
0
int msMoveClassDown(layerObj *layer, int nClassIndex) {
157
0
  classObj *psTmpClass = NULL;
158
0
  if (layer && nClassIndex < layer->numclasses - 1 && nClassIndex >= 0) {
159
0
    psTmpClass = layer->class[nClassIndex];
160
161
0
    layer->class[nClassIndex] = layer->class[nClassIndex + 1];
162
163
0
    layer->class[nClassIndex + 1] = psTmpClass;
164
165
0
    return (MS_SUCCESS);
166
0
  }
167
0
  msSetError(MS_CHILDERR, "Invalid index: %d", "msMoveClassDown()",
168
0
             nClassIndex);
169
0
  return (MS_FAILURE);
170
0
}
171
172
/**
173
 * Set the extent of a layer.
174
 */
175
176
int msLayerSetExtent(layerObj *layer, double minx, double miny, double maxx,
177
0
                     double maxy) {
178
179
0
  layer->extent.minx = minx;
180
0
  layer->extent.miny = miny;
181
0
  layer->extent.maxx = maxx;
182
0
  layer->extent.maxy = maxy;
183
184
0
  if (minx == -1.0 && miny == -1.0 && maxx == -1.0 && maxy == -1.0)
185
0
    return (MS_SUCCESS);
186
187
0
  if (!MS_VALID_EXTENT(layer->extent)) {
188
0
    msSetError(MS_MISCERR,
189
0
               "Given layer extent is invalid. minx=%lf, miny=%lf, maxx=%lf, "
190
0
               "maxy=%lf.",
191
0
               "msLayerSetExtent()", layer->extent.minx, layer->extent.miny,
192
0
               layer->extent.maxx, layer->extent.maxy);
193
0
    return (MS_FAILURE);
194
0
  }
195
196
0
  return (MS_SUCCESS);
197
0
}