Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Expressions to be set for LABEL properties using MapScript #6904

Merged
merged 6 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mapfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2022,6 +2022,8 @@ static void writeLabel(FILE *stream, int indent, labelObj *label)

if(label->numbindings > 0 && label->bindings[MS_LABEL_BINDING_PRIORITY].item)
writeAttributeBinding(stream, indent, "PRIORITY", &(label->bindings[MS_LABEL_BINDING_PRIORITY]));
else if (label->nexprbindings > 0 && label->exprBindings[MS_LABEL_BINDING_PRIORITY].string)
writeExpression(stream, indent, "PRIORITY", &(label->exprBindings[MS_LABEL_BINDING_PRIORITY]));
else writeNumber(stream, indent, "PRIORITY", MS_DEFAULT_LABEL_PRIORITY, label->priority);

writeNumber(stream, indent, "REPEATDISTANCE", 0, label->repeatdistance);
Expand Down
11 changes: 11 additions & 0 deletions mapscript/python/tests/cases/label_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ def testLabelBinding(self):
new_label.setBinding(mapscript.MS_LABEL_BINDING_COLOR, "NEW_BINDING")
assert (new_label.getBinding(mapscript.MS_LABEL_BINDING_COLOR) == "NEW_BINDING")

def testLabelExpressionBinding(self):
"""expression binding can be set and get"""
new_label = mapscript.labelObj()
assert (not new_label.getExpressionBinding(mapscript.MS_LABEL_BINDING_PRIORITY))
new_label.setExpressionBinding(mapscript.MS_LABEL_BINDING_PRIORITY, "[MY_ATTRIBUTE] * 2")
assert (new_label.getExpressionBinding(mapscript.MS_LABEL_BINDING_PRIORITY) == "([MY_ATTRIBUTE] * 2)")

exp = 'LABEL\n SIZE 10\n OFFSET 0 0\n POSITION CC\n ' \
'PRIORITY ([MY_ATTRIBUTE] * 2)\n SHADOWSIZE 1 1\nEND # LABEL\n'
assert (new_label.convertToString() == exp)


class LabelCacheMemberTestCase(MapTestCase):

Expand Down
47 changes: 46 additions & 1 deletion mapscript/swiginc/label.i
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,52 @@
return MS_SUCCESS;
}

/// Set the label expression.
/// Remove an expression binding for a specified label property.
int removeExpressionBinding(int binding)
{
if(binding < 0 || binding >= MS_LABEL_BINDING_LENGTH) return MS_FAILURE;

if(self->exprBindings[binding].string) {
msFreeExpression(&self->exprBindings[binding]);
self->nexprbindings--;
}

return MS_SUCCESS;
}

/// Get the expression binding for a specified label property. Returns NULL if there is no binding for this property.
%newobject getExpressionBinding;
const char *getExpressionBinding(int binding)
{
if(binding < 0 || binding >= MS_LABEL_BINDING_LENGTH) return NULL;

return msGetExpressionString(&(self->exprBindings[binding]));
}

/// Set the expression binding for a specified label property. Binding constants look like this: ``MS_LABEL_BINDING_[attribute name]``
/// Expressions are automatically wrapped in brackets, so do not need to be added to the input string
/// >>> new_label.setExpressionBinding(MS_LABEL_BINDING_PRIORITY, "[priority] * 2")
int setExpressionBinding(int binding, const char *text)
{
if (!text || strlen(text) == 0) {
return MS_FAILURE;
}

if(binding < 0 || binding >= MS_LABEL_BINDING_LENGTH) return MS_FAILURE;

if(self->exprBindings[binding].string) {
msFreeExpression(&self->exprBindings[binding]);
self->nexprbindings--;
}

self->exprBindings[binding].string = msStrdup(text);
self->exprBindings[binding].type = MS_EXPRESSION;
self->nexprbindings++;

return MS_SUCCESS;
}

/// Set the label expression property
int setExpression(char *expression)
{
if (!expression || strlen(expression) == 0) {
Expand Down