Skip to content

Instantly share code, notes, and snippets.

@desmondrawls
Created April 23, 2020 01:03
Show Gist options
  • Save desmondrawls/6a6bcfd90547d115d2bf5a49981e2d00 to your computer and use it in GitHub Desktop.
Save desmondrawls/6a6bcfd90547d115d2bf5a49981e2d00 to your computer and use it in GitHub Desktop.
north_south = {
'0': '2',
'2': '0',
'1': '3',
'3': '1'
}
east_west = {
'2': '3',
'3': '2',
'1': '0',
'0': '1'
}
north_wall = ['0', '1']
south_wall = ['2', '3']
east_wall = ['1', '3']
west_wall = ['0', '2']
def parent(quadkey):
return quadkey[:-1]
def next_in_line(ancestor, descendants):
return [ancestor[-1], *descendants]
def inheritance(mapping, descendants, ancestor):
if len(descendants) == 0:
return ancestor
else:
return inheritance(mapping, descendants[1:], ancestor + mapping[descendants[0]])
def next_lineage(mapping, stop, descendants, ancestor):
if ancestor[-1] in stop:
return inheritance(mapping, next_in_line(ancestor, descendants), parent(ancestor))
elif len(ancestor) == 2:
return None
else:
return next_lineage(mapping, stop, next_in_line(ancestor, descendants), parent(ancestor))
def lineage(mapping, stop, quadkey):
return next_lineage(mapping, stop, [], quadkey)
def southern(quadkey):
return lineage(north_south, north_wall, quadkey)
def northern(quadkey):
return lineage(north_south, south_wall, quadkey)
def western(quadkey):
return lineage(east_west, east_wall, quadkey)
def eastern(quadkey):
return lineage(east_west, west_wall, quadkey)
def neighbors(quadkey):
return (northern(quadkey), southern(quadkey), eastern(quadkey), western(quadkey))
class TestNeighbors(unittest.TestCase):
def test_neighbors_top(self):
north, south, east, west = neighbors('01')
self.assertEqual('03', south)
self.assertEqual('00', west)
self.assertEqual(None, north)
self.assertEqual(None, east)
def test_neighbors_shared_grandparent(self):
north, south, east, west = neighbors('003')
self.assertEqual('021', south)
self.assertEqual('002', west)
self.assertEqual('001', north)
self.assertEqual('012', east)
def test_neighbors_shared_great_grandparent(self):
north, south, east, west = neighbors('0122')
self.assertEqual('0300', south)
self.assertEqual('0033', west)
self.assertEqual('0120', north)
self.assertEqual('0123', east)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment