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

Conversation

hwbllmnn
Copy link
Contributor

@hwbllmnn hwbllmnn commented Jul 12, 2017

From #1648

This fixes GEOS-8034. I'm unsure why the literals were using java.awt.Color objects before, even the tests were checking for that. I tested with Geoserver 2.11 and ysld styles work just fine with this fix. I'll also provide a fix for the master branch shortly.

Bug report https://osgeo-org.atlassian.net/browse/GEOS-8034

if (color != null) {
hex = String.format("#%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue());
hex = hex.toUpperCase();
}
Copy link
Member

Choose a reason for hiding this comment

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

Okay what is this logic trying to do? Is there a bug report? There is a bunch of color converters that cover these cases, would be good to avoid duplication.

Okay updated the description with details from #1648

@jodygarnett
Copy link
Member

jodygarnett commented Aug 28, 2017

@smithkm do you want to have a look at this?

Since this is a bug fix we can merge it after the code freeze

@jodygarnett
Copy link
Member

We have a tension between design (using converters configured with Hints) and YSLD expected functionality.

Functionality to check:

  • three digit hex {{'#00F'}}
  • octal colors {{'0x007'}}

assertEquals(new Color(0x0000FF), ((Literal) Util.color("#0000FF", factory)).getValue());
assertEquals(new Color(0xFFFF00), ((Literal) Util.color("0xFFFF00", factory)).getValue());
assertEquals(new Color(0x00FFFF),
assertEquals("#FF0000", ((Literal) Util.color("FF0000", factory)).getValue());
Copy link
Member

Choose a reason for hiding this comment

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

We need to make sure that litearls with color objects are still supported, please do not remove the above tests, but you can add to them :)

@hwbllmnn
Copy link
Contributor Author

Please see my comment on #1648 , I ported the fix here as well.

Copy link
Member

@tbarsballe tbarsballe left a comment

Choose a reason for hiding this comment

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

Looks like you have found the true source of the error, although I still have some concerns about your approach. See comments inline.

}
}
}

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)

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());
    }

@hwbllmnn
Copy link
Contributor Author

Ok, I've incorporated your changes, will backport to 17.x now.

@tbarsballe tbarsballe merged commit 13aa075 into geotools:master Nov 15, 2017
@tbarsballe
Copy link
Member

I've squashed and merged, and will backport manually, so that the changes are encapsulated within a single commit.

tbarsballe pushed a commit that referenced this pull request Nov 15, 2017
* [GEOS-8034] Fixed ysld color conversion

* Revert "[GEOS-8034] Fixed ysld color conversion"

This reverts commit 8adae77.

* fixed ysdl encoder to allow for Color objects

* incorporate requested changes
tbarsballe pushed a commit that referenced this pull request Nov 15, 2017
* [GEOS-8034] Fixed ysld color conversion

* Revert "[GEOS-8034] Fixed ysld color conversion"

This reverts commit 8adae77.

* fixed ysdl encoder to allow for Color objects

* incorporate requested changes
@hwbllmnn hwbllmnn deleted the fix-ysld-color-conversion-master branch November 16, 2017 15:26
johngschulz pushed a commit to johngschulz/geotools that referenced this pull request Nov 17, 2017
* [GEOS-8034] Fixed ysld color conversion

* Revert "[GEOS-8034] Fixed ysld color conversion"

This reverts commit 8adae77.

* fixed ysdl encoder to allow for Color objects

* incorporate requested changes

Added TODOs to determine where exceptions need to be changed
johngschulz pushed a commit to johngschulz/geotools that referenced this pull request Nov 17, 2017
* [GEOS-8034] Fixed ysld color conversion

* Revert "[GEOS-8034] Fixed ysld color conversion"

This reverts commit 8adae77.

* fixed ysdl encoder to allow for Color objects

* incorporate requested changes

Added TODOs to determine where exceptions need to be changed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants