[OSM-dev] Code to determine tile boundaries?

Frederik Ramm frederik at remote.org
Mon Feb 26 11:17:18 GMT 2007


Hi,

> I need to determine the boundaries of a given (z-12) tile at home tile.
> Reversing the equatation for mapping longitutes to the x coordinate is
> easy, but I can't find a way to map latitudes to the y coordinate  
> (except
> calculating all possible tiles and caching the southern latitude).
>
> Is there some code somewhere in svn that I could adapt?

If you want Java:

360 	        private int latToTileY(double lat) {
361 	                double l = lat / 180 * Math.PI;
362 	                double pf = Math.log(Math.tan(l) + (1/Math.cos 
(l)));
363 	                return (int) (2048.0 * (Math.PI - pf) / Math.PI);
364 	        }
365 	
366 	        private int lonToTileX(double lon) {
367 	                return (int) (512.0 * (lon + 180.0) / 45.0);
368 	        }
369 	
370 	        private double tileYToLat(int y) {
371 	                return Math.atan(Math.sinh(Math.PI -  
(Math.PI*y / 2048.0))) * 180 / Math.PI;
372 	        }
373 	
374 	        private double tileXToLon(int x) {
375 	                return x * 45.0 / 512.0 - 180.0;
376 	        }

this is from my "slippymap" plugin for JOSM (in SVN). The zoom level  
has been removed from these equations (they are for level 12). I also  
have Java code with the zoom level built in but that's not yet in  
SVN. My code is based on OJW's PHP coordinate converter (http:// 
almien.co.uk/OSM/Tools/Coord/source.php), but I have simplified his  
code a bit based on his hint that "LimitY=ProjectF(85.05111)" is  
actually the number Pi.

Bye
Frederik

-- 
Frederik Ramm  ##  eMail frederik at remote.org  ##  N49°00.09' E008°23.33'






More information about the dev mailing list