Skip to content

Commit

Permalink
use OSM for sharing link, fixes #46
Browse files Browse the repository at this point in the history
  • Loading branch information
rinigus committed Sep 7, 2018
1 parent 842fd4e commit 4bb38a7
Showing 1 changed file with 65 additions and 4 deletions.
69 changes: 65 additions & 4 deletions poor/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,15 @@ def format_distance_and_bearing(meters, bearing, n=2, short=True):

def format_location_message(x, y, html=False):
"""Format coordinates of a point into a location message."""
osm_url = short_osm(y,x)
if html:
return ('<a href="geo:{y:.5f},{x:.5f}">geo:{y:.5f},{x:.5f}</a><br>'
'<a href="http://maps.google.com/?q={y:.5f},{x:.5f}">'
'http://maps.google.com/?q={y:.5f},{x:.5f}</a>'
.format(x=x, y=y))
'<a href="{osm}">'
'{osm}</a>'
.format(x=x, y=y, osm=osm_url))
else:
return ('geo:{y:.5f},{x:.5f} '
'http://maps.google.com/?q={y:.5f},{x:.5f}'
'{osm}'
.format(x=x, y=y))

def format_time(seconds):
Expand Down Expand Up @@ -465,3 +466,63 @@ def write_json(data, path):
.format(repr(path), str(error)),
file=sys.stderr)
raise # Exception

######################################################################################
## Utility functions from external sources
######################################################################################

## OSM schort link, from https://gist.github.com/mdornseif/5652824
# osm_shortlink.py - MAximillian Dornseif 2013 - Public Domain
# see http://wiki.openstreetmap.org/wiki/Shortlink
# https://github.com/openstreetmap/openstreetmap-website/blob/master/lib/short_link.rb
# and makeShortCode in
# https://github.com/openstreetmap/openstreetmap-website/blob/master/app/assets/javascripts/application.js

# array of 64 chars to encode 6 bits. this is almost like base64 encoding, but
# the symbolic chars are different, as base64's + and / aren't very
# URL-friendly.
ARRAY = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~'

import math

def short_osm(lat, lon, zoom=16):
"""Return a short link representing a location in OpenStreetmap.
Provide coordinates and optional zoom level. e.g.:
>>> short_osm(50.671530961990356, 6.09715461730957)
http://osm.org/go/0GAjIv8h
>>> short_osm(0, 0, 3)
http://osm.org/go/wAAA--
>>> short_osm(0, 0, 4)
http://osm.org/go/wAAA
"""

def _encode(lat, lon, z):
"""given a location and zoom, return a short string representing it."""
x = int((lon + 180.0) * 2**32 / 360.0)
y = int((lat + 90.0) * 2**32 / 180.0)
code = _interleave(x, y)
str = ''
# add eight to the zoom level, which approximates an accuracy of
# one pixel in a tile.
for i in range(int(math.ceil((z + 8) / 3.0))):
digit = (code >> (56 - 6 * i)) & 0x3f;
str += ARRAY[digit]
# append characters onto the end of the string to represent
# partial zoom levels (characters themselves have a granularity
# of 3 zoom levels).
for i in range((z + 8) % 3):
str += "-"
return str


def _interleave(x, y):
"""combine 2 32 bit integers to a 64 bit integer"""
c = 0
for i in range(31, 0, -1):
c = (c << 1) | ((x >> i) & 1)
c = (c << 1) | ((y >> i) & 1)
return c

return 'http://osm.org/go/' + _encode(lat, lon, zoom) + "?m"

0 comments on commit 4bb38a7

Please sign in to comment.