Ignore:
Timestamp:
2018-05-12T14:18:57+02:00 (6 years ago)
Author:
wiktorn
Message:

Imagery definition refactor

Extend imagery definitions by:

  • allowing setting default layers for WMS_ENDPOINT and WMTS
  • allowing setting minimum expires time for tile for this imagery
  • allowing setting custom headers that will be sent for all requests

(get map, get capabilities) for this imagery

Additional changes in code:

  • use TileJobOptions to pass miscellaneous options to loaders
  • refactor WMSImagery to use SAX parser

See: #15981, #7953, #16224, #15940, #16249

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/AddImageryLayerAction.java

    r13388 r13733  
    1313import java.util.ArrayList;
    1414import java.util.Collection;
    15 import java.util.HashSet;
    1615import java.util.List;
    17 import java.util.Set;
     16import java.util.stream.Collectors;
    1817
    1918import javax.swing.JComboBox;
     
    2726import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
    2827import org.openstreetmap.josm.data.imagery.WMTSTileSource;
     28import org.openstreetmap.josm.data.imagery.WMTSTileSource.WMTSGetCapabilitiesException;
    2929import org.openstreetmap.josm.gui.ExtendedDialog;
    3030import org.openstreetmap.josm.gui.layer.AlignImageryPanel;
     
    3434import org.openstreetmap.josm.gui.util.GuiHelper;
    3535import org.openstreetmap.josm.io.imagery.WMSImagery;
    36 import org.openstreetmap.josm.io.imagery.WMSImagery.LayerDetails;
    3736import org.openstreetmap.josm.io.imagery.WMSImagery.WMSGetCapabilitiesException;
    3837import org.openstreetmap.josm.tools.CheckParameterUtil;
     
    9695            case WMS_ENDPOINT:
    9796                // 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                }
    99102            case WMTS:
    100103                // 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                }
    111117            default:
    112118                return info;
     
    130136            }
    131137            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);
    132144        }
    133145        return null;
     
    166178     */
    167179    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;
    209244    }
    210245
Note: See TracChangeset for help on using the changeset viewer.