// 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) { } }
/** * Gets the full extent of map context's layers. The only reason this method is defined is to * avoid having try-catch blocks all through other methods. */ private void setFullExtent() { if (content != null && content.layers().size() > 0) { try { fullExtent = content.getMaxBounds(); /* * Guard agains degenerate envelopes (e.g. empty * map layer or single point feature) */ if (fullExtent == null) { // set arbitrary bounds centred on 0,0 fullExtent = worldEnvelope(); // new ReferencedEnvelope(-1, 1, -1, 1, // context.getCoordinateReferenceSystem()); } // else { // double w = fullExtent.getWidth(); // double h = fullExtent.getHeight(); // double x = fullExtent.getMinimum(0); // double y = fullExtent.getMinimum(1); // // double xmin = x; // double xmax = x + w; // if (w <= 0.0) { // xmin = x - 1.0; // xmax = x + 1.0; // } // // double ymin = y; // double ymax = y + h; // if (h <= 0.0) { // ymin = y - 1.0; // ymax = y + 1.0; // } // // fullExtent = new ReferencedEnvelope(xmin, xmax, ymin, ymax, // context.getCoordinateReferenceSystem()); // } } catch (Exception ex) { throw new IllegalStateException(ex); } } else { fullExtent = null; } }
public void fit() throws Exception { MapContent generalMapContext = getGeneralMapContext(); Dimension size = getSize(); Envelope projectedBounds = generalMapContext.getMaxBounds(); double destWidth = projectedBounds.getWidth(); double destHeight = projectedBounds.getHeight(); Coordinate centre = projectedBounds.centre(); Point2D.Double screenLT = awtScreen2Cartesian(new Point(0, 0)); Point2D.Double screenBR = awtScreen2Cartesian(new Point(size.width, size.height)); double srcWidth = screenBR.x - screenLT.x; double srcHeight = screenBR.y - screenLT.y; double sx = srcWidth / destWidth; double sy = srcHeight / destHeight; double coef = Math.min(sx, sy); coef = snapScale(coef); scaleView(coef, coef, false); Point2D.Double projectedScreenCenter = screen2Cartesian(new Point(0, 0)); translateView(projectedScreenCenter.x - centre.x, projectedScreenCenter.y - centre.y, true); repaint(); }