Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

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.

share|improve this answer
    
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! –  tomtaylor Jan 17 '12 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.

share|improve this answer

Read my answer here, it can give some info about your needs.

Beside this code you have to loop your polygon coordinate adding to 256 pixel tile length. If you don't achieve it, I can write code for you.

I hope it helps you....

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.