Index: /trunk/data/projection/epsg
===================================================================
--- /trunk/data/projection/epsg	(revision 8583)
+++ /trunk/data/projection/epsg	(revision 8584)
@@ -22,13 +22,13 @@
 <2154> +proj=lcc +lat_0=46.5 +lat_1=44 +lat_2=49 +lon_0=3 +x_0=700000 +y_0=6600000 +ellps=GRS80 +nadgrids=null +units=m +bounds=-5.5,41,10.2,51  <>
 # PUWG 2000 Zone 5 (Poland)
-<2176> +proj=tmerc +lon_0=15 +k_0=0.999923 +x_0=5500000 +ellps=GRS80 +nadgrids=null +units=m +bounds=13.5,49,16.5,54.84  <>
+<2176> +proj=tmerc +lon_0=15 +k_0=0.999923 +x_0=5500000 +ellps=GRS80 +nadgrids=null +units=m +axis=neu +bounds=13.5,49,16.5,54.84  <>
 # PUWG 2000 Zone 6 (Poland)
-<2177> +proj=tmerc +lon_0=18 +k_0=0.999923 +x_0=6500000 +ellps=GRS80 +nadgrids=null +units=m +bounds=16.5,49,19.5,54.84  <>
+<2177> +proj=tmerc +lon_0=18 +k_0=0.999923 +x_0=6500000 +ellps=GRS80 +nadgrids=null +units=m +axis=neu +bounds=16.5,49,19.5,54.84  <>
 # PUWG 2000 Zone 7 (Poland)
-<2178> +proj=tmerc +lon_0=21 +k_0=0.999923 +x_0=7500000 +ellps=GRS80 +nadgrids=null +units=m +bounds=19.5,49,22.5,54.84  <>
+<2178> +proj=tmerc +lon_0=21 +k_0=0.999923 +x_0=7500000 +ellps=GRS80 +nadgrids=null +units=m +axis=neu +bounds=19.5,49,22.5,54.84  <>
 # PUWG 2000 Zone 8 (Poland)
-<2179> +proj=tmerc +lon_0=24 +k_0=0.999923 +x_0=8500000 +ellps=GRS80 +nadgrids=null +units=m +bounds=22.5,49,25.5,54.84  <>
+<2179> +proj=tmerc +lon_0=24 +k_0=0.999923 +x_0=8500000 +ellps=GRS80 +nadgrids=null +units=m +axis=neu +bounds=22.5,49,25.5,54.84  <>
 # PUWG 1992 (Poland)
-<2180> +proj=tmerc +lon_0=19 +k_0=0.9993 +x_0=500000 +y_0=-5300000 +ellps=GRS80 +nadgrids=null +units=m +bounds=14.12,49,24.15,54.84  <>
+<2180> +proj=tmerc +lon_0=19 +k_0=0.9993 +x_0=500000 +y_0=-5300000 +ellps=GRS80 +nadgrids=null +units=m +axis=neu +bounds=14.12,49,24.15,54.84  <>
 # UTM France (DOM) Fort Marigot
 <2969> +proj=tmerc +lon_0=-63 +k_0=0.9996 +x_0=500000 +ellps=intl +towgs84=136.596,248.148,-429.789 +units=m +bounds=-63.25,17.6,-62.5,18.5  <>
@@ -46,9 +46,9 @@
 <3004> +proj=tmerc +lat_0=0 +lon_0=15 +k=0.9996 +x_0=2520000 +y_0=0 +ellps=intl +units=m +bounds=11,34,24,48 <>
 # SWEREF99 13 30 / EPSG:3008 (Sweden)
-<3008> +proj=tmerc +lon_0=13.5 +x_0=150000 +ellps=GRS80 +nadgrids=null +units=m +bounds=12.1,55.2,14.65,62.26  <>
+<3008> +proj=tmerc +lon_0=13.5 +x_0=150000 +ellps=GRS80 +nadgrids=null +units=m +axis=neu +bounds=12.1,55.2,14.65,62.26  <>
 # LKS-92 (Latvia TM)
-<3059> +proj=tmerc +lon_0=24 +k_0=0.9996 +x_0=500000 +y_0=-6000000 +ellps=GRS80 +nadgrids=null +units=m +bounds=-180,-90,180,90  <>
+<3059> +proj=tmerc +lon_0=24 +k_0=0.9996 +x_0=500000 +y_0=-6000000 +ellps=GRS80 +nadgrids=null +units=m +axis=neu +bounds=-180,-90,180,90  <>
 # Lambert Zone (Estonia)
