[3719] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[3715] | 2 | package org.openstreetmap.josm.gui.layer;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.awt.event.ActionEvent;
|
---|
| 7 | import java.util.ArrayList;
|
---|
[8526] | 8 | import java.util.Arrays;
|
---|
[11858] | 9 | import java.util.Collection;
|
---|
[3715] | 10 | import java.util.List;
|
---|
[11858] | 11 | import java.util.Objects;
|
---|
[3715] | 12 |
|
---|
| 13 | import javax.swing.AbstractAction;
|
---|
| 14 | import javax.swing.Action;
|
---|
| 15 |
|
---|
[8598] | 16 | import org.apache.commons.jcs.access.CacheAccess;
|
---|
[8526] | 17 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
|
---|
[8543] | 18 | import org.openstreetmap.josm.Main;
|
---|
[8598] | 19 | import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
|
---|
[10990] | 20 | import org.openstreetmap.josm.data.imagery.AbstractWMSTileSource;
|
---|
[3715] | 21 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
[3733] | 22 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
[3715] | 23 | import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
|
---|
[8526] | 24 | import org.openstreetmap.josm.data.imagery.TemplatedWMSTileSource;
|
---|
| 25 | import org.openstreetmap.josm.data.imagery.WMSCachedTileLoader;
|
---|
[3715] | 26 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
| 27 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
[4633] | 28 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
[11858] | 29 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
[10568] | 30 | import org.openstreetmap.josm.gui.layer.imagery.TileSourceDisplaySettings;
|
---|
[11168] | 31 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[11848] | 32 | import org.openstreetmap.josm.tools.Utils;
|
---|
[3715] | 33 |
|
---|
| 34 | /**
|
---|
| 35 | * This is a layer that grabs the current screen from an WMS server. The data
|
---|
| 36 | * fetched this way is tiled and managed to the disc to reduce server load.
|
---|
[8526] | 37 | *
|
---|
[3715] | 38 | */
|
---|
[10990] | 39 | public class WMSLayer extends AbstractCachedTileSourceLayer<AbstractWMSTileSource> {
|
---|
[10568] | 40 | private static final String PREFERENCE_PREFIX = "imagery.wms";
|
---|
| 41 | /**
|
---|
| 42 | * Registers all setting properties
|
---|
| 43 | */
|
---|
| 44 | static {
|
---|
| 45 | new TileSourceDisplaySettings(PREFERENCE_PREFIX);
|
---|
| 46 | }
|
---|
[8598] | 47 |
|
---|
[8526] | 48 | /** default tile size for WMS Layer */
|
---|
[10568] | 49 | public static final IntegerProperty PROP_IMAGE_SIZE = new IntegerProperty(PREFERENCE_PREFIX + ".imageSize", 512);
|
---|
[8598] | 50 |
|
---|
[8526] | 51 | /** should WMS layer autozoom in default mode */
|
---|
[10568] | 52 | public static final BooleanProperty PROP_DEFAULT_AUTOZOOM = new BooleanProperty(PREFERENCE_PREFIX + ".default_autozoom", true);
|
---|
[3715] | 53 |
|
---|
[8598] | 54 | private static final String CACHE_REGION_NAME = "WMS";
|
---|
| 55 |
|
---|
[11858] | 56 | private final List<String> serverProjections;
|
---|
[8598] | 57 |
|
---|
[3747] | 58 | /**
|
---|
[7119] | 59 | * Constructs a new {@code WMSLayer}.
|
---|
[8526] | 60 | * @param info ImageryInfo description of the layer
|
---|
[7119] | 61 | */
|
---|
[3715] | 62 | public WMSLayer(ImageryInfo info) {
|
---|
| 63 | super(info);
|
---|
[11168] | 64 | CheckParameterUtil.ensureThat(info.getImageryType() == ImageryType.WMS, "ImageryType is WMS");
|
---|
| 65 | CheckParameterUtil.ensureParameterNotNull(info.getUrl(), "info.url");
|
---|
| 66 | TemplatedWMSTileSource.checkUrl(info.getUrl());
|
---|
[11858] | 67 | this.serverProjections = new ArrayList<>(info.getServerProjections());
|
---|
[5391] | 68 | }
|
---|
| 69 |
|
---|
| 70 | @Override
|
---|
[10568] | 71 | protected TileSourceDisplaySettings createDisplaySettings() {
|
---|
| 72 | return new TileSourceDisplaySettings(PREFERENCE_PREFIX);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | @Override
|
---|
[8526] | 76 | public Action[] getMenuEntries() {
|
---|
| 77 | List<Action> ret = new ArrayList<>();
|
---|
| 78 | ret.addAll(Arrays.asList(super.getMenuEntries()));
|
---|
| 79 | ret.add(SeparatorLayerAction.INSTANCE);
|
---|
| 80 | ret.add(new LayerSaveAction(this));
|
---|
| 81 | ret.add(new LayerSaveAsAction(this));
|
---|
| 82 | ret.add(new BookmarkWmsAction());
|
---|
[11017] | 83 | return ret.toArray(new Action[ret.size()]);
|
---|
[3715] | 84 | }
|
---|
| 85 |
|
---|
[8384] | 86 | @Override
|
---|
[11167] | 87 | protected AbstractWMSTileSource getTileSource() {
|
---|
[11858] | 88 | AbstractWMSTileSource tileSource = new TemplatedWMSTileSource(
|
---|
| 89 | info, chooseProjection(Main.getProjection()));
|
---|
[11168] | 90 | info.setAttribution(tileSource);
|
---|
| 91 | return tileSource;
|
---|
[3715] | 92 | }
|
---|
| 93 |
|
---|
| 94 | /**
|
---|
| 95 | * This action will add a WMS layer menu entry with the current WMS layer
|
---|
| 96 | * URL and name extended by the current resolution.
|
---|
| 97 | * When using the menu entry again, the WMS cache will be used properly.
|
---|
| 98 | */
|
---|
| 99 | public class BookmarkWmsAction extends AbstractAction {
|
---|
[7119] | 100 | /**
|
---|
| 101 | * Constructs a new {@code BookmarkWmsAction}.
|
---|
| 102 | */
|
---|
[3715] | 103 | public BookmarkWmsAction() {
|
---|
| 104 | super(tr("Set WMS Bookmark"));
|
---|
| 105 | }
|
---|
[8510] | 106 |
|
---|
[3715] | 107 | @Override
|
---|
| 108 | public void actionPerformed(ActionEvent ev) {
|
---|
| 109 | ImageryLayerInfo.addLayer(new ImageryInfo(info));
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 |
|
---|
[4183] | 113 | @Override
|
---|
[11858] | 114 | public Collection<String> getNativeProjections() {
|
---|
| 115 | return serverProjections;
|
---|
[8542] | 116 | }
|
---|
| 117 |
|
---|
| 118 | @Override
|
---|
[11858] | 119 | public void projectionChanged(Projection oldValue, Projection newValue) {
|
---|
[11956] | 120 | super.projectionChanged(oldValue, newValue);
|
---|
[11858] | 121 | Projection tileProjection = chooseProjection(newValue);
|
---|
| 122 | if (!Objects.equals(tileSource.getTileProjection(), tileProjection)) {
|
---|
| 123 | tileSource.setTileProjection(tileProjection);
|
---|
[8542] | 124 | }
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[11858] | 127 | private Projection chooseProjection(Projection requested) {
|
---|
| 128 | if (serverProjections.contains(requested.toCode())) {
|
---|
| 129 | return requested;
|
---|
| 130 | } else {
|
---|
| 131 | for (String code : serverProjections) {
|
---|
| 132 | Projection proj = Projections.getProjectionByCode(code);
|
---|
| 133 | if (proj != null) {
|
---|
| 134 | Main.info(tr("Reprojecting layer {0} from {1} to {2}. For best image quality and performance,"
|
---|
| 135 | + " switch to one of the supported projections: {3}",
|
---|
| 136 | getName(), proj.toCode(), Main.getProjection().toCode(), Utils.join(", ", getNativeProjections())));
|
---|
| 137 | return proj;
|
---|
| 138 | }
|
---|
[9134] | 139 | }
|
---|
[11858] | 140 | Main.warn(tr("Unable to find supported projection for layer {0}. Using {1}.", getName(), requested.toCode()));
|
---|
| 141 | return requested;
|
---|
[9134] | 142 | }
|
---|
[8542] | 143 | }
|
---|
[8598] | 144 |
|
---|
| 145 | @Override
|
---|
| 146 | protected Class<? extends TileLoader> getTileLoaderClass() {
|
---|
| 147 | return WMSCachedTileLoader.class;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | @Override
|
---|
| 151 | protected String getCacheName() {
|
---|
| 152 | return CACHE_REGION_NAME;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | /**
|
---|
| 156 | * @return cache region for WMS layer
|
---|
| 157 | */
|
---|
| 158 | public static CacheAccess<String, BufferedImageCacheEntry> getCache() {
|
---|
| 159 | return AbstractCachedTileSourceLayer.getCache(CACHE_REGION_NAME);
|
---|
| 160 | }
|
---|
[8557] | 161 | }
|
---|