Changeset 11103 in osm for applications/viewer/jmapviewer/src/org/openstreetmap/gui
- Timestamp:
- 2008-10-09T20:16:57+02:00 (16 years ago)
- Location:
- applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/Demo.java
r9846 r11103 109 109 map.addMapMarker(new MapMarkerDot(49.71, 8.64)); 110 110 map.addMapMarker(new MapMarkerDot(48.71, -1)); 111 map.addMapMarker(new MapMarkerDot(49.8 07, 8.644));111 map.addMapMarker(new MapMarkerDot(49.8588, 8.643)); 112 112 113 113 // map.setDisplayPositionByLatLon(49.807, 8.6, 11); -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/JMapViewer.java
r9846 r11103 45 45 * Vectors for clock-wise tile painting 46 46 */ 47 protected static final Point[] move = 48 { new Point(1, 0), new Point(0, 1),new Point(-1, 0), new Point(0, -1) };49 50 public static final int MAX_ZOOM = 18;47 protected static final Point[] move = { new Point(1, 0), new Point(0, 1), 48 new Point(-1, 0), new Point(0, -1) }; 49 50 public static final int MAX_ZOOM = 22; 51 51 public static final int MIN_ZOOM = 0; 52 52 … … 107 107 setPreferredSize(new Dimension(400, 400)); 108 108 try { 109 loadingImage = 110 ImageIO.read(JMapViewer.class.getResourceAsStream("images/hourglass.png"));109 loadingImage = ImageIO.read(JMapViewer.class 110 .getResourceAsStream("images/hourglass.png")); 111 111 } catch (Exception e1) { 112 112 loadingImage = null; … … 128 128 int size = 18; 129 129 try { 130 ImageIcon icon = new ImageIcon(getClass().getResource("images/plus.png")); 130 ImageIcon icon = new ImageIcon(getClass().getResource( 131 "images/plus.png")); 131 132 zoomInButton = new JButton(icon); 132 133 } catch (Exception e) { … … 144 145 add(zoomInButton); 145 146 try { 146 ImageIcon icon = new ImageIcon(getClass().getResource("images/minus.png")); 147 ImageIcon icon = new ImageIcon(getClass().getResource( 148 "images/minus.png")); 147 149 zoomOutButton = new JButton(icon); 148 150 } catch (Exception e) { … … 173 175 */ 174 176 public void setDisplayPositionByLatLon(double lat, double lon, int zoom) { 175 setDisplayPositionByLatLon(new Point(getWidth() / 2, getHeight() / 2), lat, lon, zoom); 177 setDisplayPositionByLatLon(new Point(getWidth() / 2, getHeight() / 2), 178 lat, lon, zoom); 176 179 } 177 180 … … 192 195 * {@link TileSource#getMaxZoom()} 193 196 */ 194 public void setDisplayPositionByLatLon(Point mapPoint, double lat, double lon, int zoom) { 197 public void setDisplayPositionByLatLon(Point mapPoint, double lat, 198 double lon, int zoom) { 195 199 int x = OsmMercator.LonToX(lon, zoom); 196 200 int y = OsmMercator.LatToY(lat, zoom); … … 199 203 200 204 public void setDisplayPosition(int x, int y, int zoom) { 201 setDisplayPosition(new Point(getWidth() / 2, getHeight() / 2), x, y, zoom); 205 setDisplayPosition(new Point(getWidth() / 2, getHeight() / 2), x, y, 206 zoom); 202 207 } 203 208 … … 236 241 int x_max = Integer.MIN_VALUE; 237 242 int y_max = Integer.MIN_VALUE; 243 int mapZoomMax = tileSource.getMaxZoom(); 238 244 for (MapMarker marker : mapMarkerList) { 239 int x = OsmMercator.LonToX(marker.getLon(), MAX_ZOOM);240 int y = OsmMercator.LatToY(marker.getLat(), MAX_ZOOM);245 int x = OsmMercator.LonToX(marker.getLon(), mapZoomMax); 246 int y = OsmMercator.LatToY(marker.getLat(), mapZoomMax); 241 247 x_max = Math.max(x_max, x); 242 248 y_max = Math.max(y_max, y); … … 249 255 // System.out.println(y_min + " < y < " + y_max); 250 256 // System.out.println("tiles: " + width + " " + height); 251 int newZoom = MAX_ZOOM;257 int newZoom = mapZoomMax; 252 258 int x = x_max - x_min; 253 259 int y = y_max - y_min; … … 260 266 x = x_min + (x_max - x_min) / 2; 261 267 y = y_min + (y_max - y_min) / 2; 262 int z = 1 << ( MAX_ZOOM- newZoom);268 int z = 1 << (mapZoomMax - newZoom); 263 269 x /= z; 264 270 y /= z; … … 346 352 x++; 347 353 for (int j = 0; j < x; j++) { 348 if (x_min <= posx && posx <= x_max && y_min <= posy && posy <= y_max) { 354 if (x_min <= posx && posx <= x_max && y_min <= posy 355 && posy <= y_max) { 349 356 // tile is visible 350 357 Tile tile = getTile(tilex, tiley, zoom); … … 463 470 } 464 471 if (!tile.isLoaded()) { 465 jobDispatcher.addJob(tileLoader.createTileLoaderJob(tileSource, tilex, tiley, zoom)); 472 jobDispatcher.addJob(tileLoader.createTileLoaderJob(tileSource, 473 tilex, tiley, zoom)); 466 474 } 467 475 return tile; … … 544 552 545 553 public void setTileSource(TileSource tileSource) { 554 if (tileSource.getMaxZoom() > MAX_ZOOM) 555 throw new RuntimeException("Zoom level too high"); 546 556 this.tileSource = tileSource; 547 557 zoomSlider.setMaximum(tileSource.getMaxZoom()); … … 552 562 } 553 563 564 public void tileLoadingFinished(Tile tile) { 565 repaint(); 566 } 567 554 568 } -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/OsmFileCacheTileLoader.java
r9937 r11103 30 30 public class OsmFileCacheTileLoader extends OsmTileLoader { 31 31 32 private static final String TILE_FILE_EXT = ".png";33 32 private static final String ETAG_FILE_EXT = ".etag"; 34 33 … … 59 58 } 60 59 61 public Runnable createTileLoaderJob(final TileSource source, final int tilex, final int tiley,62 final int zoom) {60 public Runnable createTileLoaderJob(final TileSource source, 61 final int tilex, final int tiley, final int zoom) { 63 62 return new FileLoadJob(source, tilex, tiley, zoom); 64 63 } … … 121 120 case LastModified: 122 121 if (!isOsmTileNewer(fileAge)) { 123 System.out 124 .println("LastModified: Local version is up to date: " + tile); 122 // System.out.println( 123 // "LastModified: Local version is up to date: " + 124 // tile); 125 125 tile.setLoaded(true); 126 tileFile.setLastModified(System.currentTimeMillis() - maxCacheFileAge127 + recheckAfter);126 tileFile.setLastModified(System.currentTimeMillis() 127 - maxCacheFileAge + recheckAfter); 128 128 return; 129 129 } … … 138 138 switch (source.getTileUpdate()) { 139 139 case IfNoneMatch: 140 urlConn.addRequestProperty("If-None-Match", fileETag); 140 urlConn.addRequestProperty("If-None-Match", 141 fileETag); 141 142 break; 142 143 case ETag: 143 144 if (hasOsmTileETag(fileETag)) { 144 145 tile.setLoaded(true); 145 tileFile.setLastModified(System.currentTimeMillis() 146 tileFile.setLastModified(System 147 .currentTimeMillis() 146 148 - maxCacheFileAge + recheckAfter); 147 149 return; … … 159 161 System.out.println("Local version is up to date: " + tile); 160 162 tile.setLoaded(true); 161 tileFile.setLastModified(System.currentTimeMillis() - maxCacheFileAge162 + recheckAfter);163 tileFile.setLastModified(System.currentTimeMillis() 164 - maxCacheFileAge + recheckAfter); 163 165 return; 164 166 } … … 168 170 tile.loadImage(new ByteArrayInputStream(buffer)); 169 171 tile.setLoaded(true); 170 listener. repaint();172 listener.tileLoadingFinished(tile); 171 173 saveTileToFile(buffer); 172 174 } else { … … 175 177 } catch (Exception e) { 176 178 if (input == null /* || !input.isStopped() */) 177 System.err.println("failed loading " + zoom + "/" + tilex + "/" + tiley + " "178 + e.getMessage());179 System.err.println("failed loading " + zoom + "/" + tilex 180 + "/" + tiley + " " + e.getMessage()); 179 181 } finally { 180 182 tile.loading = false; … … 196 198 if (!oldTile) { 197 199 tile.setLoaded(true); 198 listener. repaint();200 listener.tileLoadingFinished(tile); 199 201 fileTilePainted = true; 200 202 return true; 201 203 } 202 listener. repaint();204 listener.tileLoadingFinished(tile); 203 205 fileTilePainted = true; 204 206 } catch (Exception e) { … … 216 218 } 217 219 218 protected byte[] loadTileInBuffer(URLConnection urlConn) throws IOException { 220 protected byte[] loadTileInBuffer(URLConnection urlConn) 221 throws IOException { 219 222 input = urlConn.getInputStream(); 220 ByteArrayOutputStream bout = new ByteArrayOutputStream(input.available()); 223 ByteArrayOutputStream bout = new ByteArrayOutputStream(input 224 .available()); 221 225 byte[] buffer = new byte[2048]; 222 226 boolean finished = false; … … 252 256 URL url; 253 257 url = new URL(tile.getUrl()); 254 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); 258 HttpURLConnection urlConn = (HttpURLConnection) url 259 .openConnection(); 255 260 urlConn.setRequestMethod("HEAD"); 256 261 urlConn.setReadTimeout(30000); // 30 seconds read timeout … … 267 272 URL url; 268 273 url = new URL(tile.getUrl()); 269 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); 274 HttpURLConnection urlConn = (HttpURLConnection) url 275 .openConnection(); 270 276 urlConn.setRequestMethod("HEAD"); 271 277 urlConn.setReadTimeout(30000); // 30 seconds read timeout … … 280 286 281 287 protected File getTileFile() throws IOException { 282 return new File(tileCacheDir + "/" + tile.getZoom() + "_" + tile.getXtile() + "_" 283 + tile.getYtile() + TILE_FILE_EXT); 288 return new File(tileCacheDir + "/" + tile.getZoom() + "_" 289 + tile.getXtile() + "_" + tile.getYtile() + "." 290 + source.getTileType()); 284 291 } 285 292 286 293 protected void saveTileToFile(byte[] rawData) { 287 294 try { 288 FileOutputStream f = 289 new FileOutputStream(tileCacheDir + "/" + tile.getZoom() + "_"290 + tile.getXtile() + "_" + tile.getYtile() + TILE_FILE_EXT);295 FileOutputStream f = new FileOutputStream(tileCacheDir + "/" 296 + tile.getZoom() + "_" + tile.getXtile() + "_" 297 + tile.getYtile() + "." + source.getTileType()); 291 298 f.write(rawData); 292 299 f.close(); 293 300 // System.out.println("Saved tile to file: " + tile); 294 301 } catch (Exception e) { 295 System.err.println("Failed to save tile content: " + e.getLocalizedMessage()); 302 System.err.println("Failed to save tile content: " 303 + e.getLocalizedMessage()); 296 304 } 297 305 } … … 299 307 protected void saveETagToFile(String eTag) { 300 308 try { 301 FileOutputStream f = 302 new FileOutputStream(tileCacheDir + "/" + tile.getZoom() + "_"303 + tile.getXtile() + "_"+ tile.getYtile() + ETAG_FILE_EXT);309 FileOutputStream f = new FileOutputStream(tileCacheDir + "/" 310 + tile.getZoom() + "_" + tile.getXtile() + "_" 311 + tile.getYtile() + ETAG_FILE_EXT); 304 312 f.write(eTag.getBytes(ETAG_CHARSET.name())); 305 313 f.close(); 306 314 } catch (Exception e) { 307 System.err.println("Failed to save ETag: " + e.getLocalizedMessage()); 315 System.err.println("Failed to save ETag: " 316 + e.getLocalizedMessage()); 308 317 } 309 318 } … … 311 320 protected String loadETagfromFile() { 312 321 try { 313 FileInputStream f = 314 new FileInputStream(tileCacheDir + "/" + tile.getZoom() + "_"315 + tile.getXtile() + "_"+ tile.getYtile() + ETAG_FILE_EXT);322 FileInputStream f = new FileInputStream(tileCacheDir + "/" 323 + tile.getZoom() + "_" + tile.getXtile() + "_" 324 + tile.getYtile() + ETAG_FILE_EXT); 316 325 byte[] buf = new byte[f.available()]; 317 326 f.read(buf); -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/OsmTileLoader.java
r9846 r11103 46 46 tile.loadImage(input); 47 47 tile.setLoaded(true); 48 listener. repaint();48 listener.tileLoadingFinished(tile); 49 49 input.close(); 50 50 input = null; -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/OsmTileSource.java
r9846 r11103 23 23 return getName(); 24 24 } 25 26 public String getTileType() { 27 return "png"; 28 } 29 25 30 } 26 31 27 32 public static class Mapnik extends AbstractOsmTileSource { 28 33 34 public static String NAME = "Mapnik"; 35 29 36 public String getName() { 30 return "Mapnik";37 return NAME; 31 38 } 32 39 … … 43 50 44 51 public static class CycleMap extends AbstractOsmTileSource { 52 53 public static String NAME = "OSM Cycle Map"; 45 54 46 55 public String getName() { 47 return "OSM Cycle Map";56 return NAME; 48 57 } 49 58 … … 61 70 public static class TilesAtHome extends AbstractOsmTileSource { 62 71 72 public static String NAME = "TilesAtHome"; 73 63 74 public int getMaxZoom() { 64 75 return 17; … … 66 77 67 78 public String getName() { 68 return "TilesAtHome";79 return NAME; 69 80 } 70 81 -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/interfaces/TileLoaderListener.java
r9846 r11103 1 1 package org.openstreetmap.gui.jmapviewer.interfaces; 2 3 import org.openstreetmap.gui.jmapviewer.Tile; 2 4 3 5 //License: GPL. Copyright 2008 by Jan Peter Stotz 4 6 5 7 public interface TileLoaderListener { 6 7 public void repaint(); 8 9 /** 10 * Will be called if a new {@link Tile} has been loaded successfully. 11 * Loaded can mean downloaded or loaded from file cache. 12 * 13 * @param tile 14 */ 15 public void tileLoadingFinished(Tile tile); 8 16 9 17 public TileCache getTileCache(); -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/interfaces/TileSource.java
r9846 r11103 61 61 62 62 /** 63 * Constructs the tile url. 64 * 63 65 * @param zoom 64 66 * @param tilex … … 67 69 */ 68 70 public String getTileUrl(int zoom, int tilex, int tiley); 71 72 /** 73 * Specifies the tile image type. For tiles rendered by Mapnik or 74 * Osmarenderer this is usually <code>"png"</code>. 75 * 76 * @return file extension of the tile image type 77 */ 78 public String getTileType(); 69 79 }
Note:
See TracChangeset
for help on using the changeset viewer.