001 package org.openstreetmap.gui.jmapviewer.tilesources;
002
003 //License: GPL.
004
005 import java.awt.Image;
006 import java.io.IOException;
007
008 import javax.swing.ImageIcon;
009
010 import org.openstreetmap.gui.jmapviewer.Coordinate;
011 import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
012
013 public abstract class AbstractTMSTileSource extends AbstractTileSource {
014
015 protected String name;
016 protected String baseUrl;
017
018 public AbstractTMSTileSource(String name, String base_url) {
019 this.name = name;
020 this.baseUrl = base_url;
021 if(baseUrl.endsWith("/")) {
022 baseUrl = baseUrl.substring(0,baseUrl.length()-1);
023 }
024 }
025
026 @Override
027 public String getName() {
028 return name;
029 }
030
031 @Override
032 public int getMaxZoom() {
033 return 21;
034 }
035
036 @Override
037 public int getMinZoom() {
038 return 0;
039 }
040
041 public String getExtension() {
042 return "png";
043 }
044
045 /**
046 * @throws IOException when subclass cannot return the tile URL
047 */
048 public String getTilePath(int zoom, int tilex, int tiley) throws IOException {
049 return "/" + zoom + "/" + tilex + "/" + tiley + "." + getExtension();
050 }
051
052 public String getBaseUrl() {
053 return this.baseUrl;
054 }
055
056 @Override
057 public String getTileUrl(int zoom, int tilex, int tiley) throws IOException {
058 return this.getBaseUrl() + getTilePath(zoom, tilex, tiley);
059 }
060
061 @Override
062 public String toString() {
063 return getName();
064 }
065
066 @Override
067 public String getTileType() {
068 return "png";
069 }
070
071 @Override
072 public int getTileSize() {
073 return 256;
074 }
075
076 @Override
077 public double latToTileY(double lat, int zoom) {
078 double l = lat / 180 * Math.PI;
079 double pf = Math.log(Math.tan(l) + (1 / Math.cos(l)));
080 return Math.pow(2.0, zoom - 1) * (Math.PI - pf) / Math.PI;
081 }
082
083 @Override
084 public double lonToTileX(double lon, int zoom) {
085 return Math.pow(2.0, zoom - 3) * (lon + 180.0) / 45.0;
086 }
087
088 @Override
089 public double tileYToLat(int y, int zoom) {
090 return Math.atan(Math.sinh(Math.PI - (Math.PI * y / Math.pow(2.0, zoom - 1)))) * 180 / Math.PI;
091 }
092
093 @Override
094 public double tileXToLon(int x, int zoom) {
095 return x * 45.0 / Math.pow(2.0, zoom - 3) - 180.0;
096 }
097 }