Changeset 11216 in josm for trunk/src/org/openstreetmap/josm/data/imagery/WMTSTileSource.java
- Timestamp:
- 2016-11-04T09:58:50+01:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/imagery/WMTSTileSource.java
r11189 r11216 16 16 import java.util.List; 17 17 import java.util.Map; 18 import java.util.Map.Entry; 18 19 import java.util.Set; 19 20 import java.util.SortedSet; … … 23 24 import java.util.regex.Matcher; 24 25 import java.util.regex.Pattern; 26 import java.util.stream.Collectors; 25 27 26 28 import javax.swing.JPanel; … … 159 161 160 162 private static final class SelectLayerDialog extends ExtendedDialog { 161 private final transient L ayer[]layers;163 private final transient List<Entry<String,List<Layer>>> layers; 162 164 private final JTable list; 163 165 164 166 SelectLayerDialog(Collection<Layer> layers) { 165 167 super(Main.parent, tr("Select WMTS layer"), new String[]{tr("Add layers"), tr("Cancel")}); 166 this.layers = layers.toArray(new Layer[layers.size()]);168 this.layers = groupLayersByName(layers); 167 169 //getLayersTable(layers, Main.getProjection()) 168 170 this.list = new JTable( … … 172 174 switch (columnIndex) { 173 175 case 0: 174 return SelectLayerDialog.this.layers [rowIndex].name;176 return SelectLayerDialog.this.layers.get(rowIndex).getKey(); 175 177 case 1: 176 return SelectLayerDialog.this.layers[rowIndex].tileMatrixSet.crs; 178 return SelectLayerDialog.this.layers.get(rowIndex).getValue() 179 .stream() 180 .map(x -> x.tileMatrixSet.crs) 181 .collect(Collectors.joining(", ")); 177 182 case 2: 178 return SelectLayerDialog.this.layers[rowIndex].tileMatrixSet.identifier; 183 return SelectLayerDialog.this.layers.get(rowIndex).getValue() 184 .stream() 185 .map(x -> x.tileMatrixSet.identifier) 186 .collect(Collectors.joining(", ")); 179 187 default: 180 188 throw new IllegalArgumentException(); … … 184 192 @Override 185 193 public int getRowCount() { 186 return SelectLayerDialog.this.layers. length;194 return SelectLayerDialog.this.layers.size(); 187 195 } 188 196 … … 216 224 } 217 225 218 public Layer getSelectedLayer() { 226 private List<Entry<String, List<Layer>>> groupLayersByName(Collection<Layer> layers) { 227 Map<String, List<Layer>> layerByName = layers.stream().collect(Collectors.groupingBy(x -> x.name)); 228 return layerByName.entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(Collectors.toList()); 229 } 230 231 public String getSelectedLayer() { 219 232 int index = list.getSelectedRow(); 220 233 if (index < 0) { 221 234 return null; //nothing selected 222 235 } 223 return layers [index];236 return layers.get(index).getKey(); 224 237 } 225 238 } … … 234 247 private ScaleList nativeScaleList; 235 248 249 private String defaultLayer; 250 236 251 /** 237 252 * Creates a tile source based on imagery info … … 242 257 public WMTSTileSource(ImageryInfo info) throws IOException { 243 258 super(info); 259 CheckParameterUtil.ensureThat(info.getDefaultLayers().size() < 2, "At most 1 default layer for WMTS is supported"); 260 244 261 this.baseUrl = GetCapabilitiesParseHelper.normalizeCapabilitiesUrl(handleTemplate(info.getUrl())); 245 262 this.layers = getCapabilities(); 263 this.defaultLayer = info.getDefaultLayers().isEmpty() ? null : info.getDefaultLayers().iterator().next(); 246 264 if (this.layers.isEmpty()) 247 265 throw new IllegalArgumentException(tr("No layers defined by getCapabilities document: {0}", info.getUrl())); 248 266 } 249 267 250 private static Layer userSelectLayer(Collection<Layer> layers) { 251 if (layers.size() == 1) 252 return layers.iterator().next(); 253 Layer ret = null; 268 /** 269 * Creates a dialog based on this tile source with all available layers and returns the name of selected layer 270 * @return Name of selected layer 271 */ 272 public String userSelectLayer() { 273 Collection<String> layerNames = layers.stream().map(x -> x.name).collect(Collectors.toSet()); 274 275 // if there is only one layer name no point in asking 276 if (layerNames.size() == 1) 277 return layerNames.iterator().next(); 254 278 255 279 final SelectLayerDialog layerSelection = new SelectLayerDialog(layers); 256 280 if (layerSelection.showDialog().getValue() == 1) { 257 ret = layerSelection.getSelectedLayer(); 258 // TODO: save layer information into ImageryInfo / ImageryPreferences? 259 } 260 if (ret == null) { 261 // user canceled operation or did not choose any layer 262 throw new IllegalArgumentException(tr("No layer selected")); 263 } 264 return ret; 281 return layerSelection.getSelectedLayer(); 282 } 283 return null; 265 284 } 266 285 … … 536 555 // getLayers will return only layers matching the name, if the user already choose the layer 537 556 // so we will not ask the user again to chose the layer, if he just changes projection 538 Collection<Layer> candidates = getLayers(currentLayer != null ? currentLayer.name : null, proj.toCode()); 539 if (!candidates.isEmpty()) { 540 Layer newLayer = userSelectLayer(candidates); 557 Collection<Layer> candidates = getLayers(currentLayer != null ? currentLayer.name : defaultLayer, proj.toCode()); 558 if (candidates.size() == 1) { 559 560 Layer newLayer = candidates.iterator().next(); 541 561 if (newLayer != null) { 542 562 this.currentTileMatrixSet = newLayer.tileMatrixSet; … … 548 568 this.nativeScaleList = new ScaleList(scales); 549 569 } 570 } else if (candidates.size() > 1) { 571 Main.warn("More than one layer WMTS available: {0} for projection {1} and name {2}. Do not know which to process", 572 candidates.stream().map(x -> x.name + ": " + x.tileMatrixSet.identifier).collect(Collectors.joining(", ")), 573 proj.toCode(), 574 currentLayer != null ? currentLayer.name : defaultLayer 575 ); 550 576 } 551 577 this.crsScale = getTileSize() * 0.28e-03 / proj.getMetersPerUnit();
Note:
See TracChangeset
for help on using the changeset viewer.