| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.actions;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 6 |
|
|---|
| 7 | import java.awt.GridBagConstraints;
|
|---|
| 8 | import java.awt.GridBagLayout;
|
|---|
| 9 | import java.awt.event.ActionEvent;
|
|---|
| 10 | import java.awt.event.KeyEvent;
|
|---|
| 11 | import java.io.IOException;
|
|---|
| 12 | import java.util.ArrayList;
|
|---|
| 13 | import java.util.List;
|
|---|
| 14 | import java.util.regex.Matcher;
|
|---|
| 15 | import java.util.regex.Pattern;
|
|---|
| 16 |
|
|---|
| 17 | import javax.swing.ButtonGroup;
|
|---|
| 18 | import javax.swing.JLabel;
|
|---|
| 19 | import javax.swing.JOptionPane;
|
|---|
| 20 | import javax.swing.JPanel;
|
|---|
| 21 | import javax.swing.JRadioButton;
|
|---|
| 22 |
|
|---|
| 23 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
|---|
| 24 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
|---|
| 25 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
|---|
| 26 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 27 | import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
|
|---|
| 28 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
|---|
| 29 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
|---|
| 30 | import org.openstreetmap.josm.gui.widgets.UrlLabel;
|
|---|
| 31 | import org.openstreetmap.josm.io.imagery.WMSImagery.WMSGetCapabilitiesException;
|
|---|
| 32 | import org.openstreetmap.josm.tools.GBC;
|
|---|
| 33 | import org.openstreetmap.josm.tools.Logging;
|
|---|
| 34 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * Download rectified images from various services.
|
|---|
| 38 | * @since 3715
|
|---|
| 39 | */
|
|---|
| 40 | public class MapRectifierWMSmenuAction extends JosmAction {
|
|---|
| 41 |
|
|---|
| 42 | /**
|
|---|
| 43 | * Class that bundles all required information of a rectifier service
|
|---|
| 44 | */
|
|---|
| 45 | public static class RectifierService {
|
|---|
| 46 | private final String name;
|
|---|
| 47 | private final String url;
|
|---|
| 48 | private final String wmsUrl;
|
|---|
| 49 | private final Pattern urlRegEx;
|
|---|
| 50 | private final Pattern idValidator;
|
|---|
| 51 | private JRadioButton btn;
|
|---|
| 52 |
|
|---|
| 53 | /**
|
|---|
| 54 | * Constructs a new {@code RectifierService}.
|
|---|
| 55 | * @param name Name of the rectifing service
|
|---|
| 56 | * @param url URL to the service where users can register, upload, etc.
|
|---|
| 57 | * @param wmsUrl URL to the WMS server where JOSM will grab the images. Insert __s__ where the ID should be placed
|
|---|
| 58 | * @param urlRegEx a regular expression that determines if a given URL is one of the service and returns the WMS id if so
|
|---|
| 59 | * @param idValidator regular expression that checks if a given ID is syntactically valid
|
|---|
| 60 | */
|
|---|
| 61 | public RectifierService(String name, String url, String wmsUrl, String urlRegEx, String idValidator) {
|
|---|
| 62 | this.name = name;
|
|---|
| 63 | this.url = url;
|
|---|
| 64 | this.wmsUrl = wmsUrl;
|
|---|
| 65 | this.urlRegEx = Pattern.compile(urlRegEx);
|
|---|
| 66 | this.idValidator = Pattern.compile(idValidator);
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | private boolean isSelected() {
|
|---|
| 70 | return btn.isSelected();
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | /**
|
|---|
| 75 | * List of available rectifier services.
|
|---|
| 76 | */
|
|---|
| 77 | private final transient List<RectifierService> services = new ArrayList<>();
|
|---|
| 78 |
|
|---|
| 79 | /**
|
|---|
| 80 | * Constructs a new {@code MapRectifierWMSmenuAction}.
|
|---|
| 81 | */
|
|---|
| 82 | public MapRectifierWMSmenuAction() {
|
|---|
| 83 | super(tr("Rectified Image..."),
|
|---|
| 84 | "OLmarker",
|
|---|
| 85 | tr("Download Rectified Images From Various Services"),
|
|---|
| 86 | Shortcut.registerShortcut("imagery:rectimg",
|
|---|
| 87 | tr("Imagery: {0}", tr("Rectified Image...")),
|
|---|
| 88 | KeyEvent.CHAR_UNDEFINED, Shortcut.NONE),
|
|---|
| 89 | true
|
|---|
| 90 | );
|
|---|
| 91 | setHelpId(ht("/Menu/Imagery"));
|
|---|
| 92 |
|
|---|
| 93 | // Add default services
|
|---|
| 94 | services.add(
|
|---|
| 95 | new RectifierService("Metacarta Map Rectifier",
|
|---|
| 96 | "http://labs.metacarta.com/rectifier/",
|
|---|
| 97 | "http://labs.metacarta.com/rectifier/wms.cgi?id=__s__&srs=EPSG:4326"
|
|---|
| 98 | + "&Service=WMS&Version=1.1.0&Request=GetMap&format=image/png&",
|
|---|
| 99 | // This matches more than the "classic" WMS link, so users can pretty much
|
|---|
| 100 | // copy any link as long as it includes the ID
|
|---|
| 101 | "labs\\.metacarta\\.com/(?:.*?)(?:/|=)([0-9]+)(?:\\?|/|\\.|$)",
|
|---|
| 102 | "^[0-9]+$")
|
|---|
| 103 | );
|
|---|
| 104 | services.add(
|
|---|
| 105 | new RectifierService("Map Warper",
|
|---|
| 106 | "http://mapwarper.net/",
|
|---|
| 107 | "http://mapwarper.net/maps/wms/__s__?request=GetMap&version=1.1.1"
|
|---|
| 108 | + "&styles=&format=image/png&srs=epsg:4326&exceptions=application/vnd.ogc.se_inimage&",
|
|---|
| 109 | // This matches more than the "classic" WMS link, so users can pretty much
|
|---|
| 110 | // copy any link as long as it includes the ID
|
|---|
| 111 | "(?:mapwarper\\.net|warper\\.geothings\\.net)/(?:.*?)/([0-9]+)(?:\\?|/|\\.|$)",
|
|---|
| 112 | "^[0-9]+$")
|
|---|
| 113 | );
|
|---|
| 114 |
|
|---|
| 115 | // This service serves the purpose of "just this once" without forcing the user
|
|---|
| 116 | // to commit the link to the preferences
|
|---|
| 117 |
|
|---|
| 118 | // Clipboard content gets trimmed, so matching whitespace only ensures that this
|
|---|
| 119 | // service will never be selected automatically.
|
|---|
| 120 | services.add(new RectifierService(tr("Custom WMS Link"), "", "", "^\\s+$", ""));
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | @Override
|
|---|
| 124 | public void actionPerformed(ActionEvent e) {
|
|---|
| 125 | if (!isEnabled()) return;
|
|---|
| 126 | JPanel panel = new JPanel(new GridBagLayout());
|
|---|
| 127 | panel.add(new JLabel(tr("Supported Rectifier Services:")), GBC.eol());
|
|---|
| 128 |
|
|---|
| 129 | JosmTextField tfWmsUrl = new JosmTextField(30);
|
|---|
| 130 |
|
|---|
| 131 | String clip = ClipboardUtils.getClipboardStringContent();
|
|---|
| 132 | clip = clip == null ? "" : clip.trim();
|
|---|
| 133 | ButtonGroup group = new ButtonGroup();
|
|---|
| 134 |
|
|---|
| 135 | JRadioButton firstBtn = null;
|
|---|
| 136 | for (RectifierService s : services) {
|
|---|
| 137 | JRadioButton serviceBtn = new JRadioButton(s.name);
|
|---|
| 138 | if (firstBtn == null) {
|
|---|
| 139 | firstBtn = serviceBtn;
|
|---|
| 140 | }
|
|---|
| 141 | // Checks clipboard contents against current service if no match has been found yet.
|
|---|
| 142 | // If the contents match, they will be inserted into the text field and the corresponding
|
|---|
| 143 | // service will be pre-selected.
|
|---|
| 144 | if (!clip.isEmpty() && tfWmsUrl.getText().isEmpty()
|
|---|
| 145 | && (s.urlRegEx.matcher(clip).find() || s.idValidator.matcher(clip).matches())) {
|
|---|
| 146 | serviceBtn.setSelected(true);
|
|---|
| 147 | tfWmsUrl.setText(clip);
|
|---|
| 148 | }
|
|---|
| 149 | s.btn = serviceBtn;
|
|---|
| 150 | group.add(serviceBtn);
|
|---|
| 151 | if (!s.url.isEmpty()) {
|
|---|
| 152 | panel.add(serviceBtn, GBC.std());
|
|---|
| 153 | panel.add(new UrlLabel(s.url, tr("Visit Homepage")), GBC.eol().anchor(GridBagConstraints.LINE_END));
|
|---|
| 154 | } else {
|
|---|
| 155 | panel.add(serviceBtn, GBC.eol().anchor(GridBagConstraints.LINE_START));
|
|---|
| 156 | }
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | // Fallback in case no match was found
|
|---|
| 160 | if (tfWmsUrl.getText().isEmpty() && firstBtn != null) {
|
|---|
| 161 | firstBtn.setSelected(true);
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | panel.add(new JLabel(tr("WMS URL or Image ID:")), GBC.eol());
|
|---|
| 165 | panel.add(tfWmsUrl, GBC.eol().fill(GridBagConstraints.HORIZONTAL));
|
|---|
| 166 |
|
|---|
| 167 | ExtendedDialog diag = new ExtendedDialog(MainApplication.getMainFrame(),
|
|---|
| 168 | tr("Add Rectified Image"),
|
|---|
| 169 | tr("Add Rectified Image"), tr("Cancel"))
|
|---|
| 170 | .setContent(panel)
|
|---|
| 171 | .configureContextsensitiveHelp(ht("/Menu/Imagery"), true)
|
|---|
| 172 | .setButtonIcons("OLmarker", "cancel");
|
|---|
| 173 |
|
|---|
| 174 | // This repeatedly shows the dialog in case there has been an error.
|
|---|
| 175 | // The loop is break;-ed if the users cancels
|
|---|
| 176 | outer: while (true) {
|
|---|
| 177 | diag.showDialog();
|
|---|
| 178 | int answer = diag.getValue();
|
|---|
| 179 | // Break loop when the user cancels
|
|---|
| 180 | if (answer != 1) {
|
|---|
| 181 | break;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | String text = tfWmsUrl.getText().trim();
|
|---|
| 185 | // Loop all services until we find the selected one
|
|---|
| 186 | for (RectifierService s : services) {
|
|---|
| 187 | if (!s.isSelected()) {
|
|---|
| 188 | continue;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | // We've reached the custom WMS URL service
|
|---|
| 192 | // Just set the URL and hope everything works out
|
|---|
| 193 | if (s.wmsUrl.isEmpty()) {
|
|---|
| 194 | try {
|
|---|
| 195 | addWMSLayer(s.name + " (" + text + ')', text);
|
|---|
| 196 | break outer;
|
|---|
| 197 | } catch (IllegalStateException ex) {
|
|---|
| 198 | Logging.log(Logging.LEVEL_ERROR, ex);
|
|---|
| 199 | }
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | // First try to match if the entered string as an URL
|
|---|
| 203 | Matcher m = s.urlRegEx.matcher(text);
|
|---|
| 204 | if (m.find()) {
|
|---|
| 205 | String id = m.group(1);
|
|---|
| 206 | String newURL = s.wmsUrl.replace("__s__", id);
|
|---|
| 207 | String title = s.name + " (" + id + ')';
|
|---|
| 208 | addWMSLayer(title, newURL);
|
|---|
| 209 | break outer;
|
|---|
| 210 | }
|
|---|
| 211 | // If not, look if it's a valid ID for the selected service
|
|---|
| 212 | if (s.idValidator.matcher(text).matches()) {
|
|---|
| 213 | String newURL = s.wmsUrl.replace("__s__", text);
|
|---|
| 214 | String title = s.name + " (" + text + ')';
|
|---|
| 215 | addWMSLayer(title, newURL);
|
|---|
| 216 | break outer;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | // We've found the selected service, but the entered string isn't suitable for
|
|---|
| 220 | // it. So quit checking the other radio buttons
|
|---|
| 221 | break;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | // and display an error message. The while loop ensures that the dialog pops up again
|
|---|
| 225 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(),
|
|---|
| 226 | tr("Couldn''t match the entered link or id to the selected service. Please try again."),
|
|---|
| 227 | tr("No valid WMS URL or id"),
|
|---|
| 228 | JOptionPane.ERROR_MESSAGE);
|
|---|
| 229 | diag.setVisible(true);
|
|---|
| 230 | }
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | /**
|
|---|
| 234 | * Adds a WMS Layer with given title and URL
|
|---|
| 235 | * @param title Name of the layer as it will show up in the layer manager
|
|---|
| 236 | * @param url URL to the WMS server
|
|---|
| 237 | * @throws IllegalStateException if imagery time is neither HTML nor WMS
|
|---|
| 238 | */
|
|---|
| 239 | private static void addWMSLayer(String title, String url) {
|
|---|
| 240 | ImageryInfo info = new ImageryInfo(title, url);
|
|---|
| 241 | if (info.getImageryType() == ImageryType.WMS_ENDPOINT) {
|
|---|
| 242 | try {
|
|---|
| 243 | info = AddImageryLayerAction.getWMSLayerInfo(info);
|
|---|
| 244 | } catch (IOException | WMSGetCapabilitiesException e) {
|
|---|
| 245 | handleException(e);
|
|---|
| 246 | return;
|
|---|
| 247 | }
|
|---|
| 248 | }
|
|---|
| 249 | try {
|
|---|
| 250 | MainApplication.getLayerManager().addLayer(ImageryLayer.create(info));
|
|---|
| 251 | } catch (IllegalArgumentException e) {
|
|---|
| 252 | handleException(e);
|
|---|
| 253 | }
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | private static void handleException(Exception e) {
|
|---|
| 257 | Logging.error(e);
|
|---|
| 258 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(),
|
|---|
| 259 | e.getMessage(), tr("No valid WMS URL or id"), JOptionPane.ERROR_MESSAGE);
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | @Override
|
|---|
| 263 | protected boolean listenToSelectionChange() {
|
|---|
| 264 | return false;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | @Override
|
|---|
| 268 | protected void updateEnabledState() {
|
|---|
| 269 | setEnabled(!getLayerManager().getLayers().isEmpty());
|
|---|
| 270 | }
|
|---|
| 271 | }
|
|---|