Skip to content

Commit

Permalink
Expression to SLD: strip whitespace from non-string expressions (MapS…
Browse files Browse the repository at this point in the history
…erver#6983)

Fixes MapServer#6892

Expressions are stripped of whitespace before being used as literal values in SLD expressions. 

`EXPRESSION ( [FID] > 1 And [FID] <= 10 )` will now produce:

```xml
<ogc:Filter><ogc:And><ogc:PropertyIsGreaterThan><ogc:PropertyName>FID</ogc:PropertyName>
<ogc:Literal>1</ogc:Literal>
</ogc:PropertyIsGreaterThan><ogc:PropertyIsLessThanOrEqualTo><ogc:PropertyName>FID</ogc:PropertyName>
<ogc:Literal>10</ogc:Literal></ogc:PropertyIsLessThanOrEqualTo></ogc:And></ogc:Filter>
```

Spaces at the end of strings in quotes are preserved:

` EXPRESSION ( [locationtype] = "primary location " )` will produce:

```xml
<ogc:Filter><ogc:PropertyIsEqualTo><ogc:PropertyName>locationtype</ogc:PropertyName>
<ogc:Literal>primary location </ogc:Literal>
</ogc:PropertyIsEqualTo></ogc:Filter>
```

Whitespace in list expressions are also preserved. 

`EXPRESSION {primary location,secondary location }` will produce:

```xml
<ogc:Filter><ogc:Or><ogc:PropertyIsEqualTo><ogc:PropertyName>locationtype</ogc:PropertyName>
<ogc:Literal>primary location</ogc:Literal></ogc:PropertyIsEqualTo>
<ogc:PropertyIsEqualTo><ogc:PropertyName>locationtype</ogc:PropertyName><ogc:Literal>secondary location </ogc:Literal></ogc:PropertyIsEqualTo>
</ogc:Or></ogc:Filter><se:PointSymbolizer>
```
  • Loading branch information
geographika authored Dec 12, 2023
1 parent b43fd4f commit 1df4a96
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 13 deletions.
2 changes: 1 addition & 1 deletion msautotest/wxs/expected/wms_getstyles_expressions1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<UserStyle>
<se:FeatureTypeStyle>
<se:Rule>
<ogc:Filter><ogc:And><ogc:PropertyIsGreaterThan><ogc:PropertyName>FID</ogc:PropertyName><ogc:Literal>1</ogc:Literal></ogc:PropertyIsGreaterThan><ogc:PropertyIsLessThanOrEqualTo><ogc:PropertyName>FID</ogc:PropertyName><ogc:Literal>10 </ogc:Literal></ogc:PropertyIsLessThanOrEqualTo></ogc:And></ogc:Filter>
<ogc:Filter><ogc:And><ogc:PropertyIsGreaterThan><ogc:PropertyName>FID</ogc:PropertyName><ogc:Literal>1</ogc:Literal></ogc:PropertyIsGreaterThan><ogc:PropertyIsLessThanOrEqualTo><ogc:PropertyName>FID</ogc:PropertyName><ogc:Literal>10</ogc:Literal></ogc:PropertyIsLessThanOrEqualTo></ogc:And></ogc:Filter>
<se:PointSymbolizer>
<se:Graphic>
<se:Mark>
Expand Down
25 changes: 25 additions & 0 deletions msautotest/wxs/expected/wms_getstyles_expressions18.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<StyledLayerDescriptor version="1.1.0" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd" xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:se="http://www.opengis.net/se" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<NamedLayer>
<se:Name>Test18</se:Name>
<UserStyle>
<se:FeatureTypeStyle>
<se:Rule>
<ogc:Filter><ogc:Or><ogc:PropertyIsEqualTo><ogc:PropertyName>locationtype</ogc:PropertyName><ogc:Literal>primary location</ogc:Literal></ogc:PropertyIsEqualTo>
<ogc:PropertyIsEqualTo><ogc:PropertyName>locationtype</ogc:PropertyName><ogc:Literal>secondary location </ogc:Literal></ogc:PropertyIsEqualTo>
</ogc:Or></ogc:Filter><se:PointSymbolizer>
<se:Graphic>
<se:Mark>
<se:WellKnownName>square</se:WellKnownName>
<se:Fill>
<se:SvgParameter name="fill">#dc0000</se:SvgParameter>
</se:Fill>
</se:Mark>
<se:Size>1</se:Size>
</se:Graphic>
</se:PointSymbolizer>
</se:Rule>
</se:FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>

24 changes: 24 additions & 0 deletions msautotest/wxs/expected/wms_getstyles_expressions19.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<StyledLayerDescriptor version="1.1.0" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd" xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:se="http://www.opengis.net/se" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<NamedLayer>
<se:Name>Test19</se:Name>
<UserStyle>
<se:FeatureTypeStyle>
<se:Rule>
<ogc:Filter><ogc:PropertyIsEqualTo><ogc:PropertyName>locationtype</ogc:PropertyName><ogc:Literal>primary location</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Filter>
<se:PointSymbolizer>
<se:Graphic>
<se:Mark>
<se:WellKnownName>square</se:WellKnownName>
<se:Fill>
<se:SvgParameter name="fill">#dc0000</se:SvgParameter>
</se:Fill>
</se:Mark>
<se:Size>1</se:Size>
</se:Graphic>
</se:PointSymbolizer>
</se:Rule>
</se:FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>

2 changes: 1 addition & 1 deletion msautotest/wxs/expected/wms_getstyles_expressions2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<UserStyle>
<se:FeatureTypeStyle>
<se:Rule>
<ogc:Filter><ogc:And><ogc:PropertyIsGreaterThan><ogc:PropertyName>FID</ogc:PropertyName><ogc:Literal>1</ogc:Literal></ogc:PropertyIsGreaterThan><ogc:PropertyIsLessThanOrEqualTo><ogc:PropertyName>FID</ogc:PropertyName><ogc:Literal>10 </ogc:Literal></ogc:PropertyIsLessThanOrEqualTo></ogc:And></ogc:Filter>
<ogc:Filter><ogc:And><ogc:PropertyIsGreaterThan><ogc:PropertyName>FID</ogc:PropertyName><ogc:Literal>1</ogc:Literal></ogc:PropertyIsGreaterThan><ogc:PropertyIsLessThanOrEqualTo><ogc:PropertyName>FID</ogc:PropertyName><ogc:Literal>10</ogc:Literal></ogc:PropertyIsLessThanOrEqualTo></ogc:And></ogc:Filter>
<se:PointSymbolizer>
<se:Graphic>
<se:Mark>
Expand Down
24 changes: 24 additions & 0 deletions msautotest/wxs/expected/wms_getstyles_expressions20.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<StyledLayerDescriptor version="1.1.0" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd" xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:se="http://www.opengis.net/se" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<NamedLayer>
<se:Name>Test20</se:Name>
<UserStyle>
<se:FeatureTypeStyle>
<se:Rule>
<ogc:Filter><ogc:PropertyIsEqualTo><ogc:PropertyName>locationtype</ogc:PropertyName><ogc:Literal>primary location </ogc:Literal></ogc:PropertyIsEqualTo></ogc:Filter>
<se:PointSymbolizer>
<se:Graphic>
<se:Mark>
<se:WellKnownName>square</se:WellKnownName>
<se:Fill>
<se:SvgParameter name="fill">#dc0000</se:SvgParameter>
</se:Fill>
</se:Mark>
<se:Size>1</se:Size>
</se:Graphic>
</se:PointSymbolizer>
</se:Rule>
</se:FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>

2 changes: 1 addition & 1 deletion msautotest/wxs/expected/wms_getstyles_expressions3.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<UserStyle>
<se:FeatureTypeStyle>
<se:Rule>
<ogc:Filter><ogc:And><ogc:PropertyIsGreaterThan><ogc:PropertyName>FID</ogc:PropertyName><ogc:Literal>1</ogc:Literal></ogc:PropertyIsGreaterThan><ogc:PropertyIsLessThanOrEqualTo><ogc:PropertyName>FID</ogc:PropertyName><ogc:Literal>10 </ogc:Literal></ogc:PropertyIsLessThanOrEqualTo></ogc:And></ogc:Filter>
<ogc:Filter><ogc:And><ogc:PropertyIsGreaterThan><ogc:PropertyName>FID</ogc:PropertyName><ogc:Literal>1</ogc:Literal></ogc:PropertyIsGreaterThan><ogc:PropertyIsLessThanOrEqualTo><ogc:PropertyName>FID</ogc:PropertyName><ogc:Literal>10</ogc:Literal></ogc:PropertyIsLessThanOrEqualTo></ogc:And></ogc:Filter>
<se:PointSymbolizer>
<se:Graphic>
<se:Mark>
Expand Down
2 changes: 1 addition & 1 deletion msautotest/wxs/expected/wms_getstyles_expressions4.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<UserStyle>
<se:FeatureTypeStyle>
<se:Rule>
<ogc:Filter><ogc:Or><ogc:PropertyIsEqualTo><ogc:PropertyName>FID</ogc:PropertyName><ogc:Literal>1</ogc:Literal></ogc:PropertyIsEqualTo><ogc:PropertyIsEqualTo><ogc:PropertyName>FID</ogc:PropertyName><ogc:Literal>10 </ogc:Literal></ogc:PropertyIsEqualTo></ogc:Or></ogc:Filter>
<ogc:Filter><ogc:Or><ogc:PropertyIsEqualTo><ogc:PropertyName>FID</ogc:PropertyName><ogc:Literal>1</ogc:Literal></ogc:PropertyIsEqualTo><ogc:PropertyIsEqualTo><ogc:PropertyName>FID</ogc:PropertyName><ogc:Literal>10</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Or></ogc:Filter>
<se:PointSymbolizer>
<se:Graphic>
<se:Mark>
Expand Down
2 changes: 1 addition & 1 deletion msautotest/wxs/expected/wms_getstyles_expressions5.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<UserStyle>
<se:FeatureTypeStyle>
<se:Rule>
<ogc:Filter><ogc:Or><ogc:PropertyIsEqualTo><ogc:PropertyName>FID</ogc:PropertyName><ogc:Literal>1</ogc:Literal></ogc:PropertyIsEqualTo><ogc:PropertyIsEqualTo><ogc:PropertyName>FID</ogc:PropertyName><ogc:Literal>10 </ogc:Literal></ogc:PropertyIsEqualTo></ogc:Or></ogc:Filter>
<ogc:Filter><ogc:Or><ogc:PropertyIsEqualTo><ogc:PropertyName>FID</ogc:PropertyName><ogc:Literal>1</ogc:Literal></ogc:PropertyIsEqualTo><ogc:PropertyIsEqualTo><ogc:PropertyName>FID</ogc:PropertyName><ogc:Literal>10</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Or></ogc:Filter>
<se:PointSymbolizer>
<se:Graphic>
<se:Mark>
Expand Down
2 changes: 1 addition & 1 deletion msautotest/wxs/expected/wms_getstyles_expressions6.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<UserStyle>
<se:FeatureTypeStyle>
<se:Rule>
<ogc:Filter><ogc:Or><ogc:PropertyIsEqualTo><ogc:PropertyName>FID</ogc:PropertyName><ogc:Literal>1</ogc:Literal></ogc:PropertyIsEqualTo><ogc:PropertyIsEqualTo><ogc:PropertyName>FID</ogc:PropertyName><ogc:Literal>10 </ogc:Literal></ogc:PropertyIsEqualTo></ogc:Or></ogc:Filter>
<ogc:Filter><ogc:Or><ogc:PropertyIsEqualTo><ogc:PropertyName>FID</ogc:PropertyName><ogc:Literal>1</ogc:Literal></ogc:PropertyIsEqualTo><ogc:PropertyIsEqualTo><ogc:PropertyName>FID</ogc:PropertyName><ogc:Literal>10</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Or></ogc:Filter>
<se:PointSymbolizer>
<se:Graphic>
<se:Mark>
Expand Down
45 changes: 44 additions & 1 deletion msautotest/wxs/wms_styles_expressions.map
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
# RUN_PARMS: wms_getstyles_expressions15.xml [MAPSERV] QUERY_STRING="map=[MAPFILE]&SERVICE=WMS&VERSION=1.3.0&REQUEST=GetStyles&layers=Test15" > [RESULT_DEMIME]
# RUN_PARMS: wms_getstyles_expressions16.xml [MAPSERV] QUERY_STRING="map=[MAPFILE]&SERVICE=WMS&VERSION=1.3.0&REQUEST=GetStyles&layers=Test16" > [RESULT_DEMIME]
# RUN_PARMS: wms_getstyles_expressions17.xml [MAPSERV] QUERY_STRING="map=[MAPFILE]&SERVICE=WMS&VERSION=1.3.0&REQUEST=GetStyles&layers=Test17" > [RESULT_DEMIME]
# RUN_PARMS: wms_getstyles_expressions18.xml [MAPSERV] QUERY_STRING="map=[MAPFILE]&SERVICE=WMS&VERSION=1.3.0&REQUEST=GetStyles&layers=Test18" > [RESULT_DEMIME]
# RUN_PARMS: wms_getstyles_expressions19.xml [MAPSERV] QUERY_STRING="map=[MAPFILE]&SERVICE=WMS&VERSION=1.3.0&REQUEST=GetStyles&layers=Test19" > [RESULT_DEMIME]
# RUN_PARMS: wms_getstyles_expressions20.xml [MAPSERV] QUERY_STRING="map=[MAPFILE]&SERVICE=WMS&VERSION=1.3.0&REQUEST=GetStyles&layers=Test20" > [RESULT_DEMIME]

MAP
NAME WMS_TEST
Expand Down Expand Up @@ -246,5 +249,45 @@ MAP
WIDTH 1
END
END
END
END

# string list expression with trailing space
LAYER
NAME "Test18"
TYPE POINT
CLASSITEM "locationtype"
CLASS
EXPRESSION {primary location,secondary location }
STYLE
COLOR 220 0 0
WIDTH 1
END
END
END

# string expression
LAYER
NAME "Test19"
TYPE POINT
CLASS
EXPRESSION ( [locationtype] = "primary location" )
STYLE
COLOR 220 0 0
WIDTH 1
END
END
END

# string expression with trailing space
LAYER
NAME "Test20"
TYPE POINT
CLASS
EXPRESSION ( [locationtype] = "primary location " )
STYLE
COLOR 220 0 0
WIDTH 1
END
END
END
END
20 changes: 14 additions & 6 deletions src/mapogcsld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5309,25 +5309,31 @@ char *msSLDGetFilter(classObj *psClass, const char *pszWfsFilter) {
char *pszOgcFilter = NULL;

if (psClass && psClass->expression.string) {

char *pszExpression = msStrdup(psClass->expression.string);

/* string expression */
if (psClass->expression.type == MS_STRING) {

// ensure any trailing whitespace is removed
msStringTrimBlanks(pszExpression);
if (psClass->layer && psClass->layer->classitem) {
std::string osFilter("<ogc:Filter>");
if (pszWfsFilter) {
osFilter += "<ogc:And>";
osFilter += pszWfsFilter;
}
osFilter += GetPropertyIsEqualTo(psClass->layer->classitem,
psClass->expression.string);
osFilter +=
GetPropertyIsEqualTo(psClass->layer->classitem, pszExpression);
if (pszWfsFilter) {
osFilter += "</ogc:And>";
}
osFilter += "</ogc:Filter>\n";
pszFilter = msStrdup(osFilter.c_str());
}
} else if (psClass->expression.type == MS_EXPRESSION) {
pszFilter =
msSLDParseLogicalExpression(psClass->expression.string, pszWfsFilter);
msStringTrimBlanks(pszExpression);
pszFilter = msSLDParseLogicalExpression(pszExpression, pszWfsFilter);
} else if (psClass->expression.type == MS_LIST) {
if (psClass->layer && psClass->layer->classitem) {

Expand All @@ -5337,8 +5343,8 @@ char *msSLDGetFilter(classObj *psClass, const char *pszWfsFilter) {
int tokenCount = 0;
std::string osOrFilters;

listExpressionValues = msStringSplit(psClass->expression.string, ',',
&numListExpressionValues);
listExpressionValues =
msStringSplit(pszExpression, ',', &numListExpressionValues);

// loop through all values in the list and create a PropertyIsEqualTo
// for each value
Expand Down Expand Up @@ -5390,6 +5396,8 @@ char *msSLDGetFilter(classObj *psClass, const char *pszWfsFilter) {
pszFilter = msStrdup(osFilter.c_str());
}
}

msFree(pszExpression);
} else if (pszWfsFilter) {
std::string osFilter("<ogc:Filter>");
osFilter += pszWfsFilter;
Expand Down

0 comments on commit 1df4a96

Please sign in to comment.