Last active
September 25, 2019 07:03
-
-
Save bobpoekert/7b676ab6d49d41d1e2873db5a60fafe5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Zoom_levels *) | |
let pi = 3.1415926535897932384626433832795028841 | |
let tile_number zoom lat lon = | |
let n = 2. ** (float_of_int zoom) in | |
(* return (Math.floor((tt+180)/360*Math.pow(2,zoom1))); *) | |
let xtile = int_of_float (floor ((lon +. 180.) /. 360. *. n)) in | |
(* (Math.floor((1-Math.log(Math.tan(lat*Math.PI/180) + 1/Math.cos(lat*Math.PI/180))/Math.PI)/2 *Math.pow(2,zoom2))) *) | |
let t = tan (lat *. pi /. 180.) in | |
let c = 1. /. (cos (lat *. pi /. 180.)) in | |
let l = log (t +. c) in | |
let ytile = int_of_float ((1. -. l /. pi) /. 2. *. n) in | |
(xtile, ytile) | |
let lat_lon_of_tile_number zoom x y = | |
let x = float_of_int x in | |
let y = float_of_int y in | |
let n = 2. ** (float_of_int zoom) in | |
let lon = x /. n *. 360. -. 180. in | |
let lat = (atan (sinh (pi -. (y /. n) *. (2. *. pi)))) *. (180. /. pi) in | |
(lat, lon) | |
let%test "osm_tile_encode" = | |
let zoom = 12 in | |
let test_points = [ | |
((26.321429, -74.996991), (1194, 1737)); | |
((30.094000, -115.405597), (734, 1688)); | |
((40.437131, -112.463063), (768, 1544)); | |
((30.017105, -70.710589), (1243, 1689)); | |
((48.984368, -95.342902), (963, 1406)); | |
((33.267652, -105.280272), (850, 1646)); | |
((31.255513, -120.857241), (672, 1673)); | |
((27.077942, -115.289042), (736, 1727)); | |
((41.252644, -95.704571), (959, 1531)); | |
((40.188486, -97.798719), (935, 1547)); | |
((31.394933, -111.529368), (779, 1671)); | |
((41.067748, -111.937154), (774, 1534)); | |
((47.227250, -71.769764), (1231, 1436)); | |
((42.504822, -107.183008), (828, 1512)); | |
((33.961365, -67.391889), (1281, 1636)); | |
((28.898628, -89.116261), (1034, 1704)); | |
((27.511563, -95.900888), (956, 1722)); | |
((45.656904, -93.555989), (983, 1462)); | |
((43.346193, -114.418065), (746, 1499)); | |
((29.080281, -73.965860), (1206, 1701)); | |
((29.384627, -113.426766), (757, 1697)); | |
((31.042557, -76.524991), (1177, 1676)); | |
((26.315769, -113.755961), (753, 1737)); | |
((34.872365, -67.656485), (1278, 1624)); | |
((32.443602, -78.692860), (1152, 1657)); | |
((48.324991, -106.224055), (839, 1418)); | |
((32.422671, -114.979281), (739, 1657)); | |
((40.534805, -106.626783), (834, 1542)); | |
((38.641610, -77.235209), (1169, 1570)); | |
((24.935830, -79.582983), (1142, 1754)); | |
((37.127754, -90.230742), (1021, 1592)); | |
((30.427954, -74.407584), (1201, 1684)); | |
((32.703805, -115.933778), (728, 1653)); | |
((37.786583, -71.804438), (1231, 1583)); | |
((37.660920, -96.381761), (951, 1584)); | |
((28.904582, -95.020379), (966, 1704)); | |
((38.164259, -122.339735), (656, 1577)); | |
((40.087168, -72.999264), (1217, 1549)); | |
((45.888957, -106.202004), (839, 1459)); | |
((40.392494, -95.607258), (960, 1544)); | |
((39.919404, -103.961870), (865, 1551)); | |
((31.923791, -98.581157), (926, 1664)); | |
((26.271593, -81.010036), (1126, 1738)); | |
((35.315861, -112.135589), (772, 1618)); | |
((38.133778, -100.732939), (901, 1578)); | |
((32.956867, -97.089128), (943, 1650)); | |
((31.541555, -93.318561), (986, 1669)); | |
((38.395766, -70.840015), (1241, 1574)); | |
((32.482741, -120.399568), (678, 1656)); | |
((25.795591, -119.044764), (693, 1744)); | |
((32.227758, -116.803720), (719, 1660)); | |
((30.079864, -95.450778), (961, 1688)); | |
((35.315138, -106.065881), (841, 1618)); | |
((31.263688, -90.977309), (1012, 1673)); | |
((35.173366, -103.464978), (870, 1620)); | |
((29.489563, -89.038553), (1034, 1696)); | |
((30.988265, -76.113078), (1182, 1676)); | |
((26.299601, -95.866895), (957, 1737)); | |
((42.236239, -68.891739), (1264, 1516)); | |
((39.864524, -121.139746), (669, 1552)); | |
((33.757031, -70.080463), (1250, 1639)); | |
((33.339098, -92.010888), (1001, 1645)); | |
((28.045350, -112.482246), (768, 1715)); | |
((26.806677, -93.654111), (982, 1731)); | |
] in | |
List.fold_left (fun res ((lat, lon), (x, y)) -> | |
let res_x, res_y = tile_number zoom lat lon in | |
let _ = if (x != res_x || y != res_y) then Printf.printf "%d %d %d %d\n" x y res_x res_y in | |
res && (x = res_x) && (y = res_y) | |
) true test_points | |
let%test "osm_tile_roundtrip" = | |
let top = 49.3457868 in | |
let left = -124.7844079 in | |
let right = -66.9513812 in | |
let bottom = 24.7433195 in | |
let zoom = 12 in | |
let rec test_coords_roundtrip it = | |
if it < 1 then true else ( | |
let lat = (Random.float (top -. bottom)) +. bottom in | |
let lon = (Random.float (right -. left)) +. left in | |
let x, y = tile_number zoom lat lon in | |
let lat_res, lon_res = lat_lon_of_tile_number zoom x y in | |
if abs (int_of_float (lat -. lat_res)) < 3 && abs (int_of_float (lon -. lon_res)) < 3 then | |
test_coords_roundtrip (it - 1) | |
else | |
(Printf.printf "%d: %f %f -> %f %f\n" it lat lon lat_res lon_res; false) | |
) in | |
test_coords_roundtrip 100000 | |
(* | |
Copyright 2019 Bob Poekert | |
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | |
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | |
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment