Ignore:
Timestamp:
2015-05-14T13:25:43+02:00 (10 years ago)
Author:
stoecker
Message:

see #josm11419 - support different tile sizes, patch mainly by wiktorn

Location:
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/OsmMercator.java

    r30900 r31125  
    1111public class OsmMercator {
    1212
    13     public static int TILE_SIZE = 256;
     13    /**
     14     * default tile size
     15     */
     16    public static int DEFAUL_TILE_SIZE = 256;
     17    /** maximum latitude (north) for mercator display */
    1418    public static final double MAX_LAT = 85.05112877980659;
     19    /** minimum latitude (south) for mercator display */
    1520    public static final double MIN_LAT = -85.05112877980659;
    16     private static double EARTH_RADIUS = 6378137; // equatorial earth radius for EPSG:3857 (Mercator)
    17 
    18     public static double radius(int aZoomlevel) {
    19         return (TILE_SIZE * (1 << aZoomlevel)) / (2.0 * Math.PI);
     21    /** equatorial earth radius for EPSG:3857 (Mercator) */
     22    private static double EARTH_RADIUS = 6378137;
     23
     24    /**
     25     * instance with tile size of 256 for easy conversions
     26     */
     27    public static final OsmMercator MERCATOR_256 = new OsmMercator();
     28
     29    /** tile size of the displayed tiles */
     30    private int tileSize = DEFAUL_TILE_SIZE;
     31
     32    /**
     33     * Creates instance with default tile size of 256
     34     */
     35    public OsmMercator() {
     36    }
     37
     38    /**
     39     * Creates instance with provided tile size.
     40     * @param tileSize
     41     */
     42    public OsmMercator(int tileSize) {
     43        this.tileSize = tileSize;
     44    }
     45
     46    public double radius(int aZoomlevel) {
     47        return (tileSize * (1 << aZoomlevel)) / (2.0 * Math.PI);
    2048    }
    2149
    2250    /**
    2351     * Returns the absolut number of pixels in y or x, defined as: 2^Zoomlevel *
    24      * TILE_WIDTH where TILE_WIDTH is the width of a tile in pixels
     52     * tileSize where tileSize is the width of a tile in pixels
    2553     *
    2654     * @param aZoomlevel zoom level to request pixel data
    2755     * @return number of pixels
    2856     */
    29     public static int getMaxPixels(int aZoomlevel) {
    30         return TILE_SIZE * (1 << aZoomlevel);
    31     }
    32 
    33     public static int falseEasting(int aZoomlevel) {
     57    public int getMaxPixels(int aZoomlevel) {
     58        return tileSize * (1 << aZoomlevel);
     59    }
     60
     61    public int falseEasting(int aZoomlevel) {
    3462        return getMaxPixels(aZoomlevel) / 2;
    3563    }
    3664
    37     public static int falseNorthing(int aZoomlevel) {
     65    public int falseNorthing(int aZoomlevel) {
    3866        return (-1 * getMaxPixels(aZoomlevel) / 2);
    3967    }
     
    5179     * @author Jason Huntley
    5280     */
    53     public static double getDistance(int x1, int y1, int x2, int y2, int zoomLevel) {
     81    public double getDistance(int x1, int y1, int x2, int y2, int zoomLevel) {
    5482        double la1 = YToLat(y1, zoomLevel);
    5583        double lo1 = XToLon(x1, zoomLevel);
     
    7098     * @author Jason Huntley
    7199     */
    72     public static double getDistance(double la1, double lo1, double la2, double lo2) {
     100    public double getDistance(double la1, double lo1, double la2, double lo2) {
    73101        double aStartLat = Math.toRadians(la1);
    74102        double aStartLong = Math.toRadians(lo1);
     
    101129     * @author Jan Peter Stotz
    102130     */
    103     public static double LonToX(double aLongitude, int aZoomlevel) {
     131    public double LonToX(double aLongitude, int aZoomlevel) {
    104132        int mp = getMaxPixels(aZoomlevel);
    105133        double x = (mp * (aLongitude + 180l)) / 360l;
     
    125153     * @author Jan Peter Stotz
    126154     */
    127     public static double LatToY(double aLat, int aZoomlevel) {
     155    public double LatToY(double aLat, int aZoomlevel) {
    128156        if (aLat < MIN_LAT)
    129157            aLat = MIN_LAT;
     
    155183     * @author Jan Peter Stotz
    156184     */
    157     public static double XToLon(int aX, int aZoomlevel) {
     185    public double XToLon(int aX, int aZoomlevel) {
    158186        return ((360d * aX) / getMaxPixels(aZoomlevel)) - 180.0;
    159187    }
     
    166194     * @return [MIN_LAT..MAX_LAT] is about [-85..85]
    167195     */
    168     public static double YToLat(int aY, int aZoomlevel) {
     196    public double YToLat(int aY, int aZoomlevel) {
    169197        aY += falseNorthing(aZoomlevel);
    170198        double latitude = (Math.PI / 2) - (2 * Math.atan(Math.exp(-1.0 * aY / radius(aZoomlevel))));
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/tilesources/AbstractTMSTileSource.java

    r31124 r31125  
    1515    protected String id;
    1616    private Map<String, String> noTileHeaders;
     17    protected int tileSize;
     18    protected OsmMercator osmMercator;
    1719
    1820    public AbstractTMSTileSource(TileSourceInfo info) {
     
    2426        this.id = info.getUrl();
    2527        this.noTileHeaders = info.getNoTileHeaders();
     28        this.tileSize = info.getTileSize();
     29        osmMercator = new OsmMercator(this.tileSize);
    2630    }
    2731
     
    8185    @Override
    8286    public int getTileSize() {
    83         return OsmMercator.TILE_SIZE;
     87        return tileSize;
    8488    }
    8589
    8690    @Override
    8791    public double getDistance(double lat1, double lon1, double lat2, double lon2) {
    88         return OsmMercator.getDistance(lat1, lon1, lat2, lon2);
     92        return osmMercator.getDistance(lat1, lon1, lat2, lon2);
    8993    }
    9094
    9195    @Override
    9296    public int LonToX(double lon, int zoom) {
    93         return (int )OsmMercator.LonToX(lon, zoom);
     97        return (int )osmMercator.LonToX(lon, zoom);
    9498    }
    9599
    96100    @Override
    97101    public int LatToY(double lat, int zoom) {
    98         return (int )OsmMercator.LatToY(lat, zoom);
     102        return (int )osmMercator.LatToY(lat, zoom);
    99103    }
    100104
    101105    @Override
    102106    public double XToLon(int x, int zoom) {
    103         return OsmMercator.XToLon(x, zoom);
     107        return osmMercator.XToLon(x, zoom);
    104108    }
    105109
    106110    @Override
    107111    public double YToLat(int y, int zoom) {
    108         return OsmMercator.YToLat(y, zoom);
     112        return osmMercator.YToLat(y, zoom);
    109113    }
    110114
    111115    @Override
    112116    public double latToTileY(double lat, int zoom) {
    113         return OsmMercator.LatToY(lat, zoom) / OsmMercator.TILE_SIZE;
     117        return osmMercator.LatToY(lat, zoom) / tileSize;
    114118    }
    115119
    116120    @Override
    117121    public double lonToTileX(double lon, int zoom) {
    118         return OsmMercator.LonToX(lon, zoom) / OsmMercator.TILE_SIZE;
     122        return osmMercator.LonToX(lon, zoom) / tileSize;
    119123    }
    120124
    121125    @Override
    122126    public double tileYToLat(int y, int zoom) {
    123         return OsmMercator.YToLat(y * OsmMercator.TILE_SIZE, zoom);
     127        return osmMercator.YToLat(y * tileSize, zoom);
    124128    }
    125129
    126130    @Override
    127131    public double tileXToLon(int x, int zoom) {
    128         return OsmMercator.XToLon(x * OsmMercator.TILE_SIZE, zoom);
     132        return osmMercator.XToLon(x * tileSize, zoom);
    129133    }
    130134
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/tilesources/ScanexTileSource.java

    r31122 r31125  
    9494    @Override
    9595    public int LatToY(double lat, int zoom) {
    96         return (int )(latToTileY(lat, zoom) * OsmMercator.TILE_SIZE);
     96        return (int )(latToTileY(lat, zoom) * tileSize);
    9797    }
    9898
    9999    @Override
    100100    public double YToLat(int y, int zoom) {
    101         return tileYToLat((double )y / OsmMercator.TILE_SIZE, zoom);
     101        return tileYToLat((double )y / tileSize, zoom);
    102102    }
    103103
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/tilesources/TileSourceInfo.java

    r31122 r31125  
    33
    44import java.util.Map;
     5
     6import org.openstreetmap.gui.jmapviewer.OsmMercator;
    57
    68/**
     
    2830    protected String cookies;
    2931
     32    /** tile size of the displayed tiles */
     33    private int tileSize = OsmMercator.DEFAUL_TILE_SIZE;
    3034
    3135    /**
     
    5862
    5963    /**
    60      *
     64     * Request name of the tile source
    6165     * @return name of the tile source
    6266     */
     
    6670
    6771    /**
    68      *
     72     * Request URL of the tile source
    6973     * @return url of the tile source
    7074     */
     
    7478
    7579    /**
    76      *
     80     * Request header information for empty tiles for servers delivering such tile types
    7781     * @return map of headers, that when set, means that this is "no tile at this zoom level" situation
    7882     */
     
    8286
    8387    /**
    84      *
     88     * Request supported minimum zoom level
    8589     * @return minimum zoom level supported by tile source
    8690     */
     
    9094
    9195    /**
    92      *
     96     * Request supported maximum zoom level
    9397     * @return maximum zoom level supported by tile source
    9498     */
     
    98102
    99103    /**
    100      *
     104     * Request cookies to be sent together with request
    101105     * @return cookies to be sent along with request to tile source
    102106     */
     
    105109    }
    106110
     111    /**
     112     * Request tile size of this tile source
     113     * @return tile size provided by this tile source
     114     */
     115    public int getTileSize() {
     116        return tileSize;
     117    }
     118
     119    /**
     120     * Sets the tile size provided by this tile source
     121     * @param tileSize
     122     */
     123    public void setTileSize(int tileSize) {
     124        if (tileSize <= 0) {
     125            throw new AssertionError("Invalid tile size: " + tileSize);
     126        }
     127        this.tileSize = tileSize;
     128    }
    107129}
Note: See TracChangeset for help on using the changeset viewer.