forked from sdlime/mapserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hittest.c
302 lines (274 loc) · 9.45 KB
/
hittest.c
1
2
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
39
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*****************************************************************************
*
* Project: MapServer
* Purpose: Content Dependant Legend rendering support
* Author: Thomas Bonfort ([email protected])
*
******************************************************************************
* Copyright (c) 1996-2013 Regents of the University of Minnesota.
*
* 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"
void initStyleHitTests(styleObj *s, style_hittest *sh, int default_status) {
sh->status = default_status;
}
void initLabelHitTests(labelObj *l, label_hittest *lh, int default_status) {
int i;
lh->stylehits = msSmallCalloc(l->numstyles,sizeof(style_hittest));
lh->status = default_status;
for(i=0; i<l->numstyles; i++) {
initStyleHitTests(l->styles[i],&lh->stylehits[i],default_status);
}
}
void initClassHitTests(classObj *c, class_hittest *ch, int default_status) {
int i;
ch->stylehits = msSmallCalloc(c->numstyles,sizeof(style_hittest));
ch->labelhits = msSmallCalloc(c->numlabels,sizeof(label_hittest));
ch->status = default_status;
for(i=0; i<c->numstyles; i++) {
initStyleHitTests(c->styles[i],&ch->stylehits[i],default_status);
}
for(i=0; i<c->numlabels; i++) {
initLabelHitTests(c->labels[i],&ch->labelhits[i],default_status);
}
}
void initLayerHitTests(layerObj *l, layer_hittest *lh) {
int i,default_status;
lh->classhits = msSmallCalloc(l->numclasses,sizeof(class_hittest));
switch(l->type) {
case MS_LAYER_POLYGON:
case MS_LAYER_POINT:
case MS_LAYER_LINE:
case MS_LAYER_ANNOTATION:
default_status = 0; /* needs testing */
break;
default:
default_status = 1; /* no hittesting needed, use traditional mode */
break;
}
lh->status = default_status;
for(i=0; i<l->numclasses; i++) {
initClassHitTests(l->class[i],&lh->classhits[i], default_status);
}
}
void initMapHitTests(mapObj *map, map_hittest *mh) {
int i;
mh->layerhits = msSmallCalloc(map->numlayers,sizeof(layer_hittest));
for(i=0; i<map->numlayers; i++) {
initLayerHitTests(GET_LAYER(map,i),&mh->layerhits[i]);
}
}
void freeLabelHitTests(labelObj *l, label_hittest *lh) {
free(lh->stylehits);
}
void freeClassHitTests(classObj *c, class_hittest *ch) {
int i;
for(i=0; i<c->numlabels; i++) {
freeLabelHitTests(c->labels[i],&ch->labelhits[i]);
}
free(ch->stylehits);
free(ch->labelhits);
}
void freeLayerHitTests(layerObj *l, layer_hittest *lh) {
int i;
for(i=0; i<l->numclasses; i++) {
freeClassHitTests(l->class[i],&lh->classhits[i]);
}
free(lh->classhits);
}
void freeMapHitTests(mapObj *map, map_hittest *mh) {
int i;
for(i=0; i<map->numlayers; i++) {
freeLayerHitTests(GET_LAYER(map,i),&mh->layerhits[i]);
}
free(mh->layerhits);
}
int msHitTestShape(mapObj *map, layerObj *layer, shapeObj *shape, int drawmode, class_hittest *hittest) {
int i;
classObj *cp = layer->class[shape->classindex];
if(MS_DRAW_FEATURES(drawmode)) {
for(i=0;i<cp->numstyles;i++) {
styleObj *sp = cp->styles[i];
if(msScaleInBounds(map->scaledenom,sp->minscaledenom,sp->maxscaledenom)) {
hittest->stylehits[i].status = 1;
}
}
}
if(MS_DRAW_LABELS(drawmode)) {
for(i=0;i<cp->numlabels;i++) {
labelObj *l = cp->labels[i];
if(msGetLabelStatus(map,layer,shape,l) == MS_ON) {
int s;
hittest->labelhits[i].status = 1;
for(s=0; s<l->numstyles;s++) {
hittest->labelhits[i].stylehits[s].status = 1;
}
}
}
}
return MS_SUCCESS;
}
int msHitTestLayer(mapObj *map, layerObj *layer, layer_hittest *hittest) {
int status;
#ifdef USE_GEOS
shapeObj searchpoly;
#endif
if(!msLayerIsVisible(map,layer)) {
hittest->status = 0;
return MS_SUCCESS;
}
if(layer->type == MS_LAYER_LINE || layer->type == MS_LAYER_POLYGON || layer->type == MS_LAYER_POINT || layer->type == MS_LAYER_ANNOTATION) {
int maxfeatures=msLayerGetMaxFeaturesToDraw(layer, NULL);
int annotate = msEvalContext(map, layer, layer->labelrequires);
shapeObj shape;
int nclasses,featuresdrawn = 0;
int *classgroup;
rectObj searchrect;
int minfeaturesize=-1;
if(map->scaledenom > 0) {
if((layer->labelmaxscaledenom != -1) && (map->scaledenom >= layer->labelmaxscaledenom)) annotate = MS_FALSE;
if((layer->labelminscaledenom != -1) && (map->scaledenom < layer->labelminscaledenom)) annotate = MS_FALSE;
}
status = msLayerOpen(layer);
if(status != MS_SUCCESS) return MS_FAILURE;
/* build item list */
status = msLayerWhichItems(layer, MS_FALSE, NULL);
if(status != MS_SUCCESS) {
msLayerClose(layer);
return MS_FAILURE;
}
/* identify target shapes */
if(layer->transform == MS_TRUE) {
searchrect = map->extent;
#ifdef USE_PROJ
if((map->projection.numargs > 0) && (layer->projection.numargs > 0))
msProjectRect(&map->projection, &layer->projection, &searchrect); /* project the searchrect to source coords */
#endif
}
else {
searchrect.minx = searchrect.miny = 0;
searchrect.maxx = map->width-1;
searchrect.maxy = map->height-1;
}
#ifdef USE_GEOS
msInitShape(&searchpoly);
msRectToPolygon(searchrect,&searchpoly);
#endif
status = msLayerWhichShapes(layer, searchrect, MS_FALSE);
if(status == MS_DONE) { /* no overlap */
#ifdef USE_GEOS
msFreeShape(&searchpoly);
#endif
msLayerClose(layer);
hittest->status = 0;
return MS_SUCCESS;
} else if(status != MS_SUCCESS) {
#ifdef USE_GEOS
msFreeShape(&searchpoly);
#endif
msLayerClose(layer);
return MS_FAILURE;
}
/* step through the target shapes */
msInitShape(&shape);
nclasses = 0;
classgroup = NULL;
if(layer->classgroup && layer->numclasses > 0)
classgroup = msAllocateValidClassGroups(layer, &nclasses);
if(layer->minfeaturesize > 0)
minfeaturesize = Pix2LayerGeoref(map, layer, layer->minfeaturesize);
while((status = msLayerNextShape(layer, &shape)) == MS_SUCCESS) {
int drawmode = MS_DRAWMODE_FEATURES;
#ifdef USE_GEOS
if(!msGEOSIntersects(&shape,&searchpoly)) {
msFreeShape(&shape);
continue;
}
#else
if(shape.type == MS_SHAPE_POLYGON) {
msClipPolygonRect(&shape, map->extent);
} else {
msClipPolylineRect(&shape, map->extent);
}
if(shape.numlines == 0) {
msFreeShape(&shape);
continue;
}
#endif
/* Check if the shape size is ok to be drawn, we need to clip */
if((shape.type == MS_SHAPE_LINE || shape.type == MS_SHAPE_POLYGON) && (minfeaturesize > 0)) {
msTransformShape(&shape, map->extent, map->cellsize, NULL);
msComputeBounds(&shape);
if (msShapeCheckSize(&shape, minfeaturesize) == MS_FALSE) {
msFreeShape(&shape);
continue;
}
}
shape.classindex = msShapeGetClass(layer, map, &shape, classgroup, nclasses);
if((shape.classindex == -1) || (layer->class[shape.classindex]->status == MS_OFF)) {
msFreeShape(&shape);
continue;
}
hittest->classhits[shape.classindex].status = 1;
hittest->status = 1;
if(maxfeatures >=0 && featuresdrawn >= maxfeatures) {
msFreeShape(&shape);
status = MS_DONE;
break;
}
featuresdrawn++;
if(annotate && layer->class[shape.classindex]->numlabels > 0) {
drawmode |= MS_DRAWMODE_LABELS;
}
status = msHitTestShape(map, layer, &shape, drawmode, &hittest->classhits[shape.classindex]); /* all styles */
msFreeShape(&shape);
}
#ifdef USE_GEOS
msFreeShape(&searchpoly);
#endif
if (classgroup)
msFree(classgroup);
if(status != MS_DONE) {
msLayerClose(layer);
return MS_FAILURE;
}
msLayerClose(layer);
return MS_SUCCESS;
} else {
/* we don't hittest these layers, skip as they already have been initialised */
return MS_SUCCESS;
}
}
int msHitTestMap(mapObj *map, map_hittest *hittest) {
int i,status;
map->cellsize = msAdjustExtent(&(map->extent),map->width,map->height);
status = msCalculateScale(map->extent,map->units,map->width,map->height, map->resolution, &map->scaledenom);
if(status != MS_SUCCESS) {
return MS_FAILURE;
}
for(i=0; i<map->numlayers; i++) {
layerObj *lp = map->layers[i];
status = msHitTestLayer(map,lp,&hittest->layerhits[i]);
if(status != MS_SUCCESS) {
return MS_FAILURE;
}
}
return MS_SUCCESS;
}