-<3301> +proj=lcc +lat_0=57.51755393055556 +lat_1=59.333333333333336 +lat_2=58 +lon_0=24 +x_0=500000 +y_0=6375000 +ellps=GRS80 +nadgrids=null +units=m +bounds=21.64,56.05,28.58,61.13  <>
+<3301> +proj=lcc +lat_0=57.51755393055556 +lat_1=59.333333333333336 +lat_2=58 +lon_0=24 +x_0=500000 +y_0=6375000 +ellps=GRS80 +nadgrids=null +units=m +axis=neu +bounds=21.64,56.05,28.58,61.13  <>
 # Belgian Lambert 2008
 <3812> +proj=lcc +lat_0=50d47'52.134" +lat_1=49d50'0" +lat_2=51d10'0" +lon_0=4d21'33.177" +x_0=649328 +y_0=665262 +ellps=GRS80 +nadgrids=null +units=m +bounds=2.54,49.51,6.4,51.5  <>
@@ -74,7 +74,7 @@
 <3950> +proj=lcc +lat_0=50 +lat_1=49.25 +lat_2=50.75 +lon_0=3 +x_0=1700000 +y_0=9200000 +ellps=GRS80 +nadgrids=null +units=m +bounds=-5.5,47.5,10.2,51.1  <>
 # ETRS89
-<4258> +proj=lonlat +ellps=GRS80 +datum=GRS80 +bounds=-180,-90,180,90  <>
+<4258> +proj=lonlat +ellps=GRS80 +datum=GRS80 +axis=neu +bounds=-180,-90,180,90  <>
 # WGS 84
-<4326> +proj=lonlat +ellps=WGS84 +datum=WGS84 +bounds=-180,-90,180,90  <>
+<4326> +proj=lonlat +ellps=WGS84 +datum=WGS84 +axis=neu +bounds=-180,-90,180,90  <>
 # Swiss Grid (Switzerland)
 <21781> +proj=somerc +lat_0=46d57'8.66" +lon_0=7d26'22.5" +x_0=600000 +y_0=200000 +ellps=bessel +towgs84=674.374,15.056,405.346 +units=m +bounds=5.7,45.7,10.6,47.9  <>
@@ -114,11 +114,11 @@
 <31370> +proj=lcc +lat_0=90 +lat_1=49d50'0.00204" +lat_2=51d10'0.00204" +lon_0=4d22'2.952" +x_0=150000.013 +y_0=5400088.438 +ellps=intl +towgs84=-99.06,53.32,-112.49,0.419,-0.83,1.885,-1.0 +units=m +bounds=2.54,49.51,6.4,51.5  <>
 # Gauß-Krüger Zone 2
-<31466> +proj=tmerc +lon_0=6 +x_0=2500000 +ellps=bessel +nadgrids=BETA2007.gsb +units=m +bounds=3.5,-5,8.5,85  <>
+<31466> +proj=tmerc +lon_0=6 +x_0=2500000 +ellps=bessel +nadgrids=BETA2007.gsb +units=m +axis=neu +bounds=3.5,-5,8.5,85  <>
 # Gauß-Krüger Zone 3
-<31467> +proj=tmerc +lon_0=9 +x_0=3500000 +ellps=bessel +nadgrids=BETA2007.gsb +units=m +bounds=6.5,-5,11.5,85  <>
+<31467> +proj=tmerc +lon_0=9 +x_0=3500000 +ellps=bessel +nadgrids=BETA2007.gsb +units=m +axis=neu +bounds=6.5,-5,11.5,85  <>
 # Gauß-Krüger Zone 4
-<31468> +proj=tmerc +lon_0=12 +x_0=4500000 +ellps=bessel +nadgrids=BETA2007.gsb +units=m +bounds=9.5,-5,14.5,85  <>
+<31468> +proj=tmerc +lon_0=12 +x_0=4500000 +ellps=bessel +nadgrids=BETA2007.gsb +units=m +axis=neu +bounds=9.5,-5,14.5,85  <>
 # Gauß-Krüger Zone 5
