Index: trunk/test/data/wmts/getCapabilities-wien.xml
===================================================================
--- trunk/test/data/wmts/getCapabilities-wien.xml	(revision 8570)
+++ trunk/test/data/wmts/getCapabilities-wien.xml	(revision 8584)
@@ -60,37 +60,4 @@
 			<ResourceURL format="image/png" template="http://webgis.linz.at/WMTS/1.0.0/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png" resourceType="tile"/> 
 		</Layer>
-		<Layer>
-			<ows:Title>Stadtkarte</ows:Title>
-			<ows:WGS84BoundingBox crs="urn:ogc:def:crs:OGC:2:84">
-				<ows:LowerCorner>14.42 48.21</ows:LowerCorner>
-				<ows:UpperCorner>14.24 48.38</ows:UpperCorner>
-			</ows:WGS84BoundingBox>
-			<ows:Identifier>Stadtkarte</ows:Identifier>
-			<Style isDefault="true">
-				<ows:Identifier>normal</ows:Identifier>
-			</Style>
-			<Format>image/png</Format>
-			<TileMatrixSetLink>
-				<TileMatrixSet>Stadtkarte_standard</TileMatrixSet>  
-			</TileMatrixSetLink>
-			<ResourceURL format="image/png" template="http://webgis.linz.at/WMTS/1.0.0/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png" resourceType="tile"/>
-		</Layer>
-		<Layer>
-			<ows:Title>Beschriftungen</ows:Title>
-			<ows:WGS84BoundingBox crs="urn:ogc:def:crs:OGC:2:84">
-				<ows:LowerCorner>14.42 48.21</ows:LowerCorner>
-				<ows:UpperCorner>14.24 48.38</ows:UpperCorner>
-			</ows:WGS84BoundingBox>
-			<ows:Identifier>Beschriftungen</ows:Identifier>
-			<Style isDefault="true">
-				<ows:Identifier>normal</ows:Identifier>
-			</Style>
-			<Format>image/png</Format>
-			<TileMatrixSetLink>
-				<TileMatrixSet>Beschriftungen_alles</TileMatrixSet>
-			</TileMatrixSetLink>
-			<ResourceURL format="image/png" template="http://webgis.linz.at/WMTS/1.0.0/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png" resourceType="tile"/>
-		</Layer>
-
 		<TileMatrixSet>
 			<ows:Identifier>Orthofotos_standard</ows:Identifier>
Index: trunk/test/data/wmts/getcapabilities-pseudo-mercator.xml
===================================================================
--- trunk/test/data/wmts/getcapabilities-pseudo-mercator.xml	(revision 8570)
+++ trunk/test/data/wmts/getcapabilities-pseudo-mercator.xml	(revision 8584)
@@ -53,5 +53,5 @@
       <ows:Identifier>EPSG:3857:0</ows:Identifier>
       <ScaleDenominator>559082264.0287178</ScaleDenominator>
-      <TopLeftCorner>20037509.917339604 -20037508.342789244</TopLeftCorner>
+      <TopLeftCorner>-20037508.3428 20037508.3428</TopLeftCorner>
       <TileWidth>256</TileWidth>
       <TileHeight>256</TileHeight>
@@ -63,5 +63,5 @@
       <ows:Identifier>EPSG:3857:1</ows:Identifier>
       <ScaleDenominator>279541132.0143589</ScaleDenominator>
-      <TopLeftCorner>20037509.917339604 -20037508.342789244</TopLeftCorner>
+      <TopLeftCorner>-20037508.3428 20037508.3428</TopLeftCorner>
       <TileWidth>256</TileWidth>
       <TileHeight>256</TileHeight>
@@ -73,5 +73,5 @@
       <ows:Identifier>EPSG:3857:2</ows:Identifier>
       <ScaleDenominator>139770566.0071794</ScaleDenominator>
-      <TopLeftCorner>20037509.917339604 -20037508.342789244</TopLeftCorner>
+      <TopLeftCorner>-20037508.3428 20037508.3428</TopLeftCorner>
       <TileWidth>256</TileWidth>
       <TileHeight>256</TileHeight>
@@ -83,5 +83,5 @@
       <ows:Identifier>EPSG:3857:3</ows:Identifier>
       <ScaleDenominator>69885283.00358972</ScaleDenominator>
-      <TopLeftCorner>20037509.917339604 -20037508.342789244</TopLeftCorner>
+      <TopLeftCorner>-20037508.3428 20037508.3428</TopLeftCorner>
       <TileWidth>256</TileWidth>
       <TileHeight>256</TileHeight>
@@ -93,5 +93,5 @@
       <ows:Identifier>EPSG:3857:4</ows:Identifier>
       <ScaleDenominator>34942641.50179486</ScaleDenominator>
-      <TopLeftCorner>20037509.917339604 -20037508.342789244</TopLeftCorner>
+      <TopLeftCorner>-20037508.3428 20037508.3428</TopLeftCorner>
       <TileWidth>256</TileWidth>
       <TileHeight>256</TileHeight>
@@ -103,5 +103,5 @@
       <ows:Identifier>EPSG:3857:5</ows:Identifier>
       <ScaleDenominator>17471320.75089743</ScaleDenominator>
-      <TopLeftCorner>20037509.917339604 -20037508.342789244</TopLeftCorner>
+      <TopLeftCorner>-20037508.3428 20037508.3428</TopLeftCorner>
       <TileWidth>256</TileWidth>
       <TileHeight>256</TileHeight>
