Search in sources :

Example 1 with ImageResponse

use of org.polymap.core.data.image.ImageResponse in project polymap4-core by Polymap4.

the class FeatureRenderProcessor2 method getMapRequest.

@Override
public void getMapRequest(GetMapRequest request, ProcessorContext context) throws Exception {
    long start = System.currentTimeMillis();
    // result
    BufferedImage result = new BufferedImage(request.getWidth(), request.getHeight(), TYPE_4BYTE_ABGR);
    result.setAccelerationPriority(1);
    final Graphics2D g = result.createGraphics();
    // log.info( "IMAGE: accelerated=" + result.getCapabilities( g.getDeviceConfiguration() ).isAccelerated() );
    MapContent mapContent = new MapContent();
    try {
        // MapContent
        mapContent.getViewport().setCoordinateReferenceSystem(request.getBoundingBox().getCoordinateReferenceSystem());
        mapContent.addLayer(new FeatureLayer(fs.get(), style.get()));
        StreamingRenderer renderer = new NoThreadStreamingRenderer();
        // error handler
        renderer.addRenderListener(new RenderListener() {

            @Override
            public void featureRenderer(SimpleFeature feature) {
            }

            @Override
            public void errorOccurred(Exception e) {
                if (e.getMessage() == null || e.getMessage().contains("Error transforming bbox") || e.getMessage().contains("too close to a pole")) {
                    log.warn("Renderer: " + e.getMessage());
                } else {
                    log.error("Renderer error: ", e);
                    drawErrorMsg(g, "Unable to render.", e);
                }
            }
        });
        // 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));
        hints.add(new RenderingHints(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON));
        // hints.add( new RenderingHints(
        // RenderingHints.KEY_TEXT_ANTIALIASING,
        // RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB ) );
        // geoserver compatibility to support *env* in SLD functions
        double scale = RendererUtilities.calculateOGCScale(request.getBoundingBox(), request.getWidth(), Collections.EMPTY_MAP);
        EnvFunction.setLocalValue("wms_scale_denominator", scale);
        renderer.setJava2DHints(hints);
        g.setRenderingHints(hints);
        // render params
        Map rendererParams = new HashMap();
        rendererParams.put("optimizedDataLoadingEnabled", Boolean.TRUE);
        renderer.setRendererHints(rendererParams);
        renderer.setMapContent(mapContent);
        Rectangle paintArea = new Rectangle(request.getWidth(), request.getHeight());
        renderer.paint(g, paintArea, request.getBoundingBox());
    } catch (Throwable e) {
        log.error("Renderer error: ", e);
        drawErrorMsg(g, "Unable to render.", e);
    } finally {
        mapContent.dispose();
        EnvFunction.clearLocalValues();
        if (g != null) {
            g.dispose();
        }
    }
    log.debug("   ...done: (" + (System.currentTimeMillis() - start) + "ms).");
    context.sendResponse(new ImageResponse(result));
}
Also used : MapContent(org.geotools.map.MapContent) HashMap(java.util.HashMap) Rectangle(java.awt.Rectangle) BufferedImage(java.awt.image.BufferedImage) SimpleFeature(org.opengis.feature.simple.SimpleFeature) IOException(java.io.IOException) RenderingHints(java.awt.RenderingHints) Graphics2D(java.awt.Graphics2D) NoThreadStreamingRenderer(org.geotools.renderer.lite.NoThreadStreamingRenderer) StreamingRenderer(org.geotools.renderer.lite.StreamingRenderer) FeatureLayer(org.geotools.map.FeatureLayer) NoThreadStreamingRenderer(org.geotools.renderer.lite.NoThreadStreamingRenderer) ImageResponse(org.polymap.core.data.image.ImageResponse) RenderListener(org.geotools.renderer.RenderListener) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ImageResponse

use of org.polymap.core.data.image.ImageResponse in project polymap4-core by Polymap4.

the class ImageGrayscaleProcessor method eop.

