I'm trying to generate tiles with gdal2tiles.py and the resulting tiles are stored as 18/132639/164219. If I do the same thing with MapTiler (the GUI variant of gdal2tiles) the tile result is 18/132639/97923. I want the results from MapTiler (97923), but just with gdal2tiles, as I want to do this in a batch process and not with a GUI.

I tried a lot of things already, warping, translating, whatever, and it's getting very annoying, especially as it says that MapTiler is just a graphical interface for gdal2tiles and does basically the same.

gdal2tiles.py -z 17-19 -s EPSG:900913 -p raster overlay-level1.png
gdal2tiles.py -z 17-20 -s EPSG:3785 overlay-level1.png
gdal2tiles.py -p geodetic -s EPSG:4326 -z 18-21 -w none overlay-level1.png

Most of those result in an ERROR 5 Access window out of range in RasterIO() or ERROR 5: Illegal values for buffer size.

This one works, but results in the wrong Y values:

gdal2tiles.py -s EPSG:4326 -z 16-19 -w none overlay-level1.png

Any ideas what's wrong there?

  • I tried to produce tiles to publish my map online. Want to get same structure as produce last version 0.5.2 of maptiler but want do that using gdal script. I have tested - last maptiler to compare how should it looks like - old version of maptiler - gdal2tiles.py 25611 2013-02-07 10:24:56Z bishop $ (from qgis valmiera package) - gdal2tiles.py 19288 2010-04-02 18:36:17Z rouault $ + your (Andre Joost) modification http://gis.stackexchange.com/questions/63024/gdal2tiles-maptiles-from-bsb-kap-are-switched
    – user27845
    Mar 9, 2014 at 11:29

5 Answers 5


The solution is much simpler than expected. Just turn TMS settings to true in Leaflet.

L.tileLayer('http://{s}.somedomain.com/{foo}/{z}/{x}/{y}.png', {tms: true});
  • Note that gdal2 gdal2tiles.py (GDAL 2.1.3, released 2017/20/01) uses the "correct" or Google/OSM convention for x vs y, so tiles generated may not need such fixes. Nov 27, 2017 at 21:32
  • @AhmedFasih, It still not works. tested from GDAL 2.3.1
    – Jane
    Jul 16, 2018 at 13:00

gdal2tiles uses the TMS specification for tile naming (bottom to top), while Maptiler allows you to use the Google/Openstreetmap tile naming system (top to bottom).

See my answer to this topic on how to modify gdal2tiles.py for Google/OSM tile naming:

GDAL2Tiles: MapTiles from BSB/KAP are Switched

  • I made the changes, but the Y-numbers remain the same? I'm working with CRC EPSG:4326, which is probably also an issue, as leaflet requires EPSG:3857.
    – user20442
    Jul 25, 2013 at 15:26
  • You should have replaced ty and y by ty2 and y2 in the quoted existing lines as well. My source and tiles were all in EPSG:3857.
    – AndreJ
    Jul 25, 2013 at 16:58
  • Oh ok. Well, since I don't want to change gdal2tiles.py on all involved machines, the tms: true variant is the better solution. Thanks!
    – user20442
    Jul 25, 2013 at 17:09

A late contribution, but perhaps it helps...

As an alternative to changing the code in gdal2tiles.py or adding the special flag to leaflet, the file names of the tiles can be changed. This was an easy fix for creating tiles to use with ArcGIS online. Here is a rough bash script that will toggle the z/x/y convention back and forth between OSM/Google-Maps (gdal2tiles default) and TMS (used in ArcGIS Online)

# Each time it runs it will toggle the convention between OSM/Google-Maps and TMS

for x in $(find $tiledir -name "*.png")

   #echo $x
   # isolate where tile folders begin

   #echo $tiles
   # separate each z,x,y value
   zdir=$(echo $tiles | cut -d'/' -f1-1)
   xdir=$(echo $tiles | cut -d'/' -f2-2)
   ypng=$(echo $tiles | cut -d'/' -f3-3)

   # remove extension from y.png files

   # calculate new y using formula referenced here:
   #  http://osgeo-org.1560.x6.nabble.com/gdal2tiles-tiles-in-wrong-hemisphere-and-or-Openlayers-problem-td3742809.html

   (( newy=(2**zdir - 1)-y ))

   #echo $zdir,$xdir,$y   $newy is newy

   # rename the files
   mv -v $x $tiledir/$zdir/$xdir/$newy.png



This question is a bit old, but there's now an "official" solution to this problem. Since GDAL version 3.1, gdal2tiles.py now includes a --xyz option. (For a full list of options see this page.)

gdal2tiles by default names the tiles based on the TMS tile scheme. Most mapping software (like Google Maps) expect XYZ tiles. The only difference between them is the Y tile coordinate. For TMS, y=0 is at the southern edge of the map. For XYZ, y=0 is at the northern edge. Specifying --xyz when using gdal2tiles will tell it to use the XYZ scheme instead of TMS.

I found that a visual is helpful for understanding these two different schemes. This website from MapTiler includes an interactive map that shows the tiles and tile coordinates for the two schemes.

Usage Example:

gdal2tiles.py --xyz --zoom=3-10 --processes=2 dataset-raster.tif tile-output
  • 1
    Thank you, this should be the accepted answer. Sep 26, 2023 at 16:08

You can download the updated gdal2tiles.py for Google/OSM tiles by link


  • Welcome to GIS@SE, thanks for your answer, however you should explain in your answer what the difference is in your recommended download link as compared to the original question, so that future users will understand as well. best Regards Oct 22, 2014 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.