View Javadoc
1   package au.gov.amsa.animator;
2   
3   import java.awt.Color;
4   import java.awt.Graphics2D;
5   import java.awt.Rectangle;
6   import java.awt.RenderingHints;
7   import java.awt.geom.AffineTransform;
8   import java.awt.geom.Point2D;
9   import java.awt.geom.Point2D.Float;
10  import java.awt.image.BufferedImage;
11  import java.io.File;
12  import java.io.IOException;
13  
14  import javax.imageio.ImageIO;
15  
16  import org.geotools.geometry.jts.ReferencedEnvelope;
17  import org.geotools.map.MapContent;
18  import org.geotools.referencing.crs.DefaultGeographicCRS;
19  import org.geotools.renderer.lite.RendererUtilities;
20  import org.geotools.renderer.lite.StreamingRenderer;
21  
22  public class ImageDemo {
23  
24      public static void main(String[] args) throws IOException {
25          // Coastlines, country borders and a star on Canberra
26          MapContent map = Map.createMap();
27  
28          // set the view bounds
29          double minLon = 90;
30          double maxLon = 175;
31          double minLat = -50;
32          double maxLat = 0;
33          ReferencedEnvelope bounds = new ReferencedEnvelope(minLon, maxLon, minLat, maxLat, DefaultGeographicCRS.WGS84);
34  
35          // just set the width of the output image, the height will be set so that
36          // referenced envelope fits exactly in the image bounds
37          int width = 800;
38          double ratio = (maxLat - minLat) / (maxLon - minLon);
39          int proportionalHeight = (int) Math.round(width * ratio);
40  
41          // create a buffered image
42          Rectangle imageBounds = new Rectangle(0, 0, width, proportionalHeight);
43          BufferedImage image = createImage(imageBounds);
44          Graphics2D gr = image.createGraphics();
45          gr.setPaint(Color.WHITE);
46          gr.fill(imageBounds);
47          
48          // paint the MapContent on to the image
49          StreamingRenderer renderer = new StreamingRenderer();
50          renderer.setMapContent(map);
51          renderer.paint(gr, imageBounds, bounds);
52          
53          // the transform converts lat, long to image coordinates
54          AffineTransform worldToScreen = RendererUtilities.worldToScreenTransform(bounds,
55                  new Rectangle(0, 0, image.getWidth(), image.getHeight()));
56  
57          // Draw the string Canberra
58          Float p = toScreen(worldToScreen, -35.2809, 149.1300);
59          gr.setColor(Color.blue);
60          gr.drawString("Canberra", p.x + 5, p.y);
61          
62          // output the buffered image to a file
63          ImageIO.write(image, "png", new File("target/image.png"));
64      }
65  
66      private static BufferedImage createImage(Rectangle imageBounds) {
67          BufferedImage img = new BufferedImage(imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_RGB);
68          Graphics2D g = img.createGraphics();
69          g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
70          g.setBackground(Color.white);
71          return img;
72      }
73  
74      private static Point2D.Float toScreen(AffineTransform worldToScreen, double lat, double lon) {
75          Point2D.Float a = new Point2D.Float((float) lon, (float) lat);
76          Point2D.Float b = new Point2D.Float();
77          worldToScreen.transform(a, b);
78          return b;
79      }
80  
81  }