Changeset 16158 in osm for applications/editors/josm


Ignore:
Timestamp:
2009-06-26T21:44:57+02:00 (15 years ago)
Author:
lvarga
Message:

Another three patches, look previous logs.

HashMap doesn't support concurrent modification. Hashtable
is already fully synchronized, so use it instead.

These just encapsulate a few common operations. They also make sure
that we don't do things like try and download the same tile from two
threads concurrently.

A Tile is just a container for x,y values. It has one helper
to convert from those x,y to a pixel position.
TileSets are a set of tiles at a certain zoom. They encapsulate
all the old z12x0, z12x1, z12y0, z12y1 functionality and allow
for multiple zoom calculations to be made at the same time and
at zooms other than currentZoomLevel.

File:
1 edited

Legend:

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

    r16157 r16158  
    1212import java.awt.event.MouseEvent;
    1313import java.awt.image.ImageObserver;
     14import java.util.ArrayList;
    1415import java.util.Comparator;
    15 import java.util.HashMap;
     16import java.util.Hashtable;
     17import java.util.List;
    1618import java.util.TreeSet;
    1719
     
    5052     */
    5153    public int currentZoomLevel = SlippyMapPreferences.getMinZoomLvl();
    52     private HashMap<SlippyMapKey, SlippyMapTile> tileStorage = null;
     54    private Hashtable<SlippyMapKey, SlippyMapTile> tileStorage = null;
    5355
    5456    Point[][] pixelpos = new Point[21][21];
     
    209211        // the setting isnt saved yet.
    210212        int maxZoom = 30; // SlippyMapPreferences.getMaxZoomLvl();
    211         tileStorage = new HashMap<SlippyMapKey, SlippyMapTile>();
     213        tileStorage = new Hashtable<SlippyMapKey, SlippyMapTile>();
    212214
    213215        checkTileStorage();
     
    266268    }
    267269
     270    synchronized SlippyMapTile getTile(int x, int y, int zoom) {
     271                SlippyMapKey key = new SlippyMapKey(x, y, zoom);
     272                if (!key.valid) {
     273                        key = null;
     274                }
     275                return tileStorage.get(key);
     276        }
     277
     278        synchronized SlippyMapTile putTile(SlippyMapTile tile, int x, int y, int zoom) {
     279                SlippyMapKey key = new SlippyMapKey(x, y, zoom);
     280                if (!key.valid) {
     281                        key = null;
     282                }
     283                return tileStorage.put(key, tile);
     284        }
     285
     286        synchronized SlippyMapTile getOrCreateTile(int x, int y, int zoom) {
     287                SlippyMapTile tile = getTile(x, y, zoom);
     288                if (tile != null) {
     289                        return tile;
     290                }
     291                tile = new SlippyMapTile(x, y, zoom);
     292                putTile(tile, x, y, zoom);
     293                return tile;
     294        }
     295   
    268296    void loadAllTiles() {
    269297        MapView mv = Main.map.mapView;
     
    356384    }
    357385
     386        private class Tile {
     387                public int x;
     388                public int y;
     389
     390                Tile(int x, int y) {
     391                        this.x = x;
     392                        this.y = y;
     393                }
     394
     395                public Point pixelPos(int zoom) {
     396                        double lon = tileXToLon(this.x, zoom);
     397                        LatLon tmpLL = new LatLon(tileYToLat(this.y, zoom), lon);
     398                        MapView mv = Main.map.mapView;
     399                        return mv.getPoint(Main.proj.latlon2eastNorth(tmpLL));
     400                }
     401        }
     402
     403        private class TileSet {
     404                int z12x0, z12x1, z12y0, z12y1;
     405                int zoom;
     406
     407                TileSet(LatLon topLeft, LatLon botRight, int zoom) {
     408                        this.zoom = zoom;
     409                        z12x0 = lonToTileX(topLeft.lon(), zoom);
     410                        z12x1 = lonToTileX(botRight.lon(), zoom);
     411                        z12y0 = latToTileY(topLeft.lat(), zoom);
     412                        z12y1 = latToTileY(botRight.lat(), zoom);
     413                        if (z12x0 > z12x1) {
     414                                int tmp = z12x0;
     415                                z12x0 = z12x1;
     416                                z12x1 = tmp;
     417                        }
     418                        if (z12y0 > z12y1) {
     419                                int tmp = z12y0;
     420                                z12y0 = z12y1;
     421                                z12y1 = tmp;
     422                        }
     423                }
     424
     425                int tilesSpanned() {
     426                        int x_span = z12x1 - z12x0;
     427                        int y_span = z12y1 - z12y0;
     428                        return x_span * y_span;
     429                }
     430
     431                /*
     432                 * This is pretty silly. Should probably just be implemented as an
     433                 * iterator to keep from having to instantiate all these tiles.
     434                 */
     435                List<Tile> allTiles()
     436        {
     437            List<Tile> ret = new ArrayList<Tile>();
     438            for (int x = z12x0 - 1; x <= z12x1 + 1; x++) {
     439                if (x < 0)
     440                    continue;
     441                for (int y = z12y0 - 1; y <= z12y1 + 1; y++) {
     442                    if (y < 0)
     443                        continue;
     444                    Tile t = new Tile(x, y);
     445                    ret.add(t);
     446                }
     447            }
     448            return ret;
     449        }
     450
     451                boolean topTile(Tile t) {
     452                        if (t.y == z12y0 - 1)
     453                                return true;
     454                        return false;
     455                }
     456
     457                boolean leftTile(Tile t) {
     458                        if (t.x == z12x0 - 1)
     459                                return true;
     460                        return false;
     461                }
     462        }
     463       
    358464    /**
    359465     */
Note: See TracChangeset for help on using the changeset viewer.