Changeset 13733 in josm for trunk/src/org/openstreetmap/josm/actions
- Timestamp:
- 2018-05-12T14:18:57+02:00 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/AddImageryLayerAction.java
r13388 r13733 13 13 import java.util.ArrayList; 14 14 import java.util.Collection; 15 import java.util.HashSet;16 15 import java.util.List; 17 import java.util. Set;16 import java.util.stream.Collectors; 18 17 19 18 import javax.swing.JComboBox; … … 27 26 import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType; 28 27 import org.openstreetmap.josm.data.imagery.WMTSTileSource; 28 import org.openstreetmap.josm.data.imagery.WMTSTileSource.WMTSGetCapabilitiesException; 29 29 import org.openstreetmap.josm.gui.ExtendedDialog; 30 30 import org.openstreetmap.josm.gui.layer.AlignImageryPanel; … … 34 34 import org.openstreetmap.josm.gui.util.GuiHelper; 35 35 import org.openstreetmap.josm.io.imagery.WMSImagery; 36 import org.openstreetmap.josm.io.imagery.WMSImagery.LayerDetails;37 36 import org.openstreetmap.josm.io.imagery.WMSImagery.WMSGetCapabilitiesException; 38 37 import org.openstreetmap.josm.tools.CheckParameterUtil; … … 96 95 case WMS_ENDPOINT: 97 96 // convert to WMS type 98 return getWMSLayerInfo(info); 97 if (info.getDefaultLayers() == null || info.getDefaultLayers().isEmpty()) { 98 return getWMSLayerInfo(info); 99 } else { 100 return info; 101 } 99 102 case WMTS: 100 103 // specify which layer to use 101 DefaultLayer layerId = new WMTSTileSource(info).userSelectLayer(); 102 if (layerId != null) { 103 ImageryInfo copy = new ImageryInfo(info); 104 Collection<DefaultLayer> defaultLayers = new ArrayList<>(1); 105 defaultLayers.add(layerId); 106 copy.setDefaultLayers(defaultLayers); 107 return copy; 108 } 109 // layer not selected - refuse to add 110 return null; 104 if (info.getDefaultLayers() == null || info.getDefaultLayers().isEmpty()) { 105 DefaultLayer layerId = new WMTSTileSource(info).userSelectLayer(); 106 if (layerId != null) { 107 ImageryInfo copy = new ImageryInfo(info); 108 List<DefaultLayer> defaultLayers = new ArrayList<>(1); 109 defaultLayers.add(layerId); 110 copy.setDefaultLayers(defaultLayers); 111 return copy; 112 } 113 return null; 114 } else { 115 return info; 116 } 111 117 default: 112 118 return info; … … 130 136 } 131 137 Logging.log(Logging.LEVEL_ERROR, "Could not parse WMS layer list. Incoming data:\n"+ex.getIncomingData(), ex); 138 } catch (WMTSGetCapabilitiesException e) { 139 if (!GraphicsEnvironment.isHeadless()) { 140 JOptionPane.showMessageDialog(Main.parent, tr("Could not parse WMTS layer list."), 141 tr("WMS Error"), JOptionPane.ERROR_MESSAGE); 142 } 143 Logging.log(Logging.LEVEL_ERROR, "Could not parse WMTS layer list.", e); 132 144 } 133 145 return null; … … 166 178 */ 167 179 protected static ImageryInfo getWMSLayerInfo(ImageryInfo info) throws IOException, WMSGetCapabilitiesException { 168 CheckParameterUtil.ensureThat(ImageryType.WMS_ENDPOINT.equals(info.getImageryType()), "wms_endpoint imagery type expected"); 169 170 final WMSImagery wms = new WMSImagery(); 171 wms.attemptGetCapabilities(info.getUrl()); 172 173 final WMSLayerTree tree = new WMSLayerTree(); 174 tree.updateTree(wms); 175 List<String> wmsFormats = wms.getFormats(); 176 final JComboBox<String> formats = new JComboBox<>(wmsFormats.toArray(new String[0])); 177 formats.setSelectedItem(wms.getPreferredFormats()); 178 formats.setToolTipText(tr("Select image format for WMS layer")); 179 180 if (!GraphicsEnvironment.isHeadless() && 1 != new SelectWmsLayersDialog(tree, formats).showDialog().getValue()) { 181 return null; 182 } 183 184 final String url = wms.buildGetMapUrl( 185 tree.getSelectedLayers(), (String) formats.getSelectedItem()); 186 Set<String> supportedCrs = new HashSet<>(); 187 boolean first = true; 188 StringBuilder layersString = new StringBuilder(); 189 for (LayerDetails layer: tree.getSelectedLayers()) { 190 if (first) { 191 supportedCrs.addAll(layer.getProjections()); 192 first = false; 193 } 194 layersString.append(layer.name); 195 layersString.append(", "); 196 supportedCrs.retainAll(layer.getProjections()); 197 } 198 199 // copy all information from WMS 200 ImageryInfo ret = new ImageryInfo(info); 201 // and update according to user choice 202 ret.setUrl(url); 203 ret.setImageryType(ImageryType.WMS); 204 if (layersString.length() > 2) { 205 ret.setName(ret.getName() + ' ' + layersString.substring(0, layersString.length() - 2)); 206 } 207 ret.setServerProjections(supportedCrs); 208 return ret; 180 try { 181 CheckParameterUtil.ensureThat(ImageryType.WMS_ENDPOINT.equals(info.getImageryType()), "wms_endpoint imagery type expected"); 182 final WMSImagery wms = new WMSImagery(info.getUrl()); 183 184 final WMSLayerTree tree = new WMSLayerTree(); 185 tree.updateTree(wms); 186 187 Collection<String> wmsFormats = wms.getFormats(); 188 final JComboBox<String> formats = new JComboBox<>(wmsFormats.toArray(new String[wmsFormats.size()])); 189 formats.setSelectedItem(wms.getPreferredFormat()); 190 formats.setToolTipText(tr("Select image format for WMS layer")); 191 192 if (!GraphicsEnvironment.isHeadless()) { 193 if (1 != new ExtendedDialog(Main.parent, tr("Select WMS layers"), new String[]{tr("Add layers"), tr("Cancel")}) { { 194 final JScrollPane scrollPane = new JScrollPane(tree.getLayerTree()); 195 scrollPane.setPreferredSize(new Dimension(400, 400)); 196 final JPanel panel = new JPanel(new GridBagLayout()); 197 panel.add(scrollPane, GBC.eol().fill()); 198 panel.add(formats, GBC.eol().fill(GBC.HORIZONTAL)); 199 setContent(panel); 200 } }.showDialog().getValue()) { 201 return null; 202 } 203 } 204 205 final String url = wms.buildGetMapUrl( 206 tree.getSelectedLayers().stream().map(x -> x.getName()).collect(Collectors.toList()), 207 (List<String>) null, 208 (String) formats.getSelectedItem(), 209 true // TODO: ask the user if (s)he wants transparent layer 210 ); 211 212 String selectedLayers = tree.getSelectedLayers().stream() 213 .map(x -> x.getName()) 214 .collect(Collectors.joining(", ")); 215 ImageryInfo ret = new ImageryInfo(info.getName() + selectedLayers, 216 url, 217 "wms", 218 info.getEulaAcceptanceRequired(), 219 info.getCookies()); 220 221 ret.setServerProjections(wms.getServerProjections(tree.getSelectedLayers())); 222 223 return ret; 224 } catch (MalformedURLException ex) { 225 if (!GraphicsEnvironment.isHeadless()) { 226 JOptionPane.showMessageDialog(Main.parent, tr("Invalid service URL."), 227 tr("WMS Error"), JOptionPane.ERROR_MESSAGE); 228 } 229 Logging.log(Logging.LEVEL_ERROR, ex); 230 } catch (IOException ex) { 231 if (!GraphicsEnvironment.isHeadless()) { 232 JOptionPane.showMessageDialog(Main.parent, tr("Could not retrieve WMS layer list."), 233 tr("WMS Error"), JOptionPane.ERROR_MESSAGE); 234 } 235 Logging.log(Logging.LEVEL_ERROR, ex); 236 } catch (WMSGetCapabilitiesException ex) { 237 if (!GraphicsEnvironment.isHeadless()) { 238 JOptionPane.showMessageDialog(Main.parent, tr("Could not parse WMS layer list."), 239 tr("WMS Error"), JOptionPane.ERROR_MESSAGE); 240 } 241 Logging.log(Logging.LEVEL_ERROR, "Could not parse WMS layer list. Incoming data:\n"+ex.getIncomingData(), ex); 242 } 243 return null; 209 244 } 210 245
Note:
See TracChangeset
for help on using the changeset viewer.