As an example I have a BSB file from NOAA which is a navigational map. The map has a SRS of WGS84 and a Mercator projection.

I initially translate the file from BSB format to GTiff format using the following command:

gdal_translate -of GTiff 18554_1.kap noaa.tif

I can view this GTiff file using Paint Shop Pro quite fine. I then translate the file from GTiff format to VRT format using the following command:

gdal_translate -of vrt –expand rgba noaa.tif noaa.vrt

I then try to generate a tile matrix set using the following command:

gdal2tiles.py noaa.vrt tiles_folder

Where the map tiles are located in tiles_folder. If I try to open the openmaps.html or google.html, the map zoom in and out looks good. However, on tiles generator like MBTiles, I see tiles swap. See the image below. I am pulling my hair off to get the tiles to line up. I am guessing it is projection error. I am not understanding those projection types. It would great someone can explain it while finding the error. I am planning to use on a tiling airplane maps/charts app like this. By the way, on GDAL how do I specify the BSB (reference) file that is associated with KAP (data) file? I have seen cases where only one BSB file referred by multiple KAP files.

Gdal2tiles.py Tile Image Swaped

  • It might be more revealing to characterize the problem not as a "swap" but as a top-to-bottom reversal of the tiles. That suggests @Andre Joost's answer may be on target.
    – whuber
    Jul 7, 2013 at 20:36
  • As you say, the .bsb file is a reference file. It is quite common for the bsb charts to have multiple 'panels'. One .kap file for each panel and one bsb for the whole 'chart'. I think it is doubtful that gdal will ever use the .bsb file as it is primarily concerned with imagery, but then again it is kind of like a vrt...kind of. so maybe someday.
    – s_a
    Jul 11, 2013 at 5:50

2 Answers 2


I noticed that gdal2tiles numbers the tiles from south to north (according to the TMS specification), while Openstreetmap and others do it from north to south. For my personal use, I changed the code of gdal2tiles to get it right again.

See also: http://osgeo-org.1560.x6.nabble.com/gdal2tiles-tiles-in-wrong-hemisphere-and-or-Openlayers-problem-td3742809.html


This is what I changed in Version 19288 2010-04-02:

line 1186, insert one line:

ty1=(2**tz - 1) - ty


tilefilename = os.path.join(self.output, str(tz), str(tx), "%s.%s" % (ty1, self.tileext))

line 1330, insert

ty2=(2**tz - 1) - ty


tilefilename = os.path.join( self.output, str(tz), str(tx), "%s.%s" % (ty2, self.tileext) )

line 1363, insert

y2=(2**(tz+1) - 1) - y


dsquerytile = gdal.Open( os.path.join( self.output, str(tz+1), str(x), "%s.%s" % (y2, self.tileext)), gdal.GA_ReadOnly)

Line numbers may have changed in current gdal 1.10.0.

  • how do you change the code in gdal2tiles.py. I am not good at python. I am not sure what sections on gdal2tiles.py where I need to edit the code.
    – user914425
    Jul 11, 2013 at 19:17
  • See my extended answer.
    – AndreJ
    Jul 12, 2013 at 14:25
  • Andre, I will let you know as soon as I make above changes to gdal2tiles.py. Thank you so much for doing all that effort to identify lines of code needs to be changed.
    – user914425
    Jul 22, 2013 at 18:26
  • 2
    @AndreJ, thank you so much for this answer - I was having issues getting tiles to work in ArcGIS Online, and your suggested edits worked for me. For anyone else who encounters this issue I also found an updated gdal2tiles.py file which includes support for both TMS and XYZ tile naming formats, along with some other enhancements. It can be found here: github.com/pramsey/gdal2tilesp Oct 12, 2015 at 3:16

I ran into a similar issue with using leafletjs, and I resolved it by setting the tms flag to true (no script changes required). See http://leafletjs.com/reference.html#tilelayer-tms.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.