# How to list all names of tile inside a given polygon?

I'd like to generate a specific set of slippy map tiles to cover certain a certain land mass.

Given a polygon shapefile, is there something that will quickly generate a listing of the z/x/y tile names that sit inside that polygon at a specific zoom level? Ideally, it would allow for buffer around the polygon to give some space.

Not 100% sure if this would work but one possible algorithm would be to get the bounding box of the shapefile, convert this to Spherical Mercartor (see here for some tips), then work out the tile extent. This might go something like this:

``````xmaxextent = 40075016.685578488 (max extent of spherical mercartor x)
ymaxextent = 40075016.685578488 (max extent of spherical mercartor y)

mintile_x = ((min x of bbox + (xmaxextent / 2)) / xmaxextent) * (2 ^ (zoomlayer + 1))
maxtile_x = ((max x of bbox + (xmaxextent / 2)) / xmaxextent) * (2 ^ (zoomlayer + 1))
mintile_y = ((min y of bbox + (ymaxextent / 2)) / ymaxextent) * (2 ^ (zoomlayer + 1))
maxtile_y = ((max y of bbox + (ymaxextent / 2)) / ymaxextent) * (2 ^ (zoomlayer + 1))
``````

You will have to round the min numbers down and the max numbers up because they will be calculated as decimals, but essentially that should give you the range for tile extents for any given zoom layer, i.e. (@z2,5-6x,4-5y). You'll then probably need to write a script or something to convert those ranges into real tile numbers. In this hypothetical case that would be:

2/5/4

2/5/5

2/6/4

2/6/5

This link gives some useful background info and a python script to work out the tile number corresponding to any given lat/lon.

• Thanks, but that's not quite what I'm looking for. I want the tiles for only the area inside polygon, not the bounding box surrounding the polygon. It's a bit trickier! Jan 17, 2012 at 21:09

Depending on how exact you want to be about 'inside' versus 'outside', a good option would be using Shapely for your point-in-polygon search, and then generating all possible tiles within the bounding box and testing each (by centerpoint, or by constructing a rectangle geometry) for whether they're in or out.