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