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);
}
}
Aggregations