Search in sources :

Example 1 with Pipeline

use of org.polymap.core.data.pipeline.Pipeline in project polymap4-core by Polymap4.

the class SimpleWmsServer method doGet.

@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    log.debug("Request: " + request.getQueryString());
    try {
        final Map<String, String> kvp = parseKvpSet(request.getQueryString());
        sessionContext.execute(() -> {
            // LAYERS
            final String layer = kvp.get("LAYERS");
            assert !layer.contains(INNER_DELIMETER);
            final String style = kvp.get("STYLES");
            assert style == null || !style.contains(INNER_DELIMETER);
            log.info("layers=" + layer + ", style=" + style);
            // WIDTH/HEIGHT
            int width = Integer.parseInt(kvp.get("WIDTH"));
            int height = Integer.parseInt(kvp.get("HEIGHT"));
            // BBOX
            ReferencedEnvelope bbox = parseBBox(kvp.get("BBOX"));
            String srsCode = kvp.get("SRS");
            CoordinateReferenceSystem crs = CRS.decode(srsCode);
            bbox = new ReferencedEnvelope(bbox, crs);
            // FORMAT
            String format = kvp.get("FORMAT");
            format = format != null ? format : "image/png";
            log.debug("    --layers= " + layer);
            log.debug("    --imageSize= " + width + "x" + height);
            log.debug("    --bbox= " + bbox);
            crs = bbox.getCoordinateReferenceSystem();
            log.debug("    --CRS= " + bbox.getCoordinateReferenceSystem().getName());
            // find/create pipeline
            final Pipeline pipeline = pipelines.get(layer, key -> createPipeline(key));
            long modifiedSince = request.getDateHeader("If-Modified-Since");
            final ProcessorRequest pr = new GetMapRequest(Collections.singletonList(layer), Collections.singletonList(style), srsCode, bbox, format, width, height, modifiedSince);
            // process
            Lazy<ServletOutputStream> out = new PlainLazyInit(() -> {
                try {
                    return response.getOutputStream();
                } catch (Exception e) {
                    log.warn("Pipeline exception: " + e, e);
                    response.setStatus(502);
                    return null;
                }
            });
            try {
                createPipelineExecutor().execute(pipeline, pr, new ResponseHandler() {

                    @Override
                    public void handle(ProcessorResponse pipeResponse) throws Exception {
                        if (pipeResponse == EncodedImageResponse.NOT_MODIFIED) {
                            response.setStatus(304);
                        } else {
                            long lastModified = ((EncodedImageResponse) pipeResponse).getLastModified();
                            // allow the browser to use a cached tile for max-age without a request
                            if (lastModified > 0) {
                                long maxAge = ((EncodedImageResponse) pipeResponse).getExpires() - System.currentTimeMillis() / 1000;
                                response.setDateHeader("Last-Modified", lastModified);
                                response.setHeader("Cache-Control", "public,must-revalidate");
                            } else // disable browser cache if there is no internal Cache for this layer
                            {
                                response.setHeader("Cache-Control", "no-cache,no-store,must-revalidate");
                                response.setDateHeader("Expires", 0);
                                response.setHeader("Pragma", "no-cache");
                            }
                            byte[] chunk = ((EncodedImageResponse) pipeResponse).getChunk();
                            int len = ((EncodedImageResponse) pipeResponse).getChunkSize();
                            out.get().write(chunk, 0, len);
                        }
                    }
                });
            } catch (Throwable e) {
                log.warn("Pipeline exception: " + e, e);
                response.setStatus(502);
            }
            return null;
        });
    } catch (IOException e) {
        // assuming that this is an EOF exception
        log.info("Exception: " + e);
    } catch (Exception e) {
        log.warn(e.toString(), e);
    } finally {
    // XXX do I have to close out?
    // out.close();
    }
}
Also used : ProcessorResponse(org.polymap.core.data.pipeline.ProcessorResponse) ResponseHandler(org.polymap.core.data.pipeline.ResponseHandler) ServletOutputStream(javax.servlet.ServletOutputStream) IOException(java.io.IOException) PlainLazyInit(org.polymap.core.runtime.PlainLazyInit) FactoryException(org.opengis.referencing.FactoryException) ServletException(javax.servlet.ServletException) NoSuchAuthorityCodeException(org.opengis.referencing.NoSuchAuthorityCodeException) ServiceException(org.osgi.framework.ServiceException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Pipeline(org.polymap.core.data.pipeline.Pipeline) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) EncodedImageResponse(org.polymap.core.data.image.EncodedImageResponse) ProcessorRequest(org.polymap.core.data.pipeline.ProcessorRequest) GetMapRequest(org.polymap.core.data.image.GetMapRequest) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 2 with Pipeline