-<31469> +proj=tmerc +lon_0=15 +x_0=5500000 +ellps=bessel +nadgrids=BETA2007.gsb +units=m +bounds=12.5,-5,17.5,85  <>
+<31469> +proj=tmerc +lon_0=15 +x_0=5500000 +ellps=bessel +nadgrids=BETA2007.gsb +units=m +axis=neu +bounds=12.5,-5,17.5,85  <>
 # UTM zone 1N
 <32601> +proj=tmerc +lon_0=-177 +k_0=0.9996 +x_0=500000 +ellps=WGS84 +datum=WGS84 +units=m +bounds=-182,-5,-172,85  <>
Index: /trunk/src/org/openstreetmap/josm/data/imagery/TemplatedWMSTileSource.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/data/imagery/TemplatedWMSTileSource.java	(revision 8583)
+++ /trunk/src/org/openstreetmap/josm/data/imagery/TemplatedWMSTileSource.java	(revision 8584)
@@ -139,6 +139,7 @@
         if (baseUrl.toLowerCase().contains("crs=epsg:4326")) {
             switchLatLon = true;
-        } else if (baseUrl.toLowerCase().contains("crs=") && "EPSG:4326".equals(myProjCode)) {
-            switchLatLon = true;
+        } else if (baseUrl.toLowerCase().contains("crs=")) {
+            // assume WMS 1.3.0
+            switchLatLon = Main.getProjection().switchXY();
         }
         String bbox;
Index: /trunk/src/org/openstreetmap/josm/data/imagery/WMTSTileSource.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/data/imagery/WMTSTileSource.java	(revision 8583)
+++ /trunk/src/org/openstreetmap/josm/data/imagery/WMTSTileSource.java	(revision 8584)
@@ -47,4 +47,5 @@
 import org.openstreetmap.josm.data.coor.LatLon;
 import org.openstreetmap.josm.data.projection.Projection;
+import org.openstreetmap.josm.data.projection.Projections;
 import org.openstreetmap.josm.gui.ExtendedDialog;
 import org.openstreetmap.josm.io.CachedFile;
@@ -66,5 +67,5 @@
     private static final String PATTERN_HEADER  = "\\{header\\(([^,]+),([^}]+)\\)\\}";
 
-    private static final String URL_GET_ENCODING_PARAMS = "SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER={layer}&STYLE={style}&"
+    private static final String URL_GET_ENCODING_PARAMS = "SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER={layer}&STYLE={Style}&"
             + "FORMAT={format}&tileMatrixSet={TileMatrixSet}&tileMatrix={TileMatrix}&tileRow={TileRow}&tileCol={TileCol}";
 
@@ -229,7 +230,6 @@
 
         } catch (Exception e) {
-            Main.error(e);
-        }
-        return null;
+            throw new IllegalArgumentException(e);
+        }
     }
 
@@ -268,4 +268,9 @@
             matrixSet.crs = crsToCode(getStringByXpath(matrixSetNode, "SupportedCRS"));
             NodeList tileMatrixList = getByXpath(matrixSetNode, "TileMatrix");
+            Projection matrixProj = Projections.getProjectionByCode(matrixSet.crs);
+            if (matrixProj == null) {
+                // use current projection if none found. Maybe user is using custom string
+                matrixProj = Main.getProjection();
+            }
             for (int matrixId = 0; matrixId < tileMatrixList.getLength(); matrixId++) {
                 Node tileMatrixNode = tileMatrixList.item(matrixId);
@@ -274,5 +279,10 @@
                 tileMatrix.scaleDenominator = Double.parseDouble(getStringByXpath(tileMatrixNode, "ScaleDenominator"));
                 String[] topLeftCorner = getStringByXpath(tileMatrixNode, "TopLeftCorner").split(" ");
-                tileMatrix.topLeftCorner = new EastNorth(Double.parseDouble(topLeftCorner[1]), Double.parseDouble(topLeftCorner[0]));
+
+                if(matrixProj.switchXY()) {
+                    tileMatrix.topLeftCorner = new EastNorth(Double.parseDouble(topLeftCorner[1]), Double.parseDouble(topLeftCorner[0]));
+                } else {
+                    tileMatrix.topLeftCorner = new EastNorth(Double.parseDouble(topLeftCorner[0]), Double.parseDouble(topLeftCorner[1]));
+                }
                 tileMatrix.tileHeight = Integer.parseInt(getStringByXpath(tileMatrixNode, "TileHeight"));
                 tileMatrix.tileWidth = Integer.parseInt(getStringByXpath(tileMatrixNode, "TileHeight"));
@@ -291,5 +301,5 @@
     private static String crsToCode(String crsIdentifier) {
         if (crsIdentifier.startsWith("urn:ogc:def:crs:")) {
-            return crsIdentifier.replaceFirst("urn:ogc:def:crs:([^:]*):[^:]*:(.*)$", "$1:$2");
+            return crsIdentifier.replaceFirst("urn:ogc:def:crs:([^:]*):.*:(.*)$", "$1:$2");
         }
         return crsIdentifier;
@@ -439,5 +449,5 @@
         }
         double scale = matrix.scaleDenominator * this.crsScale;
-        EastNorth ret = new EastNorth(matrix.topLeftCorner.getX() + x * scale, matrix.topLeftCorner.getY() - y * scale);
+        EastNorth ret = new EastNorth(matrix.topLeftCorner.east() + x * scale, matrix.topLeftCorner.north() - y * scale);
         return Main.getProjection().eastNorth2latlon(ret).toCoordinate();
     }
