Search in sources :

Example 1 with MapContent

use of org.geotools.map.MapContent 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 MapContent

use of org.geotools.map.MapContent in project spatial-portal by AtlasOfLivingAustralia.

the class AreaUploadShapefileWizardController method executeShapeImageRenderer.

private void executeShapeImageRenderer(Filter filter) {
    try {
        LOGGER.debug("Generating image");
        SimpleFeatureCollection features1;
        if (filter == null) {
            features1 = source.getFeatures();
        } else {
            features1 = source.getFeatures(filter);
        }
        // Create a map content and add our shapefile to it
        MapContent map = new MapContent();
        org.geotools.styling.Style style = SLD.createSimpleStyle(source.getSchema());
        Layer layer = new FeatureLayer(features1, style);
        map.addLayer(layer);
        GTRenderer renderer = new StreamingRenderer();
        renderer.setMapContent(map);
        int imageWidth = 800;
        int imageHeight = 300;
        Rectangle imageBounds;
        ReferencedEnvelope mapBounds;
        mapBounds = map.getMaxBounds();
        double heightToWidth = mapBounds.getSpan(1) / mapBounds.getSpan(0);
        if (heightToWidth * imageWidth > imageHeight) {
            imageBounds = new Rectangle(0, 0, (int) Math.round(imageHeight / heightToWidth), imageHeight);
        } else {
            imageBounds = new Rectangle(0, 0, imageWidth, (int) Math.round(imageWidth * heightToWidth));
        }
        BufferedImage image = new BufferedImage(imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_RGB);
        Graphics2D gr = image.createGraphics();
        gr.setPaint(Color.WHITE);
        gr.fill(imageBounds);
        renderer.paint(gr, imageBounds, mapBounds);
        img.setContent(image);
    } catch (Exception e) {
        LOGGER.debug("Unable to generate image for selected shapefile", e);
    }
}
Also used : MapContent(org.geotools.map.MapContent) FeatureLayer(org.geotools.map.FeatureLayer) Layer(org.geotools.map.Layer) MapLayer(au.org.emii.portal.menu.MapLayer) BufferedImage(java.awt.image.BufferedImage) ParseException(com.vividsolutions.jts.io.ParseException) IOException(java.io.IOException) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) StreamingRenderer(org.geotools.renderer.lite.StreamingRenderer) FeatureLayer(org.geotools.map.FeatureLayer) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) GTRenderer(org.geotools.renderer.GTRenderer)

Example 3 with MapContent

use of org.geotools.map.MapContent in project sldeditor by robward-scisys.

the class RenderPanelImpl method renderRasterMap.

/**
 * Render raster map.
 *
 * @param imageSize the image size
 * @param style the style
 * @param dpi the dpi
 */
private void renderRasterMap(Rectangle imageSize, Style style, int dpi) {
    DataSourceInterface dataSource = DataSourceFactory.getDataSource();
    AbstractGridCoverage2DReader gridCoverage = dataSource.getGridCoverageReader();
    if (gridCoverage == null) {
        validSymbol = false;
    }
    GridReaderLayer rasterLayer = null;
    MapViewport viewport = null;
    List<Layer> layerList = new ArrayList<Layer>();
    if (style != null) {
        rasterLayer = new GridReaderLayer(gridCoverage, style);
        layerList.add(rasterLayer);
        viewport = new MapViewport(rasterLayer.getBounds());
    }
    boolean hasGeometry = true;
    MapContent map = new MapContent();
    map.addLayers(layerList);
    map.setViewport(viewport);
    try {
        Map<Object, Object> hints = new HashMap<Object, Object>();
        if (OVERRIDE_DPI) {
            hints.put(StreamingRenderer.DPI_KEY, dpi);
        }
        // This ensures all the labelling is cleared
        hints.put(StreamingRenderer.LABEL_CACHE_KEY, new LabelCacheImpl());
        renderer.setRendererHints(hints);
        renderer.setMapContent(map);
        BufferedImage image = new BufferedImage(imageSize.width, imageSize.height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphics = image.createGraphics();
        if (useAntiAlias) {
            graphics.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
        }
        try {
            if (!hasGeometry) {
                graphics.setColor(Color.BLACK);
                int y = imageSize.height / 2;
                Font font = new Font(Font.SERIF, Font.BOLD, 14);
                graphics.setFont(font);
                graphics.drawString(Localisation.getString(RenderPanelImpl.class, "RenderPanelImpl.error1"), 10, y - 14);
            } else {
                if (rasterLayer != null) {
                    ReferencedEnvelope bounds = rasterLayer.getBounds();
                    renderer.paint(graphics, imageSize, bounds);
                }
                this.bImage = image;
            }
        } finally {
            graphics.dispose();
        }
    } finally {
        map.dispose();
    }
}
Also used : MapContent(org.geotools.map.MapContent) HashMap(java.util.HashMap) AbstractGridCoverage2DReader(org.geotools.coverage.grid.io.AbstractGridCoverage2DReader) ArrayList(java.util.ArrayList) LabelCacheImpl(org.geotools.renderer.label.LabelCacheImpl) GridReaderLayer(org.geotools.map.GridReaderLayer) FeatureLayer(org.geotools.map.FeatureLayer) Layer(org.geotools.map.Layer) BufferedImage(java.awt.image.BufferedImage) RenderingHints(java.awt.RenderingHints) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D) DataSourceInterface(com.sldeditor.datasource.DataSourceInterface) GridReaderLayer(org.geotools.map.GridReaderLayer) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) MapViewport(org.geotools.map.MapViewport)

