javageotools

Geotools - how to style geometrycollection geometries correctly


I'm plotting some geometries on a map using StreamingRenderer that uses one of the two styles:

//line:
LineSymbolizer lineSym = sf.createLineSymbolizer(stroke, null);
//for polygons
PolygonSymbolizer polySym = sf.createPolygonSymbolizer(stroke, fill, null);

This works fine for Linestring, Multilinestring and Polygons.

But for GeometryCollection, if i'm using a Polygon style, but the collection is of LineStrings, then the image becomes "filled", and vice versa, for Line style, then polygon collection becomes non-filled. I thought about splitting a GeometryCollection into separate polygons, but that has problems too, because then the label might not get placed correctly.

Is there a way to create a style that is applicable for inner Geometries inside a GeometryCollection correctly so polygons are lines are styled the same if they are stand alone or inside a GeometryCollection?

Examples:

MULTILINESTRING((100 10, 130 15, 120 12, 100 10))
GEOMETRYCOLLECTION(MULTILINESTRING((100 10, 130 15, 120 12, 100 10)))
GEOMETRYCOLLECTION(POLYGON((100 10, 130 15, 120 12, 100 10)))
POLYGON((100 10, 130 15, 120 12, 100 10

Solution

  • Put simply, what you are seeing is the correct behaviour for `GeometryCollection', the SLD specification says that if you apply a polygon symbolizer to a linestring then it should join the ends and fill the resulting polygon.

    There are various ways of trying to fix this, you can split the collections up into 3 different sets and apply the correct style, or create separate geometry columns or apply a filter based on the geometryType function to specify different rules. There's a page in the GeoServer documentation that gives more details.

    See this answer for more discussion.