use of org.polymap.core.style.serialize.sld2.SLDSerializer2 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