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