TileCache has support for KML superoverlays. For any TileCache layer for which the source data is EPSG:4326, the KML Service provides GroundOverlay data with LevelOfDetail NetworkLinks to deeper levels of data.
A simple document which does this – for a given layername – is included in doc/examples/overlay.kml.
This sample document is designed to make available an entire worldwide set of tiles via TileCache: typically, one would create a more complex KML generator that would, for example, specify a LookAt tag as well, so that a user could open the data in Google Earth based on where they were looking. Also, this KML file will let them zoom out – but it means that the server does need to generate (possibly many) KML documents to get the viewer to where they want to be, because the KML Client will need to trawl its way through the entire Pyramid down to where the user is viewing. However, the image data is not included with the KML document, so this should be a lightweight server side operation: it’s just somewhat slow to do many round trips, in general, so specifying (as a top level) some reasonaable compromise between the whole world and the targeted viewing plane might make sense.
TileCache KML documents are based around the TMS URL scheme. This means that for a TMS tile:
http://tile.openaerialmap.org/tiles/1.0.0/openaerialmap/0/0/0.png
the corresponding KML document is
http://tile.openaerialmap.org/tiles/1.0.0/openaerialmap/0/0/0.kml
By default, TileCache uses a ‘whole world’ extent, split into two tiles: western hemisphere and eastern. This means that to get the whole world into Google Earth, you would need to include links to 0/0/0.kml and 0/0/1.kml.
TileCache can calculate a tile z/x/y from a bounding box using the getCell function on a layer. To use this:
>>> import TileCache.Service
>>> s = TileCache.Service.load("tilecache.cfg")
>>> s.layers
{'basic': <TileCache.Layers.WMS.WMS object at ...>}
>>> s.layers['basic']
<TileCache.Layers.WMS.WMS object at ...>
>>> basic = s.layers['basic']
>>> basic.getCell((-180,-90,0,90))
(0, 0, 0)
>>> cell = basic.getCell((-10,-90,12,-80), exact=False)
>>> cell
(8, 0, 3)
Once you’ve done this, you can then use Python to construct a KML doc for the tile:
>>> from TileCache.Layer import Tile
>>> tile = Tile(basic, cell[0], cell[1], cell[2])
>>> from TileCache.Services.KML import KML
>>> kml = KML(s)
>>> doc = kml.generate_kml_doc(tile, base_path="http://example.com/tilecache.cgi", include_wrapper=False)
>>> len(doc)
2546
>>> doc[550:600]
'\n </GroundOverlay>\n <NetworkLink>\n <nam'