INTRODUCTION:
The OSM (Open Street Maps) MENU page of this site provides many examples of using Open Street Map tiles for making maps of various regions of Earth.
In making those maps and HTML pages, it became necessary to perform some math calculations (involving logarithms and trigonometric functions) in order to translate latitude-longitude coordinates of an Earth location to the x,y-integers that specify an OSM 'tile' --- for a particular zoom-level (an integer between 0 and 19).
There is information on OSM tiles --- and how to perform calculations to convert between lat-lon numbers (decimal degrees) and x-y numbers (non-negative integers) --- at the following reference.
OSM tiles Reference:
http://wiki.openstreetmap.org/wiki/ Slippy_map_tilenames#Zoom_levels
Brief overview:
OSM tiles are 256x256 pixel
PNG files ---
which have several advantages over
JPEG and
GIF image files.
With each additional zoom-level,
the number of OSM tiles that
cover the (non-polar parts of) Earth
goes up a factor of 2 x 2 = 4.
For each tile, its filename (URL) format is
... tile.openstreetmap.org/zoom/x/y.png
where
zoom=zoom-level-integer (from 0 to 19),
x=longitude-locator-integer (non-negative column integer),
y=latitude-locator-integer (non-negative row integer).
In the link above, see sample code to calculate x and y
for a given zoom-level and a given latitude and longitude.
Tcl Code Sample(s):
The OSM page-link above provides examples of how to do the pertinent 'tile calculations' in several computer languages --- but I often use Tcl (or Tcl and Tk) to make handy utilities. (See the 'tkGooies' at freedomenv.com.)
Tcl is a very nice scripting language to use for various kinds of geometric or scientific calculations. I decided to make a script to perform the 'lat-lon to x-y' calculation --- based on the code at the OSM page whose link is above.
Following is the code for such a Tcl script --- which I put in a file named 'LatLon_to_osmColRow.tcl' --- but you can copy the code into a text editor and save it under whatever filename you prefer. (Remember to give the file execute permission.)
The first line of the script calls on the 'tclsh' (Tcl shell)
interpreter to execute the Tcl commands that follow in the script.
You can change that line to suit your particular operating
environment.
#!/usr/bin/tclsh
set lat_deg 42.164167
set lon_deg -92.026111
## For a view indicating some residential street names:
set zoomlevel 14
## For a view of houses on streets in the area:
# set zoomlevel 16
set TITLE "For Vinton, Iowa - Lat,Lon: $lat_deg , $lon_deg degrees"
set pi [expr {4.0 * atan(1.0)}]
set radsPERdeg [expr {$pi/180.0}]
set lat_rad [expr {$radsPERdeg * $lat_deg}]
set n [expr {pow(2,$zoomlevel)}]
set Nxtile [expr {int( ($lon_deg + 180.0) / 360.0 * $n)}]
set Nytile [expr {int( (1.0 - log(tan($lat_rad) + (1 / cos($lat_rad))) / $pi) / 2.0 * $n)}]
puts "$TITLE"
puts "zoomlevel: $zoomlevel"
puts "Nxtile: $Nxtile"
puts "Nytile: $Nytile"
To run this code on a Linux computer, I simply opened a terminal window (positioned in the directory that contained this script file) and typed './LatLon_to_osmColRow.tcl' and pressed Enter.
Actually, since I do not like typing long strings of characters (even if they are less than 50 characters), I did the 'ls' command and highlighted-and-pasted the filename string at the terminal (shell interpreter) command prompt.
The script showed the following output in the terminal window.
$ ./LatLon_to_osmColRow.tcl
For Vinton, Iowa - Lat,Lon: 42.164167 , -92.026111 degrees
zoomlevel: 14
Nxtile: 4003
Nytile: 6071
For different locations, you can simply edit the script and change the three variables --- lat_deg, lon_deg, and zoomlevel --- to get the x-y OSM numbers for that location and zoom-level.
I find it helpful to use Wikipedia to get the lat-lon numbers (in decimal degrees).
For example, I could go to the Vinton, Iowa Wikipedia page and click on the latitude-longitude coordinates at the top of that page.
Those coordinates are not in decimal degrees format, but the page that comes up (a 'GeoHack' page, a 'Wikimedia Toolforge' page at 'tools.wmflabs.org') shows the coordinates in decimal degrees.
Other Tcl scripts for OSM 'tile calculations':
I have not had motivation yet to write a script for the opposite calculation --- namely, convert an OSM x-y pair of integers to a latitude-longitude in decimal degress --- for a given OSM zoom level.
If I ever make such a Tcl script, I may publish it here.
I should mention that I have a couple of Tcl-Tk utilities that I made for doing these OSM calculations --- and for fetching related OSM tiles.
Those Tcl-Tk 'tkGooie' scripts can be seen via a 'MAPtools' page at www.freedomenv.com --- namely
More specifically, you can see Tcl-Tk code at these OSM-MAPtools pages:
THANK YOU
I want to give a BIG THANK YOU to the people of the openstreetmap.org project --- the people who provide the tiles, documentation, servers, etc. that make it possible for people to make maps in an 'open' (freedom-filled) environment.
Page was created 2019 Feb 21.