Search in sources :

Example 1 with SLDSerializer2

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

IOException (java.io.IOException)1 TransformerException (javax.xml.transform.TransformerException)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 Context (org.polymap.core.style.serialize.FeatureStyleSerializer.Context)1 SLDSerializer2 (org.polymap.core.style.serialize.sld2.SLDSerializer2)1 ConcurrentEntityModificationException (org.polymap.model2.runtime.ConcurrentEntityModificationException)1