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