1 package tim.prune.gui.map;
4 import java.awt.Toolkit;
5 import java.awt.image.ImageObserver;
6 import java.net.MalformedURLException;
11 * Class to handle the caching of map tiles from openstreetmap
13 public class MapTileCacher implements ImageObserver
15 /** Parent to be informed of updates */
16 private MapCanvas _parent = null;
17 /** Default grid size */
18 private static final int GRID_SIZE = 11;
19 /** Array of images to hold tiles */
20 private Image[] _tiles = new Image[GRID_SIZE * GRID_SIZE];
21 /** Current zoom level */
22 private int _zoom = -1;
23 /** X coordinate of central tile */
24 private int _tileX = -1;
25 /** Y coordinate of central tile */
26 private int _tileY = -1;
27 /** X coord of grid centre */
28 private int _gridCentreX = 0;
29 /** Y coord of grid centre */
30 private int _gridCentreY = 0;
35 * @param inParent parent canvas to be informed of updates
37 public MapTileCacher(MapCanvas inParent)
43 * Recentre the map and clear the cache
44 * @param inZoom zoom level
45 * @param inTileX x coord of central tile
46 * @param inTileY y coord of central tile
48 public void centreMap(int inZoom, int inTileX, int inTileY)
55 _gridCentreX = getCacheCoordinate(_gridCentreX + inTileX - _tileX);
56 _gridCentreY = getCacheCoordinate(_gridCentreY + inTileY - _tileY);
59 // Mark boundaries as invalid
60 for (int i=0; i<GRID_SIZE; i++)
62 _tiles[getArrayIndex(_tileX + GRID_SIZE/2 + 1, _tileY + i - GRID_SIZE/2)] = null;
63 _tiles[getArrayIndex(_tileX + i - GRID_SIZE/2, _tileY + GRID_SIZE/2 + 1)] = null;
68 * Clear all the cached images
70 public void clearAll()
72 // Clear all images if zoom changed
73 for (int i=0; i<_tiles.length; i++) {
79 * @param inX x index of tile
80 * @param inY y index of tile
81 * @return selected tile if already loaded, or null otherwise
83 public Image getTile(int inX, int inY)
85 int arrayIndex = getArrayIndex(inX, inY);
86 Image image = _tiles[arrayIndex];
89 // image already finished loading so return it
93 // Trigger load if not already triggered
94 // Work out tile coords for URL
95 int urlX = getUrlCoordinate(inX, _zoom);
96 int urlY = getUrlCoordinate(inY, _zoom);
99 String url = "http://tile.openstreetmap.org/" + _zoom + "/" + urlX + "/" + urlY + ".png";
100 // Load image asynchronously, using observer
101 image = Toolkit.getDefaultToolkit().createImage(new URL(url));
102 _tiles[arrayIndex] = image;
103 if (image.getWidth(this) > 0) {return image;}
105 catch (MalformedURLException urle) {} // ignore
111 * Get the array index for the given coordinates
112 * @param inX x coord of tile
113 * @param inY y coord of tile
114 * @return array index
116 private int getArrayIndex(int inX, int inY)
118 //System.out.println("Getting array index for (" + inX + ", " + inY + ") where the centre is at (" + _tileX + ", " + _tileY
119 // + ") and grid coords (" + _gridCentreX + ", " + _gridCentreY + ")");
120 int x = getCacheCoordinate(inX - _tileX + _gridCentreX);
121 int y = getCacheCoordinate(inY - _tileY + _gridCentreY);
122 //System.out.println("Transformed to (" + x + ", " + y + ")");
123 return (x + y * GRID_SIZE);
127 * Transform a coordinate from map tiles to array coordinates
128 * @param inTile coordinate of tile
129 * @return coordinate in array (wrapping around cache grid)
131 private static int getCacheCoordinate(int inTile)
134 while (tile >= GRID_SIZE) {tile -= GRID_SIZE;}
135 while (tile < 0) {tile += GRID_SIZE;}
140 * Make sure a url coordinate is within range
141 * @param inTile coordinate of tile in map system
142 * @param inZoom zoom factor
143 * @return coordinate for url (either vertical or horizontal)
145 private static int getUrlCoordinate(int inTile, int inZoom)
147 int mapSize = 1 << inZoom;
149 while (coord >= mapSize) {coord -= mapSize;}
150 while (coord < 0) {coord += mapSize;}
151 // coord is now between 0 and mapsize
156 * Convert to string for debug
157 * @see java.lang.Object#toString()
159 public String toString()
161 StringBuffer result = new StringBuffer("Grid centre (" + _gridCentreX + "," + _gridCentreY + ") - (" + _tileX + "," + _tileY + ")\n");
162 for (int i=0; i<GRID_SIZE; i++)
164 for (int j=0; j<GRID_SIZE; j++) {
165 if (i == _gridCentreY && j == _gridCentreX) {
166 result.append(_tiles[j + i*GRID_SIZE] == null?"c":"C");
169 result.append(_tiles[j + i*GRID_SIZE] == null?".":"*");
174 return result.toString();
178 * Method called by image loader to inform of updates to the tiles
179 * @param img the image
180 * @param infoflags flags describing how much of the image is known
183 * @param width ignored
184 * @param height ignored
185 * @return false to carry on loading, true to stop
187 public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
189 boolean loaded = (infoflags & ImageObserver.ALLBITS) > 0;
190 boolean error = (infoflags & ImageObserver.ERROR) > 0;
191 if (loaded || error) {
192 _parent.tilesUpdated(loaded);