1

I have a folder full of tiles, and I'd like to convert them into a single GeoTIFF file.

The folder structure looks like this:

10 //the zoom
    554 //y
        860.jpg //x
        861.jpg
        862.jpg

Not quite sure what that structure is called Mapbox Tiles? XYZ? Slippy tiles?

Either way, I tried the following:

gdal_translate.exe -if MBTiles -of GTiff "C:\maps\tiles" "C:\maps\output.tiff"

But it returns:

ERROR 4: "C:\maps\tiles": Permission denied

Does GDAL support conversion of folders full of tiles?

Fidel
  • 115
  • 7
  • 1
    does your user have permission to that directory? – Ian Turton Nov 13 '21 at 11:21
  • 2
    I was under the impression that the MBTiles format was sqlite DB based not file based (see GDAL MBTiles driver doc and the MBTiles specification) – user2856 Nov 13 '21 at 11:38
  • 1
    If your folder is full of georeferenced Tiff files, you could create a virtual raster (VRT) first and translate it to a single GeoTIFF file afterwards. See https://gis.stackexchange.com/questions/230553/merging-all-tiles-from-one-directory-using-gdal/230588#230588 for more info. – christoph Nov 13 '21 at 13:14
  • If your tiles are not georeferenced, you need to create World files for each of them at first step. – christoph Nov 13 '21 at 13:21
  • Thanks guys. @Ian - yes they have permission. – Fidel Nov 13 '21 at 13:23
  • @user2856 I updated the question with the folder structure. – Fidel Nov 13 '21 at 13:23
  • @Christoph thanks, that sounds good. The tiles are plain jpgs, but their place in the folder structure tells you their coords, in accordance with this equation: https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#C.23_2 – Fidel Nov 13 '21 at 13:23
  • Guess you need something like gdal2tiles.py, but the other way round. Maybe you could look into MapProxy (https://mapproxy.org). There you could configure your tile directory as file cache and try to reproduce the Tile Map Service. – christoph Nov 13 '21 at 13:35
  • 3
    The GDAL driver that supports using a x/y/z folder as source is WMS driver with the TMS minidriver https://gdal.org/drivers/raster/wms.html#tms. – user30184 Nov 13 '21 at 13:44

2 Answers2

1

You can do this, by saving it in a text file and changing the extension to .bat:

set pathin=F:\mydirectoryname\In
set pathout=F:\mydirectoryname\Out
set filename=merged_file

gdalbuildvrt -allow_projection_difference index.vrt %pathin%*.tif gdal_translate index.vrt %pathout%%filename%.tif -tr 0.3 0.3 -co COMPRESS=LZW -co BIGTIFF=YES -a_nodata 0

Then simply run the bat file. This has the cool bonus of resampling if needed. I do this through the OSGeo4W shell that comes with QGIS - there you already have the GDAL library.

miln40
  • 1,111
  • 12
  • 20
0
  1. Download GDAL. Windows binaries can be found here.

  2. Create a file called tiles.xml, which describes your folder structure. For example:

    <GDAL_WMS>
        <Service name="TMS">
            <ServerUrl>file://C:/map data/Sydney/${z}/${y}/${x}.jpg</ServerUrl>
            <!--<ServerUrl>http://localhost:5000/map%20data/Sydney/${z}/${y}/${x}.jpg</ServerUrl>-->
    
        &lt;Layer&gt;basic&lt;/Layer&gt;
        &lt;Format&gt;jpg&lt;/Format&gt;
    &lt;/Service&gt;
    
    &lt;DataWindow&gt;
        &lt;TileLevel&gt;12&lt;/TileLevel&gt;
        &lt;TileY&gt;-360&lt;/TileY&gt;
        &lt;YOrigin&gt;top&lt;/YOrigin&gt;
    &lt;/DataWindow&gt;
    
    &lt;BlockSizeX&gt;256&lt;/BlockSizeX&gt;
    &lt;BlockSizeY&gt;256&lt;/BlockSizeY&gt;
    &lt;BandsCount&gt;3&lt;/BandsCount&gt;
    &lt;ZeroBlockHttpCodes&gt;404&lt;/ZeroBlockHttpCodes&gt;
    &lt;MaxConnections&gt;1&lt;/MaxConnections&gt;
    

    </GDAL_WMS>

  3. Open a console window and set up an environment variable:

    set PROJ_LIB="C:\Portable\gdal\release-1928-x64-gdal-3-3-3-mapserver-7-6-4\bin\proj7\share"
    
  4. Run the following commands:

    cd "C:\Portable\gdal\release-1928-x64-gdal-3-3-3-mapserver-7-6-4\bin\gdal\apps\"
    

    gdal_translate.exe -of GTiff "C:\map data\Sydney\tiles.xml" "C:\map data\sydney.tiff" -projwin 151.033285 -33.743981 151.300567 -33.957583

Information about gdal_translate.exe can be found here.

Information about the WMS/TMS xml file can be found here.

Fidel
  • 115
  • 7