@Consumes({ EndOfProcessing.class })
@Produces({ EndOfProcessing.class })
public void eop(ProcessorResponse r, ProcessorContext context) throws Exception {
    ByteArrayOutputStream buf = (ByteArrayOutputStream) context.get("buf");
    if (buf != null) {
        Image image = Toolkit.getDefaultToolkit().createImage(buf.toByteArray());
        Image gray = grayscale(image);
        ImageEncodeProcessor encodeProcessor = new ImageEncodeProcessor();
        encodeProcessor.encodeImageResponse(new ImageResponse(gray), context);
    } else {
        context.sendResponse(r);
    }
}
Also used : ImageEncodeProcessor(org.polymap.core.data.image.ImageEncodeProcessor) ImageResponse(org.polymap.core.data.image.ImageResponse) EncodedImageResponse(org.polymap.core.data.image.EncodedImageResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) Consumes(org.polymap.core.data.pipeline.Consumes) Produces(org.polymap.core.data.pipeline.Produces)

Example 3 with ImageResponse

use of org.polymap.core.data.image.ImageResponse 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 4 with ImageResponse

use of org.polymap.core.data.image.ImageResponse in project polymap4-core by Polymap4.

the class RasterRenderProcessor method getMapRequest.

@Override
public void getMapRequest(GetMapRequest request, ProcessorContext context) throws Exception {
    // result
    BufferedImage result = new BufferedImage(request.getWidth(), request.getHeight(), TYPE_4BYTE_ABGR);
    final Graphics2D g = result.createGraphics();
    MapContent mapContent = new MapContent();
    try {
        mapContent.getViewport().setCoordinateReferenceSystem(request.getBoundingBox().getCoordinateReferenceSystem());
        mapContent.addLayer(new GridReaderLayer(reader, style.get()));
        // renderer
        StreamingRenderer renderer = new StreamingRenderer();
        // error handler
        renderer.addRenderListener(new RenderListener() {

            public void featureRenderer(SimpleFeature feature) {
            }

            public void errorOccurred(Exception e) {
                log.error("Renderer error: ", e);
                drawErrorMsg(g, "Fehler bei der Darstellung.", e);
            }
        });
        // rendering hints
        RenderingHints hints = new RenderingHints(KEY_RENDERING, VALUE_RENDER_QUALITY);
        hints.add(new RenderingHints(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON));
        hints.add(new RenderingHints(KEY_TEXT_ANTIALIASING, VALUE_TEXT_ANTIALIAS_ON));
        renderer.setJava2DHints(hints);
        // render params
        Map rendererParams = new HashMap();
        rendererParams.put("optimizedDataLoadingEnabled", Boolean.TRUE);
        renderer.setRendererHints(rendererParams);
        renderer.setMapContent(mapContent);
        Rectangle paintArea = new Rectangle(request.getWidth(), request.getHeight());
        renderer.paint(g, paintArea, request.getBoundingBox());
    } catch (Throwable e) {
        log.error("Renderer error: ", e);
        drawErrorMsg(g, null, e);
    } finally {
        mapContent.dispose();
        EnvFunction.clearLocalValues();
        if (g != null) {
            g.dispose();
        }
    }
    context.sendResponse(new ImageResponse(result));
}
Also used : MapContent(org.geotools.map.MapContent) HashMap(java.util.HashMap) Rectangle(java.awt.Rectangle) BufferedImage(java.awt.image.BufferedImage) SimpleFeature(org.opengis.feature.simple.SimpleFeature) RenderingHints(java.awt.RenderingHints) Graphics2D(java.awt.Graphics2D) StreamingRenderer(org.geotools.renderer.lite.StreamingRenderer) GridReaderLayer(org.geotools.map.GridReaderLayer) ImageResponse(org.polymap.core.data.image.ImageResponse) RenderListener(org.geotools.renderer.RenderListener) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

BufferedImage (java.awt.image.BufferedImage)4 ImageResponse (org.polymap.core.data.image.ImageResponse)4 Graphics2D (java.awt.Graphics2D)3 RenderingHints (java.awt.RenderingHints)3 HashMap (java.util.HashMap)3 Image (java.awt.Image)2 Rectangle (java.awt.Rectangle)2 Map (java.util.Map)2 MapContent (org.geotools.map.MapContent)2 RenderListener (org.geotools.renderer.RenderListener)2 StreamingRenderer (org.geotools.renderer.lite.StreamingRenderer)2 SimpleFeature (org.opengis.feature.simple.SimpleFeature)2 RenderedImage (java.awt.image.RenderedImage)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 Job (org.eclipse.core.runtime.jobs.Job)1 ServiceException (org.geoserver.platform.ServiceException)1