Changeset 9780 in osm
- Timestamp:
- 2008-08-13T13:46:32+02:00 (16 years ago)
- Location:
- applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer
- Files:
-
- 2 added
- 9 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/Demo.java
r9713 r9780 6 6 import java.awt.event.ActionEvent; 7 7 import java.awt.event.ActionListener; 8 import java.awt.event.ItemEvent; 9 import java.awt.event.ItemListener; 8 10 9 11 import javax.swing.JButton; 10 12 import javax.swing.JCheckBox; 13 import javax.swing.JComboBox; 11 14 import javax.swing.JFrame; 12 15 import javax.swing.JLabel; 13 16 import javax.swing.JPanel; 17 18 import org.openstreetmap.gui.jmapviewer.interfaces.TileSource; 14 19 15 20 /** … … 29 34 final JMapViewer map = new JMapViewer(); 30 35 // final JMapViewer map = new JMapViewer(new MemoryTileCache(),4); 31 // map.setTileLoader(new OsmFileCacheTileLoader(map, 32 // OsmTileLoader.MAP_MAPNIK)); 36 map.setTileLoader(new OsmFileCacheTileLoader(map)); 33 37 // new DefaultMapController(map); 34 38 setLayout(new BorderLayout()); … … 36 40 setExtendedState(JFrame.MAXIMIZED_BOTH); 37 41 JPanel panel = new JPanel(); 42 JPanel helpPanel = new JPanel(); 38 43 add(panel, BorderLayout.NORTH); 39 JLabel label = 44 add(helpPanel, BorderLayout.SOUTH); 45 JLabel helpLabel = 40 46 new JLabel("Use right mouse button to move,\n " 41 47 + "left double click or mouse wheel to zoom."); 42 panel.add(label);48 helpPanel.add(helpLabel); 43 49 JButton button = new JButton("setDisplayToFitMapMarkers"); 44 50 button.addActionListener(new ActionListener() { … … 48 54 } 49 55 }); 50 panel.add(button); 56 JComboBox tileSourceSelector = 57 new JComboBox(new Object[] { new OsmTileSource.Mapnik(), 58 new OsmTileSource.TilesAtHome(), new OsmTileSource.CycleMap() }); 59 tileSourceSelector.addItemListener(new ItemListener() { 60 public void itemStateChanged(ItemEvent e) { 61 map.setTileSource((TileSource) e.getItem()); 62 } 63 }); 64 panel.add(tileSourceSelector); 51 65 final JCheckBox showMapMarker = new JCheckBox("Map markers visible"); 52 66 showMapMarker.setSelected(map.getMapMarkersVisible()); … … 76 90 }); 77 91 panel.add(showZoomControls); 92 panel.add(button); 78 93 add(map, BorderLayout.CENTER); 79 94 -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/JMapController.java
r9530 r9780 21 21 public abstract class JMapController { 22 22 23 JMapViewer map;23 protected JMapViewer map; 24 24 25 25 public JMapController(JMapViewer map) { -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/JMapViewer.java
r9713 r9780 26 26 import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker; 27 27 import org.openstreetmap.gui.jmapviewer.interfaces.TileCache; 28 import org.openstreetmap.gui.jmapviewer.interfaces.TileSource; 28 29 import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader; 29 30 … … 51 52 protected TileLoader tileLoader; 52 53 protected TileCache tileCache; 54 protected TileSource tileSource; 55 53 56 protected List<MapMarker> mapMarkerList; 54 57 protected boolean mapMarkersVisible; … … 91 94 public JMapViewer(TileCache tileCache, int downloadThreadCount) { 92 95 super(); 96 tileSource = new OsmTileSource.Mapnik(); 93 97 tileLoader = new OsmTileLoader(this); 94 98 this.tileCache = tileCache; … … 111 115 112 116 protected void initializeZoomSlider() { 113 zoomSlider = new JSlider(MIN_ZOOM, MAX_ZOOM);117 zoomSlider = new JSlider(MIN_ZOOM, tileSource.getMaxZoom()); 114 118 zoomSlider.setOrientation(JSlider.VERTICAL); 115 119 zoomSlider.setBounds(10, 10, 30, 150); … … 184 188 * longitude of the specified coordinate 185 189 * @param zoom 186 * {@link #MIN_ZOOM} <= zoom level <= {@link #MAX_ZOOM} 190 * {@link #MIN_ZOOM} <= zoom level <= 191 * {@link TileSource#getMaxZoom()} 187 192 */ 188 193 public void setDisplayPositionByLatLon(Point mapPoint, double lat, double lon, int zoom) { … … 197 202 198 203 public void setDisplayPosition(Point mapPoint, int x, int y, int zoom) { 199 if (zoom > MAX_ZOOM|| zoom < MIN_ZOOM)204 if (zoom > tileSource.getMaxZoom() || zoom < MIN_ZOOM) 200 205 return; 201 206 … … 329 334 int y_max = getHeight(); 330 335 336 // paint the tiles in a spiral, starting from center of the map 331 337 boolean painted = true; 332 338 int x = 0; 333 339 while (painted) { 334 340 painted = false; 335 for (int y = 0; y < 4; y++) {336 if ( y% 2 == 0)341 for (int i = 0; i < 4; i++) { 342 if (i % 2 == 0) 337 343 x++; 338 for (int z = 0; z < x; z++) { 339 if (x_min <= posx && posx <= x_max && y_min <= posy && posy <= y_max) { // tile 340 // is 341 // visible 344 for (int j = 0; j < x; j++) { 345 if (x_min <= posx && posx <= x_max && y_min <= posy && posy <= y_max) { 346 // tile is visible 342 347 Tile tile = getTile(tilex, tiley, zoom); 343 348 if (tile != null) { … … 418 423 419 424 public void setZoom(int zoom, Point mapPoint) { 420 if (zoom > MAX_ZOOM|| zoom == this.zoom)425 if (zoom > tileSource.getMaxZoom() || zoom == this.zoom) 421 426 return; 422 427 Point2D.Double zoomPos = getPosition(mapPoint); … … 444 449 if (tilex < 0 || tilex >= max || tiley < 0 || tiley >= max) 445 450 return null; 446 Tile tile = tileCache.getTile(tile x, tiley, zoom);451 Tile tile = tileCache.getTile(tileSource, tilex, tiley, zoom); 447 452 if (tile == null) { 448 tile = new Tile(tile x, tiley, zoom, loadingImage);453 tile = new Tile(tileSource, tilex, tiley, zoom, loadingImage); 449 454 tileCache.addTile(tile); 450 455 tile.loadPlaceholderFromCache(tileCache); 451 456 } 452 457 if (!tile.isLoaded()) { 453 jobDispatcher.addJob(tileLoader.createTileLoaderJob(tile x, tiley, zoom));458 jobDispatcher.addJob(tileLoader.createTileLoaderJob(tileSource, tilex, tiley, zoom)); 454 459 } 455 460 return tile; … … 527 532 } 528 533 534 public TileSource getTileLayerSource() { 535 return tileSource; 536 } 537 538 public void setTileSource(TileSource tileSource) { 539 this.tileSource = tileSource; 540 zoomSlider.setMaximum(tileSource.getMaxZoom()); 541 jobDispatcher.cancelOutstandingJobs(); 542 if (zoom > tileSource.getMaxZoom()) 543 setZoom(tileSource.getMaxZoom()); 544 repaint(); 545 } 546 529 547 } -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/MemoryTileCache.java
r9494 r9780 7 7 8 8 import org.openstreetmap.gui.jmapviewer.interfaces.TileCache; 9 import org.openstreetmap.gui.jmapviewer.interfaces.TileSource; 9 10 10 11 /** … … 44 45 } 45 46 46 public Tile getTile( int x, int y, int z) {47 CacheEntry entry = hashtable.get(Tile.getTileKey( x, y, z));47 public Tile getTile(TileSource source, int x, int y, int z) { 48 CacheEntry entry = hashtable.get(Tile.getTileKey(source, x, y, z)); 48 49 if (entry == null) 49 50 return null; -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/OsmFileCacheTileLoader.java
r9619 r9780 16 16 import org.openstreetmap.gui.jmapviewer.interfaces.Job; 17 17 import org.openstreetmap.gui.jmapviewer.interfaces.TileCache; 18 import org.openstreetmap.gui.jmapviewer.interfaces.TileSource; 18 19 import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader; 19 20 … … 32 33 public static final long FILE_AGE_ONE_WEEK = FILE_AGE_ONE_DAY * 7; 33 34 34 protected String tileCacheDir;35 protected String cacheDirBase; 35 36 36 37 protected long maxFileAge = FILE_AGE_ONE_WEEK; 37 38 38 public OsmFileCacheTileLoader(JMapViewer map , String baseUrl) {39 super(map , baseUrl);39 public OsmFileCacheTileLoader(JMapViewer map) { 40 super(map); 40 41 String tempDir = System.getProperty("java.io.tmpdir"); 41 42 try { … … 43 44 throw new IOException(); 44 45 File cacheDir = new File(tempDir, "JMapViewerTiles"); 45 cacheDir = new File(cacheDir, Integer.toString(baseUrl.hashCode()));46 46 // System.out.println(cacheDir); 47 47 if (!cacheDir.exists() && !cacheDir.mkdirs()) 48 48 throw new IOException(); 49 tileCacheDir= cacheDir.getAbsolutePath();49 cacheDirBase = cacheDir.getAbsolutePath(); 50 50 } catch (Exception e) { 51 tileCacheDir = "tiles"; 52 } 53 } 54 55 public OsmFileCacheTileLoader(JMapViewer map) { 56 this(map, MAP_MAPNIK); 57 } 58 59 public Job createTileLoaderJob(final int tilex, final int tiley, final int zoom) { 60 return new FileLoadJob(tilex, tiley, zoom); 51 cacheDirBase = "tiles"; 52 } 53 } 54 55 public Job createTileLoaderJob(final TileSource source, final int tilex, final int tiley, 56 final int zoom) { 57 return new FileLoadJob(source, tilex, tiley, zoom); 61 58 } 62 59 … … 65 62 66 63 int tilex, tiley, zoom; 67 68 public FileLoadJob(int tilex, int tiley, int zoom) { 64 TileSource source; 65 File tileCacheDir; 66 67 public FileLoadJob(TileSource source, int tilex, int tiley, int zoom) { 69 68 super(); 69 this.source = source; 70 70 this.tilex = tilex; 71 71 this.tiley = tiley; … … 77 77 Tile tile; 78 78 synchronized (cache) { 79 tile = cache.getTile( tilex, tiley, zoom);79 tile = cache.getTile(source, tilex, tiley, zoom); 80 80 if (tile == null || tile.isLoaded() || tile.loading) 81 81 return; 82 82 tile.loading = true; 83 83 } 84 tileCacheDir = new File(cacheDirBase, source.getName()); 85 if (!tileCacheDir.exists()) 86 tileCacheDir.mkdirs(); 84 87 try { 85 88 long fileAge = 0; … … 135 138 } catch (Exception e) { 136 139 if (input == null /* || !input.isStopped() */) 137 System.err.println("failed loading " + zoom + "/" + tilex 138 + "/" + tiley + " " +e.getMessage());140 System.err.println("failed loading " + zoom + "/" + tilex + "/" + tiley + " " 141 + e.getMessage()); 139 142 } finally { 140 143 tile.loading = false; … … 142 145 } 143 146 144 protected byte[] loadTileInBuffer(URLConnection urlConn) 145 throws IOException { 147 protected byte[] loadTileInBuffer(URLConnection urlConn) throws IOException { 146 148 input = urlConn.getInputStream(); 147 ByteArrayOutputStream bout = new ByteArrayOutputStream(input 148 .available()); 149 ByteArrayOutputStream bout = new ByteArrayOutputStream(input.available()); 149 150 byte[] buffer = new byte[2048]; 150 151 boolean finished = false; … … 176 177 * @throws IOException 177 178 */ 178 protected boolean isOsmTileNewer(Tile tile, long fileAge) 179 throws IOException { 179 protected boolean isOsmTileNewer(Tile tile, long fileAge) throws IOException { 180 180 URL url; 181 url = new URL(baseUrl + "/" + tile.getKey() + ".png"); 182 HttpURLConnection urlConn = (HttpURLConnection) url 183 .openConnection(); 181 url = new URL(tile.getUrl()); 182 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); 184 183 urlConn.setRequestMethod("HEAD"); 185 184 urlConn.setReadTimeout(30000); // 30 seconds read … … 194 193 195 194 protected File getTileFile(Tile tile) throws IOException { 196 return new File(tileCacheDir + "/" + tile.getZoom() + "_" 197 + tile.get Xtile() + "_" + tile.getYtile() + FILE_EXT);195 return new File(tileCacheDir + "/" + tile.getZoom() + "_" + tile.getXtile() + "_" 196 + tile.getYtile() + FILE_EXT); 198 197 } 199 198 200 199 protected void saveTileToFile(Tile tile, byte[] rawData) { 201 200 try { 202 FileOutputStream f = new FileOutputStream(tileCacheDir + "/"203 + tile.getZoom() + "_" + tile.getXtile() + "_"204 + tile.getYtile() + FILE_EXT);201 FileOutputStream f = 202 new FileOutputStream(tileCacheDir + "/" + tile.getZoom() + "_" 203 + tile.getXtile() + "_" + tile.getYtile() + FILE_EXT); 205 204 f.write(rawData); 206 205 f.close(); 207 206 // System.out.println("Saved tile to file: " + tile); 208 207 } catch (Exception e) { 209 System.err.println("Failed to save tile content: " 210 + e.getLocalizedMessage()); 208 System.err.println("Failed to save tile content: " + e.getLocalizedMessage()); 211 209 } 212 210 } … … 232 230 } 233 231 234 public String get TileCacheDir() {235 return tileCacheDir;232 public String getCacheDirBase() { 233 return cacheDirBase; 236 234 } 237 235 … … 239 237 File dir = new File(tileCacheDir); 240 238 dir.mkdirs(); 241 this. tileCacheDir= dir.getAbsolutePath();239 this.cacheDirBase = dir.getAbsolutePath(); 242 240 } 243 241 -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/OsmTileLoader.java
r9619 r9780 10 10 import org.openstreetmap.gui.jmapviewer.interfaces.Job; 11 11 import org.openstreetmap.gui.jmapviewer.interfaces.TileCache; 12 import org.openstreetmap.gui.jmapviewer.interfaces.TileSource; 12 13 import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader; 13 14 … … 19 20 public class OsmTileLoader implements TileLoader { 20 21 21 public static final String MAP_MAPNIK = "http://tile.openstreetmap.org";22 public static final String MAP_OSMA = "http://tah.openstreetmap.org/Tiles/tile";23 24 protected String baseUrl;25 26 22 protected JMapViewer map; 27 23 28 24 public OsmTileLoader(JMapViewer map) { 29 this (map, MAP_MAPNIK);25 this.map = map; 30 26 } 31 27 32 public OsmTileLoader(JMapViewer map, String baseUrl) { 33 this.map = map; 34 this.baseUrl = baseUrl; 35 } 36 37 public Job createTileLoaderJob(final int tilex, final int tiley, 28 public Job createTileLoaderJob(final TileSource source, final int tilex, final int tiley, 38 29 final int zoom) { 39 30 return new Job() { … … 45 36 Tile tile; 46 37 synchronized (cache) { 47 tile = cache.getTile( tilex, tiley, zoom);38 tile = cache.getTile(source, tilex, tiley, zoom); 48 39 if (tile == null || tile.isLoaded() || tile.loading) 49 40 return; … … 83 74 protected HttpURLConnection loadTileFromOsm(Tile tile) throws IOException { 84 75 URL url; 85 url = new URL( baseUrl + "/" + tile.getKey() + ".png");76 url = new URL(tile.getUrl()); 86 77 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); 87 78 urlConn.setReadTimeout(30000); // 30 seconds read -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/Tile.java
r9618 r9780 13 13 14 14 import org.openstreetmap.gui.jmapviewer.interfaces.TileCache; 15 import org.openstreetmap.gui.jmapviewer.interfaces.TileSource; 15 16 16 17 /** … … 22 23 public class Tile { 23 24 25 protected TileSource source; 24 26 protected int xtile; 25 27 protected int ytile; … … 35 37 * Creates a tile with empty image. 36 38 * 39 * @param source 37 40 * @param xtile 38 41 * @param ytile 39 42 * @param zoom 40 43 */ 41 public Tile( int xtile, int ytile, int zoom) {44 public Tile(TileSource source, int xtile, int ytile, int zoom) { 42 45 super(); 46 this.source = source; 43 47 this.xtile = xtile; 44 48 this.ytile = ytile; 45 49 this.zoom = zoom; 46 50 this.image = null; 47 this.key = getTileKey( xtile, ytile, zoom);48 } 49 50 public Tile( int xtile, int ytile, int zoom, BufferedImage image) {51 this( xtile, ytile, zoom);51 this.key = getTileKey(source, xtile, ytile, zoom); 52 } 53 54 public Tile(TileSource source, int xtile, int ytile, int zoom, BufferedImage image) { 55 this(source, xtile, ytile, zoom); 52 56 this.image = image; 53 57 } … … 59 63 */ 60 64 public void loadPlaceholderFromCache(TileCache cache) { 61 BufferedImage tmpImage = new BufferedImage(WIDTH, HEIGHT, 62 BufferedImage.TYPE_INT_RGB); 65 BufferedImage tmpImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); 63 66 Graphics2D g = (Graphics2D) tmpImage.getGraphics(); 64 67 // g.drawImage(image, 0, 0, null); … … 76 79 for (int x = 0; x < factor; x++) { 77 80 for (int y = 0; y < factor; y++) { 78 Tile tile = cache.getTile(xtile_high + x, ytile_high79 + y, zoom_high);81 Tile tile = 82 cache.getTile(source, xtile_high + x, ytile_high + y, zoom_high); 80 83 if (tile != null && tile.isLoaded()) { 81 84 paintedTileCount++; … … 101 104 at.setTransform(scale, 0, 0, scale, -translate_x, -translate_y); 102 105 g.setTransform(at); 103 Tile tile = cache.getTile( xtile_low, ytile_low, zoom_low);106 Tile tile = cache.getTile(source, xtile_low, ytile_low, zoom_low); 104 107 if (tile != null && tile.isLoaded()) { 105 108 tile.paint(g, 0, 0); … … 111 114 } 112 115 116 public TileSource getSource() { 117 return source; 118 } 119 113 120 /** 114 121 * @return tile number on the x axis of this tile … … 157 164 public void setLoaded(boolean loaded) { 158 165 this.loaded = loaded; 166 } 167 168 public String getUrl() { 169 return source.getTileUrl(zoom, xtile, ytile); 159 170 } 160 171 … … 177 188 @Override 178 189 public String toString() { 179 return "Tile " + getTileKey(xtile, ytile, zoom);190 return "Tile " + key; 180 191 } 181 192 … … 185 196 return false; 186 197 Tile tile = (Tile) obj; 187 return (xtile == tile.xtile) && (ytile == tile.ytile) 188 && (zoom == tile.zoom); 189 } 190 191 public static String getTileKey(int xtile, int ytile, int zoom) { 192 return zoom + "/" + xtile + "/" + ytile; 198 return (xtile == tile.xtile) && (ytile == tile.ytile) && (zoom == tile.zoom); 199 } 200 201 public static String getTileKey(TileSource source, int xtile, int ytile, int zoom) { 202 return zoom + "/" + xtile + "/" + ytile + "@" + source.getName(); 193 203 } 194 204 -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/interfaces/TileCache.java
r9494 r9780 18 18 * will be returned. 19 19 * 20 * @param source 20 21 * @param x 21 22 * tile number on the x axis of the tile to be retrieved … … 27 28 * present in the cache 28 29 */ 29 public Tile getTile( int x, int y, int z);30 public Tile getTile(TileSource source, int x, int y, int z); 30 31 31 32 /** -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/interfaces/TileLoader.java
r9619 r9780 16 16 * load action. 17 17 * 18 * @param tileLayerSource 18 19 * @param tilex 19 20 * @param tiley … … 22 23 * action. 23 24 */ 24 public Job createTileLoaderJob( int tilex, int tiley, int zoom);25 public Job createTileLoaderJob(TileSource tileLayerSource, int tilex, int tiley, int zoom); 25 26 } -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/interfaces/TileSource.java
r9712 r9780 1 1 package org.openstreetmap.gui.jmapviewer.interfaces; 2 3 import org.openstreetmap.gui.jmapviewer.JMapViewer; 2 4 3 5 //License: GPL. Copyright 2008 by Jan Peter Stotz … … 7 9 * @author Jan Peter Stotz 8 10 */ 9 public interface Tile LayerSource {11 public interface TileSource { 10 12 11 13 /** 12 * Specifies the maximum zoom value. The number of zoom levels is [0..{@link #getMaxZoom()}] 13 * 14 * @return maximum zoom value 14 * Specifies the different mechanisms for detecting updated tiles 15 * respectively only download newer tiles than those stored locally. 16 * 17 * <ul> 18 * <li>{@link #IfNoneMatch} Server provides ETag header entry for all tiles 19 * and <b>supports</b> conditional download via <code>If-None-Match</code> 20 * header entry.</li> 21 * <li>{@link #ETag} Server provides ETag header entry for all tiles but 22 * <b>does not support</b> conditional download via 23 * <code>If-None-Match</code> header entry.</li> 24 * <li>{@link #IfModifiedSince} Server provides Last-Modified header entry 25 * for all tiles and <b>supports</b> conditional download via 26 * <code>If-Modified-Since</code> header entry.</li> 27 * <li>{@link #LastModified} Server provides Last-Modified header entry for 28 * all tiles but <b>does not support</b> conditional download via 29 * <code>If-Modified-Since</code> header entry.</li> 30 * </ul> 31 * 32 */ 33 public enum TileUpdateDetection { 34 IfNoneMatch, ETag, IfModifiedSince, LastModified 35 }; 36 37 /** 38 * Specifies the maximum zoom value. The number of zoom levels is [0.. 39 * {@link #getMaxZoom()}]. 40 * 41 * @return maximum zoom value that has to be smaller or equal to 42 * {@link JMapViewer#MAX_ZOOM} 15 43 */ 16 44 public int getMaxZoom(); 17 45 18 46 /** 19 * @return Name of the tile layer 47 * A tile layer name has to be unique and has to consist only of characters 48 * valid for filenames. 49 * 50 * @return Name of the tile layer 20 51 */ 21 52 public String getName(); 22 53 23 54 /** 24 55 * @param zoom
Note:
See TracChangeset
for help on using the changeset viewer.