Index: /trunk/src/org/openstreetmap/josm/data/imagery/WMTSTileSource.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/data/imagery/WMTSTileSource.java	(revision 8585)
+++ /trunk/src/org/openstreetmap/josm/data/imagery/WMTSTileSource.java	(revision 8586)
@@ -80,4 +80,6 @@
         int tileWidth;
         int tileHeight;
+        public int matrixWidth = -1;
+        public int matrixHeight = -1;
     }
 
@@ -99,4 +101,5 @@
         Map<String, TileMatrixSet> tileMatrixSetByCRS = new ConcurrentHashMap<>();
         public String baseUrl;
+        public String style;
     }
 
@@ -163,5 +166,4 @@
     private double crsScale;
     private TransferMode transferMode;
-    private String style = "";
 
     /**
@@ -248,4 +250,8 @@
             layer.name = getStringByXpath(layerNode, "Identifier");
             layer.baseUrl = getStringByXpath(layerNode, "ResourceURL[@resourceType='tile']/@template");
+            layer.style = getStringByXpath(layerNode, "Style[@isDefault='true']/Identifier");
+            if (layer.style == null) {
+                layer.style = "";
+            }
             NodeList tileMatrixSetLinks = getByXpath(layerNode, "TileMatrixSetLink");
             for (int tileMatrixId = 0; tileMatrixId < tileMatrixSetLinks.getLength(); tileMatrixId++) {
@@ -287,4 +293,6 @@
                 tileMatrix.tileHeight = Integer.parseInt(getStringByXpath(tileMatrixNode, "TileHeight"));
                 tileMatrix.tileWidth = Integer.parseInt(getStringByXpath(tileMatrixNode, "TileHeight"));
+                tileMatrix.matrixWidth = getOptionalIntegerByXpath(tileMatrixNode, "MatrixWidth");
+                tileMatrix.matrixHeight = getOptionalIntegerByXpath(tileMatrixNode, "MatrixHeight");
                 if (tileMatrix.tileHeight != tileMatrix.tileWidth) {
                     throw new AssertionError(tr("Only square tiles are supported. {0}x{1} returned by server for TileMatrix identifier {2}",
@@ -304,4 +312,12 @@
         }
         return crsIdentifier;
+    }
+
+    private int getOptionalIntegerByXpath(Node document, String xpathQuery) throws XPathExpressionException {
+        String ret = getStringByXpath(document, xpathQuery);
+        if (ret == null || "".equals(ret)) {
+            return -1;
+        }
+        return Integer.parseInt(ret);
     }
 
@@ -384,5 +400,5 @@
                 .replaceAll("\\{TileRow\\}", Integer.toString(tiley))
                 .replaceAll("\\{TileCol\\}", Integer.toString(tilex))
-                .replaceAll("\\{Style\\}", this.style);
+                .replaceAll("\\{Style\\}", this.currentLayer.style);
     }
 
@@ -595,6 +611,11 @@
             return 0;
         }
+
+        if (matrix.matrixHeight != -1) {
+            return matrix.matrixHeight;
+        }
+
         double scale = matrix.scaleDenominator * this.crsScale;
-        Bounds bounds = Main.getProjection().getWorldBoundsLatLon();
+        Bounds bounds = proj.getWorldBoundsLatLon();
         EastNorth min = proj.latlon2eastNorth(bounds.getMin());
         EastNorth max = proj.latlon2eastNorth(bounds.getMax());
@@ -607,6 +628,10 @@
             return 0;
         }
+        if (matrix.matrixWidth != -1) {
+            return matrix.matrixWidth;
+        }
+
         double scale = matrix.scaleDenominator * this.crsScale;
-        Bounds bounds = Main.getProjection().getWorldBoundsLatLon();
+        Bounds bounds = proj.getWorldBoundsLatLon();
         EastNorth min = proj.latlon2eastNorth(bounds.getMin());
         EastNorth max = proj.latlon2eastNorth(bounds.getMax());
Index: /trunk/src/org/openstreetmap/josm/gui/layer/WMTSLayer.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/gui/layer/WMTSLayer.java	(revision 8585)
+++ /trunk/src/org/openstreetmap/josm/gui/layer/WMTSLayer.java	(revision 8586)
@@ -5,4 +5,5 @@
 import java.util.Map;
 
+import org.openstreetmap.gui.jmapviewer.TileXY;
 import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
 import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
@@ -81,10 +82,12 @@
      */
     private double getTileToScreenRatio(int zoom) {
-         ICoordinate north = tileSource.tileXYToLatLon(0, 0, zoom);
-         ICoordinate south = tileSource.tileXYToLatLon(0, 1, zoom);
-
          MapView mv = Main.map.mapView;
          LatLon topLeft = mv.getLatLon(0, 0);
          LatLon botLeft = mv.getLatLon(0, tileSource.getTileSize());
+
+         TileXY topLeftTile = tileSource.latLonToTileXY(topLeft.toCoordinate(), zoom);
+
+         ICoordinate north = tileSource.tileXYToLatLon(topLeftTile.getXIndex(), topLeftTile.getYIndex(), zoom);
+         ICoordinate south = tileSource.tileXYToLatLon(topLeftTile.getXIndex(), topLeftTile.getYIndex() + 1, zoom);
 
          return Math.abs((north.getLat() - south.getLat()) / (topLeft.lat() - botLeft.lat()));
@@ -95,8 +98,8 @@
         if (!Main.isDisplayingMapView()) return 1;
 
-        for (int i = getMinZoomLvl(); i <= getMaxZoomLvl(); i++) {
+        for (int i = getMinZoomLvl() + 1; i <= getMaxZoomLvl(); i++) {
             double ret = getTileToScreenRatio(i);
             if (ret < 1) {
-                return i;
+                return i - 1;
             }
         }
Index: /trunk/test/data/wmts/WMTSCapabilities-Wallonie.xml
===================================================================
--- /trunk/test/data/wmts/WMTSCapabilities-Wallonie.xml	(revision 8585)
+++ /trunk/test/data/wmts/WMTSCapabilities-Wallonie.xml	(revision 8586)
@@ -1,183 +1,246 @@
 <?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>DONNEES_BASE_FOND_PLAN_ANNOTATIONS_2012_RW_NB</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://geoservices.wallonie.be/arcgis/rest/services/DONNEES_BASE/FOND_PLAN_ANNOTATIONS_2012_RW_NB/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://geoservices.wallonie.be/arcgis/rest/services/DONNEES_BASE/FOND_PLAN_ANNOTATIONS_2012_RW_NB/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://geoservices.wallonie.be/arcgis/rest/services/DONNEES_BASE/FOND_PLAN_ANNOTATIONS_2012_RW_NB/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://geoservices.wallonie.be/arcgis/rest/services/DONNEES_BASE/FOND_PLAN_ANNOTATIONS_2012_RW_NB/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>DONNEES_BASE_FOND_PLAN_ANNOTATIONS_2012_RW_NB</ows:Title> <ows:Identifier>DONNEES_BASE_FOND_PLAN_ANNOTATIONS_2012_RW_NB</ows:Identifier>
-<ows:BoundingBox crs="urn:ogc:def:crs:EPSG::31370">
-<ows:LowerCorner>42300.92807390103 21237.786800000817</ows:LowerCorner>
-<ows:UpperCorner>295130.32186220103 167836.14678540602</ows:UpperCorner>
-</ows:BoundingBox> <ows:WGS84BoundingBox crs="urn:ogc:def:crs:OGC:2:84">
-<ows:LowerCorner>2.840548314430268 49.485372459967245</ows:LowerCorner>
-<ows:UpperCorner>6.427849693016202 50.820959517561256</ows:UpperCorner>
-</ows:WGS84BoundingBox>
-<Style isDefault="true">
-<ows:Title>Default Style</ows:Title>
-<ows:Identifier>default</ows:Identifier>
-</Style>
-<Format>image/png</Format>
-<TileMatrixSetLink>
-<TileMatrixSet>default028mm</TileMatrixSet>
-</TileMatrixSetLink>
-<ResourceURL format="image/png" resourceType="tile" template="http://geoservices.wallonie.be/arcgis/rest/services/DONNEES_BASE/FOND_PLAN_ANNOTATIONS_2012_RW_NB/MapServer/WMTS/tile/1.0.0/DONNEES_BASE_FOND_PLAN_ANNOTATIONS_2012_RW_NB/{Style}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png" />
-</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::31370</ows:SupportedCRS>
-<TileMatrix>
-<ows:Identifier>0</ows:Identifier>
-<ScaleDenominator>1417413.5491126343</ScaleDenominator>
-<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner> <TileWidth>512</TileWidth> <TileHeight>512</TileHeight>
-<MatrixWidth>178</MatrixWidth> <MatrixHeight>204</MatrixHeight>
-</TileMatrix>
-<TileMatrix>
-<ows:Identifier>1</ows:Identifier>
-<ScaleDenominator>1181177.957593862</ScaleDenominator>
-<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner> <TileWidth>512</TileWidth> <TileHeight>512</TileHeight>
-<MatrixWidth>214</MatrixWidth> <MatrixHeight>245</MatrixHeight>
-</TileMatrix>
-<TileMatrix>
-<ows:Identifier>2</ows:Identifier>
-<ScaleDenominator>944942.3660750897</ScaleDenominator>
-<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner> <TileWidth>512</TileWidth> <TileHeight>512</TileHeight>
-<MatrixWidth>267</MatrixWidth> <MatrixHeight>306</MatrixHeight>
-</TileMatrix>
-<TileMatrix>
-<ows:Identifier>3</ows:Identifier>
-<ScaleDenominator>708706.7745563171</ScaleDenominator>
-<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner> <TileWidth>512</TileWidth> <TileHeight>512</TileHeight>
-<MatrixWidth>356</MatrixWidth> <MatrixHeight>408</MatrixHeight>
-</TileMatrix>
-<TileMatrix>
-<ows:Identifier>4</ows:Identifier>
-<ScaleDenominator>472471.18303754483</ScaleDenominator>
-<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner> <TileWidth>512</TileWidth> <TileHeight>512</TileHeight>
-<MatrixWidth>534</MatrixWidth> <MatrixHeight>612</MatrixHeight>
-</TileMatrix>
-<TileMatrix>
-<ows:Identifier>5</ows:Identifier>
-<ScaleDenominator>236235.59151877242</ScaleDenominator>
-<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner> <TileWidth>512</TileWidth> <TileHeight>512</TileHeight>
-<MatrixWidth>1068</MatrixWidth> <MatrixHeight>1223</MatrixHeight>
-</TileMatrix>
-<TileMatrix>
-<ows:Identifier>6</ows:Identifier>
-<ScaleDenominator>94494.23660750895</ScaleDenominator>
-<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner> <TileWidth>512</TileWidth> <TileHeight>512</TileHeight>
-<MatrixWidth>2670</MatrixWidth> <MatrixHeight>3057</MatrixHeight>
-</TileMatrix>
-<TileMatrix>
-<ows:Identifier>7</ows:Identifier>
-<ScaleDenominator>70870.67745563173</ScaleDenominator>
-<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner> <TileWidth>512</TileWidth> <TileHeight>512</TileHeight>
-<MatrixWidth>3560</MatrixWidth> <MatrixHeight>4075</MatrixHeight>
-</TileMatrix>
-<TileMatrix>
-<ows:Identifier>8</ows:Identifier>
-<ScaleDenominator>47247.118303754476</ScaleDenominator>
-<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner> <TileWidth>512</TileWidth> <TileHeight>512</TileHeight>
-<MatrixWidth>5340</MatrixWidth> <MatrixHeight>6113</MatrixHeight>
-</TileMatrix>
-<TileMatrix>
-<ows:Identifier>9</ows:Identifier>
-<ScaleDenominator>23623.559151877238</ScaleDenominator>
-<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner> <TileWidth>512</TileWidth> <TileHeight>512</TileHeight>
-<MatrixWidth>10680</MatrixWidth> <MatrixHeight>12225</MatrixHeight>
-</TileMatrix>
-<TileMatrix>
-<ows:Identifier>10</ows:Identifier>
-<ScaleDenominator>14174.135491126344</ScaleDenominator>
-<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner> <TileWidth>512</TileWidth> <TileHeight>512</TileHeight>
-<MatrixWidth>17800</MatrixWidth> <MatrixHeight>20375</MatrixHeight>
-</TileMatrix>
-<TileMatrix>
-<ows:Identifier>11</ows:Identifier>
-<ScaleDenominator>9449.423660750896</ScaleDenominator>
-<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner> <TileWidth>512</TileWidth> <TileHeight>512</TileHeight>
-<MatrixWidth>26699</MatrixWidth> <MatrixHeight>30563</MatrixHeight>
-</TileMatrix>
-<TileMatrix>
-<ows:Identifier>12</ows:Identifier>
-<ScaleDenominator>4724.711830375448</ScaleDenominator>
-<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner> <TileWidth>512</TileWidth> <TileHeight>512</TileHeight>
-<MatrixWidth>53398</MatrixWidth> <MatrixHeight>61125</MatrixHeight>
-</TileMatrix>
-<TileMatrix>
-<ows:Identifier>13</ows:Identifier>
-<ScaleDenominator>2362.355915187724</ScaleDenominator>
-<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner> <TileWidth>512</TileWidth> <TileHeight>512</TileHeight>
-<MatrixWidth>106795</MatrixWidth> <MatrixHeight>122249</MatrixHeight>
-</TileMatrix>
-<TileMatrix>
-<ows:Identifier>14</ows:Identifier>
-<ScaleDenominator>944.9423660750896</ScaleDenominator>
-<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner> <TileWidth>512</TileWidth> <TileHeight>512</TileHeight>
-<MatrixWidth>266987</MatrixWidth> <MatrixHeight>305621</MatrixHeight>
-</TileMatrix>
-<TileMatrix>
-<ows:Identifier>15</ows:Identifier>
-<ScaleDenominator>472.4711830375448</ScaleDenominator>
-<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner> <TileWidth>512</TileWidth> <TileHeight>512</TileHeight>
-<MatrixWidth>533973</MatrixWidth> <MatrixHeight>611241</MatrixHeight>
-</TileMatrix>
-<TileMatrix>
-<ows:Identifier>16</ows:Identifier>
-<ScaleDenominator>236.2355915187724</ScaleDenominator>
-<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner> <TileWidth>512</TileWidth> <TileHeight>512</TileHeight>
-<MatrixWidth>1067946</MatrixWidth> <MatrixHeight>1222482</MatrixHeight>
-</TileMatrix>
-</TileMatrixSet>
-</Contents>
-<ServiceMetadataURL xlink:href="http://geoservices.wallonie.be/arcgis/rest/services/DONNEES_BASE/FOND_PLAN_ANNOTATIONS_2012_RW_NB/MapServer/WMTS/1.0.0/WMTSCapabilities.xml" /> </Capabilities>
+	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>DONNEES_BASE_FOND_PLAN_ANNOTATIONS_2012_RW_NB</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://geoservices.wallonie.be/arcgis/rest/services/DONNEES_BASE/FOND_PLAN_ANNOTATIONS_2012_RW_NB/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://geoservices.wallonie.be/arcgis/rest/services/DONNEES_BASE/FOND_PLAN_ANNOTATIONS_2012_RW_NB/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://geoservices.wallonie.be/arcgis/rest/services/DONNEES_BASE/FOND_PLAN_ANNOTATIONS_2012_RW_NB/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://geoservices.wallonie.be/arcgis/rest/services/DONNEES_BASE/FOND_PLAN_ANNOTATIONS_2012_RW_NB/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>DONNEES_BASE_FOND_PLAN_ANNOTATIONS_2012_RW_NB</ows:Title>
+			<ows:Identifier>DONNEES_BASE_FOND_PLAN_ANNOTATIONS_2012_RW_NB</ows:Identifier>
+			<ows:BoundingBox crs="urn:ogc:def:crs:EPSG::31370">
+				<ows:LowerCorner>42300.92807390103 21237.786800000817</ows:LowerCorner>
+				<ows:UpperCorner>295130.32186220103 167836.14678540602</ows:UpperCorner>
+			</ows:BoundingBox>
+			<ows:WGS84BoundingBox crs="urn:ogc:def:crs:OGC:2:84">
+				<ows:LowerCorner>2.840548314430268 49.485372459967245</ows:LowerCorner>
+				<ows:UpperCorner>6.427849693016202 50.820959517561256</ows:UpperCorner>
+			</ows:WGS84BoundingBox>
+			<Style isDefault="true">
+				<ows:Title>Default Style</ows:Title>
+				<ows:Identifier>default</ows:Identifier>
+			</Style>
+			<Format>image/png</Format>
+			<TileMatrixSetLink>
+				<TileMatrixSet>default028mm</TileMatrixSet>
+			</TileMatrixSetLink>
+			<ResourceURL format="image/png" resourceType="tile"
+				template="http://geoservices.wallonie.be/arcgis/rest/services/DONNEES_BASE/FOND_PLAN_ANNOTATIONS_2012_RW_NB/MapServer/WMTS/tile/1.0.0/DONNEES_BASE_FOND_PLAN_ANNOTATIONS_2012_RW_NB/{Style}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png" />
+		</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::31370</ows:SupportedCRS>
+			<TileMatrix>
+				<ows:Identifier>0</ows:Identifier>
+				<ScaleDenominator>1417413.5491126343</ScaleDenominator>
+				<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner>
+				<TileWidth>512</TileWidth>
+				<TileHeight>512</TileHeight>
+				<MatrixWidth>178</MatrixWidth>
+				<MatrixHeight>204</MatrixHeight>
+			</TileMatrix>
+			<TileMatrix>
+				<ows:Identifier>1</ows:Identifier>
+				<ScaleDenominator>1181177.957593862</ScaleDenominator>
+				<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner>
+				<TileWidth>512</TileWidth>
+				<TileHeight>512</TileHeight>
+				<MatrixWidth>214</MatrixWidth>
+				<MatrixHeight>245</MatrixHeight>
+			</TileMatrix>
+			<TileMatrix>
+				<ows:Identifier>2</ows:Identifier>
+				<ScaleDenominator>944942.3660750897</ScaleDenominator>
+				<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner>
+				<TileWidth>512</TileWidth>
+				<TileHeight>512</TileHeight>
+				<MatrixWidth>267</MatrixWidth>
+				<MatrixHeight>306</MatrixHeight>
+			</TileMatrix>
+			<TileMatrix>
+				<ows:Identifier>3</ows:Identifier>
+				<ScaleDenominator>708706.7745563171</ScaleDenominator>
+				<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner>
+				<TileWidth>512</TileWidth>
+				<TileHeight>512</TileHeight>
+				<MatrixWidth>356</MatrixWidth>
+				<MatrixHeight>408</MatrixHeight>
+			</TileMatrix>
+			<TileMatrix>
+				<ows:Identifier>4</ows:Identifier>
+				<ScaleDenominator>472471.18303754483</ScaleDenominator>
+				<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner>
+				<TileWidth>512</TileWidth>
+				<TileHeight>512</TileHeight>
+				<MatrixWidth>534</MatrixWidth>
+				<MatrixHeight>612</MatrixHeight>
+			</TileMatrix>
+			<TileMatrix>
+				<ows:Identifier>5</ows:Identifier>
+				<ScaleDenominator>236235.59151877242</ScaleDenominator>
+				<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner>
+				<TileWidth>512</TileWidth>
+				<TileHeight>512</TileHeight>
+				<MatrixWidth>1068</MatrixWidth>
+				<MatrixHeight>1223</MatrixHeight>
+			</TileMatrix>
+			<TileMatrix>
+				<ows:Identifier>6</ows:Identifier>
+				<ScaleDenominator>94494.23660750895</ScaleDenominator>
+				<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner>
+				<TileWidth>512</TileWidth>
+				<TileHeight>512</TileHeight>
+				<MatrixWidth>2670</MatrixWidth>
+				<MatrixHeight>3057</MatrixHeight>
+			</TileMatrix>
+			<TileMatrix>
+				<ows:Identifier>7</ows:Identifier>
+				<ScaleDenominator>70870.67745563173</ScaleDenominator>
+				<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner>
+				<TileWidth>512</TileWidth>
+				<TileHeight>512</TileHeight>
+				<MatrixWidth>3560</MatrixWidth>
+				<MatrixHeight>4075</MatrixHeight>
+			</TileMatrix>
+			<TileMatrix>
+				<ows:Identifier>8</ows:Identifier>
+				<ScaleDenominator>47247.118303754476</ScaleDenominator>
+				<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner>
+				<TileWidth>512</TileWidth>
+				<TileHeight>512</TileHeight>
+				<MatrixWidth>5340</MatrixWidth>
+				<MatrixHeight>6113</MatrixHeight>
+			</TileMatrix>
+			<TileMatrix>
+				<ows:Identifier>9</ows:Identifier>
+				<ScaleDenominator>23623.559151877238</ScaleDenominator>
+				<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner>
+				<TileWidth>512</TileWidth>
+				<TileHeight>512</TileHeight>
+				<MatrixWidth>10680</MatrixWidth>
+				<MatrixHeight>12225</MatrixHeight>
+			</TileMatrix>
+			<TileMatrix>
+				<ows:Identifier>10</ows:Identifier>
+				<ScaleDenominator>14174.135491126344</ScaleDenominator>
+				<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner>
+				<TileWidth>512</TileWidth>
+				<TileHeight>512</TileHeight>
+				<MatrixWidth>17800</MatrixWidth>
+				<MatrixHeight>20375</MatrixHeight>
+			</TileMatrix>
+			<TileMatrix>
+				<ows:Identifier>11</ows:Identifier>
+				<ScaleDenominator>9449.423660750896</ScaleDenominator>
+				<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner>
+				<TileWidth>512</TileWidth>
+				<TileHeight>512</TileHeight>
+				<MatrixWidth>26699</MatrixWidth>
+				<MatrixHeight>30563</MatrixHeight>
+			</TileMatrix>
+			<TileMatrix>
+				<ows:Identifier>12</ows:Identifier>
+				<ScaleDenominator>4724.711830375448</ScaleDenominator>
+				<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner>
+				<TileWidth>512</TileWidth>
+				<TileHeight>512</TileHeight>
+				<MatrixWidth>53398</MatrixWidth>
+				<MatrixHeight>61125</MatrixHeight>
+			</TileMatrix>
+			<TileMatrix>
+				<ows:Identifier>13</ows:Identifier>
+				<ScaleDenominator>2362.355915187724</ScaleDenominator>
+				<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner>
+				<TileWidth>512</TileWidth>
+				<TileHeight>512</TileHeight>
+				<MatrixWidth>106795</MatrixWidth>
+				<MatrixHeight>122249</MatrixHeight>
+			</TileMatrix>
+			<TileMatrix>
+				<ows:Identifier>14</ows:Identifier>
+				<ScaleDenominator>944.9423660750896</ScaleDenominator>
+				<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner>
+				<TileWidth>512</TileWidth>
+				<TileHeight>512</TileHeight>
+				<MatrixWidth>266987</MatrixWidth>
+				<MatrixHeight>305621</MatrixHeight>
+			</TileMatrix>
+			<TileMatrix>
+				<ows:Identifier>15</ows:Identifier>
+				<ScaleDenominator>472.4711830375448</ScaleDenominator>
+				<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner>
+				<TileWidth>512</TileWidth>
+				<TileHeight>512</TileHeight>
+				<MatrixWidth>533973</MatrixWidth>
+				<MatrixHeight>611241</MatrixHeight>
+			</TileMatrix>
+			<TileMatrix>
+				<ows:Identifier>16</ows:Identifier>
+				<ScaleDenominator>236.2355915187724</ScaleDenominator>
+				<TopLeftCorner>-3.58727E7 4.14227E7</TopLeftCorner>
+				<TileWidth>512</TileWidth>
+				<TileHeight>512</TileHeight>
+				<MatrixWidth>1067946</MatrixWidth>
+				<MatrixHeight>1222482</MatrixHeight>
+			</TileMatrix>
+		</TileMatrixSet>
+	</Contents>
+	<ServiceMetadataURL
+		xlink:href="http://geoservices.wallonie.be/arcgis/rest/services/DONNEES_BASE/FOND_PLAN_ANNOTATIONS_2012_RW_NB/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 8585)
+++ /trunk/test/unit/org/openstreetmap/josm/data/imagery/WMTSTileSourceTest.java	(revision 8586)
@@ -11,5 +11,4 @@
 import org.junit.BeforeClass;
 import org.junit.Test;
-import org.openstreetmap.gui.jmapviewer.TileXY;
 import org.openstreetmap.gui.jmapviewer.tilesources.TemplatedTMSTileSource;
 import org.openstreetmap.josm.JOSMFixture;
@@ -70,5 +69,5 @@
         assertEquals("TileXMax", 2, testSource.getTileXMax(2));
         assertEquals("TileYMax", 2, testSource.getTileYMax(2));
-        assertEquals("TileXMax", 5, testSource.getTileXMax(3));
+        assertEquals("TileXMax", 4, testSource.getTileXMax(3));
         assertEquals("TileYMax", 4, testSource.getTileYMax(3));
 
@@ -79,15 +78,26 @@
         Main.setProjection(Projections.getProjectionByCode("EPSG:31370"));
         WMTSTileSource testSource = new WMTSTileSource(testImageryWALLONIE);
+        assertEquals("http://geoservices.wallonie.be/arcgis/rest/services/DONNEES_BASE/FOND_PLAN_ANNOTATIONS_2012_RW_NB/"
+                + "MapServer/WMTS/tile/1.0.0/DONNEES_BASE_FOND_PLAN_ANNOTATIONS_2012_RW_NB/default/default028mm/5/1219/1063.png",
+                testSource.getTileUrl(6, 1063, 1219));
+
+        // +bounds=2.54,49.51,6.4,51.5
         Bounds wallonieBounds = new Bounds(
                 new LatLon(49.485372459967245, 2.840548314430268),
                 new LatLon(50.820959517561256, 6.427849693016202)
                 );
-        verifyBounds(wallonieBounds, testSource, 10, 20324, 17724);
+        verifyBounds(wallonieBounds, testSource, 6, 1063, 1219);
+        verifyBounds(wallonieBounds, testSource, 11, 17724, 20324);
+        LatLon ll = new LatLon(testSource.tileXYToLatLon(1063, 1219, 6));
 
     }
 
     private void verifyBounds(Bounds bounds, WMTSTileSource testSource, int z, int x, int y) {
-        LatLon ret = new LatLon(testSource.tileXYToLatLon(y, y, z));
+        LatLon ret = new LatLon(testSource.tileXYToLatLon(x, y, z));
         assertTrue(ret.toDisplayString() + " doesn't lie within: " + bounds.toString(), bounds.contains(ret));
+        int tileXmax = testSource.getTileXMax(z);
+        int tileYmax = testSource.getTileYMax(z);
+        assertTrue("tile x: " + x + " is greater than allowed max: " + tileXmax, tileXmax >= x);
+        assertTrue("tile y: " + y + " is greater than allowed max: " + tileYmax, tileYmax >= y);
     }
 
@@ -96,7 +106,6 @@
         Main.setProjection(Projections.getProjectionByCode("EPSG:3857"));
         WMTSTileSource testSource = new WMTSTileSource(testImageryWIEN);
-        int zoomOffset = 10;
-
-        // Linz - 11/1105/709.png
+        int zoomOffset = 9;
+
         verifyMercatorTile(testSource, 0, 0, 1, zoomOffset);
         verifyMercatorTile(testSource, 1105, 709, 2, zoomOffset);
@@ -106,10 +115,4 @@
         verifyMercatorTile(testSource, 1, 1, 2, zoomOffset);
 
-
-        LatLon ll = new LatLon(testSource.tileXYToLatLon(500,  500, 1));
-
-        TileXY xy = testSource.latLonToTileXY(new LatLon(48.21, 14.24).toCoordinate(), 1);
-        assertTrue("X index is negative: " + xy.getXIndex(), xy.getXIndex() > 0);
-        assertTrue(xy.getYIndex() > 0);
         for (int x = 0; x < 4; x++) {
             for (int y = 0; y < 4; y++) {
@@ -119,16 +122,23 @@
         for (int x = 0; x < 8; x++) {
             for (int y = 0; y < 4; y++) {
-                verifyMercatorTile(testSource, x, y, zoomOffset);
-            }
-        }
-
-        verifyMercatorTile(testSource, 2 << 9 - 1, 2 << 8 - 1, zoomOffset);
-
-        assertEquals("TileXMax", 1, testSource.getTileXMax(1));
-        assertEquals("TileYMax", 1, testSource.getTileYMax(1));
-        assertEquals("TileXMax", 2, testSource.getTileXMax(2));
-        assertEquals("TileYMax", 2, testSource.getTileYMax(2));
-        assertEquals("TileXMax", 4, testSource.getTileXMax(3));
-        assertEquals("TileYMax", 4, testSource.getTileYMax(3));
+                verifyMercatorTile(testSource, x, y, 4, zoomOffset);
+            }
+        }
+
+        verifyMercatorTile(testSource, 2 << 9 - 1, 2 << 8 - 1, 2, zoomOffset);
+
+        verifyMercatorMax(testSource, 1, zoomOffset);
+        verifyMercatorMax(testSource, 2, zoomOffset);
+        verifyMercatorMax(testSource, 3, zoomOffset);
+    }
+
+    private void verifyMercatorMax(WMTSTileSource testSource, int zoom, int zoomOffset) {
+        TemplatedTMSTileSource verifier = new TemplatedTMSTileSource(testImageryTMS);
+        int result = testSource.getTileXMax(zoom);
+        int expected = verifier.getTileXMax(zoom + zoomOffset);
+        assertTrue("TileXMax expected: " + expected + " got: " + result, Math.abs(result - expected) < 5);
+        result = testSource.getTileYMax(zoom);
+        expected = verifier.getTileYMax(zoom + zoomOffset);
+        assertTrue("TileYMax expected: " + expected + " got: " + result, Math.abs(result - expected) < 5);
     }
 
