Answers to: Given a polygon path, how do I find the number of tiles?https://help.openstreetmap.org/questions/40761/given-a-polygon-path-how-do-i-find-the-number-of-tiles<p>Given a latitude and longitude, I can find the correct tile number.
In Javascript like <a href="http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#ECMAScript_.28JavaScript.2FActionScript.2C_etc..29">this</a></p>
<pre><code>function long2tile(lon,zoom) {
return (Math.floor((lon+180)/360*Math.pow(2,zoom)));
}
function lat2tile(lat,zoom) {
return (Math.floor((1-Math.log(Math.tan(lat*Math.PI/180) + 1/Math.cos(lat*Math.PI/180))/Math.PI)/2 *Math.pow(2,zoom)));
}`
</code></pre>
<p>But how can i find all tiles by given polygon?</p>enSat, 07 Feb 2015 01:33:57 +0000Answer by sanserhttps://help.openstreetmap.org/questions/40761/given-a-polygon-path-how-do-i-find-the-number-of-tiles/40832<p>Your question implicitly triggers another, even more important vector mapping related question, namely - is it necessary to pre-create the inner/trivial tiles of a polygon/area or those can be regenerated on the fly. I will try to answer both but on a generalized algorithmic level. <br>
I assume we have a common understanding of tiles, a tiled coordinate space, of tile referencing, when a point belongs to a tile (inside or on the left or top edge), a tile coverage ... and a like. Further, you have some basic and effective functions like: pointTOpolygon(...) that checks the relation of a given point to a given polygon (on, inside, outside); pointTOline(...) that checks the relation of a point to a (straight) line (on, one-side, other-side) ... and a like. So, the major algorithm steps are: <br>
1. Find the tile coverage/matrix of the polygon's bounding box (detect the tiles containing three corners of the box the rest of the coverage is then predefined). All the other tiles are of no interest further. <br>
2. For any edge-vectors of the polygon detect the tile candidates that might have common points with the edge. The candidates are quickly detectable (early filtering). They are never under, over, left or right to the vector. <br>
3. For any tile candidates check whether it has a common point with the edge-vector. If any two corner points are on the different sides of the line defined by the edge, or the edge is horizontal and has common points with the top tile-edge (excluding the top-right corner), or the polygon edge is vertical and has common points with the left tile edge (excluding the bottom-left corner) then mark the tile candidate as a member of the polygon tile coverage. <br>
Note that the tile coverage (the set of the marked tiles) is still organized in a row/column structure. In any of the tile rows the consecutive marked tiles are forming tile-runs. Any of the rows must have at least one run. If more runs exist they are separated by empty/unmarked tiles. Let us denote the runs R1, R2 ... Rn from the left to right in a tile row. Now, if you want the inner/trivial tiles (if any), the next procedure might be used to detect them: <br>
4. In any tile rows, where the number of runs is n>1, start with the first pair of runs (Rf,Rs) from the left. Select a point P (example gratia, the central point) of the first empty/unmarked tile just to the right from Rf. If P is inside the polygon, then all the tiles between Rf and Rs are inner/trivial tiles (you may mark them). Reset n-=2 and if n>1 take the pair to the right from Rs as (Rf,Rs). If P is not inside the polygon then all the tiles between Rf and Rs are outside the polygon and may be just ignored. In this case reset n-=1 and if n>1 take the pair to the right of Rs as (Rf,Rs). Finally, if n>1 just repeat the procedure from selecting the point P. <br>
Now, in this way you have all the none trivial tiles covering the polygon (area borders) and the trivial tiles that are inside the polygon (area). </p>sanserSat, 07 Feb 2015 01:33:57 +0000https://help.openstreetmap.org/questions/40761/given-a-polygon-path-how-do-i-find-the-number-of-tiles/40832