1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Collection;
|
---|
8 |
|
---|
9 | import org.apache.commons.jcs.access.CacheAccess;
|
---|
10 | import org.openstreetmap.gui.jmapviewer.OsmMercator;
|
---|
11 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
|
---|
12 | import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource;
|
---|
13 | import org.openstreetmap.gui.jmapviewer.tilesources.ScanexTileSource;
|
---|
14 | import org.openstreetmap.gui.jmapviewer.tilesources.TMSTileSource;
|
---|
15 | import org.openstreetmap.gui.jmapviewer.tilesources.TemplatedTMSTileSource;
|
---|
16 | import org.openstreetmap.josm.Main;
|
---|
17 | import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
|
---|
18 | import org.openstreetmap.josm.data.imagery.CachedAttributionBingAerialTileSource;
|
---|
19 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
20 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
21 | import org.openstreetmap.josm.data.imagery.TMSCachedTileLoader;
|
---|
22 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
23 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
24 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Class that displays a slippy map layer.
|
---|
28 | *
|
---|
29 | * @author Frederik Ramm
|
---|
30 | * @author LuVar <lubomir.varga@freemap.sk>
|
---|
31 | * @author Dave Hansen <dave@sr71.net>
|
---|
32 | * @author Upliner <upliner@gmail.com>
|
---|
33 | * @since 3715
|
---|
34 | */
|
---|
35 | public class TMSLayer extends AbstractCachedTileSourceLayer<TMSTileSource> implements NativeScaleLayer {
|
---|
36 | private static final String CACHE_REGION_NAME = "TMS";
|
---|
37 |
|
---|
38 | private static final String PREFERENCE_PREFIX = "imagery.tms";
|
---|
39 |
|
---|
40 | /** minimum zoom level for TMS layer */
|
---|
41 | public static final IntegerProperty PROP_MIN_ZOOM_LVL = new IntegerProperty(PREFERENCE_PREFIX + ".min_zoom_lvl",
|
---|
42 | AbstractTileSourceLayer.PROP_MIN_ZOOM_LVL.get());
|
---|
43 | /** maximum zoom level for TMS layer */
|
---|
44 | public static final IntegerProperty PROP_MAX_ZOOM_LVL = new IntegerProperty(PREFERENCE_PREFIX + ".max_zoom_lvl",
|
---|
45 | AbstractTileSourceLayer.PROP_MAX_ZOOM_LVL.get());
|
---|
46 | /** shall TMS layers be added to download dialog */
|
---|
47 | public static final BooleanProperty PROP_ADD_TO_SLIPPYMAP_CHOOSER = new BooleanProperty(PREFERENCE_PREFIX + ".add_to_slippymap_chooser",
|
---|
48 | true);
|
---|
49 |
|
---|
50 | private static final ScaleList nativeScaleList = initNativeScaleList();
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Create a layer based on ImageryInfo
|
---|
54 | * @param info description of the layer
|
---|
55 | */
|
---|
56 | public TMSLayer(ImageryInfo info) {
|
---|
57 | super(info);
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Creates and returns a new TileSource instance depending on the {@link ImageryType}
|
---|
62 | * of the passed ImageryInfo object.
|
---|
63 | *
|
---|
64 | * If no appropriate TileSource is found, null is returned.
|
---|
65 | * Currently supported ImageryType are {@link ImageryType#TMS},
|
---|
66 | * {@link ImageryType#BING}, {@link ImageryType#SCANEX}.
|
---|
67 | *
|
---|
68 | *
|
---|
69 | * @param info imagery info
|
---|
70 | * @return a new TileSource instance or null if no TileSource for the ImageryInfo/ImageryType could be found.
|
---|
71 | * @throws IllegalArgumentException if url from imagery info is null or invalid
|
---|
72 | */
|
---|
73 | @Override
|
---|
74 | protected TMSTileSource getTileSource(ImageryInfo info) {
|
---|
75 | return getTileSourceStatic(info, () -> {
|
---|
76 | Main.debug("Attribution loaded, running loadAllErrorTiles");
|
---|
77 | this.loadAllErrorTiles(false);
|
---|
78 | });
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Override
|
---|
82 | public final boolean isProjectionSupported(Projection proj) {
|
---|
83 | return "EPSG:3857".equals(proj.toCode()) || "EPSG:4326".equals(proj.toCode());
|
---|
84 | }
|
---|
85 |
|
---|
86 | @Override
|
---|
87 | public final String nameSupportedProjections() {
|
---|
88 | return tr("EPSG:4326 and Mercator projection are supported");
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Creates and returns a new TileSource instance depending on the {@link ImageryType}
|
---|
93 | * of the passed ImageryInfo object.
|
---|
94 | *
|
---|
95 | * If no appropriate TileSource is found, null is returned.
|
---|
96 | * Currently supported ImageryType are {@link ImageryType#TMS},
|
---|
97 | * {@link ImageryType#BING}, {@link ImageryType#SCANEX}.
|
---|
98 | *
|
---|
99 | * @param info imagery info
|
---|
100 | * @return a new TileSource instance or null if no TileSource for the ImageryInfo/ImageryType could be found.
|
---|
101 | * @throws IllegalArgumentException if url from imagery info is null or invalid
|
---|
102 | */
|
---|
103 | public static AbstractTMSTileSource getTileSourceStatic(ImageryInfo info) {
|
---|
104 | return getTileSourceStatic(info, null);
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Creates and returns a new TileSource instance depending on the {@link ImageryType}
|
---|
109 | * of the passed ImageryInfo object.
|
---|
110 | *
|
---|
111 | * If no appropriate TileSource is found, null is returned.
|
---|
112 | * Currently supported ImageryType are {@link ImageryType#TMS},
|
---|
113 | * {@link ImageryType#BING}, {@link ImageryType#SCANEX}.
|
---|
114 | *
|
---|
115 | * @param info imagery info
|
---|
116 | * @param attributionLoadedTask task to be run once attribution is loaded, might be null, if nothing special shall happen
|
---|
117 | * @return a new TileSource instance or null if no TileSource for the ImageryInfo/ImageryType could be found.
|
---|
118 | * @throws IllegalArgumentException if url from imagery info is null or invalid
|
---|
119 | */
|
---|
120 | public static TMSTileSource getTileSourceStatic(ImageryInfo info, Runnable attributionLoadedTask) {
|
---|
121 | if (info.getImageryType() == ImageryType.TMS) {
|
---|
122 | TemplatedTMSTileSource.checkUrl(info.getUrl());
|
---|
123 | TMSTileSource t = new TemplatedTMSTileSource(info);
|
---|
124 | info.setAttribution(t);
|
---|
125 | return t;
|
---|
126 | } else if (info.getImageryType() == ImageryType.BING) {
|
---|
127 | return new CachedAttributionBingAerialTileSource(info, attributionLoadedTask);
|
---|
128 | } else if (info.getImageryType() == ImageryType.SCANEX) {
|
---|
129 | return new ScanexTileSource(info);
|
---|
130 | }
|
---|
131 | return null;
|
---|
132 | }
|
---|
133 |
|
---|
134 | @Override
|
---|
135 | protected Class<? extends TileLoader> getTileLoaderClass() {
|
---|
136 | return TMSCachedTileLoader.class;
|
---|
137 | }
|
---|
138 |
|
---|
139 | @Override
|
---|
140 | protected String getCacheName() {
|
---|
141 | return CACHE_REGION_NAME;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * @return cache for TMS region
|
---|
146 | */
|
---|
147 | public static CacheAccess<String, BufferedImageCacheEntry> getCache() {
|
---|
148 | return AbstractCachedTileSourceLayer.getCache(CACHE_REGION_NAME);
|
---|
149 | }
|
---|
150 |
|
---|
151 | @Override
|
---|
152 | public ScaleList getNativeScales() {
|
---|
153 | return nativeScaleList;
|
---|
154 | }
|
---|
155 |
|
---|
156 | private static ScaleList initNativeScaleList() {
|
---|
157 | Collection<Double> scales = new ArrayList<>(AbstractTileSourceLayer.MAX_ZOOM);
|
---|
158 | for (int zoom = AbstractTileSourceLayer.MIN_ZOOM; zoom <= AbstractTileSourceLayer.MAX_ZOOM; zoom++) {
|
---|
159 | double scale = OsmMercator.EARTH_RADIUS * Math.PI * 2 / Math.pow(2, zoom) / OsmMercator.DEFAUL_TILE_SIZE;
|
---|
160 | scales.add(scale);
|
---|
161 | }
|
---|
162 | return new ScaleList(scales);
|
---|
163 | }
|
---|
164 | }
|
---|