| 1 | // License: GPL. Copyright 2007 by Tim Haussmann
|
|---|
| 2 |
|
|---|
| 3 | import java.awt.geom.AffineTransform;
|
|---|
| 4 | import java.awt.image.BufferedImage;
|
|---|
| 5 | import java.awt.Color;
|
|---|
| 6 | import java.awt.Graphics;
|
|---|
| 7 | import java.awt.Graphics2D;
|
|---|
| 8 | import java.awt.RenderingHints;
|
|---|
| 9 | import java.awt.Shape;
|
|---|
| 10 |
|
|---|
| 11 | /**
|
|---|
| 12 | * @author Tim Haussmann
|
|---|
| 13 | */
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 | public class OsmTile {
|
|---|
| 17 |
|
|---|
| 18 | public static final int WIDTH = 256;
|
|---|
| 19 | public static final int HEIGHT = 256;
|
|---|
| 20 |
|
|---|
| 21 | private int iX = 0;
|
|---|
| 22 | private int iY = 0;
|
|---|
| 23 |
|
|---|
| 24 | public static final int TileBackgroundColor = 0xe9d3b1;
|
|---|
| 25 |
|
|---|
| 26 | private BufferedImage iMapImage;
|
|---|
| 27 |
|
|---|
| 28 | private int iZoomLevel = -1;
|
|---|
| 29 | private int iIndexY = -1;
|
|---|
| 30 | private int iIndexX = -1;
|
|---|
| 31 |
|
|---|
| 32 | //image does not exist
|
|---|
| 33 | private boolean isInvalid = false;
|
|---|
| 34 |
|
|---|
| 35 | //for a faster equals implementation
|
|---|
| 36 | private int iHash;
|
|---|
| 37 |
|
|---|
| 38 | public OsmTile( int aZoomLevel, int aIndexX, int aIndexY){
|
|---|
| 39 |
|
|---|
| 40 | iZoomLevel = aZoomLevel;
|
|---|
| 41 | iIndexX = aIndexX;
|
|---|
| 42 | iIndexY = aIndexY;
|
|---|
| 43 |
|
|---|
| 44 | iX = WIDTH * iIndexX;
|
|---|
| 45 | iY = HEIGHT * iIndexY;
|
|---|
| 46 |
|
|---|
| 47 | iHash = toString().hashCode();
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | public int getZoomlevel(){
|
|---|
| 51 | return iZoomLevel;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | public void paint(Graphics g,TileDB db){
|
|---|
| 55 |
|
|---|
| 56 | if(iMapImage != null && ! isInvalid){
|
|---|
| 57 | g.drawImage( iMapImage, iX, iY, null );
|
|---|
| 58 | }
|
|---|
| 59 | else if(isInvalid){
|
|---|
| 60 | //draw nothing
|
|---|
| 61 | }
|
|---|
| 62 | else{
|
|---|
| 63 | // first try to interpolate tile from parent
|
|---|
| 64 | OsmTile parent = getParent(db);
|
|---|
| 65 | if (parent!=null){
|
|---|
| 66 | Graphics2D g2d = (Graphics2D) g;
|
|---|
| 67 | AffineTransform oldTr = g2d.getTransform();
|
|---|
| 68 | Shape oldClip = g2d.getClip();
|
|---|
| 69 | g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
|
|---|
| 70 |
|
|---|
| 71 | // since the parent will paint 4 tiles in the
|
|---|
| 72 | // current zoom level we have to clip the drawing
|
|---|
| 73 | // region to the current tile
|
|---|
| 74 | g2d.clipRect(iX, iY, WIDTH, HEIGHT);
|
|---|
| 75 | g2d.scale(2, 2);
|
|---|
| 76 | parent.paint(g,db);
|
|---|
| 77 |
|
|---|
| 78 | g2d.setTransform(oldTr);
|
|---|
| 79 | g2d.setClip(oldClip);
|
|---|
| 80 | }
|
|---|
| 81 | else{
|
|---|
| 82 | // otherwise draw placeholder
|
|---|
| 83 | g.setColor(Color.RED);
|
|---|
| 84 | g.drawLine(iX, iY, iX+WIDTH-1, iY+HEIGHT-1);
|
|---|
| 85 | g.drawLine(iX, iY+HEIGHT-1, iX+WIDTH-1, iY);
|
|---|
| 86 | g.drawRect(iX, iY, WIDTH-1, HEIGHT-1);
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | public String toString() {
|
|---|
| 92 | return String.valueOf(iZoomLevel) +"/" +
|
|---|
| 93 | String.valueOf(iIndexX) + "/" +
|
|---|
| 94 | String.valueOf(iIndexY);
|
|---|
| 95 | }
|
|---|
| 96 | public static String key(int aZoomLevel, int aIndexX, int aIndexY){
|
|---|
| 97 | return String.valueOf(aZoomLevel) +"/" +
|
|---|
| 98 | String.valueOf(aIndexX) + "/" +
|
|---|
| 99 | String.valueOf(aIndexY);
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | /**
|
|---|
| 103 | * Callback for the TileDB to set the Image of this tile after loading.
|
|---|
| 104 | * @param aImage
|
|---|
| 105 | */
|
|---|
| 106 | public void setImage(BufferedImage aImage) {
|
|---|
| 107 | iMapImage = aImage;
|
|---|
| 108 | if(iMapImage == null){
|
|---|
| 109 | isInvalid = true;
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /**
|
|---|
| 114 | * @return the path of this map tile on the remote server (i.e. '/1/2/12.png')
|
|---|
| 115 | */
|
|---|
| 116 | public String getRemotePath() {
|
|---|
| 117 | return "/"+toString()+".png";
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | public OsmTile getParent(TileDB db){
|
|---|
| 121 | return iZoomLevel == 0 ? null : db.get(parentKey());
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | public String parentKey() {
|
|---|
| 125 | return key(iZoomLevel - 1,iIndexX/2,iIndexY/2);
|
|---|
| 126 | }
|
|---|
| 127 | }
|
|---|