forked from adamlawrencium/UberLens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhexmath.py
84 lines (69 loc) · 2.16 KB
/
hexmath.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# -*- coding: utf-8 -*-
"""
Hexagonal Grid Generator
(c) Michael McDermott, for One Hudson
"""
import math
import sys
import json
LTM = True
building_44 = [47.641387,-122.132817]
centroid = building_44
latfud = 0.9985/math.cos(math.radians(centroid[0]))
lonfud = 0.9981
SIN60 = math.sin(math.radians(60))
def get_hex_neighbors(x, y, major, ltm=LTM):
if ltm:
major /= 111120.
aa = 1.5 * major * lonfud
cc = major * SIN60 * latfud
lst = [(x, y + 2 * cc), (x + aa, y + cc), (x + aa, y - cc),
(x - aa, y - cc), (x, y - 2 * cc), (x - aa, y + cc)]
return lst
def latlon_hash((x, y), prec=6):
""" "rounds" a location
prec = precision (decimal digits of longitude degrees)
6: about 11 cm resolution
5: about 1.1 m
"""
x = abs(round(x, prec))
y = abs(round(y, prec))
return str(x) + str(y)
def generate_hexgrid(x, y, depth, major):
OUTPUT = {}
hexcenters = [(x, y)]
hexhashes = [latlon_hash((x, y))]
lastshellsize = 0
# Loop through main layers
for idx in range(2, depth + 1):
active = hexcenters[-lastshellsize:]
# Loop through outermost layer
for node in active:
newnodes = get_hex_neighbors(node[0], node[1], major)
# for n in newnodes:
# print n
# print newnodes
# return
# Loop through outermost nodes' neighbors
for newnode in newnodes:
llhash = latlon_hash(newnode)
if llhash not in hexhashes:
hexcenters.append(newnode)
hexhashes.append(llhash)
lastshellsize += 6
OUTPUT['hexcenters'] = []
for center in hexcenters:
OUTPUT['hexcenters'].append(center)
# print json.dumps(OUTPUT)
print json.dumps(hexcenters)
if __name__ == "__main__":
# a = get_hex_neighbors(47.641387, -122.132817, 1, True)
lat = float(sys.argv[1])
lng = float(sys.argv[2])
depth = int(sys.argv[3])
major = int(sys.argv[4])
# print lat, lng, depth, major
generate_hexgrid(lat, lng, depth, major)
# print generate_hexgrid(47.641387, -122.132817, 3, 10)
# for x in a:
# print x