Ignore:
Timestamp:
2011-02-19T16:49:09+01:00 (14 years ago)
Author:
stotz
Message:

TileSource implementation moved to own package;
code clean-up and bugfixes (attribution text was not displayed)

Location:
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer
Files:
2 added
4 edited
5 moved

Legend:

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

    r25314 r25369  
    1919import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
    2020import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
     21import org.openstreetmap.gui.jmapviewer.tilesources.BingAerialTileSource;
     22import org.openstreetmap.gui.jmapviewer.tilesources.OsmTileSource;
    2123
    2224/**
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/JMapViewer.java

    r24706 r25369  
    3232import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
    3333import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
     34import org.openstreetmap.gui.jmapviewer.tilesources.OsmTileSource;
    3435
    3536/**
     
    745746
    746747    private void paintAttribution(Graphics g) {
    747         if (!tileSource.requiresAttribution()) return;
     748        if (!tileSource.requiresAttribution())
     749            return;
    748750        // Draw attribution
    749751        Font font = g.getFont();
     
    771773
    772774        g.setFont(ATTR_FONT);
    773         Coordinate topLeft = getPosition(0,0);
    774         Coordinate bottomRight = getPosition(getWidth(),getHeight());
     775        Coordinate topLeft = getPosition(0, 0);
     776        Coordinate bottomRight = getPosition(getWidth(), getHeight());
    775777        String attributionText = tileSource.getAttributionText(zoom, topLeft, bottomRight);
    776         Rectangle2D stringBounds = g.getFontMetrics().getStringBounds(attributionText, g);
    777         {
     778        if (attributionText != null) {
     779            Rectangle2D stringBounds = g.getFontMetrics().getStringBounds(attributionText, g);
    778780            int x = getWidth() - (int) stringBounds.getWidth();
    779781            int y = getHeight() - textHeight;
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/TileController.java

    r18772 r25369  
    66import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
    77import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
     8import org.openstreetmap.gui.jmapviewer.tilesources.OsmTileSource;
    89
    910public class TileController {
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/interfaces/TileSource.java

    r25265 r25369  
    9898
    9999    /**
    100      * @return True if the tile source requires attribution.
     100     * @return True if the tile source requires attribution in text or image form.
    101101     */
    102102    public boolean requiresAttribution();
     
    128128
    129129    public double lonToTileX(double lon, int zoom);
    130      
     130
    131131    public double tileYToLat(int y, int zoom);
    132            
     132
    133133    public double tileXToLon(int x, int zoom);
    134134}
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/tilesources/BingAerialTileSource.java

    r25366 r25369  
    1 package org.openstreetmap.gui.jmapviewer;
     1package org.openstreetmap.gui.jmapviewer.tilesources;
    22
    33import java.awt.Image;
     
    1414import javax.imageio.ImageIO;
    1515
     16import org.openstreetmap.gui.jmapviewer.Coordinate;
    1617import org.xml.sax.Attributes;
    1718import org.xml.sax.InputSource;
     
    2122import org.xml.sax.helpers.XMLReaderFactory;
    2223
    23 public class BingAerialTileSource extends OsmTileSource.AbstractOsmTileSource {
     24public class BingAerialTileSource extends AbstractOsmTileSource {
    2425    private static String API_KEY = "Arzdiw4nlOJzRwOz__qailc8NiR31Tt51dN2D7cm57NrnceZnCpgOkmJhNpGoppU";
    2526    private static Future<List<Attribution>> attributions;
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/tilesources/OsmTileSource.java

    r25366 r25369  
    1 package org.openstreetmap.gui.jmapviewer;
    2 
    3 import java.awt.Image;
    4 import java.io.IOException;
    5 
    6 import javax.swing.ImageIcon;
    7 
    8 import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
     1package org.openstreetmap.gui.jmapviewer.tilesources;
    92
    103public class OsmTileSource {
     
    125    public static final String MAP_MAPNIK = "http://tile.openstreetmap.org";
    136    public static final String MAP_OSMA = "http://tah.openstreetmap.org/Tiles";
    14 
    15     public static abstract class AbstractOsmTileSource implements TileSource {
    16         protected String NAME;
    17         protected String BASE_URL;
    18         protected String ATTR_IMG_URL;
    19         protected boolean REQUIRES_ATTRIBUTION = true;
    20 
    21         public AbstractOsmTileSource(String name, String base_url) {
    22             this(name, base_url, null);
    23         }
    24 
    25         public AbstractOsmTileSource(String name, String base_url, String attr_img_url) {
    26             NAME = name;
    27             BASE_URL = base_url;
    28             ATTR_IMG_URL = attr_img_url;
    29             if(ATTR_IMG_URL == null) {
    30                  REQUIRES_ATTRIBUTION = false;
    31             }
    32         }
    33 
    34         public String getName() {
    35             return NAME;
    36         }
    37 
    38         public int getMaxZoom() {
    39             return 18;
    40         }
    41 
    42         public int getMinZoom() {
    43             return 0;
    44         }
    45 
    46         public String getExtension() {
    47             return "png";
    48         }
    49 
    50         /**
    51          * @throws IOException when subclass cannot return the tile URL
    52          */
    53         public String getTilePath(int zoom, int tilex, int tiley) throws IOException {
    54             return "/" + zoom + "/" + tilex + "/" + tiley + "." + getExtension();
    55         }
    56 
    57         public String getBaseUrl() {
    58             return this.BASE_URL;
    59         }
    60 
    61         public String getTileUrl(int zoom, int tilex, int tiley) throws IOException {
    62             return this.getBaseUrl() + getTilePath(zoom, tilex, tiley);
    63         }
    64 
    65         @Override
    66         public String toString() {
    67             return getName();
    68         }
    69 
    70         public String getTileType() {
    71             return "png";
    72         }
    73 
    74         public int getTileSize() {
    75             return 256;
    76         }
    77 
    78         public Image getAttributionImage() {
    79             if (ATTR_IMG_URL != null)
    80                 return new ImageIcon(ATTR_IMG_URL).getImage();
    81             else
    82                 return null;
    83         }
    84 
    85         public boolean requiresAttribution() {
    86             return REQUIRES_ATTRIBUTION;
    87         }
    88 
    89         public String getAttributionText(int zoom, Coordinate topLeft, Coordinate botRight) {
    90             return "© OpenStreetMap contributors, CC-BY-SA ";
    91         }
    92 
    93         public String getAttributionLinkURL() {
    94             return "http://openstreetmap.org/";
    95         }
    96 
    97         public String getTermsOfUseURL() {
    98             return "http://www.openstreetmap.org/copyright";
    99         }
    100 
    101         public double latToTileY(double lat, int zoom) {
    102             double l = lat / 180 * Math.PI;
    103             double pf = Math.log(Math.tan(l) + (1 / Math.cos(l)));
    104             return Math.pow(2.0, zoom - 1) * (Math.PI - pf) / Math.PI;
    105         }
    106 
    107         public double lonToTileX(double lon, int zoom) {
    108             return Math.pow(2.0, zoom - 3) * (lon + 180.0) / 45.0;
    109         }
    110 
    111         public double tileYToLat(int y, int zoom) {
    112             return Math.atan(Math.sinh(Math.PI - (Math.PI * y / Math.pow(2.0, zoom - 1)))) * 180 / Math.PI;
    113         }
    114 
    115         public double tileXToLon(int x, int zoom) {
    116             return x * 45.0 / Math.pow(2.0, zoom - 3) - 180.0;
    117         }
    118     }
    1197
    1208    public static class Mapnik extends AbstractOsmTileSource {
     
    14331        @Override
    14432        public String getBaseUrl() {
    145             String url = String.format(this.BASE_URL, new Object[] { SERVER[SERVER_NUM] });
     33            String url = String.format(this.baseUrl, new Object[] { SERVER[SERVER_NUM] });
    14634            SERVER_NUM = (SERVER_NUM + 1) % SERVER.length;
    14735            return url;
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/tilesources/ScanexTileSource.java

    r25366 r25369  
    1 package org.openstreetmap.gui.jmapviewer;
     1package org.openstreetmap.gui.jmapviewer.tilesources;
    22
    3 public class ScanexTileSource extends OsmTileSource.AbstractOsmTileSource {
     3import org.openstreetmap.gui.jmapviewer.OsmMercator;
     4
     5public class ScanexTileSource extends AbstractOsmTileSource {
    46    private static String API_KEY = "4018C5A9AECAD8868ED5DEB2E41D09F7";
    57
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/tilesources/TMSTileSource.java

    r25366 r25369  
    1 package org.openstreetmap.gui.jmapviewer;
     1package org.openstreetmap.gui.jmapviewer.tilesources;
    22
    33
    4 public class TMSTileSource extends OsmTileSource.AbstractOsmTileSource {
     4public class TMSTileSource extends AbstractOsmTileSource {
    55    private int maxZoom;
    66
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/tilesources/TemplatedTMSTileSource.java

    r25366 r25369  
    1 package org.openstreetmap.gui.jmapviewer;
     1package org.openstreetmap.gui.jmapviewer.tilesources;
    22
    33
    4 public class TemplatedTMSTileSource extends OsmTileSource.AbstractOsmTileSource {
     4public class TemplatedTMSTileSource extends AbstractOsmTileSource {
    55    private int maxZoom;
    66   
     
    1111
    1212    public String getTileUrl(int zoom, int tilex, int tiley) {
    13         return this.BASE_URL
     13        return this.baseUrl
    1414        .replaceAll("\\{zoom\\}", Integer.toString(zoom))
    1515        .replaceAll("\\{x\\}", Integer.toString(tilex))
Note: See TracChangeset for help on using the changeset viewer.