Search in sources :

Example 1 with Context

use of org.polymap.core.style.serialize.FeatureStyleSerializer.Context in project polymap4-core by Polymap4.

the class PolygonStyleSerializer method serialize.

@Override
public void serialize(PolygonStyle style, Style result) {
    // default symbolizer
    PolygonSymbolizer polygon = sf.createPolygonSymbolizer();
    polygon.setFill(sf.getDefaultFill());
    polygon.setStroke(sf.getDefaultStroke());
    FeatureTypeStyle fts = defaultFeatureTypeStyle(result, style, polygon);
    fts.setName(style.title.opt().orElse("PolygonStyle"));
    fts.getDescription().setTitle(style.title.opt().orElse("PolygonStyle"));
    accessor.set(rule -> (PolygonSymbolizer) rule.symbolizers().get(0));
    // fill
    style.fill.opt().ifPresent(fill -> {
        new FillCompositeSerializer(context).accessor.put(rule -> accessor.get().apply(rule).getFill()).serialize(fill, fts);
    });
    // stroke
    style.stroke.opt().ifPresent(stroke -> {
        new StrokeCompositeSerializer(context).accessor.put(rule -> accessor.get().apply(rule).getStroke()).serialize(stroke, fts);
    });
}
Also used : PolygonStyle(org.polymap.core.style.model.feature.PolygonStyle) Style(org.geotools.styling.Style) Context(org.polymap.core.style.serialize.FeatureStyleSerializer.Context) PolygonSymbolizer(org.geotools.styling.PolygonSymbolizer) FeatureTypeStyle(org.geotools.styling.FeatureTypeStyle) PolygonSymbolizer(org.geotools.styling.PolygonSymbolizer) FeatureTypeStyle(org.geotools.styling.FeatureTypeStyle)

Example 2 with Context

use of org.polymap.core.style.serialize.FeatureStyleSerializer.Context in project polymap4-core by Polymap4.

the class TextStyleSerializer method serialize.

@Override
public void serialize(TextStyle style, Style result) {
    // default symbolizer
    TextSymbolizer text = sf.createTextSymbolizer();
    text.setFill(sf.getDefaultFill());
    style.halo.opt().ifPresent(halo -> text.setHalo(sf.createHalo(sf.getDefaultFill(), ff.literal(1))));
    style.font.opt().ifPresent(font -> text.setFont(sf.getDefaultFont()));
    FeatureTypeStyle fts = defaultFeatureTypeStyle(result, style, text);
    fts.setName(style.title.opt().orElse("TextStyle"));
    fts.getDescription().setTitle(style.title.opt().orElse("TextStyle"));
    accessor.set(rule -> (TextSymbolizer) rule.symbolizers().get(0));
    serialize(style, fts);
    // font
    style.font.opt().ifPresent(font -> {
        new FontCompositeSerializer(context).accessor.put(rule -> accessor.get().apply(rule).getFont()).serialize(font, fts);
    });
    // halo
    style.halo.opt().ifPresent(halo -> {
        // XXX for what reason ever for halo color an awt.Color does not seem to work
        set(fts, halo.color, (value, sym) -> sym.getHalo().getFill().setColor(ff.literal(SLDSerializer2.toHexString((Color) ((Literal) value).getValue()))));
        set(fts, halo.opacity, (value, sym) -> sym.getHalo().getFill().setOpacity(value));
        set(fts, halo.width, (value, sym) -> sym.getHalo().setRadius(value));
    });
}
Also used : Color(java.awt.Color) Style(org.geotools.styling.Style) TextStyle(org.polymap.core.style.model.feature.TextStyle) Literal(org.opengis.filter.expression.Literal) Context(org.polymap.core.style.serialize.FeatureStyleSerializer.Context) TextSymbolizer(org.geotools.styling.TextSymbolizer) FeatureTypeStyle(org.geotools.styling.FeatureTypeStyle) TextSymbolizer(org.geotools.styling.TextSymbolizer) Color(java.awt.Color) FeatureTypeStyle(org.geotools.styling.FeatureTypeStyle)

Example 3 with Context

use of org.polymap.core.style.serialize.FeatureStyleSerializer.Context in project polymap4-core by Polymap4.

the class LineStyleSerializer method serialize.

@Override
public void serialize(LineStyle style, FeatureTypeStyle fts) {
    accessor.set(rule -> rule.symbolizers().stream().map(s -> (LineSymbolizer) s).collect(Collectors.toList()));
    // inner line: 2nd LineSymbolizer
    style.fill.opt().ifPresent(fillStroke -> {
        new StrokeCompositeSerializer(context).accessor.put(rule -> accessor.get().apply(rule).get(1).getStroke()).serialize(fillStroke, fts);
    });
    // stroke line: 1st LineSymbolizer
    style.stroke.opt().ifPresent(outerStroke -> {
        set(fts, outerStroke.width, (value, syms) -> {
            Literal innerWidth = (Literal) syms.get(1).getStroke().getWidth();
            syms.get(0).getStroke().setWidth(ff.add(ff.multiply(value, ff.literal(2)), innerWidth));
        });
        set(fts, outerStroke.color, (value, syms) -> syms.get(0).getStroke().setColor(value));
        set(fts, outerStroke.opacity, (value, syms) -> syms.get(0).getStroke().setOpacity(value));
    // XXX linestyle
    });
}
Also used : Style(org.geotools.styling.Style) List(java.util.List) LineStyle(org.polymap.core.style.model.feature.LineStyle) Literal(org.opengis.filter.expression.Literal) Context(org.polymap.core.style.serialize.FeatureStyleSerializer.Context) Collectors(java.util.stream.Collectors) LineSymbolizer(org.geotools.styling.LineSymbolizer) FeatureTypeStyle(org.geotools.styling.FeatureTypeStyle) Literal(org.opengis.filter.expression.Literal)

