Ignore:
Timestamp:
2009-02-09T08:42:12+01:00 (16 years ago)
Author:
frederik
Message:

added zoom and autozoom. patch by lubomir.varga@…

Location:
applications/editors/josm/plugins/slippymap/src/org/openstreetmap/josm/plugins/slippymap
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/slippymap/src/org/openstreetmap/josm/plugins/slippymap/SlippyMapLayer.java

    r13497 r13625  
    3535/**
    3636 * Class that displays a slippy map layer.
    37  *
     37 * 
    3838 * @author Frederik Ramm <frederik@remote.org>
    39  *
     39 * @author LuVar <lubomir.varga@freemap.sk>
     40 *
    4041 */
    4142public class SlippyMapLayer extends Layer implements ImageObserver,
    4243        PreferenceChangedListener
    4344{
    44     ArrayList<HashMap<Integer, SlippyMapTile>> tileStorage = null;
     45        public int currentZoomLevel = 14;
     46    ArrayList<HashMap<SlippyMapKey, SlippyMapTile>> tileStorage = null;
    4547
    4648    Point[][]                                  pixelpos    = new Point[21][21];
     
    5759    {
    5860        super(tr("Slippy Map"));
    59     background = true;
     61        background = true;
    6062
    6163        clearTileStorage();
     
    110112            }
    111113        }));
     114       
     115        //increase and decrease commands
     116        tileOptionMenu.add(new JMenuItem(new AbstractAction(tr("Increase zoom"))
     117        {
     118            public void actionPerformed(ActionEvent ae)
     119            {
     120                increaseZoomLevel();
     121                needRedraw = true;
     122                Main.map.repaint();
     123            }
     124        }));
     125       
     126        tileOptionMenu.add(new JMenuItem(new AbstractAction(tr("Decrease zoom"))
     127        {
     128            public void actionPerformed(ActionEvent ae)
     129            {
     130                decreaseZoomLevel();
     131                needRedraw = true;
     132                Main.map.repaint();
     133            }
     134        }));
     135        //end of adding menu commands
    112136
    113137        SwingUtilities.invokeLater(new Runnable()
     
    150174    }
    151175
     176    /**
     177     * Zoom in, go closer to map.
     178     */
     179    public void increaseZoomLevel() {
     180        //TODO max lvl should be in preferences...
     181        if(currentZoomLevel < 17) {
     182                currentZoomLevel++;
     183                loadAllTiles();
     184        }
     185    }
     186   
     187    /**
     188     * Zoom out from map.
     189     */
     190    public void decreaseZoomLevel() {
     191        if(currentZoomLevel > 10) {
     192                currentZoomLevel--;
     193                loadAllTiles();
     194        }
     195    }
     196   
    152197    private void clearTileStorage()
    153198    {
    154         tileStorage = new ArrayList<HashMap<Integer, SlippyMapTile>>(20);
     199        tileStorage = new ArrayList<HashMap<SlippyMapKey, SlippyMapTile>>(20);
    155200
    156201        for (int i = 0; i < 18; i++)
    157             tileStorage.add(new HashMap<Integer, SlippyMapTile>());
     202            tileStorage.add(new HashMap<SlippyMapKey, SlippyMapTile>());
    158203    }
    159204
     
    179224            z12y1 = tmp;
    180225        }
    181         if (z12x1 - z12x0 > 18)
     226        //if there is more than 18 tiles on screen in any direction, do not load all tiles!
     227        if (z12x1 - z12x0 > 18) {
     228                System.out.println("Not downloading all tiles because there is more than 18 tiles on X axis!");
    182229            return;
    183         if (z12y1 - z12y0 > 18)
    184             return;
     230        }
     231        if (z12y1 - z12y0 > 18) {
     232                System.out.println("Not downloading all tiles because there is more than 18 tiles on Y axis!");
     233                return;
     234        }
     235       
    185236        for (int x = z12x0 - 1; x <= z12x1; x++)
    186237        {
    187238            for (int y = z12y0 - 1; y <= z12y1; y++)
    188239            {
    189                 int key = (y << 19) + x;
    190 
    191                 SlippyMapTile tile = tileStorage.get(12).get(key);
     240                SlippyMapKey key = new SlippyMapKey(x,y);
     241
     242                SlippyMapTile tile = tileStorage.get(currentZoomLevel).get(key);
    192243
    193244                if (tile == null)
    194                     tileStorage.get(12).put(key,
    195                             tile = new SlippyMapTile(x, y, 12));
     245                    tileStorage.get(currentZoomLevel).put(key,
     246                            tile = new SlippyMapTile(x, y, currentZoomLevel));
    196247
    197248                if (tile.getImage() == null)
     
    244295            z12y1 = tmp;
    245296        }
    246 
     297       
    247298        if (z12x1 - z12x0 > 18)
    248299            return;
     
    269320            for (int y = z12y0 - 1; y <= z12y1; y++)
    270321            {
    271                 int key = (y << 19) + x;
    272                 SlippyMapTile tile = tileStorage.get(12).get(key);
     322                SlippyMapKey key = new SlippyMapKey(x,y);
     323                SlippyMapTile tile = tileStorage.get(currentZoomLevel).get(key);
    273324
    274325                if (tile == null)
    275326                {
    276                     tileStorage.get(12).put(key,
    277                             tile = new SlippyMapTile(x, y, 12));
     327                    tileStorage.get(currentZoomLevel).put(key,
     328                            tile = new SlippyMapTile(x, y, currentZoomLevel));
    278329                }
    279330                Image img = tile.getImage();
     
    304355            for (int y = z12y0 - 1; y <= z12y1; y++)
    305356            {
    306                 int key = (y << 19) + x;
     357                SlippyMapKey key = new SlippyMapKey(x,y);
    307358                int texty = p.y + 2 + fontHeight;
    308                 SlippyMapTile tile = tileStorage.get(12).get(key);
     359                SlippyMapTile tile = tileStorage.get(currentZoomLevel).get(key);
    309360                p = pixelpos[x - z12x0 + 1][y - z12y0 + 2];
    310                 g.drawString("x=" + x + " y=" + y + " z=12", p.x + 2, texty);
     361                g.drawString("x=" + x + " y=" + y + " z=" + currentZoomLevel + "", p.x + 2, texty);
    311362                texty += 1 + fontHeight;
    312363                if ((x % 32 == 0) && (y % 32 == 0))
     
    346397        oldg.drawImage(bufferImage, 0, 0, null);
    347398
    348     }
     399        if((z12x1 - z12x0 < 2) || (z12y1 - z12y0 < 2)) {
     400                increaseZoomLevel();
     401                this.paint(oldg, mv);
     402        }
     403       
     404        if((z12x1 - z12x0 > 6) || (z12y1 - z12y0 > 6)) {
     405                decreaseZoomLevel();
     406                this.paint(oldg, mv);
     407        }
     408       
     409        g.drawString("currentZoomLevel=" + currentZoomLevel, 120, 120);
     410    }//end of paint metod
    349411
    350412    SlippyMapTile getTileForPixelpos(int px, int py)
     
    375437            return null;
    376438
    377         int key = (tiley << 19) + tilex;
    378         SlippyMapTile tile = tileStorage.get(12).get(key);
     439        SlippyMapKey key = new SlippyMapKey(tilex,tiley);
     440        SlippyMapTile tile = tileStorage.get(currentZoomLevel).get(key);
    379441        if (tile == null)
    380             tileStorage.get(12).put(key,
    381                     tile = new SlippyMapTile(tilex, tiley, 12));
     442            tileStorage.get(currentZoomLevel).put(key,
     443                    tile = new SlippyMapTile(tilex, tiley, currentZoomLevel));
    382444        return tile;
    383445    }
     
    435497        double l = lat / 180 * Math.PI;
    436498        double pf = Math.log(Math.tan(l) + (1 / Math.cos(l)));
    437         return (int) (2048.0 * (Math.PI - pf) / Math.PI);
     499        return (int) (Math.pow(2.0, currentZoomLevel-1) * (Math.PI - pf) / Math.PI);
    438500    }
    439501
    440502    private int lonToTileX(double lon)
    441503    {
    442         return (int) (512.0 * (lon + 180.0) / 45.0);
     504        return (int) (Math.pow(2.0, currentZoomLevel-3) * (lon + 180.0) / 45.0);
    443505    }
    444506
    445507    private double tileYToLat(int y)
    446508    {
    447         return Math.atan(Math.sinh(Math.PI - (Math.PI * y / 2048.0))) * 180
     509        return Math.atan(Math.sinh(Math.PI - (Math.PI * y / Math.pow(2.0, currentZoomLevel-1)))) * 180
    448510                / Math.PI;
    449511    }
     
    451513    private double tileXToLon(int x)
    452514    {
    453         return x * 45.0 / 512.0 - 180.0;
     515        return x * 45.0 / Math.pow(2.0, currentZoomLevel-3) - 180.0;
    454516    }
    455517
     
    468530    /*
    469531     * (non-Javadoc)
    470      *
     532     * 
    471533     * @see org.openstreetmap.josm.data.Preferences.PreferenceChangedListener#preferenceChanged(java.lang.String,
    472534     *      java.lang.String)
  • applications/editors/josm/plugins/slippymap/src/org/openstreetmap/josm/plugins/slippymap/SlippyMapPlugin.java

    r13497 r13625  
    1616    public SlippyMapPlugin()
    1717    {
    18 
    1918    }
    2019
  • applications/editors/josm/plugins/slippymap/src/org/openstreetmap/josm/plugins/slippymap/SlippyMapPreferenceSetting.java

    r13497 r13625  
    33import static org.openstreetmap.josm.tools.I18n.tr;
    44
    5 import java.awt.Component;
    6 
    7 import javax.swing.DefaultListCellRenderer;
    85import javax.swing.JComboBox;
    96import javax.swing.JLabel;
    10 import javax.swing.JList;
    11 import javax.swing.ListCellRenderer;
    12 import javax.swing.UIManager;
    13 import javax.swing.UIManager.LookAndFeelInfo;
    147
    158import org.openstreetmap.josm.Main;
     
    2417 *
    2518 */
    26 public class SlippyMapPreferenceSetting implements PreferenceSetting
    27 {
     19public class SlippyMapPreferenceSetting implements PreferenceSetting {
    2820    /**
    2921     * ComboBox with all known tile sources.
     
    6052    public boolean ok()
    6153    {
    62         Main.pref.put(SlippyMapPreferences.PREFERENCE_TILE_URL, tileSourceCombo
    63                 .getSelectedItem().toString());
     54        Main.pref.put(SlippyMapPreferences.PREFERENCE_TILE_URL, tileSourceCombo.getSelectedItem().toString());
     55        //restart isnt required
    6456        return false;
    6557    }
  • applications/editors/josm/plugins/slippymap/src/org/openstreetmap/josm/plugins/slippymap/SlippyMapPreferences.java

    r13497 r13625  
    55/**
    66 * Preferences for Slippy Map Tiles
    7  *
     7 * 
    88 * @author Hakan Tandogan <hakan@gurkensalat.com>
    9  *
     9 * @author LuVar <lubomir.varga@freemap.sk>
     10 *
    1011 */
    1112public class SlippyMapPreferences
     
    1415
    1516    public static String PREFERENCE_TILE_URL = PREFERENCE_PREFIX + ".tile_url";
    16 
     17   
    1718    public static String getMapUrl()
    1819    {
     
    3536                "http://tah.openstreetmap.org/Tiles/maplint", // maplint
    3637                "http://tile.openstreetmap.org", // mapnik
    37                 "http://hypercube.telascience.org/tiles/1.0.0/coastline" // coastline
     38                "http://hypercube.telascience.org/tiles/1.0.0/coastline", // coastline
     39                "http://www.freemap.sk/layers/allinone/?", //freemapy.sk
     40                "http://www.freemap.sk/layers/tiles/?", //freemapy.sk pokus 2
    3841        };
    3942        return defaultTileSources;
  • applications/editors/josm/plugins/slippymap/src/org/openstreetmap/josm/plugins/slippymap/SlippyMapTile.java

    r13497 r13625  
    1313/**
    1414 * Class that contains information about one single slippy map tile.
    15  *
     15 * 
    1616 * @author Frederik Ramm <frederik@remote.org>
    17  *
     17 * @author LuVar <lubomir.varga@freemap.sk>
     18 *
    1819 */
    1920public class SlippyMapTile
     
    2122    private Image  tileImage;
    2223
    23     int            x;
    24     int            y;
    25     int            z;
     24    int x;
     25    int y;
     26    int z;
    2627
    2728    private String metadata;
     
    4546            URL imageURL = new URL(SlippyMapPreferences.getMapUrl() + "/" + z
    4647                    + "/" + x + "/" + y + ".png");
    47 
     48           
    4849            tileImage = Toolkit.getDefaultToolkit().createImage(imageURL);
    4950        }
     
    6566            URL dev = new URL(
    6667                    "http://tah.openstreetmap.org/Tiles/info_short.php?x=" + x
    67                             + "&y=" + y + "&z=12/layer=tile");
     68                            + "&y=" + y + "&z=" + z + "/layer=tile");
    6869            URLConnection devc = dev.openConnection();
    6970            BufferedReader in = new BufferedReader(new InputStreamReader(devc
Note: See TracChangeset for help on using the changeset viewer.