-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexagons.rb
80 lines (52 loc) · 2.05 KB
/
hexagons.rb
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
require 'json'
#coordinates of each hexagon
def hexagon length, x_start, y_start
hex = [[length + x_start, y_start],
[((length/2) + x_start), (length * Math.sqrt(length/2)) + y_start],
[((-length/2) + x_start), (length * Math.sqrt(length/2)) + y_start],
[(-length + x_start), y_start],
[(-length/2 + x_start), (-length * Math.sqrt(length/2)) + y_start],
[(length/2) + x_start, (-length * Math.sqrt(length/2)) + y_start],
[length + x_start, y_start]]
end
#will be used to make a feature collection of individual polygons rather than one feature of multipolygons
def generate_polygons2
#container to hold the full file
geojson = {"type" => "FeatureCollection"}
#array to hold the features
features = []
features << {"type" => "Feature",
"geometry" => hexagon(2, 1, 1),
"properties" => {"p_id" => 0}}
geojson["features"] = features
puts geojson.to_json
end
#origin is starting coordinates [x,y], length in degs for now, bbox: [xmin, xmax, ymin, ymax]
def generate_multihex length, bbox
#container to hold the full file
geojson = {"type" => "MultiPolygon"}
#array to hold the features
features = []
origin = [bbox[0], bbox[3]]
current_pos = origin
up = false #hack to align the hexagons y location
while current_pos[0] < bbox[1]
while current_pos[1] > bbox[2]
features << [hexagon(length, current_pos[0], current_pos[1])]
current_pos = [current_pos[0], current_pos[1] - ((length * Math.sqrt(length / 2)) * 2)]
puts current_pos[1]
end
#shift the hexagon creation to the next column with a shift in the y to nest them
if up
current_pos = [current_pos[0] + length*1.5, bbox[3]]
up = false
else
current_pos = [current_pos[0] + length*1.5, bbox[3] + (length * Math.sqrt(length/2.0))]
up = true
end
end
geojson["coordinates"] = features
puts geojson.to_json
store_to_cartodb geojson.to_json
end
generate_multihex 2.0, [3.0,10.0,3.0,10.0]