All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mapfish.print.map.geotools.AbstractFeatureSourceLayer Maven / Gradle / Ivy

package org.mapfish.print.map.geotools;

import com.google.common.collect.Lists;
import org.geotools.data.FeatureSource;
import org.geotools.data.collection.CollectionFeatureSource;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.styling.Style;
import org.geotools.styling.visitor.RescaleStyleVisitor;
import org.mapfish.print.attribute.map.MapfishMapContext;
import org.mapfish.print.http.MfClientHttpRequestFactory;
import org.mapfish.print.map.AbstractLayerParams;

import java.util.List;
import java.util.concurrent.ExecutorService;
import javax.annotation.Nonnull;

/**
 * A layer that wraps a Geotools Feature Source and a style object.
 *
 * @author Jesse on 3/26/14.
 */
public abstract class AbstractFeatureSourceLayer extends AbstractGeotoolsLayer {

    private FeatureSourceSupplier featureSourceSupplier;
    private FeatureSource featureSource = null;
    private StyleSupplier styleSupplier;
    private final Boolean renderAsSvg;

    /**
     * Constructor.
     *
     * @param executorService the thread pool for doing the rendering.
     * @param featureSourceSupplier a function that creates the feature source.  This will only be called once.
     * @param styleSupplier         a function that creates the style for styling the features. This will only be called once.
     * @param renderAsSvg is the layer rendered as SVG?
     * @param params the parameters for this layer
     */
    public AbstractFeatureSourceLayer(final ExecutorService executorService,
                                      final FeatureSourceSupplier featureSourceSupplier,
                                      final StyleSupplier styleSupplier,
                                      final boolean renderAsSvg,
                                      final AbstractLayerParams params) {
        super(executorService, params);
        this.featureSourceSupplier = featureSourceSupplier;
        this.styleSupplier = styleSupplier;

        this.renderAsSvg = renderAsSvg;
    }

    @SuppressWarnings("unchecked")
    public final void setStyle(final StyleSupplier style) {
        this.styleSupplier = style;
    }

    /**
     * Get the feature source (either load from the supplier or return the cached source).
     * @param httpRequestFactory The factory for making http requests.
     * @param mapContext The map context.
     */
    public final FeatureSource getFeatureSource(final MfClientHttpRequestFactory httpRequestFactory,
            final MapfishMapContext mapContext) {
        if (this.featureSource == null) {
            this.featureSource = this.featureSourceSupplier.load(httpRequestFactory, mapContext);
        }
        return this.featureSource;
    }

    @Override
    public final List getLayers(final MfClientHttpRequestFactory httpRequestFactory,
                                                 final MapfishMapContext mapContext,
                                                 final boolean isFirstLayer) throws Exception {
        FeatureSource source = getFeatureSource(httpRequestFactory, mapContext);
        Style style = this.styleSupplier.load(httpRequestFactory, source, mapContext);

        if (mapContext.isDpiSensitiveStyle() && mapContext.getDPI() > mapContext.getRequestorDPI()) {
            // rescale styles for a higher dpi print
            double scaleFactor = mapContext.getDPI() / mapContext.getRequestorDPI();
            RescaleStyleVisitor scale = new RescaleStyleVisitor(scaleFactor);
            style.accept(scale);
            style = (Style) scale.getCopy();
        }

        return Lists.newArrayList(new FeatureLayer(source, style));
    }

    public final void setFeatureCollection(final SimpleFeatureCollection featureCollection) {
        this.featureSourceSupplier = new FeatureSourceSupplier() {

            @Nonnull
            @Override
            public FeatureSource load(@Nonnull final MfClientHttpRequestFactory requestFactory,
                                      @Nonnull final MapfishMapContext mapContext) {
                return new CollectionFeatureSource(featureCollection);
            }
        };
    }
    
    /**
     * Is the layer rendered as SVG?
     */
    public final Boolean shouldRenderAsSvg() {
        return this.renderAsSvg;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy