Changeset 8752 in josm
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/imagery/WMTSTileSource.java
r8674 r8752 15 15 import java.util.Collection; 16 16 import java.util.Comparator; 17 import java.util.HashSet; 17 18 import java.util.Map; 18 19 import java.util.Set; … … 23 24 import java.util.regex.Pattern; 24 25 25 import javax.swing.JList;26 26 import javax.swing.JPanel; 27 import javax.swing.JTable; 27 28 import javax.swing.ListSelectionModel; 29 import javax.swing.table.AbstractTableModel; 28 30 import javax.xml.XMLConstants; 29 31 import javax.xml.namespace.QName; … … 98 100 private String format; 99 101 private String name; 100 private Map<String, TileMatrixSet> tileMatrixSetByCRS = new ConcurrentHashMap<>();102 private TileMatrixSet tileMatrixSet; 101 103 private String baseUrl; 102 104 private String style; … … 129 131 private static final class SelectLayerDialog extends ExtendedDialog { 130 132 private final Layer[] layers; 131 private final J List<String>list;133 private final JTable list; 132 134 133 135 public SelectLayerDialog(Collection<Layer> layers) { 134 136 super(Main.parent, tr("Select WMTS layer"), new String[]{tr("Add layers"), tr("Cancel")}); 135 137 this.layers = layers.toArray(new Layer[]{}); 136 this.list = new JList<>(getLayerNames(layers)); 138 //getLayersTable(layers, Main.getProjection()) 139 this.list = new JTable( 140 new AbstractTableModel() { 141 @Override 142 public Object getValueAt(int rowIndex, int columnIndex) { 143 switch (columnIndex) { 144 case 0: 145 return SelectLayerDialog.this.layers[rowIndex].name; 146 case 1: 147 return SelectLayerDialog.this.layers[rowIndex].tileMatrixSet.crs; 148 case 2: 149 return SelectLayerDialog.this.layers[rowIndex].tileMatrixSet.identifier; 150 default: 151 throw new IllegalArgumentException(); 152 } 153 } 154 155 @Override 156 public int getRowCount() { 157 return SelectLayerDialog.this.layers.length; 158 } 159 160 @Override 161 public int getColumnCount() { 162 return 3; 163 } 164 @Override 165 public String getColumnName(int column) { 166 switch (column) { 167 case 0: return tr("Layer name"); 168 case 1: return tr("Projection"); 169 case 2: return tr("Matrix set identifier"); 170 default: 171 throw new IllegalArgumentException(); 172 } 173 } 174 @Override 175 public boolean isCellEditable(int row, int column) { return false; } 176 }); 137 177 this.list.setPreferredSize(new Dimension(400, 400)); 138 178 this.list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 179 this.list.setRowSelectionAllowed(true); 180 this.list.setColumnSelectionAllowed(false); 139 181 JPanel panel = new JPanel(new GridBagLayout()); 140 182 panel.add(this.list, GBC.eol().fill()); … … 142 184 } 143 185 144 private static String[] getLayerNames(Collection<Layer> layers) {145 Collection<String> ret = new ArrayList<>();146 for (Layer layer: layers) {147 ret.add(layer.name);148 }149 return ret.toArray(new String[]{});150 }151 152 186 public Layer getSelectedLayer() { 153 int index = list.getSelected Index();187 int index = list.getSelectedRow(); 154 188 if (index < 0) { 155 189 return null; //nothing selected … … 175 209 this.baseUrl = normalizeCapabilitiesUrl(handleTemplate(info.getUrl())); 176 210 this.layers = getCapabilities(); 177 if (layers.size() > 1) { 178 final SelectLayerDialog layerSelection = new SelectLayerDialog(layers); 179 if (layerSelection.showDialog().getValue() == 1) { 180 this.currentLayer = layerSelection.getSelectedLayer(); 181 // TODO: save layer information into ImageryInfo / ImageryPreferences? 182 } 183 184 if (this.currentLayer == null) { 185 // user canceled operation or did not choose any layer 186 throw new IllegalArgumentException(tr("No layer selected")); 187 } 188 189 } else if (layers.size() == 1) { 190 this.currentLayer = this.layers.iterator().next(); 191 } else { 211 if (this.layers.isEmpty()) 192 212 throw new IllegalArgumentException(tr("No layers defined by getCapabilities document: {0}", info.getUrl())); 193 } 194 195 initProjection(); 213 214 // Not needed ? initProjection(); 215 } 216 217 private Layer userSelectLayer(Collection<Layer> layers) { 218 if (layers.size() == 1) 219 return layers.iterator().next(); 220 Layer ret = null; 221 222 final SelectLayerDialog layerSelection = new SelectLayerDialog(layers); 223 if (layerSelection.showDialog().getValue() == 1) { 224 ret = layerSelection.getSelectedLayer(); 225 // TODO: save layer information into ImageryInfo / ImageryPreferences? 226 } 227 if (ret == null) { 228 // user canceled operation or did not choose any layer 229 throw new IllegalArgumentException(tr("No layer selected")); 230 } 231 return ret; 196 232 } 197 233 … … 239 275 Map<String, TileMatrixSet> matrixSetById = parseMatrices(getByXpath(document, "/Capabilities/Contents/TileMatrixSet")); 240 276 return parseLayer(layersNodeList, matrixSetById); 241 242 277 } catch (Exception e) { 243 278 throw new IllegalArgumentException(e); … … 255 290 for (int layerId = 0; layerId < nodeList.getLength(); layerId++) { 256 291 Node layerNode = nodeList.item(layerId); 257 Layer layer = new Layer();258 layer.format = getStringByXpath(layerNode, "Format");259 layer.name = getStringByXpath(layerNode, "Identifier");260 layer.baseUrl = getStringByXpath(layerNode, "ResourceURL[@resourceType='tile']/@template");261 layer.style = getStringByXpath(layerNode, "Style[@isDefault='true']/Identifier");262 if (layer.style == null) {263 layer.style = "";264 }265 292 NodeList tileMatrixSetLinks = getByXpath(layerNode, "TileMatrixSetLink"); 293 294 // we add an layer for all matrix sets to allow user to choose, with which tileset he wants to work 266 295 for (int tileMatrixId = 0; tileMatrixId < tileMatrixSetLinks.getLength(); tileMatrixId++) { 296 Layer layer = new Layer(); 297 layer.format = getStringByXpath(layerNode, "Format"); 298 layer.name = getStringByXpath(layerNode, "Identifier"); 299 layer.baseUrl = getStringByXpath(layerNode, "ResourceURL[@resourceType='tile']/@template"); 300 layer.style = getStringByXpath(layerNode, "Style[@isDefault='true']/Identifier"); 301 if (layer.style == null) { 302 layer.style = ""; 303 } 267 304 Node tileMatrixLink = tileMatrixSetLinks.item(tileMatrixId); 268 305 TileMatrixSet tms = matrixSetById.get(getStringByXpath(tileMatrixLink, "TileMatrixSet")); 269 layer.tileMatrixSet ByCRS.put(tms.crs, tms);270 }271 ret.add(layer);306 layer.tileMatrixSet = tms; 307 ret.add(layer); 308 } 272 309 } 273 310 return ret; … … 357 394 */ 358 395 public void initProjection(Projection proj) { 359 this.currentTileMatrixSet = currentLayer.tileMatrixSetByCRS.get(proj.toCode()); 360 if (this.currentTileMatrixSet == null) { 361 Main.warn("Unsupported CRS selected"); 362 // take first, maybe it will work (if user sets custom projections, codes will not match) 363 this.currentTileMatrixSet = currentLayer.tileMatrixSetByCRS.values().iterator().next(); 364 } 396 String layerName = null; 397 if (currentLayer != null) { 398 layerName = currentLayer.name; 399 } 400 Collection<Layer> candidates = getLayers(layerName, proj.toCode()); 401 if (!candidates.isEmpty()) { 402 Layer newLayer = userSelectLayer(candidates); 403 if (newLayer != null) { 404 this.currentTileMatrixSet = newLayer.tileMatrixSet; 405 this.currentLayer = newLayer; 406 } 407 } 408 365 409 this.crsScale = getTileSize() * 0.28e-03 / proj.getMetersPerUnit(); 410 } 411 412 private Collection<Layer> getLayers(String name, String projectionCode) { 413 Collection<Layer> ret = new ArrayList<>(); 414 for (Layer layer: this.layers) { 415 if ((name == null || name.equals(layer.name)) && (projectionCode == null || projectionCode.equals(layer.tileMatrixSet.crs))) { 416 ret.add(layer); 417 } 418 } 419 return ret; 366 420 } 367 421 … … 385 439 public String getTileUrl(int zoom, int tilex, int tiley) { 386 440 String url; 441 if (currentLayer == null) { 442 return ""; 443 } 444 387 445 switch (transferMode) { 388 446 case KVP: … … 619 677 */ 620 678 public Set<String> getSupportedProjections() { 621 return this.currentLayer.tileMatrixSetByCRS.keySet(); 679 Set<String> ret = new HashSet<>(); 680 if (currentLayer == null) { 681 for(Layer layer: this.layers) { 682 ret.add(layer.tileMatrixSet.crs); 683 } 684 } else { 685 for(Layer layer: this.layers) { 686 if (currentLayer.name.equals(layer.name)) { 687 ret.add(layer.tileMatrixSet.crs); 688 } 689 } 690 } 691 return ret; 622 692 } 623 693 -
trunk/src/org/openstreetmap/josm/gui/layer/WMTSLayer.java
r8624 r8752 3 3 4 4 import java.io.IOException; 5 import java.util.Set; 5 6 6 7 import org.apache.commons.jcs.access.CacheAccess; … … 95 96 @Override 96 97 public boolean isProjectionSupported(Projection proj) { 97 return ((WMTSTileSource) tileSource).getSupportedProjections().contains(proj.toCode()); 98 Set<String> supportedProjections = ((WMTSTileSource) tileSource).getSupportedProjections(); 99 return supportedProjections.contains(proj.toCode()); 98 100 } 99 101 … … 104 106 ret.append(e).append(", "); 105 107 } 106 return ret. substring(0, ret.length()-2);108 return ret.length() > 2 ? ret.substring(0, ret.length()-2) : ret.toString(); 107 109 } 108 110 -
trunk/test/unit/org/openstreetmap/josm/data/imagery/WMTSTileSourceTest.java
r8624 r8752 26 26 private ImageryInfo testImageryWIEN = getImagery("test/data/wmts/getCapabilities-wien.xml"); 27 27 private ImageryInfo testImageryWALLONIE = getImagery("test/data/wmts/WMTSCapabilities-Wallonie.xml"); 28 private ImageryInfo testImageryOntario = getImagery("test/data/wmts/WMTSCapabilities-Ontario.xml"); 28 29 29 30 @BeforeClass … … 48 49 Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); 49 50 WMTSTileSource testSource = new WMTSTileSource(testImageryPSEUDO_MERCATOR); 51 testSource.initProjection(); 50 52 51 53 verifyMercatorTile(testSource, 0, 0, 1); … … 78 80 Main.setProjection(Projections.getProjectionByCode("EPSG:31370")); 79 81 WMTSTileSource testSource = new WMTSTileSource(testImageryWALLONIE); 82 testSource.initProjection(); 83 80 84 assertEquals("http://geoservices.wallonie.be/arcgis/rest/services/DONNEES_BASE/FOND_PLAN_ANNOTATIONS_2012_RW_NB/" 81 85 + "MapServer/WMTS/tile/1.0.0/DONNEES_BASE_FOND_PLAN_ANNOTATIONS_2012_RW_NB/default/default028mm/5/1219/1063.png", … … 95 99 Main.setProjection(Projections.getProjectionByCode("EPSG:31370")); 96 100 WMTSTileSource testSource = new WMTSTileSource(getImagery("test/data/wmts/WMTSCapabilities-Wallonie-nomatrixdimension.xml")); 101 testSource.initProjection(); 102 97 103 Bounds wallonieBounds = new Bounds( 98 104 new LatLon(49.485372459967245, 2.840548314430268), … … 117 123 Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); 118 124 WMTSTileSource testSource = new WMTSTileSource(testImageryWIEN); 125 testSource.initProjection(); 119 126 int zoomOffset = 9; 120 127 … … 158 165 Main.setProjection(Projections.getProjectionByCode("EPSG:4326")); 159 166 WMTSTileSource testSource = new WMTSTileSource(testImageryTOPO_PL); 167 testSource.initProjection(); 160 168 verifyTile(new LatLon(56, 12), testSource, 0, 0, 1); 161 169 verifyTile(new LatLon(56, 12), testSource, 0, 0, 2); … … 179 187 Main.setProjection(Projections.getProjectionByCode("EPSG:4326")); 180 188 WMTSTileSource testSource = new WMTSTileSource(testImageryORTO_PL); 189 testSource.initProjection(); 181 190 verifyTile(new LatLon(53.5993712684958, 19.560669777688176), testSource, 12412, 3941, 14); 182 191 verifyTile(new LatLon(49.783096954497786, 22.79034127751704), testSource, 17714, 10206, 14); … … 187 196 Main.setProjection(Projections.getProjectionByCode("EPSG:2180")); 188 197 WMTSTileSource testSource = new WMTSTileSource(testImageryORTO_PL); 198 testSource.initProjection(); 189 199 190 200 verifyTile(new LatLon(53.59940948387726, 19.560544913270064), testSource, 6453, 3140, 14); 191 201 verifyTile(new LatLon(49.782984840526055, 22.790064966993445), testSource, 9932, 9305, 14); 202 } 203 204 // disabled as this needs user action 205 // @Test 206 public void testTwoTileSetsForOneProjection() throws Exception { 207 Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); 208 WMTSTileSource testSource = new WMTSTileSource(testImageryOntario); 209 testSource.initProjection(); 210 verifyTile(new LatLon(45.4105023, -75.7153702), testSource, 303751, 375502, 12); 211 verifyTile(new LatLon(45.4601306, -75.7617187), testSource, 1186, 1466, 4); 212 192 213 } 193 214
Note:
See TracChangeset
for help on using the changeset viewer.