Index: trunk/src/org/openstreetmap/josm/actions/AddImageryLayerAction.java
===================================================================
--- trunk/src/org/openstreetmap/josm/actions/AddImageryLayerAction.java	(revision 11256)
+++ trunk/src/org/openstreetmap/josm/actions/AddImageryLayerAction.java	(revision 11257)
@@ -23,4 +23,5 @@
 
 import org.openstreetmap.josm.Main;
+import org.openstreetmap.josm.data.imagery.DefaultLayer;
 import org.openstreetmap.josm.data.imagery.ImageryInfo;
 import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
@@ -82,8 +83,8 @@
             case WMTS:
                 // specify which layer to use
-                String layerId = new WMTSTileSource(info).userSelectLayer();
+                DefaultLayer layerId = new WMTSTileSource(info).userSelectLayer();
                 if (layerId != null) {
                     ImageryInfo copy = new ImageryInfo(info);
-                    Collection<String> defaultLayers = new ArrayList<>(1);
+                    Collection<DefaultLayer> defaultLayers = new ArrayList<>(1);
                     defaultLayers.add(layerId);
                     copy.setDefaultLayers(defaultLayers);
Index: trunk/src/org/openstreetmap/josm/data/imagery/DefaultLayer.java
===================================================================
--- trunk/src/org/openstreetmap/josm/data/imagery/DefaultLayer.java	(revision 11257)
+++ trunk/src/org/openstreetmap/josm/data/imagery/DefaultLayer.java	(revision 11257)
@@ -0,0 +1,32 @@
+// License: GPL. For details, see LICENSE file.
+package org.openstreetmap.josm.data.imagery;
+
+/**
+ *
+ * Simple class representing default layer that might be set in imagery information
+ *
+ * This simple class is needed - as for WMS there is different information needed to specify layer than for WMTS
+ *
+ * @author Wiktor Niesiobedzki
+ *
+ */
+public class DefaultLayer {
+
+    protected String layerName;
+
+    /**
+     * Constructor
+     * @param layerName that is the DefaultLayer
+     */
+    public DefaultLayer(String layerName) {
+        this.layerName = layerName;
+    }
+
+    /**
+     * @return layer name of the default layer
+     */
+    public String getLayerName() {
+        return layerName;
+    }
+
+}
Index: trunk/src/org/openstreetmap/josm/data/imagery/ImageryInfo.java
===================================================================
--- trunk/src/org/openstreetmap/josm/data/imagery/ImageryInfo.java	(revision 11256)
+++ trunk/src/org/openstreetmap/josm/data/imagery/ImageryInfo.java	(revision 11257)
@@ -195,5 +195,5 @@
     private boolean isEpsg4326To3857Supported;
     /** which layers should be activated by default on layer addition. **/
-    private Collection<String> defaultLayers = Collections.emptyList();
+    private Collection<DefaultLayer> defaultLayers = Collections.emptyList();
     // when adding a field, also adapt the ImageryInfo(ImageryInfo)
     // and ImageryInfo(ImageryPreferenceEntry) constructor, equals method, and ImageryPreferenceEntry
@@ -1156,5 +1156,5 @@
      * @return Collection of the layer names
      */
-    public Collection<String> getDefaultLayers() {
+    public Collection<DefaultLayer> getDefaultLayers() {
         return defaultLayers;
     }
@@ -1164,5 +1164,10 @@
      * @param layers set the list of default layers
      */
-    public void setDefaultLayers(Collection<String> layers) {
+    public void setDefaultLayers(Collection<DefaultLayer> layers) {
+        if (ImageryType.WMTS.equals(this.imageryType)) {
+            CheckParameterUtil.ensureThat(layers == null ||
+                    layers.isEmpty() ||
+                    layers.iterator().next() instanceof WMTSDefaultLayer, "Incorrect default layer");
+        }
         this.defaultLayers = layers;
     }
Index: trunk/src/org/openstreetmap/josm/data/imagery/WMTSDefaultLayer.java
===================================================================
--- trunk/src/org/openstreetmap/josm/data/imagery/WMTSDefaultLayer.java	(revision 11257)
+++ trunk/src/org/openstreetmap/josm/data/imagery/WMTSDefaultLayer.java	(revision 11257)
@@ -0,0 +1,15 @@
+// License: GPL. For details, see LICENSE file.
+package org.openstreetmap.josm.data.imagery;
+
+public class WMTSDefaultLayer extends DefaultLayer {
+    String tileMatrixSet;
+
+    public WMTSDefaultLayer(String layerName, String tileMatrixSet) {
+        super(layerName);
+        this.tileMatrixSet = tileMatrixSet;
+    }
+
+    public String getTileMatrixSet() {
+        return tileMatrixSet;
+    }
+}
Index: trunk/src/org/openstreetmap/josm/data/imagery/WMTSTileSource.java
===================================================================
--- trunk/src/org/openstreetmap/josm/data/imagery/WMTSTileSource.java	(revision 11256)
+++ trunk/src/org/openstreetmap/josm/data/imagery/WMTSTileSource.java	(revision 11257)
@@ -174,5 +174,8 @@
                             switch (columnIndex) {
                             case 0:
-                                return SelectLayerDialog.this.layers.get(rowIndex).getKey();
+                                return SelectLayerDialog.this.layers.get(rowIndex).getValue()
+                                        .stream()
+                                        .map(x -> x.name)
+                                        .collect(Collectors.joining(", ")); //this should be only one
                             case 1:
                                 return SelectLayerDialog.this.layers.get(rowIndex).getValue()
@@ -184,5 +187,5 @@
                                         .stream()
                                         .map(x -> x.tileMatrixSet.identifier)
-                                        .collect(Collectors.joining(", "));
+                                        .collect(Collectors.joining(", ")); //this should be only one
                             default:
                                 throw new IllegalArgumentException();
@@ -224,15 +227,11 @@
         }
 
-        private static List<Entry<String, List<Layer>>> groupLayersByName(Collection<Layer> layers) {
-            Map<String, List<Layer>> layerByName = layers.stream().collect(Collectors.groupingBy(x -> x.name));
-            return layerByName.entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(Collectors.toList());
-        }
-
-        public String getSelectedLayer() {
+        public DefaultLayer getSelectedLayer() {
             int index = list.getSelectedRow();
             if (index < 0) {
                 return null; //nothing selected
             }
-            return layers.get(index).getKey();
+            Layer selectedLayer = layers.get(index).getValue().iterator().next();
+            return new WMTSDefaultLayer(selectedLayer.name, selectedLayer.tileMatrixSet.identifier);
         }
     }
@@ -247,5 +246,6 @@
     private ScaleList nativeScaleList;
 
-    private final String defaultLayer;
+    private final WMTSDefaultLayer defaultLayer;
+
 
     /**
@@ -261,5 +261,5 @@
         this.baseUrl = GetCapabilitiesParseHelper.normalizeCapabilitiesUrl(handleTemplate(info.getUrl()));
         this.layers = getCapabilities();
-        this.defaultLayer = info.getDefaultLayers().isEmpty() ? null : info.getDefaultLayers().iterator().next();
+        this.defaultLayer = info.getDefaultLayers().isEmpty() ? null : (WMTSDefaultLayer) info.getDefaultLayers().iterator().next();
         if (this.layers.isEmpty())
             throw new IllegalArgumentException(tr("No layers defined by getCapabilities document: {0}", info.getUrl()));
@@ -270,10 +270,12 @@
      * @return Name of selected layer
      */
-    public String userSelectLayer() {
-        Collection<String> layerNames = layers.stream().map(x -> x.name).collect(Collectors.toSet());
+    public DefaultLayer userSelectLayer() {
+        Collection<Entry<String, List<Layer>>> grouppedLayers = groupLayersByName(layers);;
 
         // if there is only one layer name no point in asking
-        if (layerNames.size() == 1)
-            return layerNames.iterator().next();
+        if (grouppedLayers.size() == 1) {
+            Layer selectedLayer = grouppedLayers.iterator().next().getValue().iterator().next();
+            return new WMTSDefaultLayer(selectedLayer.name, selectedLayer.tileMatrixSet.identifier);
+        }
 
         final SelectLayerDialog layerSelection = new SelectLayerDialog(layers);
@@ -294,4 +296,10 @@
         matcher.appendTail(output);
         return output.toString();
+    }
+
+    private static List<Entry<String, List<Layer>>> groupLayersByName(Collection<Layer> layers) {
+        Map<String, List<Layer>> layerByName = layers.stream().collect(
+                Collectors.groupingBy(x -> x.name + '\u001c' + x.tileMatrixSet.identifier));
+        return layerByName.entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(Collectors.toList());
     }
 
@@ -555,7 +563,9 @@
         // getLayers will return only layers matching the name, if the user already choose the layer
         // so we will not ask the user again to chose the layer, if he just changes projection
-        Collection<Layer> candidates = getLayers(currentLayer != null ? currentLayer.name : defaultLayer, proj.toCode());
+        Collection<Layer> candidates = getLayers(
+                currentLayer != null ? new WMTSDefaultLayer(currentLayer.name, currentLayer.tileMatrixSet.identifier) : defaultLayer,
+                proj.toCode());
+
         if (candidates.size() == 1) {
-
             Layer newLayer = candidates.iterator().next();
             if (newLayer != null) {
@@ -580,13 +590,17 @@
     /**
      *
-     * @param name of the layer to match
+     * @param searchLayer which layer do we look for
      * @param projectionCode projection code to match
      * @return Collection of layers matching the name of the layer and projection, or only projection if name is not provided
      */
-    private Collection<Layer> getLayers(String name, String projectionCode) {
+    private Collection<Layer> getLayers(WMTSDefaultLayer searchLayer, String projectionCode) {
         Collection<Layer> ret = new ArrayList<>();
         if (this.layers != null) {
             for (Layer layer: this.layers) {
-                if ((name == null || name.equals(layer.name)) && (projectionCode == null || projectionCode.equals(layer.tileMatrixSet.crs))) {
+                if ((searchLayer == null || (// if it's null, then accept all layers
+                        searchLayer.getLayerName().equals(layer.name) &&
+                        searchLayer.getTileMatrixSet().equals(layer.tileMatrixSet.identifier)))
+                        && (projectionCode == null || // if it's null, then accept any projection
+                        projectionCode.equals(layer.tileMatrixSet.crs))) {
                     ret.add(layer);
                 }
Index: trunk/test/data/wmts/bug13975-multiple-tile-matrices-for-one-layer-projection.xml
===================================================================
--- trunk/test/data/wmts/bug13975-multiple-tile-matrices-for-one-layer-projection.xml	(revision 11257)
+++ trunk/test/data/wmts/bug13975-multiple-tile-matrices-for-one-layer-projection.xml	(revision 11257)
@@ -0,0 +1,377 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Capabilities xmlns="http://www.opengis.net/wmts/1.0" xmlns:ows="http://www.opengis.net/ows/1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gml="http://www.opengis.net/gml" xsi:schemaLocation="http://www.opengis.net/wmts/1.0 http://schemas.opengis.net/wmts/1.0/wmtsGetCapabilities_response.xsd" version="1.0.0">
+<!-- Service Identification --> <ows:ServiceIdentification>
+<ows:Title>Mashhad_BaseMap_1</ows:Title>
+<ows:ServiceType>OGC WMTS</ows:ServiceType>
+<ows:ServiceTypeVersion>1.0.0</ows:ServiceTypeVersion>
+</ows:ServiceIdentification> <!-- Operations Metadata --> <ows:OperationsMetadata>
+<ows:Operation name="GetCapabilities">
+<ows:DCP>
+<ows:HTTP>
+<ows:Get xlink:href="http://188.253.0.155:6080/arcgis/rest/services/Mashhad_BaseMap_1/MapServer/WMTS/1.0.0/WMTSCapabilities.xml">
+<ows:Constraint name="GetEncoding">
+<ows:AllowedValues>
+<ows:Value>RESTful</ows:Value>
+</ows:AllowedValues>
+</ows:Constraint>
+</ows:Get>
+<!-- add KVP binding in 10.1 -->
+<ows:Get xlink:href="http://188.253.0.155:6080/arcgis/rest/services/Mashhad_BaseMap_1/MapServer/WMTS?">
+<ows:Constraint name="GetEncoding">
+<ows:AllowedValues>
+<ows:Value>KVP</ows:Value>
+</ows:AllowedValues>
+</ows:Constraint>
+</ows:Get>
+</ows:HTTP>
+</ows:DCP>
+</ows:Operation>
+<ows:Operation name="GetTile">
+<ows:DCP>
+<ows:HTTP>
+<ows:Get xlink:href="http://188.253.0.155:6080/arcgis/rest/services/Mashhad_BaseMap_1/MapServer/WMTS/tile/1.0.0/">
+<ows:Constraint name="GetEncoding">
+<ows:AllowedValues>
+<ows:Value>RESTful</ows:Value>
+</ows:AllowedValues>
+</ows:Constraint>
+</ows:Get>
+<ows:Get xlink:href="http://188.253.0.155:6080/arcgis/rest/services/Mashhad_BaseMap_1/MapServer/WMTS?">
+<ows:Constraint name="GetEncoding">
+<ows:AllowedValues>
+<ows:Value>KVP</ows:Value>
+</ows:AllowedValues>
+</ows:Constraint>
+</ows:Get>
+</ows:HTTP>
+</ows:DCP>
+</ows:Operation>
+</ows:OperationsMetadata> <Contents>
+<!--Layer--> <Layer>
+<ows:Title>Mashhad_BaseMap_1</ows:Title> <ows:Identifier>Mashhad_BaseMap_1</ows:Identifier>
+<ows:BoundingBox crs="urn:ogc:def:crs:EPSG::3857">
+<ows:LowerCorner>6588340.738203676 4304495.929392335</ows:LowerCorner>
+<ows:UpperCorner>6678536.942362351 4378339.757654444</ows:UpperCorner>
+</ows:BoundingBox> <ows:WGS84BoundingBox crs="urn:ogc:def:crs:OGC:2:84">
+<ows:LowerCorner>59.184071821156536 36.028153408122506</ows:LowerCorner>
+<ows:UpperCorner>59.99431810880956 36.56279324540633</ows:UpperCorner>
+</ows:WGS84BoundingBox>
+<Style isDefault="true">
+<ows:Title>Default Style</ows:Title>
+<ows:Identifier>default</ows:Identifier>
+</Style>
+<Format>image/jpgpng</Format>
+<TileMatrixSetLink>
+<TileMatrixSet>default028mm</TileMatrixSet>
+</TileMatrixSetLink>
+<TileMatrixSetLink>
+<!--Only show this TileMatrixSet if the tiling scheme is compliant to Google Maps (and that happens with tile width = 256 px)-->
+<TileMatrixSet>GoogleMapsCompatible</TileMatrixSet>
+</TileMatrixSetLink>
+<ResourceURL format="image/jpgpng" resourceType="tile" template="http://188.253.0.155:6080/arcgis/rest/services/Mashhad_BaseMap_1/MapServer/WMTS/tile/1.0.0/Mashhad_BaseMap_1/{Style}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}"/>
+</Layer> <!--TileMatrixSet-->
+<TileMatrixSet>
+<ows:Title>TileMatrix using 0.28mm</ows:Title>
+<ows:Abstract>The tile matrix set that has scale values calculated based on the dpi defined by OGC specification (dpi assumes 0.28mm as the physical distance of a pixel).</ows:Abstract> <ows:Identifier>default028mm</ows:Identifier>
+<ows:SupportedCRS>urn:ogc:def:crs:EPSG::3857</ows:SupportedCRS>
+<TileMatrix>
+<ows:Identifier>0</ows:Identifier>
+<ScaleDenominator>5.590822640285016E8</ScaleDenominator>
+<TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner> <TileWidth>256</TileWidth> <TileHeight>256</TileHeight>
+<MatrixWidth>1</MatrixWidth> <MatrixHeight>1</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>1</ows:Identifier>
+<ScaleDenominator>2.7954113201425034E8</ScaleDenominator>
+<TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner> <TileWidth>256</TileWidth> <TileHeight>256</TileHeight>
+<MatrixWidth>2</MatrixWidth> <MatrixHeight>1</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>2</ows:Identifier>
+<ScaleDenominator>1.3977056600712562E8</ScaleDenominator>
+<TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner> <TileWidth>256</TileWidth> <TileHeight>256</TileHeight>
+<MatrixWidth>3</MatrixWidth> <MatrixHeight>2</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>3</ows:Identifier>
+<ScaleDenominator>6.988528300356235E7</ScaleDenominator>
+<TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner> <TileWidth>256</TileWidth> <TileHeight>256</TileHeight>
+<MatrixWidth>6</MatrixWidth> <MatrixHeight>4</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>4</ows:Identifier>
+<ScaleDenominator>3.494264150178117E7</ScaleDenominator>
+<TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner> <TileWidth>256</TileWidth> <TileHeight>256</TileHeight>
+<MatrixWidth>11</MatrixWidth> <MatrixHeight>7</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>5</ows:Identifier>
+<ScaleDenominator>1.7471320750890587E7</ScaleDenominator>
+<TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner> <TileWidth>256</TileWidth> <TileHeight>256</TileHeight>
+<MatrixWidth>22</MatrixWidth> <MatrixHeight>13</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>6</ows:Identifier>
+<ScaleDenominator>8735660.375445293</ScaleDenominator>
+<TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner> <TileWidth>256</TileWidth> <TileHeight>256</TileHeight>
+<MatrixWidth>43</MatrixWidth> <MatrixHeight>26</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>7</ows:Identifier>
+<ScaleDenominator>4367830.187722647</ScaleDenominator>
+<TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner> <TileWidth>256</TileWidth> <TileHeight>256</TileHeight>
+<MatrixWidth>86</MatrixWidth> <MatrixHeight>51</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>8</ows:Identifier>
+<ScaleDenominator>2183915.0938617955</ScaleDenominator>
+<TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner> <TileWidth>256</TileWidth> <TileHeight>256</TileHeight>
+<MatrixWidth>171</MatrixWidth> <MatrixHeight>101</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>9</ows:Identifier>
+<ScaleDenominator>1091957.5469304253</ScaleDenominator>
+<TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner> <TileWidth>256</TileWidth> <TileHeight>256</TileHeight>
+<MatrixWidth>342</MatrixWidth> <MatrixHeight>202</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>10</ows:Identifier>
+<ScaleDenominator>545978.7734656851</ScaleDenominator>
+<TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner> <TileWidth>256</TileWidth> <TileHeight>256</TileHeight>
+<MatrixWidth>683</MatrixWidth> <MatrixHeight>403</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>11</ows:Identifier>
+<ScaleDenominator>272989.38673237007</ScaleDenominator>
+<TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner> <TileWidth>256</TileWidth> <TileHeight>256</TileHeight>
+<MatrixWidth>1366</MatrixWidth> <MatrixHeight>805</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>12</ows:Identifier>
+<ScaleDenominator>136494.69336618503</ScaleDenominator>
+<TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner> <TileWidth>256</TileWidth> <TileHeight>256</TileHeight>
+<MatrixWidth>2731</MatrixWidth> <MatrixHeight>1609</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>13</ows:Identifier>
+<ScaleDenominator>68247.34668309252</ScaleDenominator>
+<TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner> <TileWidth>256</TileWidth> <TileHeight>256</TileHeight>
+<MatrixWidth>5462</MatrixWidth> <MatrixHeight>3217</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>14</ows:Identifier>
+<ScaleDenominator>34123.67334154626</ScaleDenominator>
+<TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner> <TileWidth>256</TileWidth> <TileHeight>256</TileHeight>
+<MatrixWidth>10923</MatrixWidth> <MatrixHeight>6433</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>15</ows:Identifier>
+<ScaleDenominator>17061.836671245605</ScaleDenominator>
+<TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner> <TileWidth>256</TileWidth> <TileHeight>256</TileHeight>
+<MatrixWidth>21845</MatrixWidth> <MatrixHeight>12865</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>16</ows:Identifier>
+<ScaleDenominator>8530.918335622802</ScaleDenominator>
+<TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner> <TileWidth>256</TileWidth> <TileHeight>256</TileHeight>
+<MatrixWidth>43690</MatrixWidth> <MatrixHeight>25729</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>17</ows:Identifier>
+<ScaleDenominator>4265.459167338929</ScaleDenominator>
+<TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner> <TileWidth>256</TileWidth> <TileHeight>256</TileHeight>
+<MatrixWidth>87380</MatrixWidth> <MatrixHeight>51458</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>18</ows:Identifier>
+<ScaleDenominator>2132.729584141936</ScaleDenominator>
+<TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner> <TileWidth>256</TileWidth> <TileHeight>256</TileHeight>
+<MatrixWidth>174759</MatrixWidth> <MatrixHeight>102915</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>19</ows:Identifier>
+<ScaleDenominator>1066.3647915984968</ScaleDenominator>
+<TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner> <TileWidth>256</TileWidth> <TileHeight>256</TileHeight>
+<MatrixWidth>349518</MatrixWidth> <MatrixHeight>205830</MatrixHeight>
+</TileMatrix>
+</TileMatrixSet>
+<TileMatrixSet>
+<ows:Title>GoogleMapsCompatible</ows:Title>
+<ows:Abstract>the wellknown 'GoogleMapsCompatible' tile matrix set defined by OGC WMTS specification</ows:Abstract>
+<ows:Identifier>GoogleMapsCompatible</ows:Identifier>
+<ows:SupportedCRS>urn:ogc:def:crs:EPSG:6.18.3:3857</ows:SupportedCRS>
+<WellKnownScaleSet>urn:ogc:def:wkss:OGC:1.0:GoogleMapsCompatible</WellKnownScaleSet>
+<TileMatrix>
+<ows:Identifier>0</ows:Identifier>
+<ScaleDenominator>559082264.0287178</ScaleDenominator>
+<TopLeftCorner>-20037508.34278925 20037508.34278925</TopLeftCorner>
+<TileWidth>256</TileWidth>
+<TileHeight>256</TileHeight>
+<MatrixWidth>1</MatrixWidth>
+<MatrixHeight>1</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>1</ows:Identifier>
+<ScaleDenominator>279541132.0143589</ScaleDenominator>
+<TopLeftCorner>-20037508.34278925 20037508.34278925</TopLeftCorner>
+<TileWidth>256</TileWidth>
+<TileHeight>256</TileHeight>
+<MatrixWidth>2</MatrixWidth>
+<MatrixHeight>2</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>2</ows:Identifier>
+<ScaleDenominator>139770566.0071794</ScaleDenominator>
+<TopLeftCorner>-20037508.34278925 20037508.34278925</TopLeftCorner>
+<TileWidth>256</TileWidth>
+<TileHeight>256</TileHeight>
+<MatrixWidth>4</MatrixWidth>
+<MatrixHeight>4</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>3</ows:Identifier>
+<ScaleDenominator>69885283.00358972</ScaleDenominator>
+<TopLeftCorner>-20037508.34278925 20037508.34278925</TopLeftCorner>
+<TileWidth>256</TileWidth>
+<TileHeight>256</TileHeight>
+<MatrixWidth>8</MatrixWidth>
+<MatrixHeight>8</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>4</ows:Identifier>
+<ScaleDenominator>34942641.50179486</ScaleDenominator>
+<TopLeftCorner>-20037508.34278925 20037508.34278925</TopLeftCorner>
+<TileWidth>256</TileWidth>
+<TileHeight>256</TileHeight>
+<MatrixWidth>16</MatrixWidth>
+<MatrixHeight>16</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>5</ows:Identifier>
+<ScaleDenominator>17471320.75089743</ScaleDenominator>
+<TopLeftCorner>-20037508.34278925 20037508.34278925</TopLeftCorner>
+<TileWidth>256</TileWidth>
+<TileHeight>256</TileHeight>
+<MatrixWidth>32</MatrixWidth>
+<MatrixHeight>32</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>6</ows:Identifier>
+<ScaleDenominator>8735660.375448715</ScaleDenominator>
+<TopLeftCorner>-20037508.34278925 20037508.34278925</TopLeftCorner>
+<TileWidth>256</TileWidth>
+<TileHeight>256</TileHeight>
+<MatrixWidth>64</MatrixWidth>
+<MatrixHeight>64</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>7</ows:Identifier>
+<ScaleDenominator>4367830.187724357</ScaleDenominator>
+<TopLeftCorner>-20037508.34278925 20037508.34278925</TopLeftCorner>
+<TileWidth>256</TileWidth>
+<TileHeight>256</TileHeight>
+<MatrixWidth>128</MatrixWidth>
+<MatrixHeight>128</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>8</ows:Identifier>
+<ScaleDenominator>2183915.093862179</ScaleDenominator>
+<TopLeftCorner>-20037508.34278925 20037508.34278925</TopLeftCorner>
+<TileWidth>256</TileWidth>
+<TileHeight>256</TileHeight>
+<MatrixWidth>256</MatrixWidth>
+<MatrixHeight>256</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>9</ows:Identifier>
+<ScaleDenominator>1091957.546931089</ScaleDenominator>
+<TopLeftCorner>-20037508.34278925 20037508.34278925</TopLeftCorner>
+<TileWidth>256</TileWidth>
+<TileHeight>256</TileHeight>
+<MatrixWidth>512</MatrixWidth>
+<MatrixHeight>512</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>10</ows:Identifier>
+<ScaleDenominator>545978.7734655447</ScaleDenominator>
+<TopLeftCorner>-20037508.34278925 20037508.34278925</TopLeftCorner>
+<TileWidth>256</TileWidth>
+<TileHeight>256</TileHeight>
+<MatrixWidth>1024</MatrixWidth>
+<MatrixHeight>1024</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>11</ows:Identifier>
+<ScaleDenominator>272989.3867327723</ScaleDenominator>
+<TopLeftCorner>-20037508.34278925 20037508.34278925</TopLeftCorner>
+<TileWidth>256</TileWidth>
+<TileHeight>256</TileHeight>
+<MatrixWidth>2048</MatrixWidth>
+<MatrixHeight>2048</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>12</ows:Identifier>
+<ScaleDenominator>136494.6933663862</ScaleDenominator>
+<TopLeftCorner>-20037508.34278925 20037508.34278925</TopLeftCorner>
+<TileWidth>256</TileWidth>
+<TileHeight>256</TileHeight>
+<MatrixWidth>4096</MatrixWidth>
+<MatrixHeight>4096</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>13</ows:Identifier>
+<ScaleDenominator>68247.34668319309</ScaleDenominator>
+<TopLeftCorner>-20037508.34278925 20037508.34278925</TopLeftCorner>
+<TileWidth>256</TileWidth>
+<TileHeight>256</TileHeight>
+<MatrixWidth>8192</MatrixWidth>
+<MatrixHeight>8192</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>14</ows:Identifier>
+<ScaleDenominator>34123.67334159654</ScaleDenominator>
+<TopLeftCorner>-20037508.34278925 20037508.34278925</TopLeftCorner>
+<TileWidth>256</TileWidth>
+<TileHeight>256</TileHeight>
+<MatrixWidth>16384</MatrixWidth>
+<MatrixHeight>16384</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>15</ows:Identifier>
+<ScaleDenominator>17061.83667079827</ScaleDenominator>
+<TopLeftCorner>-20037508.34278925 20037508.34278925</TopLeftCorner>
+<TileWidth>256</TileWidth>
+<TileHeight>256</TileHeight>
+<MatrixWidth>32768</MatrixWidth>
+<MatrixHeight>32768</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>16</ows:Identifier>
+<ScaleDenominator>8530.918335399136</ScaleDenominator>
+<TopLeftCorner>-20037508.34278925 20037508.34278925</TopLeftCorner>
+<TileWidth>256</TileWidth>
+<TileHeight>256</TileHeight>
+<MatrixWidth>65536</MatrixWidth>
+<MatrixHeight>65536</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>17</ows:Identifier>
+<ScaleDenominator>4265.459167699568</ScaleDenominator>
+<TopLeftCorner>-20037508.34278925 20037508.34278925</TopLeftCorner>
+<TileWidth>256</TileWidth>
+<TileHeight>256</TileHeight>
+<MatrixWidth>131072</MatrixWidth>
+<MatrixHeight>131072</MatrixHeight>
+</TileMatrix>
+<TileMatrix>
+<ows:Identifier>18</ows:Identifier>
+<ScaleDenominator>2132.729583849784</ScaleDenominator>
+<TopLeftCorner>-20037508.34278925 20037508.34278925</TopLeftCorner>
+<TileWidth>256</TileWidth>
+<TileHeight>256</TileHeight>
+<MatrixWidth>262144</MatrixWidth>
+<MatrixHeight>262144</MatrixHeight>
+</TileMatrix>
+</TileMatrixSet>
+</Contents>
+<ServiceMetadataURL xlink:href="http://188.253.0.155:6080/arcgis/rest/services/Mashhad_BaseMap_1/MapServer/WMTS/1.0.0/WMTSCapabilities.xml"/> </Capabilities>
Index: trunk/test/unit/org/openstreetmap/josm/data/imagery/WMTSTileSourceTest.java
===================================================================
--- trunk/test/unit/org/openstreetmap/josm/data/imagery/WMTSTileSourceTest.java	(revision 11256)
+++ trunk/test/unit/org/openstreetmap/josm/data/imagery/WMTSTileSourceTest.java	(revision 11257)
@@ -8,4 +8,6 @@
 import java.io.IOException;
 import java.net.MalformedURLException;
+import java.util.ArrayList;
+import java.util.Collection;
 
 import org.junit.Ignore;
@@ -38,4 +40,6 @@
     private ImageryInfo testDuplicateTags = getImagery(TestUtils.getTestDataRoot() + "wmts/bug12573-wmts-identifier.xml");
     private ImageryInfo testMissingStyleIdentifer = getImagery(TestUtils.getTestDataRoot() + "wmts/bug12573-wmts-missing-style-identifier.xml");
+    private ImageryInfo testMultipleTileMatrixForLayer = getImagery(TestUtils.getTestDataRoot() +
+            "wmts/bug13975-multiple-tile-matrices-for-one-layer-projection.xml");
 
     /**
@@ -263,4 +267,20 @@
     }
 
+    @Test
+    public void testForMultipleTileMatricesForOneLayerProjection() throws Exception {
+        Main.setProjection(Projections.getProjectionByCode("EPSG:3857"));
+        ImageryInfo copy = new ImageryInfo(testMultipleTileMatrixForLayer);
+        Collection<DefaultLayer> defaultLayers = new ArrayList<>(1);
+        defaultLayers.add(new WMTSDefaultLayer("Mashhad_BaseMap_1", "default028mm"));
+        copy.setDefaultLayers(defaultLayers);
+        WMTSTileSource testSource = new WMTSTileSource(copy);
+        testSource.initProjection(Main.getProjection());
+        assertEquals(
+                "http://188.253.0.155:6080/arcgis/rest/services/Mashhad_BaseMap_1/MapServer/WMTS/tile/1.0.0/Mashhad_BaseMap_1"
+                        + "/default/default028mm/1/3/2",
+                testSource.getTileUrl(1, 2, 3)
+                );
+    }
+
     private void verifyTile(LatLon expected, WMTSTileSource source, int x, int y, int z) {
         LatLon ll = new LatLon(source.tileXYToLatLon(x, y, z));
@@ -277,5 +297,4 @@
         LatLon result = new LatLon(testSource.tileXYToLatLon(x, y, z));
         LatLon expected = new LatLon(verifier.tileXYToLatLon(x, y, z + zoomOffset));
-        //System.out.println(z + "/" + x + "/" + y + " - result: " + result.toDisplayString() + " osmMercator: " +  expected.toDisplayString());
         assertEquals("Longitude", LatLon.normalizeLon(expected.lon() - result.lon()), 0.0, 1e-04);
         assertEquals("Latitude", expected.lat(), result.lat(), 1e-04);
