Changeset 9618 in osm for applications/viewer/jmapviewer


Ignore:
Timestamp:
2008-08-10T17:53:22+02:00 (16 years ago)
Author:
stotz
Message:

Small changes to the way tiles are loaded

Location:
applications/viewer/jmapviewer
Files:
5 added
5 edited

Legend:

Unmodified
Added
Removed
  • applications/viewer/jmapviewer

    • Property svn:ignore set to
      JMapViewer_Demo.jar
      JMapViewer_src.jar
      JMapViewer.jar
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/Demo.java

    r9494 r9618  
    1313import javax.swing.JPanel;
    1414
    15 
    1615/**
    1716 *
     
    2322public class Demo extends JFrame {
    2423
     24        private static final long serialVersionUID = 1L;
     25
    2526        public Demo() {
    2627                super("JMapViewer Demo");
    2728                setSize(400, 400);
    2829                final JMapViewer map = new JMapViewer();
    29 //              final JMapViewer map = new JMapViewer(new MemoryTileCache(),4);
    30 //              map.setTileLoader(new OsmFileCacheTileLoader(map,OsmTileLoader.MAP_MAPNIK));
     30                // final JMapViewer map = new JMapViewer(new MemoryTileCache(),4);
     31                // map.setTileLoader(new
     32                // OsmFileCacheTileLoader(map,OsmTileLoader.MAP_MAPNIK));
    3133                new DefaultMapController(map);
    3234                setLayout(new BorderLayout());
     
    3537                JPanel panel = new JPanel();
    3638                add(panel, BorderLayout.NORTH);
    37                 JLabel label =
    38                                 new JLabel(
    39                                                 "Use right mouse button to move,\n left double click or mouse wheel to zoom.");
     39                JLabel label = new JLabel(
     40                                "Use right mouse button to move,\n left double click or mouse wheel to zoom.");
    4041                panel.add(label);
    4142                JButton button = new JButton("setDisplayToFitMapMarkers");
     
    8384                map.addMapMarker(new MapMarkerDot(49.807, 8.644));
    8485
    85                 //map.setDisplayPositionByLatLon(49.807, 8.6, 11);
    86                 //map.setTileGridVisible(true);
     86                // map.setDisplayPositionByLatLon(49.807, 8.6, 11);
     87                // map.setTileGridVisible(true);
    8788        }
    8889
     
    9192         */
    9293        public static void main(String[] args) {
    93 //              Properties systemProperties = System.getProperties();
    94 //              systemProperties.setProperty("http.proxyHost","localhost");
    95 //              systemProperties.setProperty("http.proxyPort","8008");
     94                // Properties systemProperties = System.getProperties();
     95                // systemProperties.setProperty("http.proxyHost","localhost");
     96                // systemProperties.setProperty("http.proxyPort","8008");
    9697                new Demo().setVisible(true);
    9798        }
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/OsmFileCacheTileLoader.java

    r9538 r9618  
    1313import java.net.URL;
    1414import java.net.URLConnection;
    15 
    16 import javax.imageio.ImageIO;
    1715
    1816import org.openstreetmap.gui.jmapviewer.interfaces.TileCache;
     
    9088                                        f = getTileFile(tile);
    9189                                        fin = new FileInputStream(f);
    92                                         tile.setImage(ImageIO.read(fin));
     90                                        tile.loadImage(fin);
    9391                                        fin.close();
    9492                                        fileAge = f.lastModified();
     
    129127                                // }
    130128                                byte[] buffer = loadTileInBuffer(urlConn);
    131                                 tile.setImage(ImageIO.read(new ByteArrayInputStream(buffer)));
     129                                tile.loadImage(new ByteArrayInputStream(buffer));
    132130                                tile.setLoaded(true);
    133131                                map.repaint();
     
    136134                        } catch (Exception e) {
    137135                                if (input == null /* || !input.isStopped() */)
    138                                         System.err.println("failed loading " + zoom + "/" + tilex + "/" + tiley + " "
    139                                                         + e.getMessage());
     136                                        System.err.println("failed loading " + zoom + "/" + tilex
     137                                                        + "/" + tiley + " " + e.getMessage());
    140138                        } finally {
    141139                                tile.loading = false;
     
    143141                }
    144142
    145                 protected byte[] loadTileInBuffer(URLConnection urlConn) throws IOException {
     143                protected byte[] loadTileInBuffer(URLConnection urlConn)
     144                                throws IOException {
    146145                        input = urlConn.getInputStream();
    147                         ByteArrayOutputStream bout = new ByteArrayOutputStream(input.available());
     146                        ByteArrayOutputStream bout = new ByteArrayOutputStream(input
     147                                        .available());
    148148                        byte[] buffer = new byte[2048];
    149149                        boolean finished = false;
     
    175175                 * @throws IOException
    176176                 */
    177                 protected boolean isOsmTileNewer(Tile tile, long fileAge) throws IOException {
     177                protected boolean isOsmTileNewer(Tile tile, long fileAge)
     178                                throws IOException {
    178179                        URL url;
    179180                        url = new URL(baseUrl + "/" + tile.getKey() + ".png");
    180                         HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
     181                        HttpURLConnection urlConn = (HttpURLConnection) url
     182                                        .openConnection();
    181183                        urlConn.setRequestMethod("HEAD");
    182184                        urlConn.setReadTimeout(30000); // 30 seconds read
     
    191193
    192194                protected File getTileFile(Tile tile) throws IOException {
    193                         return new File(tileCacheDir + "/" + tile.getZoom() + "_" + tile.getXtile() + "_"
    194                                         + tile.getYtile() + FILE_EXT);
     195                        return new File(tileCacheDir + "/" + tile.getZoom() + "_"
     196                                        + tile.getXtile() + "_" + tile.getYtile() + FILE_EXT);
    195197                }
    196198
    197199                protected void saveTileToFile(Tile tile, byte[] rawData) {
    198200                        try {
    199                                 FileOutputStream f =
    200                                                 new FileOutputStream(tileCacheDir + "/" + tile.getZoom() + "_"
    201                                                                 + tile.getXtile() + "_" + tile.getYtile() + FILE_EXT);
     201                                FileOutputStream f = new FileOutputStream(tileCacheDir + "/"
     202                                                + tile.getZoom() + "_" + tile.getXtile() + "_"
     203                                                + tile.getYtile() + FILE_EXT);
    202204                                f.write(rawData);
    203205                                f.close();
    204206                                // System.out.println("Saved tile to file: " + tile);
    205207                        } catch (Exception e) {
    206                                 System.err.println("Failed to save tile content: " + e.getLocalizedMessage());
     208                                System.err.println("Failed to save tile content: "
     209                                                + e.getLocalizedMessage());
    207210                        }
    208211                }
     
    230233
    231234        public void setTileCacheDir(String tileCacheDir) {
    232                 this.tileCacheDir = tileCacheDir;
    233         }
    234 
    235        
     235                File dir = new File(tileCacheDir);
     236                dir.mkdirs();
     237                this.tileCacheDir = dir.getAbsolutePath();
     238        }
     239
    236240}
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/OsmTileLoader.java

    r9494 r9618  
    77import java.net.HttpURLConnection;
    88import java.net.URL;
    9 
    10 import javax.imageio.ImageIO;
    119
    1210import org.openstreetmap.gui.jmapviewer.interfaces.Job;
     
    5452                                        // Thread.sleep(500);
    5553                                        input = loadTileFromOsm(tile).getInputStream();
    56                                         tile.setImage(ImageIO.read(input));
     54                                        tile.loadImage(input);
    5755                                        tile.setLoaded(true);
    5856                                        map.repaint();
     
    6159                                } catch (Exception e) {
    6260                                        if (input == null /* || !input.isStopped() */)
    63                                                 System.err.println("failed loading " + zoom + "/" + tilex + "/" + tiley
    64                                                                 + " " + e.getMessage());
     61                                                System.err.println("failed loading " + zoom + "/"
     62                                                                + tilex + "/" + tiley + " " + e.getMessage());
    6563                                } finally {
    6664                                        tile.loading = false;
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/Tile.java

    r9494 r9618  
    77import java.awt.geom.AffineTransform;
    88import java.awt.image.BufferedImage;
     9import java.io.IOException;
     10import java.io.InputStream;
     11
     12import javax.imageio.ImageIO;
    913
    1014import org.openstreetmap.gui.jmapviewer.interfaces.TileCache;
     
    5559         */
    5660        public void loadPlaceholderFromCache(TileCache cache) {
    57                 BufferedImage tmpImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
     61                BufferedImage tmpImage = new BufferedImage(WIDTH, HEIGHT,
     62                                BufferedImage.TYPE_INT_RGB);
    5863                Graphics2D g = (Graphics2D) tmpImage.getGraphics();
    5964                // g.drawImage(image, 0, 0, null);
     
    7176                                for (int x = 0; x < factor; x++) {
    7277                                        for (int y = 0; y < factor; y++) {
    73                                                 Tile tile = cache.getTile(xtile_high + x, ytile_high + y, zoom_high);
     78                                                Tile tile = cache.getTile(xtile_high + x, ytile_high
     79                                                                + y, zoom_high);
    7480                                                if (tile != null && tile.isLoaded()) {
    7581                                                        paintedTileCount++;
     
    134140        }
    135141
     142        public void loadImage(InputStream input) throws IOException {
     143                image = ImageIO.read(input);
     144        }
     145
    136146        /**
    137147         * @return key that identifies a tile
     
    175185                        return false;
    176186                Tile tile = (Tile) obj;
    177                 return (xtile == tile.xtile) && (ytile == tile.ytile) && (zoom == tile.zoom);
     187                return (xtile == tile.xtile) && (ytile == tile.ytile)
     188                                && (zoom == tile.zoom);
    178189        }
    179190
Note: See TracChangeset for help on using the changeset viewer.