Example 4 with MapContent

use of org.geotools.map.MapContent in project sldeditor by robward-scisys.

the class RenderPanelImpl method internal_renderMap.

/**
 * Internal_render map.
 *
 * @param layers the layers
 * @param bounds the bounds
 * @param imageSize the image size
 * @param hasGeometry the has geometry
 * @param dpi the dpi
 */
private void internal_renderMap(List<Layer> layers, ReferencedEnvelope bounds, Rectangle imageSize, boolean hasGeometry, int dpi) {
    MapContent map = new MapContent();
    map.addLayers(layers);
    try {
        Map<Object, Object> hints = new HashMap<Object, Object>();
        if (OVERRIDE_DPI) {
            hints.put(StreamingRenderer.DPI_KEY, dpi);
        }
        // This ensures all the labelling is cleared
        hints.put(StreamingRenderer.LABEL_CACHE_KEY, new LabelCacheImpl());
        renderer.setRendererHints(hints);
        renderer.setMapContent(map);
        BufferedImage image = new BufferedImage(imageSize.width, imageSize.height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphics = image.createGraphics();
        if (useAntiAlias) {
            graphics.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
        }
        try {
            if (!hasGeometry) {
                graphics.setColor(Color.BLACK);
                int y = imageSize.height / 2;
                Font font = new Font(Font.SERIF, Font.BOLD, 14);
                graphics.setFont(font);
                graphics.drawString(Localisation.getString(RenderPanelImpl.class, "RenderPanelImpl.error1"), 10, y - 14);
            } else {
                renderer.paint(graphics, imageSize, bounds);
                this.bImage = image;
            }
        } finally {
            graphics.dispose();
        }
    } finally {
        map.dispose();
    }
}
Also used : MapContent(org.geotools.map.MapContent) HashMap(java.util.HashMap) LabelCacheImpl(org.geotools.renderer.label.LabelCacheImpl) BufferedImage(java.awt.image.BufferedImage) RenderingHints(java.awt.RenderingHints) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D)

Example 5 with MapContent

use of org.geotools.map.MapContent in project sldeditor by robward-scisys.

the class MapRender method internalRenderStyle.

/**
 * Internal render style.
 */
private void internalRenderStyle() {
    if (!underTest) {
        if (hasError()) {
            mapPane.resetRenderer();
            mapPane.getRenderer().addRenderListener(this);
            resetError();
        }
        wmsEnvVarValues.setImageWidth(mapPane.getWidth());
        wmsEnvVarValues.setImageHeight(mapPane.getHeight());
        MapContent mapContent = mapPane.getMapContent();
        if (mapContent == null) {
            mapContent = new MapContent();
            mapPane.setMapContent(mapContent);
        }
        Map<Object, Object> hints = new HashMap<Object, Object>();
        clearLabelCache();
        hints.put(StreamingRenderer.LABEL_CACHE_KEY, labelCache);
        mapPane.getRenderer().setRendererHints(hints);
        // Add the layers back with the updated style
        StyledLayerDescriptor sld = SelectedSymbol.getInstance().getSld();
        if (sld != null) {
            List<StyledLayer> styledLayerList = sld.layers();
            for (StyledLayer styledLayer : styledLayerList) {
                List<org.geotools.styling.Style> styleList = null;
                if (styledLayer instanceof NamedLayerImpl) {
                    NamedLayerImpl namedLayerImpl = (NamedLayerImpl) styledLayer;
                    styleList = namedLayerImpl.styles();
                } else if (styledLayer instanceof UserLayerImpl) {
                    UserLayerImpl userLayerImpl = (UserLayerImpl) styledLayer;
                    styleList = userLayerImpl.userStyles();
                }
                if (styleList != null) {
                    for (Style style : styleList) {
                        renderSymbol(mapContent, styledLayer, style);
                    }
                }
            }
        }
    }
}
Also used : StyledLayerDescriptor(org.geotools.styling.StyledLayerDescriptor) UserLayerImpl(org.geotools.styling.UserLayerImpl) MapContent(org.geotools.map.MapContent) HashMap(java.util.HashMap) StyledLayer(org.geotools.styling.StyledLayer) NamedLayerImpl(org.geotools.styling.NamedLayerImpl) Style(org.opengis.style.Style)

Aggregations

MapContent (org.geotools.map.MapContent)7 BufferedImage (java.awt.image.BufferedImage)6 Graphics2D (java.awt.Graphics2D)5 HashMap (java.util.HashMap)5 RenderingHints (java.awt.RenderingHints)4 StreamingRenderer (org.geotools.renderer.lite.StreamingRenderer)4 Rectangle (java.awt.Rectangle)3 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)3 FeatureLayer (org.geotools.map.FeatureLayer)3 Font (java.awt.Font)2 IOException (java.io.IOException)2 Map (java.util.Map)2 GridReaderLayer (org.geotools.map.GridReaderLayer)2 Layer (org.geotools.map.Layer)2 GTRenderer (org.geotools.renderer.GTRenderer)2 RenderListener (org.geotools.renderer.RenderListener)2 LabelCacheImpl (org.geotools.renderer.label.LabelCacheImpl)2 SimpleFeature (org.opengis.feature.simple.SimpleFeature)2 ImageResponse (org.polymap.core.data.image.ImageResponse)2 MapLayer (au.org.emii.portal.menu.MapLayer)1