Java examples for 2D Graphics:BufferedImage
Returns a buffered image with the corner lat/lon,keyhole id and zoom level written on it.
//package com.java2s; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.text.DecimalFormat; public class Main { /** Returns a buffered image with the corner lat/lon,keyhole id and zoom * level written on it./*from w ww. j a va 2s . co m*/ * * @param keyholeString the keyhole string to return the image for. * @return a buffered image */ public static BufferedImage getDebugTileFor(String keyholeString) { BufferedImage img = new BufferedImage(256, 256, BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics(); g.setColor(Color.gray); g.fillRect(0, 0, 256, 256); g.setColor(Color.black); int scale = 400 / keyholeString.length(); g.setFont(new Font("Serif", Font.BOLD, scale)); g.drawString(keyholeString + " (z=" + getTileZoom(keyholeString) + ")", 10, 200); Rectangle2D r = getLatLong(keyholeString); DecimalFormat df = new DecimalFormat("#.####"); g.setFont(new Font("SanSerif", 0, 15)); g.drawString( df.format(r.getMinY()) + "," + df.format(r.getMinX()) + " (w:" + df.format(r.getWidth()) + " h:" + df.format(r.getHeight()) + ")", 10, 250); g.drawString(df.format(r.getMaxY()) + "," + df.format(r.getMaxX()), 150, 20); g.drawRect(1, 1, 255, 255); g.dispose(); return img; } /** * Returns a buffered image with the corner lat/lon,x,y and zoom level * written on it. */ public static BufferedImage getDebugTileFor(int x, int y, int zoom) { BufferedImage img = new BufferedImage(256, 256, BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics(); g.setColor(Color.gray); g.fillRect(0, 0, 256, 256); g.setColor(Color.black); int scale = 20; g.setFont(new Font("Serif", Font.BOLD, scale)); g.drawString("x:" + x + " y:" + y + " z:" + zoom, 10, 200); Rectangle2D r = getLatLong(x, y, zoom); DecimalFormat df = new DecimalFormat("#.####"); g.setFont(new Font("SanSerif", 0, 15)); g.drawString( df.format(r.getMinY()) + "," + df.format(r.getMinX()) + " (w:" + df.format(r.getWidth()) + " h:" + df.format(r.getHeight()) + ")", 10, 250); g.drawString(df.format(r.getMaxY()) + "," + df.format(r.getMaxX()), 150, 20); g.drawRect(1, 1, 255, 255); g.dispose(); return img; } /** Returns the Google zoom level for the keyhole string. */ public static int getTileZoom(String keyHoleString) { return 18 - keyHoleString.length(); } /** * To get the Latitude, Longitude, Widht and Height of a Google Tile from * its name (QRTS). * * @param keyholeStr the keyhole string of the the tile. * @return a Rectangle2D with x = lon, y = lat, width=lonSpan, * height=latSpan for a keyhole string. */ public static Rectangle2D.Double getLatLong(String keyholeStr) { // must start with "t" if ((keyholeStr == null) || (keyholeStr.length() == 0) || (keyholeStr.charAt(0) != 't')) { throw new RuntimeException("Keyhole string must start with 't'"); } double lon = -180; // x double lonWidth = 360; // width 360 //double lat = -90; // y //double latHeight = 180; // height 180 double lat = -1; double latHeight = 2; for (int i = 1; i < keyholeStr.length(); i++) { lonWidth /= 2; latHeight /= 2; char c = keyholeStr.charAt(i); switch (c) { case 's': // lat += latHeight; lon += lonWidth; break; case 'r': lat += latHeight; lon += lonWidth; break; case 'q': lat += latHeight; // lon += lonWidth; break; case 't': //lat += latHeight; //lon += lonWidth; break; default: throw new RuntimeException("unknown char '" + c + "' when decoding keyhole string."); } } // convert lat and latHeight to degrees in a transverse mercator projection // note that in fact the coordinates go from about -85 to +85 not -90 to 90! latHeight += lat; latHeight = (2 * Math.atan(Math.exp(Math.PI * latHeight))) - (Math.PI / 2); latHeight *= (180 / Math.PI); lat = (2 * Math.atan(Math.exp(Math.PI * lat))) - (Math.PI / 2); lat *= (180 / Math.PI); latHeight -= lat; if (lonWidth < 0) { lon = lon + lonWidth; lonWidth = -lonWidth; } if (latHeight < 0) { lat = lat + latHeight; latHeight = -latHeight; } // lat = Math.asin(lat) * 180 / Math.PI; return new Rectangle2D.Double(lon, lat, lonWidth, latHeight); } /** * Returns a Rectangle2D with x = lon, y = lat, width=lonSpan, * height=latSpan for an x,y,zoom as used by google. */ public static Rectangle2D.Double getLatLong(int x, int y, int zoom) { double lon = -180; // x double lonWidth = 360; // width 360 //double lat = -90; // y //double latHeight = 180; // height 180 double lat = -1; double latHeight = 2; int tilesAtThisZoom = 1 << (17 - zoom); lonWidth = 360.0 / tilesAtThisZoom; lon = -180 + (x * lonWidth); latHeight = -2.0 / tilesAtThisZoom; lat = 1 + (y * latHeight); // convert lat and latHeight to degrees in a transverse mercator projection // note that in fact the coordinates go from about -85 to +85 not -90 to 90! latHeight += lat; latHeight = (2 * Math.atan(Math.exp(Math.PI * latHeight))) - (Math.PI / 2); latHeight *= (180 / Math.PI); lat = (2 * Math.atan(Math.exp(Math.PI * lat))) - (Math.PI / 2); lat *= (180 / Math.PI); latHeight -= lat; if (lonWidth < 0) { lon = lon + lonWidth; lonWidth = -lonWidth; } if (latHeight < 0) { lat = lat + latHeight; latHeight = -latHeight; } return new Rectangle2D.Double(lon, lat, lonWidth, latHeight); } }