@@ -113,5 +113,5 @@
       <ows:Identifier>EPSG:3857:6</ows:Identifier>
       <ScaleDenominator>8735660.375448715</ScaleDenominator>
-      <TopLeftCorner>20037509.917339604 -20037508.342789244</TopLeftCorner>
+      <TopLeftCorner>-20037508.3428 20037508.3428</TopLeftCorner>
       <TileWidth>256</TileWidth>
       <TileHeight>256</TileHeight>
@@ -123,5 +123,5 @@
       <ows:Identifier>EPSG:3857:7</ows:Identifier>
       <ScaleDenominator>4367830.187724357</ScaleDenominator>
-      <TopLeftCorner>20037509.917339604 -20037508.342789244</TopLeftCorner>
+      <TopLeftCorner>-20037508.3428 20037508.3428</TopLeftCorner>
       <TileWidth>256</TileWidth>
       <TileHeight>256</TileHeight>
@@ -133,5 +133,5 @@
       <ows:Identifier>EPSG:3857:8</ows:Identifier>
       <ScaleDenominator>2183915.093862179</ScaleDenominator>
-      <TopLeftCorner>20037509.917339604 -20037508.342789244</TopLeftCorner>
+      <TopLeftCorner>-20037508.3428 20037508.3428</TopLeftCorner>
       <TileWidth>256</TileWidth>
       <TileHeight>256</TileHeight>
@@ -143,5 +143,5 @@
       <ows:Identifier>EPSG:3857:9</ows:Identifier>
       <ScaleDenominator>1091957.546931089</ScaleDenominator>
-      <TopLeftCorner>20037509.917339604 -20037508.342789244</TopLeftCorner>
+      <TopLeftCorner>-20037508.3428 20037508.3428</TopLeftCorner>
       <TileWidth>256</TileWidth>
       <TileHeight>256</TileHeight>
@@ -153,5 +153,5 @@
       <ows:Identifier>EPSG:3857:10</ows:Identifier>
       <ScaleDenominator>545978.7734655447</ScaleDenominator>
-      <TopLeftCorner>20037509.917339604 -20037508.342789244</TopLeftCorner>
+      <TopLeftCorner>-20037508.3428 20037508.3428</TopLeftCorner>
       <TileWidth>256</TileWidth>
       <TileHeight>256</TileHeight>
Index: trunk/test/unit/org/openstreetmap/josm/data/imagery/WMTSTileSourceTest.java
===================================================================
--- trunk/test/unit/org/openstreetmap/josm/data/imagery/WMTSTileSourceTest.java	(revision 8570)
+++ trunk/test/unit/org/openstreetmap/josm/data/imagery/WMTSTileSourceTest.java	(revision 8584)
@@ -3,4 +3,5 @@
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 import java.io.File;
@@ -10,7 +11,9 @@
 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;
 import org.openstreetmap.josm.Main;
+import org.openstreetmap.josm.data.Bounds;
 import org.openstreetmap.josm.data.coor.LatLon;
 import org.openstreetmap.josm.data.projection.Projections;
@@ -69,4 +72,5 @@
         assertEquals("TileXMax", 5, testSource.getTileXMax(3));
         assertEquals("TileYMax", 4, testSource.getTileYMax(3));
+
     }
 
@@ -75,5 +79,18 @@
         Main.setProjection(Projections.getProjectionByCode("EPSG:31370"));
         WMTSTileSource testSource = new WMTSTileSource(testImageryWALLONIE);
-    }
+        Bounds wallonieBounds = new Bounds(
+                new LatLon(49.485372459967245, 2.840548314430268),
+                new LatLon(50.820959517561256, 6.427849693016202)
+                );
+        verifyBounds(wallonieBounds, testSource, 10, 20324, 17724);
+
+    }
+
+    private void verifyBounds(Bounds bounds, WMTSTileSource testSource, int z, int x, int y) {
+        LatLon ret = new LatLon(testSource.tileXYToLatLon(y, y, z));
+        assertTrue(ret.toDisplayString() + " doesn't lie within: " + bounds.toString(), bounds.contains(ret));
+    }
+
+
 
     @Test
@@ -83,19 +100,30 @@
         int zoomOffset = 10;
 
+        // Linz - 11/1105/709.png
         verifyMercatorTile(testSource, 0, 0, 1, zoomOffset);
+        verifyMercatorTile(testSource, 1105, 709, 2, zoomOffset);
+        verifyMercatorTile(testSource, 1, 1, 1, zoomOffset);
+        verifyMercatorTile(testSource, 2, 2, 1, zoomOffset);
         verifyMercatorTile(testSource, 0, 0, 2, zoomOffset);
         verifyMercatorTile(testSource, 1, 1, 2, zoomOffset);
-        for (int x = 0; x < 4; x++) {
-            for (int y = 0; y < 4; y++) {
+
+
+        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++) {
                 verifyMercatorTile(testSource, x, y, 3, zoomOffset);
             }
         }
-        for (int x = 0; x < 8; x++) {
-            for (int y = 0; y < 4; y++) {
+        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);
+        verifyMercatorTile(testSource, 2<<9 - 1, 2<<8 - 1, zoomOffset);
 
         assertEquals("TileXMax", 1, testSource.getTileXMax(1));
@@ -122,6 +150,7 @@
         assertEquals("TileYMax", 74, 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&TileMatrix=EPSG:4326:0&TileRow=1&TileCol=1",
+                "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&"
+                + "tileMatrix=EPSG:4326:0&tileRow=1&tileCol=1",
                 testSource.getTileUrl(1,  1,  1));
     }
