[Gdal-dev] create 256x256 tiles for google maps

Jan Hartmann j.l.h.hartmann at uva.nl
Tue Apr 10 06:22:29 EDT 2007


Hi Steve,

If I understand you right, you would like to split a map from (e.g.) 0/0 
to 1/1 degree into (say) 20 by 20 parts. A shell-script  for that would be:

north=1
east=1
let size = 1/20
y=0
while [ $y -lt $north ]
do
   let yto=$y+$size
   x=0
   while [ $x -lt $east ]
   do
     outtif=t_${y}_$x.tif
     let xto=$x+$size
     gdal_translate -projwin $x $yto $xto $y in.tif $outtif
### Note upper y first!
     let x=$x+$size
   done
   let y=$y+$size
done

To align the tiles correctly you only need to set appropriate start and 
interval values.

For conversion between UTM and latlon, you can use gdalwarp. The safest 
way would be to project the complete input file and tile from the output 
file. If that isn't feasible, you can project and tile in one step:

gdalwarp -s_srs ... -t_srs ... -te xmin ymin xmax ymax -tr xres yres 
in.tif out.tif

but I always get confused by the different number systems of input and 
output. Better do it in two steps.

I haven't looked yet at gdalsplit, but I wouldn't expect it to work much 
faster than this script, not orders of magnitude, that is. To be sure, 
gdal_translate has to be started anew for every inner loop cycle, but 
that is not as inefficient as it looks. Even after finishing, it remains 
in the OS cache, and will be loaded again from unswapped memory. That is 
also the reason, by the way, that Mapserver CGI is not dramatically 
faster than a FastCGI solution, or MapScript with PHP-CGI than 
PHP-Module. The second ones are faster for sure, but as a rule the CGI 
versions remain in memory between successive calls too, so the only 
overhead is in initializing code and data stacks.

And then, I do not tile large files that often. It needs to be done at 
times, but after that the results ar permanently available and can be 
used time and again.

Happy Easter,

Jan


Stephen Woodbridge wrote:
> Hi Jan,
> 
> Great script, thank you for sharing it.
> 
> You mentioned working with projected coordinates below. I would like to 
> do something along the lines gdalsplit, but be able to define the height 
> and width on projected coordinates, like 0.0625 degrees and have it 
> split the src image into tiles that size but the edges aligned to hit 
> 1.0 degree intervals. So like 42.0, 42.0625, 42.125, ...
> 
> Is there an easy way to do this in a script?
> Would it be easy to modify gdalsplit to do something like this?
> What if the src image is in some UTM projection and you want the tiles 
> in geographic projection?
> 
> Inquiring minds want to know :)
> 
> -Steve W
> 
> Jan Hartmann wrote:
>>
>>
>> Markus Neteler wrote:
>>> On Thu, Apr 05, 2007 at 09:43:57PM +0200, Mateusz Loskot wrote:
>>>> Paul Van Deusen napisa?(a):
>>>>> I am using imagemagick to crop a geotiff file into 256x256 gif 
>>>>> files to
>>>>> serve as tiles for google maps.
>>>>> However, it starts to choke once the tiff file gets larger than  1/2
>>>>> GB.   Can gdal_translate take
>>>>> a tif file and split it into individual tile files of a specific size?
>>>> Paul,
>>>>
>>>> AFAIK, GDAL won't help here out of the box.
>>>> Users usually take gdal_translate.cpp program and customize it to do 
>>>> this
>>>> job.
>>>> Here you can find such utility called gdalsplit and posted to the 
>>>> list a
>>>> longer while ago:
>>>>
>>>> http://lists.maptools.org/pipermail/gdal-dev/2004-September/004135.html
>>>>
>>>> May be it will work for you.
>>>
>>> If anyone happens to haave an updated version of "gdalsplit"
>>> I would be interested.
>>> Maybe some mechanism to manage user contributions would be
>>> good (like the GRASS Addons repository).
>>>
>>> Markus
>>>
>>> ------------------
>>> ITC -> dall'1 marzo 2007 Fondazione Bruno Kessler
>>> ITC -> since 1 March 2007 Fondazione Bruno Kessler
>>> ------------------
>>> _______________________________________________
>>> Gdal-dev mailing list
>>> Gdal-dev at lists.maptools.org
>>> http://lists.maptools.org/mailman/listinfo/gdal-dev
>>>
>>
>> Perhaps I am overlooking something, or didn't understand the question 
>> right, but I regularly create tiles from a large tif-file with the 
>> -srcwin <xoff yoff xsize ysize> parameter of gdal_translate. A simple 
>> bash-script to tile in.tif (30000*30000 pixels) into 256*256 tiles 
>> would be:
>>
>> width=30000
>> height=30000
>> y=0
>> while [ $y -lt $height ]
>> do
>>   x=0
>>   while [ $x -lt $width ]
>>   do
>>      outtif=t_${y}_$x.tif
>>      gdal_translate -srcwin $x $y 256 256 in.tif $outtif
>>      let x=$x+256
>>   done
>>   let y=$y+256
>> done
>>
>> This would result in about 14000 files. This is so simple that it can 
>> even be done with a primitive DOS batch file.
>>
>> Most of the time I work with projected coordinates (-projwin instead 
>> of -srcwin) but the idea is the same. Note by the way that -projwin 
>> uses the *upper* left corner as reference.
>>
>> Jan
>>
>> Dr. J. Hartmann
>> Department of Geography
>> University of Amsterdam
>> _______________________________________________
>> Gdal-dev mailing list
>> Gdal-dev at lists.maptools.org
>> http://lists.maptools.org/mailman/listinfo/gdal-dev
> 
> 



More information about the Gdal-dev mailing list