Example 4 with Context

use of org.polymap.core.style.serialize.FeatureStyleSerializer.Context in project polymap4-core by Polymap4.

the class StyleRepository method serializedFeatureStyle.

/**
 * The serialized version of the {@link FeatureStyle} with the given id. The
 * result is cached until next time the style is stored.
 *
 * @param id The id of the {@link FeatureStyle} to serialize. The instance has to
 *        be <b>stored</b>.
 * @param targetType The target type of the serialization. Possible values are:
 *        {@link geotools.styling.Style} and {@link String}.
 * @param outputFormat The output format of the serialization. Possible values are:
 *        {@link org.polymap.core.style.serialize.FeatureStyleSerializer.OutputFormat}.
 * @return The serialized version, or {@link Optional#empty()} no style exists
 *         for the given id.
 * @throws RuntimeException If targetType is not supported.
 */
public <T> Optional<T> serializedFeatureStyle(String id, Class<T> targetType, OutputFormat outputFormat) {
    // build/cache geotools.styling.Style first
    // avoid starting the SLDSerializer on the same style for different targetTypes;
    CacheKey cacheKey = new CacheKey(id, org.geotools.styling.Style.class.getName(), outputFormat);
    Optional<org.geotools.styling.Style> style = serialized.computeIfAbsent(cacheKey, key -> {
        try {
            log.debug("serializedFeatureStyle(): start... (" + key + ")");
            return featureStyle(id).map(fs -> {
                Context sc = new Context().featureStyle.put(fs).outputFormat.put(outputFormat);
                org.geotools.styling.Style result = new SLDSerializer2().serialize(sc);
                log.info("SLD: " + toSLD(result));
                return result;
            });
        } catch (Exception e) {
            log.warn("Unable to load and/or serialize the style.", e);
            return Optional.empty();
        } finally {
            log.debug("serializedFeatureStyle(): end.");
        }
    });
    // no style found
    if (!style.isPresent()) {
        return Optional.empty();
    } else // geotools.styling.Style
    if (org.geotools.styling.Style.class.isAssignableFrom(targetType)) {
        return (Optional<T>) style;
    } else // String / SLD
    if (String.class.isAssignableFrom(targetType)) {
        CacheKey ck = new CacheKey(id, targetType.getName(), outputFormat);
        return serialized.computeIfAbsent(ck, key -> {
            return Optional.of(toSLD(style.get()));
        });
    } else {
        throw new RuntimeException("Unhandled serialization result type: " + targetType);
    }
}
Also used : Context(org.polymap.core.style.serialize.FeatureStyleSerializer.Context) RasterColorMapStyle(org.polymap.core.style.model.raster.RasterColorMapStyle) RasterGrayStyle(org.polymap.core.style.model.raster.RasterGrayStyle) RasterRGBStyle(org.polymap.core.style.model.raster.RasterRGBStyle) SLDSerializer2(org.polymap.core.style.serialize.sld2.SLDSerializer2) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) ConcurrentEntityModificationException(org.polymap.model2.runtime.ConcurrentEntityModificationException)

Aggregations

Context (org.polymap.core.style.serialize.FeatureStyleSerializer.Context)4 FeatureTypeStyle (org.geotools.styling.FeatureTypeStyle)3 Style (org.geotools.styling.Style)3 Literal (org.opengis.filter.expression.Literal)2 Color (java.awt.Color)1 IOException (java.io.IOException)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 TransformerException (javax.xml.transform.TransformerException)1 LineSymbolizer (org.geotools.styling.LineSymbolizer)1 PolygonSymbolizer (org.geotools.styling.PolygonSymbolizer)1 TextSymbolizer (org.geotools.styling.TextSymbolizer)1 LineStyle (org.polymap.core.style.model.feature.LineStyle)1 PolygonStyle (org.polymap.core.style.model.feature.PolygonStyle)1 TextStyle (org.polymap.core.style.model.feature.TextStyle)1 RasterColorMapStyle (org.polymap.core.style.model.raster.RasterColorMapStyle)1 RasterGrayStyle (org.polymap.core.style.model.raster.RasterGrayStyle)1 RasterRGBStyle (org.polymap.core.style.model.raster.RasterRGBStyle)1 SLDSerializer2 (org.polymap.core.style.serialize.sld2.SLDSerializer2)1 ConcurrentEntityModificationException (org.polymap.model2.runtime.ConcurrentEntityModificationException)1