Wednesday, May 16, 2012

Manage (Google) maps tiles

Google and other maps come as a collection of many files, each of them representing a precisely defined piece of Earth, often called tiles. Some software tools can store them on your mobile device or computer for later, off-line, usage. One of them is, for example, Android application Maverick (even though, the latest Maverick, because of licensing terms, does not store Google maps but it does the same off-line storing with other maps, such as OpenStreet maps).

The files with tiles have numeric names and they are located in the directories named also numerically, including directories indicating the zoom level of the stored tiles.

For example, in the picture, there is a map of type "Google Hybrid" (this name, by the way, differs depending what platform the map was created at - but the rest is reasonably same); it has several zoom levels - numbers 10, 11 and 12 on the picture; each zoom level has tiles in directories 622, 623, etc.

In almost all cases, you do not need to know this structure. You just copy it to your mobile device (or, even better, it was already created on your mobile device so you even do not need to see this structure. Except one case: when you want to delete some tiles. You may have maps from many different places and you do not need any more some of them. How do you know which tiles to remove and which to keep? That's why I wrote a small script (in Perl) that can help with it.

The core of the script is a Perl module USNaviguide_Google_Tiles written in 2008 by John D. Coryat (who made it available under Apache 2.0 license). This module is able to convert the numerical names of tiles into their geographical coordinates. I wrote a little wrapper, clusterize.pl, around this script that does the following:
  • It creates a KML file with clusters of the tiles (for all or only for a given map type). You can open this file in Google Earth and easily see where are your map tiles from.
  • It generates another Perl script, tiles-remover.pl, that can be used to delete some tile clusters. You can choose to delete all tiles for a particular map type, or just tiles from some clusters.
The script wrapper, including the original John's script is available here. After unpacking, you can invoke it to get all options:
clusterize.pl -help
For example, this invocation:
clusterize -input /home/blah/maps -kml my-tiles.kml
 will read all map tiles from your directory /home/blah/maps and creates a file my-tiles.kml and a file tiles-removal.pl. It was written for Linux.