Skip to content

Commit 297fa3e

Browse files
committed
take into account style min/max scale in legend (MapServer#4601)
the mapscript and template code calling these updated functions are hardcoded to do scale-independant legend rendering to remain backwards compatible.
1 parent 2a758fd commit 297fa3e

File tree

7 files changed

+63
-24
lines changed

7 files changed

+63
-24
lines changed

maplegend.c

+41-12
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
* this function
4242
*/
4343
int msDrawLegendIcon(mapObj *map, layerObj *lp, classObj *theclass,
44-
int width, int height, imageObj *image, int dstX, int dstY)
44+
int width, int height, imageObj *image, int dstX, int dstY,
45+
int scale_independant)
4546
{
4647
int i, type, hasmarkersymbol;
4748
double offset;
@@ -145,13 +146,23 @@ int msDrawLegendIcon(mapObj *map, layerObj *lp, classObj *theclass,
145146
marker.y = dstY + MS_NINT(height / 2.0);
146147
hasmarkersymbol = 0;
147148
for(i=0; i<theclass->numstyles; i++) {
148-
if (theclass->styles[i]->symbol < map->symbolset.numsymbols && theclass->styles[i]->symbol > 0) {
149-
hasmarkersymbol = 1;
150-
break;
151-
}
149+
if(!scale_independant && map->scaledenom > 0) {
150+
styleObj *lp = theclass->styles[i];
151+
if((lp->maxscaledenom > 0) && (map->scaledenom > lp->maxscaledenom)) continue;
152+
if((lp->minscaledenom > 0) && (map->scaledenom <= lp->minscaledenom)) continue;
153+
}
154+
if (theclass->styles[i]->symbol < map->symbolset.numsymbols && theclass->styles[i]->symbol > 0) {
155+
hasmarkersymbol = 1;
156+
break;
157+
}
152158
}
153159
if (hasmarkersymbol) {
154160
for(i=0; i<theclass->numstyles; i++)
161+
if(!scale_independant && map->scaledenom > 0) {
162+
styleObj *lp = theclass->styles[i];
163+
if((lp->maxscaledenom > 0) && (map->scaledenom > lp->maxscaledenom)) continue;
164+
if((lp->minscaledenom > 0) && (map->scaledenom <= lp->minscaledenom)) continue;
165+
}
155166
msDrawMarkerSymbol(&map->symbolset, image_draw, &marker, theclass->styles[i], lp->scalefactor);
156167
} else if (theclass->labels && theclass->numlabels > 0) {
157168
labelObj *label = theclass->labels[0]; /* use the first label definition */
@@ -204,8 +215,14 @@ int msDrawLegendIcon(mapObj *map, layerObj *lp, classObj *theclass,
204215
msDrawMarkerSymbol(&map->symbolset,image_draw,&marker,&imgStyle,lp->scalefactor);
205216
/* TO DO: we may want to handle this differently depending on the relative size of the keyimage */
206217
} else {
207-
for(i=0; i<theclass->numstyles; i++)
218+
for(i=0; i<theclass->numstyles; i++) {
219+
if(!scale_independant && map->scaledenom > 0) {
220+
styleObj *lp = theclass->styles[i];
221+
if((lp->maxscaledenom > 0) && (map->scaledenom > lp->maxscaledenom)) continue;
222+
if((lp->minscaledenom > 0) && (map->scaledenom <= lp->minscaledenom)) continue;
223+
}
208224
msDrawMarkerSymbol(&map->symbolset, image_draw, &marker, theclass->styles[i], lp->scalefactor);
225+
}
209226
}
210227
break;
211228
case MS_LAYER_LINE:
@@ -232,8 +249,14 @@ int msDrawLegendIcon(mapObj *map, layerObj *lp, classObj *theclass,
232249
zigzag.line[0].point[3].x = dstX + width - offset;
233250
zigzag.line[0].point[3].y = dstY + offset;
234251

235-
for(i=0; i<theclass->numstyles; i++)
252+
for(i=0; i<theclass->numstyles; i++) {
253+
if(!scale_independant && map->scaledenom > 0) {
254+
styleObj *lp = theclass->styles[i];
255+
if((lp->maxscaledenom > 0) && (map->scaledenom > lp->maxscaledenom)) continue;
256+
if((lp->minscaledenom > 0) && (map->scaledenom <= lp->minscaledenom)) continue;
257+
}
236258
msDrawLineSymbol(&map->symbolset, image_draw, &zigzag, theclass->styles[i], lp->scalefactor);
259+
}
237260

238261
free(zigzag.line[0].point);
239262
free(zigzag.line);
@@ -242,8 +265,14 @@ int msDrawLegendIcon(mapObj *map, layerObj *lp, classObj *theclass,
242265
case MS_LAYER_RASTER:
243266
case MS_LAYER_CHART:
244267
case MS_LAYER_POLYGON:
245-
for(i=0; i<theclass->numstyles; i++)
268+
for(i=0; i<theclass->numstyles; i++) {
269+
if(!scale_independant && map->scaledenom > 0) {
270+
styleObj *lp = theclass->styles[i];
271+
if((lp->maxscaledenom > 0) && (map->scaledenom > lp->maxscaledenom)) continue;
272+
if((lp->minscaledenom > 0) && (map->scaledenom <= lp->minscaledenom)) continue;
273+
}
246274
msDrawShadeSymbol(&map->symbolset, image_draw, &box, theclass->styles[i], lp->scalefactor);
275+
}
247276
break;
248277
default:
249278
return MS_FAILURE;
@@ -308,7 +337,7 @@ int msDrawLegendIcon(mapObj *map, layerObj *lp, classObj *theclass,
308337
}
309338

310339

311-
imageObj *msCreateLegendIcon(mapObj* map, layerObj* lp, classObj* class, int width, int height)
340+
imageObj *msCreateLegendIcon(mapObj* map, layerObj* lp, classObj* class, int width, int height, int scale_independant)
312341
{
313342
imageObj *image;
314343
outputFormatObj *format = NULL;
@@ -344,10 +373,10 @@ imageObj *msCreateLegendIcon(mapObj* map, layerObj* lp, classObj* class, int wid
344373
msClearLayerPenValues(lp); /* just in case the mapfile has already been processed */
345374
#endif
346375
if (class) {
347-
msDrawLegendIcon(map, lp, class, width, height, image, 0, 0);
376+
msDrawLegendIcon(map, lp, class, width, height, image, 0, 0, scale_independant);
348377
} else {
349378
for (i=0; i<lp->numclasses; i++) {
350-
msDrawLegendIcon(map, lp, lp->class[i], width, height, image, 0, 0);
379+
msDrawLegendIcon(map, lp, lp->class[i], width, height, image, 0, 0, scale_independant);
351380
}
352381
}
353382
}
@@ -594,7 +623,7 @@ imageObj *msDrawLegend(mapObj *map, int scale_independent)
594623
map->cellsize = msAdjustExtent(&(map->extent), map->width, map->height);
595624
cur->layer->scalefactor = (msInchesPerUnit(cur->layer->sizeunits,0)/msInchesPerUnit(map->units,0)) / map->cellsize;
596625
}
597-
if(msDrawLegendIcon(map, cur->layer, cur->theclass, map->legend.keysizex, map->legend.keysizey, image, HMARGIN, (int) pnt.y) != MS_SUCCESS)
626+
if(msDrawLegendIcon(map, cur->layer, cur->theclass, map->legend.keysizex, map->legend.keysizey, image, HMARGIN, (int) pnt.y, scale_independent) != MS_SUCCESS)
598627
return NULL;
599628

600629
/*

mapscript/php/mapscript_i.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1081,12 +1081,12 @@ int classObj_drawLegendIcon(classObj *self, mapObj *map, layerObj *layer, int wi
10811081
#ifdef USE_GD
10821082
msClearLayerPenValues(layer); // just in case the mapfile has already been processed
10831083
#endif
1084-
return msDrawLegendIcon(map, layer, self, width, height, dstImg, dstX, dstY);
1084+
return msDrawLegendIcon(map, layer, self, width, height, dstImg, dstX, dstY, MS_TRUE);
10851085
}
10861086

10871087
imageObj *classObj_createLegendIcon(classObj *self, mapObj *map, layerObj *layer, int width, int height)
10881088
{
1089-
return msCreateLegendIcon(map, layer, self, width, height);
1089+
return msCreateLegendIcon(map, layer, self, width, height, MS_TRUE);
10901090
}
10911091

10921092

mapscript/swiginc/class.i

+2-2
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,12 @@
178178
}
179179

180180
int drawLegendIcon(mapObj *map, layerObj *layer, int width, int height, imageObj *dstImage, int dstX, int dstY) {
181-
return msDrawLegendIcon(map, layer, self, width, height, dstImage, dstX, dstY);
181+
return msDrawLegendIcon(map, layer, self, width, height, dstImage, dstX, dstY, MS_TRUE);
182182
}
183183

184184
%newobject createLegendIcon;
185185
imageObj *createLegendIcon(mapObj *map, layerObj *layer, int width, int height) {
186-
return msCreateLegendIcon(map, layer, self, width, height);
186+
return msCreateLegendIcon(map, layer, self, width, height, MS_TRUE);
187187
}
188188

189189
%newobject getLabel;

mapserver.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -2109,8 +2109,8 @@ extern "C" {
21092109
MS_DLL_EXPORT int msLegendCalcSize(mapObj *map, int scale_independent, int *size_x, int *size_y,
21102110
int *alayers, int numl_ayer);
21112111
MS_DLL_EXPORT int msEmbedLegend(mapObj *map, imageObj *img);
2112-
MS_DLL_EXPORT int msDrawLegendIcon(mapObj* map, layerObj* lp, classObj* myClass, int width, int height, imageObj *img, int dstX, int dstY);
2113-
MS_DLL_EXPORT imageObj *msCreateLegendIcon(mapObj* map, layerObj* lp, classObj* myClass, int width, int height);
2112+
MS_DLL_EXPORT int msDrawLegendIcon(mapObj* map, layerObj* lp, classObj* myClass, int width, int height, imageObj *img, int dstX, int dstY, int scale_independant);
2113+
MS_DLL_EXPORT imageObj *msCreateLegendIcon(mapObj* map, layerObj* lp, classObj* myClass, int width, int height, int scale_independant);
21142114

21152115
MS_DLL_EXPORT int msLoadFontSet(fontSetObj *fontSet, mapObj *map); /* in maplabel.c */
21162116
MS_DLL_EXPORT int msInitFontSet(fontSetObj *fontset);

mapservutil.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1578,7 +1578,7 @@ int msCGIDispatchLegendIconRequest(mapservObj *mapserv)
15781578
/* drop this reference to output format */
15791579
msApplyOutputFormat(&format, NULL, MS_NOOVERRIDE, MS_NOOVERRIDE, MS_NOOVERRIDE);
15801580

1581-
if(msDrawLegendIcon(mapserv->map, GET_LAYER(mapserv->map, layerindex), GET_LAYER(mapserv->map, layerindex)->class[classindex], mapserv->map->legend.keysizex, mapserv->map->legend.keysizey, img, 0, 0) != MS_SUCCESS)
1581+
if(msDrawLegendIcon(mapserv->map, GET_LAYER(mapserv->map, layerindex), GET_LAYER(mapserv->map, layerindex)->class[classindex], mapserv->map->legend.keysizex, mapserv->map->legend.keysizey, img, 0, 0, MS_TRUE) != MS_SUCCESS)
15821582
return MS_FAILURE;
15831583

15841584
if(mapserv->sendheaders) {

maptemplate.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -2407,10 +2407,10 @@ int processIcon(mapObj *map, int nIdxLayer, int nIdxClass, char** pszInstr, char
24072407

24082408
if(thisClass == NULL) {
24092409
/* Nonexistent class. Create an empty image */
2410-
img = msCreateLegendIcon(map, NULL, NULL, nWidth, nHeight);
2410+
img = msCreateLegendIcon(map, NULL, NULL, nWidth, nHeight, MS_TRUE);
24112411
} else {
24122412
img = msCreateLegendIcon(map, GET_LAYER(map, nIdxLayer),
2413-
thisClass, nWidth, nHeight);
2413+
thisClass, nWidth, nHeight, MS_TRUE);
24142414
}
24152415

24162416
if(!img) {

mapwms.c

+13-3
Original file line numberDiff line numberDiff line change
@@ -4611,9 +4611,19 @@ this request. Check wms/ows_enable_request settings.",
46114611
nHeight = 20;
46124612
}
46134613

4614-
img = msCreateLegendIcon(map, GET_LAYER(map, iLayerIndex),
4615-
GET_LAYER(map, iLayerIndex)->class[i],
4616-
nWidth, nHeight);
4614+
if ( psScale != NULL ) {
4615+
/* Scale-dependent legend. calculate map->scaledenom */
4616+
map->cellsize = msAdjustExtent(&(map->extent), map->width, map->height);
4617+
msCalculateScale(map->extent, map->units, map->width, map->height, map->resolution, &map->scaledenom);
4618+
img = msCreateLegendIcon(map, GET_LAYER(map, iLayerIndex),
4619+
GET_LAYER(map, iLayerIndex)->class[i],
4620+
nWidth, nHeight, MS_FALSE);
4621+
} else {
4622+
/* Scale-independent legend */
4623+
img = msCreateLegendIcon(map, GET_LAYER(map, iLayerIndex),
4624+
GET_LAYER(map, iLayerIndex)->class[i],
4625+
nWidth, nHeight, MS_TRUE);
4626+
}
46174627
}
46184628
if (img == NULL) {
46194629
msSetError(MS_IMGERR,

0 commit comments

Comments
 (0)