Search in sources :

Example 1 with Layer

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

Aggregations

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