@@ -589,5 +599,5 @@
         EastNorth min = proj.latlon2eastNorth(bounds.getMin());
         EastNorth max = proj.latlon2eastNorth(bounds.getMax());
-        return (int) Math.ceil(Math.abs(max.getY() - min.getY()) / scale);
+        return (int) Math.ceil(Math.abs(max.north() - min.north()) / scale);
     }
 
@@ -601,5 +611,5 @@
         EastNorth min = proj.latlon2eastNorth(bounds.getMin());
         EastNorth max = proj.latlon2eastNorth(bounds.getMax());
-        return (int) Math.ceil(Math.abs(max.getX() - min.getX()) / scale);
+        return (int) Math.ceil(Math.abs(max.east() - min.east()) / scale);
     }
 }
Index: /trunk/src/org/openstreetmap/josm/data/projection/CustomProjection.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/data/projection/CustomProjection.java	(revision 8583)
+++ /trunk/src/org/openstreetmap/josm/data/projection/CustomProjection.java	(revision 8584)
@@ -50,4 +50,5 @@
     protected Bounds bounds;
     private double metersPerUnit = METER_PER_UNIT_DEGREE; // default to degrees
+    private String axis = "enu"; // default axis orientation is East, North, Up
 
     /**
@@ -98,5 +99,8 @@
         no_defs("no_defs", false),
         init("init", true),
+        /** crs units to meter multiplier */
         to_meter("to_meter", true),
+        /** definition of axis for projection */
+        axis("axis", true),
         // JOSM extensions, not present in PROJ.4
         wmssrs("wmssrs", true),
@@ -212,4 +216,8 @@
             if (s != null) {
                 this.metersPerUnit = parseDouble(s, Param.to_meter.key);
+            }
+            s = parameters.get(Param.axis.key);
+            if (s != null) {
+                this.axis  = s;
             }
         }
@@ -548,4 +556,10 @@
     }
 
+    @Override
+    public boolean switchXY() {
+        // TODO: support for other axis orientation such as West South, and Up Down
+        return this.axis.startsWith("ne");
+    }
+
     private static Map<String, Double> getUnitsToMeters() {
         Map<String, Double> ret = new ConcurrentHashMap<>();
Index: /trunk/src/org/openstreetmap/josm/data/projection/Projection.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/data/projection/Projection.java	(revision 8583)
+++ /trunk/src/org/openstreetmap/josm/data/projection/Projection.java	(revision 8584)
@@ -79,3 +79,11 @@
      */
     double getMetersPerUnit();
+
+    /**
+     * Does this projection natural order of coordinates is North East,
+     * instead of East North
+     *
+     * @return true if natural order of coordinates is North East, false if East North
+     */
+    boolean switchXY();
 }
Index: /trunk/src/org/openstreetmap/josm/gui/layer/WMTSLayer.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/gui/layer/WMTSLayer.java	(revision 8583)
+++ /trunk/src/org/openstreetmap/josm/gui/layer/WMTSLayer.java	(revision 8584)
@@ -69,9 +69,9 @@
                 return tileSource;
             }
-        } catch (Exception e) {
-            Main.warn("Could not create imagery layer:");
+            return null;
+        } catch (IOException e) {
             Main.warn(e);
+            throw new IllegalArgumentException(e);
         }
-        return null;
     }
 
Index: /trunk/test/data/wmts/getCapabilities-wien.xml
===================================================================
--- /trunk/test/data/wmts/getCapabilities-wien.xml	(revision 8583)
+++ /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 8583)
+++ /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 8583)
+++ /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));
     }
