Search in sources :

Example 1 with GTRenderer

use of org.geotools.renderer.GTRenderer 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 2 with GTRenderer

use of org.geotools.renderer.GTRenderer in project dhis2-core by dhis2.

the class MapUtils method render.

// -------------------------------------------------------------------------
// Map
// -------------------------------------------------------------------------
public static BufferedImage render(InternalMap map, Integer maxWidth, Integer maxHeight) {
    MapContent mapContent = new MapContent();
    for (InternalMapLayer mapLayer : map.getLayers()) {
        for (InternalMapObject mapObject : mapLayer.getMapObjects()) {
            mapContent.addLayer(createFeatureLayerFromMapObject(mapObject));
        }
    }
    // Create a renderer for this map
    GTRenderer renderer = new StreamingRenderer();
    renderer.setMapContent(mapContent);
    // Calculate image height
    ReferencedEnvelope mapBounds = mapContent.getMaxBounds();
    double widthToHeightFactor = mapBounds.getSpan(0) / mapBounds.getSpan(1);
    int[] widthHeight = getWidthHeight(maxWidth, maxHeight, LegendSet.LEGEND_TOTAL_WIDTH, TITLE_HEIGHT, widthToHeightFactor);
    //LegendSet.LEGEND_TOTAL_WIDTH;
    Rectangle imageBounds = new Rectangle(0, 0, widthHeight[0], widthHeight[1]);
    // Create an image and get the graphics context from it
    BufferedImage image = new BufferedImage(imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = (Graphics2D) image.getGraphics();
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    renderer.paint(graphics, imageBounds, mapBounds);
    mapContent.dispose();
    return image;
}
Also used : ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) MapContent(org.geotools.map.MapContent) Rectangle(java.awt.Rectangle) GTRenderer(org.geotools.renderer.GTRenderer) BufferedImage(java.awt.image.BufferedImage) StreamingRenderer(org.geotools.renderer.lite.StreamingRenderer) Graphics2D(java.awt.Graphics2D)

Aggregations

BufferedImage (java.awt.image.BufferedImage)2 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)2 MapContent (org.geotools.map.MapContent)2 GTRenderer (org.geotools.renderer.GTRenderer)2 StreamingRenderer (org.geotools.renderer.lite.StreamingRenderer)2 MapLayer (au.org.emii.portal.menu.MapLayer)1 ParseException (com.vividsolutions.jts.io.ParseException)1 Graphics2D (java.awt.Graphics2D)1 Rectangle (java.awt.Rectangle)1 IOException (java.io.IOException)1 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)1 FeatureLayer (org.geotools.map.FeatureLayer)1 Layer (org.geotools.map.Layer)1