@@ -141,13 +151,13 @@
         verifyTile(new LatLon(51.1268639, 16.8731360), testSource, 1, 1, 2);
 
-        assertEquals("TileXMax", 37, testSource.getTileXMax(1));
-        assertEquals("TileYMax", 19, testSource.getTileYMax(1));
-        assertEquals("TileXMax", 74, testSource.getTileXMax(2));
-        assertEquals("TileYMax", 37, testSource.getTileYMax(2));
-        assertEquals("TileXMax", 148, testSource.getTileXMax(3));
-        assertEquals("TileYMax", 74, testSource.getTileYMax(3));
+        assertEquals("TileXMax", 2, testSource.getTileXMax(1));
+        assertEquals("TileYMax", 1, testSource.getTileYMax(1));
+        assertEquals("TileXMax", 3, testSource.getTileXMax(2));
+        assertEquals("TileYMax", 2, testSource.getTileYMax(2));
+        assertEquals("TileXMax", 6, testSource.getTileXMax(3));
+        assertEquals("TileYMax", 4, testSource.getTileYMax(3));
         assertEquals(
                 "http://mapy.geoportal.gov.pl/wss/service/WMTS/guest/wmts/TOPO?SERVICE=WMTS&REQUEST=GetTile&"
-                + "VERSION=1.0.0&LAYER=MAPA TOPOGRAFICZNA&STYLE=&FORMAT=image/jpeg&tileMatrixSet=EPSG:4326&"
+                + "VERSION=1.0.0&LAYER=MAPA TOPOGRAFICZNA&STYLE=default&FORMAT=image/jpeg&tileMatrixSet=EPSG:4326&"
                 + "tileMatrix=EPSG:4326:0&tileRow=1&tileCol=1",
                 testSource.getTileUrl(1,  1,  1));
@@ -159,5 +169,4 @@
         WMTSTileSource testSource = new WMTSTileSource(testImageryORTO_PL);
         verifyTile(new LatLon(53.5993712684958, 19.560669777688176), testSource, 12412, 3941, 14);
-
         verifyTile(new LatLon(49.783096954497786, 22.79034127751704), testSource, 17714, 10206, 14);
     }
@@ -187,12 +196,7 @@
         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());
+        //System.out.println(z + "/" + x + "/" + y + " - result: " + result.toDisplayString() + " osmMercator: " +  expected.toDisplayString());
         assertEquals("Longitude" , expected.lon(), result.lon(), 1e-04);
         assertEquals("Latitude", expected.lat(), result.lat(), 1e-04);
-        //assertTrue("result: " + result.toDisplayString() + " osmMercator: " +  expected.toDisplayString(), result.equalsEpsilon(expected));
-//        LatLon tileCenter = new Bounds(result, new LatLon(testSource.tileXYToLatLon(x+1, y+1, z))).getCenter();
-//        TileXY backwardsResult = testSource.latLonToTileXY(tileCenter.toCoordinate(), z);
-        //assertEquals(x, backwardsResult.getXIndex());
-        //assertEquals(y, backwardsResult.getYIndex());
     }
 }
