Skip to content

Instantly share code, notes, and snippets.

@csprance
Last active May 9, 2019 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csprance/6d96c77ed18f6b77d7728438f890d117 to your computer and use it in GitHub Desktop.
Save csprance/6d96c77ed18f6b77d7728438f890d117 to your computer and use it in GitHub Desktop.
GDAL2Tiles Instructions

Using GDAL2Tiles

Requirements:

gdal2tiles.py [-p profile] [-r resampling] [-s srs] [-z zoom]
              [-e] [-a nodata] [-v] [-h] [-k] [-n] [-u url]
              [-w webviewer] [-t title] [-c copyright]
              [-g googlekey] [-b bingkey] input_file [output_dir]

As you might suspect by the arguments, this utility is also able to automatically create a web viewer for the created tiles. In our case, as we want to do things manually and use Leaflet, we won't generate the viewers (hence, parameter 'w' will have the 'none' value).

Regarding the other parameters: profile: the default is "mercator". In our case the image is not georeferenced, so the value should be "raster" resampling: will use the default value "average" SRS: Our image doesn't have any SRS, so no value here resume mode ('e'): No, so don't use parameter Zoom level. See below: The "zoom level" parameter is relatively easy to grasp:

On a Leaflet map (or Google/Bing), the furthest way zoom level (whole world on a single tile) is 0 (zero). This corresponds to a 256x256 square image. With each zoom level the square size is multiplied by 2, thus:

  • Zoom level 0: 256 px
  • Zoom level 1: 512 px
  • Zoom level 2: 1024 px
  • Zoom level 3: 2048 px
  • Zoom level 4: 4096 px
  • Zoom level 5: 8192 px
  • Zoom level 6: 16384 px
  • (and so on)

So, in my case the image is 16017x16017, so which zoom level to choose? Well, we can choose a max of 6, but this will make the map have 367 pixels of empty white space (16384-16017). So, we have three options here: Embrace the white bands and do nothing Resize the image so that it matches precisely a power of 2 (256, 512, ..., 16384) Convert it to a GeoTIFF file (which is georeferenced), where the corners match the world map corner coordinates. For now I'll go for option 2, using another tool from GDAL to resize the image. So, to resize it to 16384 and keep it as a JPEG file I've used the following command: gdal_translate -of JPEG -out size 16384 16384 eso1119a.jpg eso.jpg

meaning: gdal_translate -of JPEG -out size «target witdth» «target height» «source file» «target file»

Now we're ready to run the GDAL2Tiles: gdal2tiles.py -p raster -z 0-6 -w none eso.jpg

Just as simple as that. Wait a couple of minutes and you should have a folder with the following structure: eso / «z» / «x» / «y».jpg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment