[GIS] How to create tiles from tiff file using python

gdalgdal2tilesgeotiff-tiffpythontiles

I'm trying to create python script that will batch create tiles from tiff file.
Using python gdal2tiles.py -s EPSG:4326 -z 11-17 -w none tif_4326.tif I get folder with png files, but this pngs is not georeferenced. When I load tiles in QGIS for verification, they are displayed in a completely different place. What am I doing wrong? And units-per-pixel="76.43702827148438" from tilemapresource.xml looks too big, is this pixel value correct?

$ gdalinfo ~/projects/tif_4326/11/1220/1212.png

Driver: PNG/Portable Network Graphics
Files: /home/roman/projects/tif_4326/11/1220/1212.png
Size is 256, 256
Coordinate System is `'
Image Structure Metadata:
  INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left  (    0.0,    0.0)
Lower Left  (    0.0,  256.0)
Upper Right (  256.0,    0.0)
Lower Right (  256.0,  256.0)
Center      (  128.0,  128.0)
Band 1 Block=256x1 Type=Byte, ColorInterp=Red
  Mask Flags: PER_DATASET ALPHA 
Band 2 Block=256x1 Type=Byte, ColorInterp=Green
  Mask Flags: PER_DATASET ALPHA 
Band 3 Block=256x1 Type=Byte, ColorInterp=Blue
  Mask Flags: PER_DATASET ALPHA 
Band 4 Block=256x1 Type=Byte, ColorInterp=Alpha

Original file info:
$ gdalinfo tif_4326.tif

Driver: GTiff/GeoTIFF
Files: tif_4326.tif
Size is 11418, 7692
Coordinate System is:
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0],
    UNIT["degree",0.0174532925199433],
    AUTHORITY["EPSG","4326"]]
Origin = (34.610612291795853,31.533709086005583)
Pixel Size = (0.000015441030299,-0.000015441030299)
Metadata:
  AREA_OR_POINT=Area
  TIFFTAG_RESOLUTIONUNIT=2 (pixels/inch)
  TIFFTAG_XRESOLUTION=820
  TIFFTAG_YRESOLUTION=820
Image Structure Metadata:
  INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left  (  34.6106123,  31.5337091) ( 34d36'38.20"E, 31d32' 1.35"N)
Lower Left  (  34.6106123,  31.4149367) ( 34d36'38.20"E, 31d24'53.77"N)
Upper Right (  34.7869180,  31.5337091) ( 34d47'12.90"E, 31d32' 1.35"N)
Lower Right (  34.7869180,  31.4149367) ( 34d47'12.90"E, 31d24'53.77"N)
Center      (  34.6987651,  31.4743229) ( 34d41'55.55"E, 31d28'27.56"N)
Band 1 Block=11418x1 Type=Byte, ColorInterp=Red
Band 2 Block=11418x1 Type=Byte, ColorInterp=Green
Band 3 Block=11418x1 Type=Byte, ColorInterp=Blue

Info for tilemapresource.xml in root folder:

<TileMap version="1.0.0" tilemapservice="http://tms.osgeo.org/1.0.0">
<Title>tif_4326.tif</Title>
<Abstract/>
<SRS>EPSG:900913</SRS>
<BoundingBox minx="34.61061229179586" miny="31.41493042161355" maxx="34.78692576493047" maxy="31.53370908600559"/>
<Origin x="34.61061229179586" y="31.41493042161355"/>
<TileFormat width="256" height="256" mime-type="image/png" extension="png"/>
<TileSets profile="mercator">
<TileSet href="11" units-per-pixel="76.43702827148438" order="11"/>
<TileSet href="12" units-per-pixel="38.21851413574219" order="12"/>
<TileSet href="13" units-per-pixel="19.10925706787109" order="13"/>
<TileSet href="14" units-per-pixel="9.55462853393555" order="14"/>
<TileSet href="15" units-per-pixel="4.77731426696777" order="15"/>
<TileSet href="16" units-per-pixel="2.38865713348389" order="16"/>
<TileSet href="17" units-per-pixel="1.19432856674194" order="17"/>
</TileSets>
</TileMap>

Best Answer

The individual image tiles which gdal2tiles.py creates and stores into directory structure are not georeferenced. Therefore for example gdalinfo sees that every tile has its origin at (0 0). However, the auxiliary tilemapresource.xml contains all the information that is needed for georeferencing.

There is a TileLayer plugin for QGIS which can interpret the tilemapresource.xml file and show your tiles correctly. Find details about how to use it with gdal2tiles from gdal2tiles and qgis.

Related Question