001// License: GPL. For details, see Readme.txt file.
002package org.openstreetmap.gui.jmapviewer.tilesources;
003
004import java.io.IOException;
005
006import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
007
008/**
009 * OSM Tile source.
010 */
011public class OsmTileSource {
012
013    /**
014     * The default "Mapnik" OSM tile source.
015     */
016    public static class Mapnik extends AbstractOsmTileSource {
017
018        private static final String PATTERN = "https://%s.tile.openstreetmap.org";
019
020        private static final String[] SERVER = {"a", "b", "c"};
021
022        private int serverNum;
023
024        /**
025         * Constructs a new {@code "Mapnik"} tile source.
026         */
027        public Mapnik() {
028            super("Mapnik", PATTERN, "MAPNIK");
029            modTileFeatures = true;
030        }
031
032        @Override
033        public String getBaseUrl() {
034            String url = String.format(this.baseUrl, new Object[] {SERVER[serverNum]});
035            serverNum = (serverNum + 1) % SERVER.length;
036            return url;
037        }
038    }
039
040    /**
041     * The "Cycle Map" OSM tile source.
042     */
043    public static class CycleMap extends AbstractOsmTileSource {
044
045        private static final String PATTERN = "http://%s.tile.opencyclemap.org/cycle";
046
047        private static final String[] SERVER = {"a", "b", "c"};
048
049        private int serverNum;
050
051        /**
052         * Constructs a new {@code CycleMap} tile source.
053         */
054        public CycleMap() {
055            super("Cyclemap", PATTERN, "opencyclemap");
056        }
057
058        @Override
059        public String getBaseUrl() {
060            String url = String.format(this.baseUrl, new Object[] {SERVER[serverNum]});
061            serverNum = (serverNum + 1) % SERVER.length;
062            return url;
063        }
064
065        @Override
066        public int getMaxZoom() {
067            return 18;
068        }
069    }
070
071    /**
072     * The "Transport Map" OSM tile source.
073     *
074     * Template for thunderforest.com.
075     */
076    public abstract static class TransportMap extends AbstractOsmTileSource {
077
078        private static final String PATTERN = "https://%s.tile.thunderforest.com/transport";
079
080        private static final String[] SERVER = {"a", "b", "c"};
081
082        private int serverNum;
083
084        /**
085         * Constructs a new {@code TransportMap} tile source.
086         */
087        public TransportMap() {
088            super("OSM Transport Map", PATTERN, "osmtransportmap");
089        }
090
091        /**
092         * Get the thunderforest API key.
093         *
094         * Needs to be registered at their web site.
095         * @return the API key
096         */
097        protected abstract String getApiKey();
098
099        @Override
100        public String getBaseUrl() {
101            String url = String.format(this.baseUrl, new Object[] {SERVER[serverNum]});
102            serverNum = (serverNum + 1) % SERVER.length;
103            return url;
104        }
105
106        @Override
107        public int getMaxZoom() {
108            return 18;
109        }
110
111        @Override
112        public String getTileUrl(int zoom, int tilex, int tiley) throws IOException {
113            return this.getBaseUrl() + getTilePath(zoom, tilex, tiley) + "?apikey=" + getApiKey();
114        }
115
116        @Override
117        public String getAttributionText(int zoom, ICoordinate topLeft, ICoordinate botRight) {
118            return "Maps © Thunderforest, Data © OpenStreetMap contributors";
119        }
120
121        @Override
122        public String getAttributionLinkURL() {
123            return "http://www.thunderforest.com/";
124        }
125    }
126
127}