use of org.polymap.core.data.pipeline.Pipeline in project polymap4-core by Polymap4.

the class PipelineMapOutputFormat method produceMap.

@Override
public final RenderedImageMap produceMap(WMSMapContent mapContent) throws ServiceException {
    // single layer? -> request ENCODED_IMAGE
    if (mapContent.layers().size() == 1) {
        try {
            Layer mapLayer = mapContent.layers().get(0);
            ILayer layer = findLayer(mapLayer);
            Pipeline pipeline = server.getOrCreatePipeline(layer, ImageProducer.class).get();
            ProcessorRequest request = prepareProcessorRequest(mapContent);
            AtomicReference<BufferedImage> result = new AtomicReference<BufferedImage>();
            server.createPipelineExecutor().execute(pipeline, request, (ImageResponse pipeResponse) -> {
                BufferedImage layerImage = (BufferedImage) pipeResponse.getImage();
                result.set(layerImage);
            });
            return buildMap(mapContent, result.get());
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    } else // multiple layers -> render into one image
    {
        List<Job> jobs = new ArrayList();
        final Map<Layer, Image> images = new HashMap();
        // run jobs for all layers
        for (final Layer mapLayer : mapContent.layers()) {
            UIJob job = new UIJob("PipelineMapOutputFormat: " + mapLayer.getTitle()) {

                @Override
                protected void runWithException(IProgressMonitor monitor) throws Exception {
                    try {
                        final ILayer layer = findLayer(mapLayer);
                        Pipeline pipeline = server.getOrCreatePipeline(layer, ImageProducer.class).get();
                        GetMapRequest request = prepareProcessorRequest(mapContent);
                        server.createPipelineExecutor().execute(pipeline, request, (ImageResponse pipeResponse) -> {
                            BufferedImage layerImage = (BufferedImage) pipeResponse.getImage();
                            images.put(mapLayer, layerImage);
                        });
                    } catch (Exception e) {
                        // XXX put a error image in the map
                        log.warn("", e);
                        images.put(mapLayer, null);
                        throw e;
                    }
                }
            };
            job.schedule();
            jobs.add(job);
        }
        // join jobs
        for (Job job : jobs) {
            try {
                job.join();
            } catch (InterruptedException e) {
                log.warn("", e);
            }
        }
        // put images together (MapContext order)
        Graphics2D g = null;
        try {
            // result image
            BufferedImage result = ImageUtils.createImage(mapContent.getMapWidth(), mapContent.getMapHeight(), null, true);
            g = result.createGraphics();
            // rendering hints
            RenderingHints hints = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            hints.add(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
            hints.add(new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON));
            g.setRenderingHints(hints);
            for (Layer mapLayer : mapContent.layers()) {
                Image layerImage = images.get(mapLayer);
                // load image data
                // new javax.swing.ImageIcon( image ).getImage();
                // ILayer layer = findLayer( mapLayer );
                int rule = AlphaComposite.SRC_OVER;
                // TODO opacity
                // ((float)layer.getOpacity()) / 100;
                float alpha = 1;
                g.setComposite(AlphaComposite.getInstance(rule, alpha));
                g.drawImage(layerImage, 0, 0, null);
            }
            return buildMap(mapContent, result);
        } finally {
            if (g != null) {
                g.dispose();
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) RenderedImage(java.awt.image.RenderedImage) BufferedImage(java.awt.image.BufferedImage) RenderingHints(java.awt.RenderingHints) ImageProducer(org.polymap.core.data.image.ImageProducer) ProcessorRequest(org.polymap.core.data.pipeline.ProcessorRequest) ImageResponse(org.polymap.core.data.image.ImageResponse) UIJob(org.polymap.core.runtime.UIJob) Job(org.eclipse.core.runtime.jobs.Job) UIJob(org.polymap.core.runtime.UIJob) ILayer(org.polymap.core.project.ILayer) AtomicReference(java.util.concurrent.atomic.AtomicReference) ILayer(org.polymap.core.project.ILayer) Layer(org.geotools.map.Layer) FactoryException(org.opengis.referencing.FactoryException) ServiceException(org.geoserver.platform.ServiceException) Pipeline(org.polymap.core.data.pipeline.Pipeline) Graphics2D(java.awt.Graphics2D) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ServiceException(org.geoserver.platform.ServiceException) GetMapRequest(org.polymap.core.data.image.GetMapRequest)

Example 3 with Pipeline

use of org.polymap.core.data.pipeline.Pipeline in project polymap4-core by Polymap4.

the class P4ImageStoreInfo method canHandle.

/**
 * Returns a newly created {@link P4DataStoreInfo}, or null if the layer is not
 * connected to a {@link FeatureStore}.
 *
 * @throws Exception
 */
public static P4ImageStoreInfo canHandle(Catalog catalog, ILayer layer) throws Exception {
    GeoServerServlet server = GeoServerServlet.instance.get();
    Optional<Pipeline> pipeline = server.getOrCreatePipeline(layer, ImageProducer.class);
    if (pipeline.isPresent()) {
        if (!(pipeline.get().getLast().processor() instanceof WmsRenderProcessor)) {
            return null;
        }
        return new P4ImageStoreInfo(catalog, layer, pipeline.get());
    } else {
        return null;
    }
}
Also used : GeoServerServlet(org.polymap.service.geoserver.GeoServerServlet) WmsRenderProcessor(org.polymap.core.data.wms.WmsRenderProcessor) Pipeline(org.polymap.core.data.pipeline.Pipeline)

Example 4 with Pipeline

use of org.polymap.core.data.pipeline.Pipeline in project polymap4-core by Polymap4.

the class P4DataStoreInfo method canHandle.

/**
 * Returns a newly created {@link P4DataStoreInfo}, or null if the layer is not
 * connected to a {@link FeatureStore}.
 *
 * @throws Exception
 */
public static P4DataStoreInfo canHandle(Catalog catalog, ILayer layer) throws Exception {
    GeoServerServlet server = GeoServerServlet.instance.get();
    Optional<Pipeline> pipeline = server.getOrCreatePipeline(layer, FeaturesProducer.class);
    if (pipeline.isPresent()) {
        PipelineFeatureSource fs = new PipelineDataStore(pipeline.get()).getFeatureSource();
        // check with P4FeatureTypeInfo
        Name name = new NameImpl(GeoServerUtils.defaultNsInfo.get().getName(), GeoServerUtils.simpleName(layer.label.get()));
        Params params = new Params();
        FeatureRenameProcessor.NAME.rawput(params, name);
        PipelineProcessorSite procSite = new PipelineProcessorSite(params);
        ProcessorDescriptor proc = new ProcessorDescriptor(FeatureRenameProcessor.class, params);
        proc.processor().init(procSite);
        fs.pipeline().addFirst(proc);
        return new P4DataStoreInfo(catalog, layer, fs);
    } else {
        return null;
    }
}
Also used : NameImpl(org.geotools.feature.NameImpl) PipelineProcessorSite(org.polymap.core.data.pipeline.PipelineProcessorSite) PipelineFeatureSource(org.polymap.core.data.PipelineFeatureSource) GeoServerServlet(org.polymap.service.geoserver.GeoServerServlet) Params(org.polymap.core.data.pipeline.PipelineProcessorSite.Params) ProcessorDescriptor(org.polymap.core.data.pipeline.ProcessorDescriptor) Pipeline(org.polymap.core.data.pipeline.Pipeline) PipelineDataStore(org.polymap.core.data.PipelineDataStore) Name(org.opengis.feature.type.Name)

Aggregations

Pipeline (org.polymap.core.data.pipeline.Pipeline)4 FactoryException (org.opengis.referencing.FactoryException)2 GetMapRequest (org.polymap.core.data.image.GetMapRequest)2 ProcessorRequest (org.polymap.core.data.pipeline.ProcessorRequest)2 GeoServerServlet (org.polymap.service.geoserver.GeoServerServlet)2 Graphics2D (java.awt.Graphics2D)1 Image (java.awt.Image)1 RenderingHints (java.awt.RenderingHints)1 BufferedImage (java.awt.image.BufferedImage)1 RenderedImage (java.awt.image.RenderedImage)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ServletException (javax.servlet.ServletException)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 Job (org.eclipse.core.runtime.jobs.Job)1 ServiceException (org.geoserver.platform.ServiceException)1