diff options
author | Marius Halden <marius.h@lden.org> | 2016-03-28 20:38:28 +0200 |
---|---|---|
committer | Marius Halden <marius.h@lden.org> | 2016-03-28 20:38:28 +0200 |
commit | 6c1118dbf2c4b15bcfcd77600d36f2389428c75e (patch) | |
tree | da81756a344a89502df5479b860b6f4a24b6ed92 /perllib/Utils/OpenStreetMap.pm | |
parent | a2d67ca6de255ff04badb7cb5a62f7d3df3ce293 (diff) | |
parent | 4345263c9de752454795ad57323e684e41e702a8 (diff) |
Merge tag 'v1.8.1' into fiksgatami-dev
Diffstat (limited to 'perllib/Utils/OpenStreetMap.pm')
-rw-r--r-- | perllib/Utils/OpenStreetMap.pm | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/perllib/Utils/OpenStreetMap.pm b/perllib/Utils/OpenStreetMap.pm new file mode 100644 index 000000000..900569a77 --- /dev/null +++ b/perllib/Utils/OpenStreetMap.pm @@ -0,0 +1,44 @@ +package Utils::OpenStreetMap; + +use strict; +use warnings; +use Exporter 'import'; +use POSIX qw(ceil); + +our @EXPORT_OK = qw(short_url); + +my $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~"; + +sub short_url { + my ($lat, $lon, $zoom) = @_; + $zoom ||= 16; + my $x = ($lon + 180) * 2**32 / 360; + my $y = ($lat + 90) * 2**32 / 180; + my $code = _interleave($x, $y); + + my $str = ''; + # add eight to the zoom level, which approximates an accuracy of + # one pixel in a tile + my $n = ceil(($zoom + 8) / 3) - 1; + for my $i (0..$n) { + my $digit = ($code >> (58 - 6 * $i)) & 0x3f; + $str .= substr($chars, $digit, 1); + } + # append characters onto the end of the string to represent + # partial zoom levels (characters themselves have a granularity + # of 3 zoom levels). + $str .= "-" x (($zoom + 8) % 3); + return "http://osm.org/go/$str"; +} + +sub _interleave { + my ($x, $y) = @_; + my $c = 0; + for (my $i=31; $i>=0; $i--) { + $c = ($c << 1) | (($x >> $i) & 1); + $c = ($c << 1) | (($y >> $i) & 1); + } + return $c; +} + +1; |