openwebrx-clone/owrx/locator.py

31 lines
823 B
Python
Raw Permalink Normal View History

2019-09-24 22:35:57 +00:00
class Locator(object):
@staticmethod
def fromCoordinates(coordinates, depth=3):
2020-03-29 16:08:26 +00:00
lat = coordinates["lat"]
lon = coordinates["lon"]
2019-09-24 22:35:57 +00:00
if not -90 < lat < 90:
raise ValueError("invalid latitude: {}".format(lat))
if not -180 < lon < 180:
raise ValueError("invalid longitude: {}".format(lon))
2019-09-24 22:35:57 +00:00
lon = lon + 180
lat = lat + 90
res = ""
res += chr(65 + int(lon / 20))
res += chr(65 + int(lat / 10))
if depth >= 2:
lon = lon % 20
lat = lat % 10
res += str(int(lon / 2))
res += str(int(lat))
if depth >= 3:
lon = lon % 2
lat = lat % 1
res += chr(97 + int(lon * 12))
res += chr(97 + int(lat * 24))
2019-09-24 22:47:34 +00:00
return res