[gdal-dev] Re: gdal2tiles: tiles in wrong hemisphere, and/or Openlayers problem

geep999 geep999 at yahoo.co.uk
Sun Nov 13 07:04:18 EST 2011

Even & Klokan,
1/ Thanks for your comments and link. The light has dawned, and I've now
understood the differences in tile naming between
TMS and XYZ tilsets.

I edited the overlays.html created by gdal2tiles.py to use both
OpenLayers.Layer.TMS and OpenLayers.Layer.XYZ.
Rather than create a 'proper' XYZ tileset, I created a function
xyz_getTileURL to allow OpenLayers.Layer.XYZ 
to read from a TMS tileset.

As it might be of interest for others struggling to understand this stuff, I
attach it below.
Or look at the source on:

xyz_getTileURL is just a modification of the existing osm_getTileURL &
with Klokan's formula y = (2^z - 1) - y used to convert the TMS Y to the XYZ

2/ I noted that overlays.html calls:
This doesn't seem to support Openlayers.Layer.XYZ
Maybe a future version of gdaltiles.py could instead reference a newer
version of Openlayers.
e.g. http://www.openlayers.org/api/2.11/OpenLayers.js


		    // create XYZ Overlay layer
		    // This is an exercise for me to use Openlayers.Layer.XYZ, and write
my own getURL function.
		    // (The TMS tileset was created by gdal2tiles.py. 
		    //  So need to convert from the TMS URL to XYZ URL with
		    // Using a ZXY tileset then "getURL: xyz_getTileURL" is not needed and
		    //  the URL would be simply
		    var tiles4_srtm = new OpenLayers.Layer.XYZ("OpenLayers.Layer.XYZ",
			{ type: 'png', getURL: xyz_getTileURL, alpha: true, isBaseLayer: false

		//This function allows a TMS tileset to be used with OpenLayers.Layer.XYZ
	        function xyz_getTileURL(bounds) {
	            var res = this.map.getResolution();
	            var x = Math.round((bounds.left - this.maxExtent.left) / (res *
	            var y = Math.round((this.maxExtent.top - bounds.top) / (res *
	            var z = this.map.getZoom();
	            var limit = Math.pow(2, z);

		        if (mapBounds.intersectsBounds( bounds ) && z >= mapMinZoom && z
<= mapMaxZoom ) {
			  x = ((x % limit) + limit) % limit;
			  y = Math.pow(2,z) - y - 1;
			  return this.url + z + "/" + x + "/" + y + "." + this.type;
			} else {
			  return "http://www.maptiler.org/img/none.png";


View this message in context: http://osgeo-org.1803224.n2.nabble.com/gdal2tiles-tiles-in-wrong-hemisphere-and-or-Openlayers-problem-tp6983306p6989780.html
Sent from the GDAL - Dev mailing list archive at Nabble.com.

More information about the gdal-dev mailing list