Skip to content

Commit

Permalink
Allow expressions in LABEL PRIORITY (#6884)
Browse files Browse the repository at this point in the history
* Allow expressions for LABEL PRIORITY

* Add msautotest images

* Add check for duplicate PRIORITY
  • Loading branch information
geographika committed May 29, 2023
1 parent 497ba5d commit db74212
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 2 deletions.
15 changes: 13 additions & 2 deletions mapfile.c
Expand Up @@ -1805,14 +1805,25 @@ static int loadLabel(labelObj *label)
}
break;
case(PRIORITY):
if((symbol = getSymbol(2, MS_NUMBER,MS_BINDING)) == -1) return(-1);
if (label->exprBindings[MS_LABEL_BINDING_PRIORITY].string) {
msFreeExpression(&label->exprBindings[MS_LABEL_BINDING_PRIORITY]);
label->nexprbindings--;
}

if((symbol = getSymbol(3, MS_EXPRESSION,MS_NUMBER,MS_BINDING)) == -1) return(-1);
if(symbol == MS_NUMBER) {
if(msCheckNumber(msyynumber, MS_NUM_CHECK_RANGE, 1, MS_MAX_LABEL_PRIORITY) == MS_FAILURE) {
msSetError(MS_MISCERR, "Invalid PRIORITY, must be an integer between 1 and %d (line %d)" , "loadLabel()", MS_MAX_LABEL_PRIORITY, msyylineno);
return(-1);
}
label->priority = (int) msyynumber;
} else {
} else if (symbol == MS_EXPRESSION) {
msFree(label->exprBindings[MS_LABEL_BINDING_PRIORITY].string);
label->exprBindings[MS_LABEL_BINDING_PRIORITY].string = msStrdup(msyystring_buffer);
label->exprBindings[MS_LABEL_BINDING_PRIORITY].type = MS_EXPRESSION;
label->nexprbindings++;
}
else {
if (label->bindings[MS_LABEL_BINDING_PRIORITY].item != NULL)
msFree(label->bindings[MS_LABEL_BINDING_PRIORITY].item);
label->bindings[MS_LABEL_BINDING_PRIORITY].item = msStrdup(msyystring_buffer);
Expand Down
6 changes: 6 additions & 0 deletions maputil.c
Expand Up @@ -391,6 +391,12 @@ static void bindLabel(layerObj *layer, shapeObj *shape, labelObj *label, int dra
bindColorAttribute(&label->outlinecolor, txt);
msFree(txt);
}
if (label->exprBindings[MS_LABEL_BINDING_PRIORITY].type == MS_EXPRESSION)
{
label->priority = msEvalDoubleExpression(
&(label->exprBindings[MS_LABEL_BINDING_PRIORITY]),
shape);
}
}
}

Expand Down
Binary file modified msautotest/renderers/data/label_alias_offset.dbf
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
80 changes: 80 additions & 0 deletions msautotest/renderers/labels-priority.map
@@ -0,0 +1,80 @@
# RUN_PARMS: label_priority_binding.png [MAP2IMG] -m [MAPFILE] -l label_priority_binding -o [RESULT]
# RUN_PARMS: label_priority_fixed.png [MAP2IMG] -m [MAPFILE] -l label_priority_fixed -o [RESULT]
# RUN_PARMS: label_priority_expression.png [MAP2IMG] -m [MAPFILE] -l label_priority_expression -o [RESULT]
#
# Test label priorities
#

MAP
IMAGETYPE "png"
SIZE 400 400
EXTENT -.5 -.5 1.5 1.5
FONTSET "../misc/fonts.lst"
PROJECTION
"init=epsg:4326"
END
LAYER
NAME "label_priority_fixed"
TYPE POINT
STATUS OFF
DATA "data/label_alias_offset.shp"
LABELITEM "label"
CLASS
# make sure pnt7 gets labelling priority
EXPRESSION ([priority] = 7)
LABEL
FONT "default"
TYPE TRUETYPE
SIZE 40
COLOR 0 0 0
PRIORITY 10
END
END
CLASS
EXPRESSION ([priority] != 7)
LABEL
FONT "default"
TYPE TRUETYPE
SIZE 12
COLOR 0 0 0
PRIORITY 1
END
END
END
LAYER
NAME "label_priority_binding"
TYPE POINT
STATUS OFF
DATA "data/label_alias_offset.shp"
LABELITEM "label"
CLASS
LABEL
FONT "default"
TYPE TRUETYPE
SIZE 12
COLOR 0 0 0
PRIORITY [priority]
END
END
END
LAYER
NAME "label_priority_expression"
TYPE POINT
STATUS OFF
DATA "data/label_alias_offset.shp"
LABELITEM "label"
CLASS
LABEL
FONT "default"
TYPE TRUETYPE
SIZE 12
COLOR 0 0 0
# add a duplicate PRIORITY that will be ignored
# but check it is cleaned up correctly
PRIORITY ([priority] * 2)
# use an expression to invert the label priorities
PRIORITY (10 - [priority])
END
END
END
END

0 comments on commit db74212

Please sign in to comment.