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

[GEOS-8034] Fix ysld color conversion master #1649

Merged
merged 5 commits into from Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -225,6 +225,17 @@ Object toObjOrNull(Expression expr, boolean isname) {
return null;

List<Expression> subExpressions = Util.splitConcatenates(expr);

if (subExpressions.size() == 1) {
Expression subExpr = subExpressions.get(0);
if (subExpr instanceof Literal) {
Object obj = ((Literal) subExpr).getValue();
if (!(obj instanceof String)) {
return obj;
}
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems quite brittle.
Tracing through the existing YSLD logic, it looks like the only time this gets called with a Color an an argument is when it is called by toColorOrNull (line 170 of this class). Rather than risk breaking the behaviour of toObjOrNull, it would be better to modify that method to return the Color object instead.

A simple check prior to calling this method should be sufficient (replace line 170 with the following):

Object obj;
if (expr instanceof Literal) {
  obj = ((Literal)expr).getValue();
  if (obj instanceof Color) {
    return obj;
  }
}
obj = toObjOrNull(expr, false);

(Additionally, this change violates the api defined in the javadocs for this method - it should return either a String or a Number; if you modify toColorOrNull instead you wont have that issue, as that method is expected to return a Color)

StringBuilder builder = new StringBuilder();
for (Expression subExpr : subExpressions) {
if (isNull(subExpr)) {
Expand Down
Expand Up @@ -32,6 +32,7 @@
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.awt.Color;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
Expand All @@ -41,6 +42,7 @@
import javax.measure.unit.NonSI;

import org.geotools.factory.CommonFactoryFinder;
import org.geotools.filter.FilterFactoryImpl;
import org.geotools.styling.ColorMapEntry;
import org.geotools.styling.FeatureTypeStyle;
import org.geotools.styling.LabelPlacement;
Expand All @@ -61,13 +63,13 @@
import org.geotools.ysld.YamlMap;
import org.geotools.ysld.YamlSeq;
import org.geotools.ysld.Ysld;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.opengis.filter.FilterFactory;
import org.opengis.filter.FilterFactory2;
import org.opengis.filter.expression.Expression;
import org.opengis.filter.expression.Function;
import org.opengis.filter.expression.Literal;
import org.opengis.style.ChannelSelection;
import org.opengis.style.ContrastMethod;
import org.opengis.style.Graphic;
Expand Down Expand Up @@ -1320,4 +1322,12 @@ public void testSymbolizerUoMOther() throws Exception {
exception.expect(IllegalArgumentException.class);
Ysld.encode(sld(fts), out);
}

@Test
public void testColorObject() throws Exception {
RasterSymbolizerEncoder encoder = new RasterSymbolizerEncoder(null);
Literal literal = new FilterFactoryImpl().literal(Color.BLACK);
Object obj = encoder.toObjOrNull(literal);
assertEquals(obj.getClass(), Color.class);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the change suggested above, this test will no longer be valid. Something that tests a full style would probably be better anyway; something like this should work (using the style example reported in the ticket):

    @Test
    public void testEncodeColorMapEntry() throws IOException {
        StyledLayerDescriptor style = new YsldParser(new ByteArrayInputStream(
                    ("name:  Test\n" +
                    "title: Test Style title\n" +
                    "abstract: Styling of Test layer\n" +
                    "feature-styles:\n" +
                    "- rules:\n" +
                    "  - title: raster\n" +
                    "    symbolizers:\n" +
                    "      - raster:\n" +
                    "          opacity: 1.0\n" +
                    "          color-map:\n" +
                    "            type: values\n" +
                    "            entries:\n" +
                    "            - ['#e20374', 1.0, 1, Lorem Ipsum (magenta = covered)]").getBytes())).parse();

        RasterSymbolizer symbolizer = (RasterSymbolizer)((NamedLayer)style.getStyledLayers()[0]).styles().get(0).featureTypeStyles().get(0).rules().get(0).symbolizers().get(0);

        ColorMap colorMap = symbolizer.getColorMap();
        RasterSymbolizerEncoder.ColorMapEntryIterator iterator = new RasterSymbolizerEncoder(symbolizer).new ColorMapEntryIterator(colorMap);
        Tuple map = iterator.next();
        assertEquals("('#E20374',1.0,1,Lorem Ipsum (magenta = covered))", map.toString());
    }

}