@SuppressWarnings("unchecked") public void createMapFor( final CoordinateReferenceSystem crs, final com.vividsolutions.jts.geom.Envelope areaOfInterest, final Graphics2D graphics) throws IOException { Geometry geographicBoundingBox = getGeographicBoundingBox(crs); MapContext mapContext = getMapContext(crs, geographicBoundingBox, areaOfInterest); graphics.setColor(new Color(153, 179, 204)); graphics.fillRect(0, 0, mapWidth, mapHeight); Rectangle paintArea = new Rectangle(mapWidth, mapHeight); mapContext.setAreaOfInterest(areaOfInterest, crs); GTRenderer renderer = new StreamingRenderer(); renderer.setContext(mapContext); RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); renderer.setJava2DHints(hints); Map renderingHints = new HashMap(); renderingHints.put("optimizedDataLoadingEnabled", Boolean.TRUE); renderingHints.put(StreamingRenderer.ADVANCED_PROJECTION_HANDLING_KEY, Boolean.TRUE); renderingHints.put(StreamingRenderer.CONTINUOUS_MAP_WRAPPING, Boolean.TRUE); renderer.setRendererHints(renderingHints); renderer.paint(graphics, paintArea, areaOfInterest); }
// public void savePDF(){ // Rectangle suggestedPageSize = getITextPageSize(page1.getPageSize()); // Rectangle pageSize = rotatePageIfNecessary(suggestedPageSize); // //rotate if we need landscape // Document document = new Document(pageSize); // // PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile)); // document.open(); // Graphics2D graphics = cb.createGraphics(pageSize.getWidth(), pageSize.getHeight()); // // // call your GTRenderer here // GTRenderer draw = new StreamingRenderer(); // draw.setMapContent(mapContent); // // draw.paint(graphics, outputArea, mapContent.getLayerBounds() ); // // // cleanup // graphics.dispose(); // // //cleanup // document.close(); // writer.close(); // } public void saveImage(final MapContent map, final String file, final int imageWidth) { GTRenderer renderer = new StreamingRenderer(); renderer.setMapContent(map); Rectangle imageBounds = null; ReferencedEnvelope mapBounds = null; try { mapBounds = map.getMaxBounds(); double heightToWidth = mapBounds.getSpan(1) / mapBounds.getSpan(0); imageBounds = new Rectangle(0, 0, imageWidth, (int) Math.round(imageWidth * heightToWidth)); } catch (Exception e) { // failed to access mapContent layers throw new RuntimeException(e); } BufferedImage image = new BufferedImage(imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_RGB); Graphics2D gr = image.createGraphics(); gr.setPaint(Color.WHITE); gr.fill(imageBounds); try { renderer.paint(gr, imageBounds, mapBounds); File fileToSave = new File(file); ImageIO.write(image, "jpeg", fileToSave); } catch (Exception e) { e.printStackTrace(); } }
public void saveNewImage(MapContent map, String file) { GTRenderer renderer = new StreamingRenderer(); renderer.setMapContent(map); System.out.println("line 139"); Rectangle imageBounds = null; try { ReferencedEnvelope mapBounds = map.getMaxBounds(); double heightToWidth = mapBounds.getSpan(1) / mapBounds.getSpan(0); int imageWidth = 600; imageBounds = new Rectangle(0, 0, imageWidth, (int) Math.round(imageWidth * heightToWidth)); } catch (Exception e) { } System.out.println("line 151"); // Rectangle imageSize = new Rectangle(600,600); BufferedImage image = new BufferedImage( imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_RGB); // darker red fill Graphics2D gr = image.createGraphics(); gr.setPaint(Color.WHITE); gr.fill(imageBounds); try { System.out.println("line 161"); renderer.paint(gr, imageBounds, map.getMaxBounds()); System.out.println("line 163"); File fileToSave = new File(file); System.out.println("line 166"); ImageIO.write(image, "jpeg", fileToSave); System.out.println("line 168"); } catch (IOException e) { } }
/** * Draws map content data into image * * @param content * @return image */ private BufferedImage saveImage(MapContent content) { BufferedImage image; if (isTile && bufferSize != 0.0d) { image = new BufferedImage(bufferedImageWidth, bufferedImageHeight, BufferedImage.TYPE_4BYTE_ABGR); } else { image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_4BYTE_ABGR); } GTRenderer renderer = new StreamingRenderer(); renderer.setMapContent(content); Graphics2D g = (Graphics2D) image.getGraphics(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); if (isTile && bufferSize != 0.0d) { renderer.paint( g, new Rectangle(bufferedImageWidth, bufferedImageHeight), content.getViewport().getBounds()); try { image = image.getSubimage( (int) (imageWidth * bufferSize) / 2, (int) (imageWidth * bufferSize) / 2, imageWidth, imageHeight); } catch (Exception e) { log.error(e, "Image cropping failed"); } } else { renderer.paint(g, new Rectangle(imageWidth, imageHeight), content.getViewport().getBounds()); } content.dispose(); return image; }
@Override protected void paintScreenContext(Graphics2D g) { if (screenContext != null) { synchronized (screenContext) { try { // let's paint selection and phanton layers screenRenderer.setMapContent(screenContext); Rectangle clip = g.getClipBounds(); Point2D.Double lu = screen2Cartesian(clip.getLocation()); Point2D.Double br = screen2Cartesian(new Point(clip.x + clip.width, clip.y + clip.height)); Geometry aoiGeometry = constructRectyPolygonGeometry(lu, br); Envelope aoiEnvelope = new ReferencedEnvelope( aoiGeometry.getEnvelopeInternal(), screenContext.getCoordinateReferenceSystem()); screenRenderer.paint(g, clip, aoiEnvelope, cartesian2Screen); } catch (NoninvertibleTransformException ex) { Logger.getLogger(JGeoPane.class.getName()).log(Level.SEVERE, null, ex); } } } super.paintScreenContext(g); }
/** * Set the renderer for this map pane. * * @param renderer the renderer to use */ public void setRenderer(GTRenderer renderer) { if (renderer != null) { Map<Object, Object> hints; if (renderer instanceof StreamingRenderer) { hints = renderer.getRendererHints(); if (hints == null) { hints = new HashMap<Object, Object>(); } if (hints.containsKey(StreamingRenderer.LABEL_CACHE_KEY)) { labelCache = (LabelCache) hints.get(StreamingRenderer.LABEL_CACHE_KEY); } else { labelCache = new LabelCacheImpl(); hints.put(StreamingRenderer.LABEL_CACHE_KEY, labelCache); } renderer.setRendererHints(hints); if (this.content != null) { renderer.setMapContent(this.content); } } } this.renderer = renderer; }
/** * Set the map context for this map pane to display * * @param content the map context */ public void setMapContent(MapContent content) { if (this.content != content) { if (this.content != null) { this.content.removeMapLayerListListener(this); // for( MapLayer layer : this.context.getLayers() ) { // if (layer instanceof ComponentListener) { // removeComponentListener((ComponentListener) layer); // } // } } this.content = content; if (content != null) { this.content.addMapLayerListListener(this); this.content.addMapBoundsListener(this); // set all layers as selected by default for the info tool for (Layer layer : content.layers()) { layer.setSelected(true); // if (layer instanceof ComponentListener) { // addComponentListener((ComponentListener) layer); // } } setFullExtent(); } if (renderer != null) { renderer.setMapContent(this.content); } MapPaneEvent ev = new MapPaneEvent(this, MapPaneEvent.Type.NEW_CONTEXT); publishEvent(ev); } }
/** * Upon a render exception check if its cause is one that we actually want to ignore, and if not * abort the rendering process so the map producer can fail. */ public void errorOccurred(final Exception renderException) { Throwable cause = renderException; while (cause != null) { if (cause instanceof TransformException || cause instanceof IllegalAttributeException || cause instanceof FactoryException || cause instanceof NoninvertibleTransformException) { if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "Ignoring renderer error", renderException); } return; } cause = cause.getCause(); } // not an ignorable cause... stop rendering LOGGER.log(Level.FINE, "Got an unexpected render exception.", renderException); this.renderException = renderException; renderer.stopRendering(); }
@SuppressWarnings("deprecation") public void handleEvent(Event event) { curPaintArea = getVisibleRect(); // System.out.println("event: " + event.type); if (event.type == SWT.MouseDown) { startX = event.x; startY = event.y; // start mouse activity mouseDown = true; } else if (event.type == SWT.MouseUp) { endX = event.x; endY = event.y; boolean mouseWasMoved = startX != endX || startY != endY; if (toolCanMove && mouseWasMoved) { // if the tool is able to move draw the moved image afterImageMove(); } // stop mouse activity mouseDown = false; isDragging = false; } else if (event.type == SWT.Paint) { // System.out.println("PAINT CALLED (DOESN'T MEAN I'M DRAWING)"); if (acceptRepaintRequests) { gc = event.gc; // System.out.println(toolCanDraw + "/" + toolCanMove + "/" + isDragging + "/" + // redrawBaseImage); /* * if the mouse is dragging and the current tool can * move the map we just draw what we already have * on white background. At the end of the moving * we will take care of adding the missing pieces. */ if (toolCanMove && isDragging) { // System.out.println("toolCanMove && isDragging"); if (gc != null && !gc.isDisposed() && swtImage != null) { /* * double buffer necessary, since the SWT.NO_BACKGROUND * needed by the canvas to properly draw background, doesn't * clean the parts outside the bounds of the moving panned image, * giving a spilling image effect. */ Image tmpImage = new Image(getDisplay(), curPaintArea.width, curPaintArea.height); GC tmpGc = new GC(tmpImage); tmpGc.setBackground(white); tmpGc.fillRectangle(0, 0, curPaintArea.width, curPaintArea.height); tmpGc.drawImage(swtImage, imageOrigin.x, imageOrigin.y); gc.drawImage(tmpImage, 0, 0); tmpImage.dispose(); } return; } /* * if the mouse is dragging and the current tool can * draw a boundingbox while dragging, we draw the box * keeping the current drawn image */ if (toolCanDraw && toolManager.getCursorTool().isDrawing() && isDragging) { // System.out.println("draw box: " + startX + "/" + startY + "/" + endX + // "/" + endY); if (swtImage != null) { drawFinalImage(swtImage); } gc.setXORMode(true); org.eclipse.swt.graphics.Color fC = gc.getForeground(); gc.setLineStyle(cursorToolLineStyle); gc.setLineWidth(cursorToolLineWidth); gc.setForeground(cursorToolColor); gc.drawRectangle(startX, startY, endX - startX, endY - startY); gc.setForeground(fC); gc.setXORMode(false); return; } if (!toolCanDraw && !toolCanMove && isDragging) { return; } if (curPaintArea == null || content == null || renderer == null) { return; } if (content.layers().size() == 0) { // if no layers available, return only if there are also no overlays gc.setForeground(yellow); gc.fillRectangle(0, 0, curPaintArea.width + 1, curPaintArea.height + 1); if (overlayImage == null) return; } final ReferencedEnvelope mapAOI = content.getViewport().getBounds(); if (mapAOI == null) { return; } if (redrawBaseImage) { MapPaneEvent ev = new MapPaneEvent(this, MapPaneEvent.Type.RENDERING_STARTED); publishEvent(ev); baseImage = new BufferedImage( curPaintArea.width + 1, curPaintArea.height + 1, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = baseImage.createGraphics(); g2d.fillRect(0, 0, curPaintArea.width + 1, curPaintArea.height + 1); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // renderer.setContext(context); java.awt.Rectangle awtRectangle = Utils.toAwtRectangle(curPaintArea); renderer.paint(g2d, awtRectangle, mapAOI, getWorldToScreenTransform()); // swtImage.dispose(); if (swtImage != null && !swtImage.isDisposed()) { swtImage.dispose(); swtImage = null; } // System.out.println("READRAWBASEIMAGE"); swtImage = new Image( getDisplay(), awtToSwt(baseImage, curPaintArea.width + 1, curPaintArea.height + 1)); } if (swtImage != null) { drawFinalImage(swtImage); } MapPaneEvent ev = new MapPaneEvent(this, MapPaneEvent.Type.RENDERING_STOPPED); publishEvent(ev); clearLabelCache = true; onRenderingCompleted(); redrawBaseImage = false; } } }