To find your Hollywood Walk of Fame Quarantine house, use this script on the command line, providing your latitude and longitude:
$ python quarantine-house.py 40.7484 -73.9856
Humphrey Bogart
Patrick Swayze
Ozzy Osbourne
Kathy Bates
Earth, Wind and Fire
The code is based on this earlier post about the "What N Things" geocoding application.
The possible names are taken from a subset of the list of people who have stars on the Hollywood Walk of Fame. The text file is hollywood-list.txt.
import sys
import math
# Settings: the resolution is 10^(-ires) and we use m words.
ires = 10000
ndp = int(math.log10(ires))
m = 5
# Read the word list
words = [line.strip() for line in open('hollywood-list.txt')]
# Total number of locations
N = 180 * 360 * ires**2
# "Length" of each of the m dimensions
d = round(N**(1/m))
if d > len(words):
print(f'Warning: I need {d} words, but got {len(words)}.')
sys.exit(1)
def encode(lat, lng):
"""Encode a latitude and longitude as m words."""
# Integer, scaled longitude and latitude as positive quantities.
ilam, iphi = int((lat+90)*ires), int((lng+180)*ires)
# The one-dimensional index corresponding to ilam, iphi.
idx = 180 * ires * iphi + ilam
# Construct the word sequence.
j = [0]*m
for i in range(m):
idx, j[i] = divmod(idx, d)
return '\n'.join(words[j[i]] for i in range(m))
def decode(code):
"""Decode three words in the list code as a latitude and longitude."""
j = [words.index(word) for word in code]
# Get the one-dimensional index corresponding to the code.
idx = 0
for i in range(m):
idx += d**i * j[i]
# Convert to longitude and latitude, accounting for the offset.
iphi, ilam = divmod(idx, 180 * ires)
lam, phi = ilam / ires - 90, iphi / ires - 180
return round(lam, ndp), round(phi, ndp)
try:
lat, lng = float(sys.argv[1]), float(sys.argv[2])
print(encode(lat, lng))
except (IndexError, ValueError):
if len(sys.argv) == m+1:
code = sys.argv[1:]
else:
code = sys.argv[1].split('\n')
print(decode(code))
Comments
Comments are pre-moderated. Please be patient and your comment will appear soon.
There are currently no comments
New Comment