Module: H3::Traversal
Overview
Grid traversal functions
Instance Method Summary collapse
-
#h3_distance(origin, h3_index) ⇒ Integer
Derive the distance between two H3 indexes.
-
#hex_range(origin, k) ⇒ Array<Integer>
Derives H3 indexes within k distance of the origin H3 index.
-
#hex_range_distances(origin, k) ⇒ Hash
Derives the hex range for the given origin at k distance, sub-grouped by distance.
-
#hex_ranges(h3_set, k, grouped: true) ⇒ Hash
Derives H3 indexes within k distance for each H3 index in the set.
-
#hex_ring(origin, k) ⇒ Array<Integer>
Derives the hollow hexagonal ring centered at origin with sides of length k.
-
#k_ring(origin, k) ⇒ Array<Integer>
Derives H3 indexes within k distance of the origin H3 index.
-
#k_ring_distances(origin, k) ⇒ Hash
Derives the k-ring for the given origin at k distance, sub-grouped by distance.
-
#max_hex_ring_size(k) ⇒ Integer
Derive the maximum hex ring size for a given distance k.
-
#max_kring_size(k) ⇒ Integer
Derive the maximum k-ring size for distance k.
Methods included from Bindings::Base
Instance Method Details
#h3_distance(origin, h3_index) ⇒ Integer
Derive the distance between two H3 indexes.
33 |
# File 'lib/h3/traversal.rb', line 33 attach_function :h3_distance, :h3Distance, [ :h3_index, :h3_index], :int |
#hex_range(origin, k) ⇒ Array<Integer>
Derives H3 indexes within k distance of the origin H3 index.
Similar to #k_ring, except that an error is raised when one of the indexes returned is a pentagon or is in the pentagon distortion area.
k-ring 0 is defined as the origin index, k-ring 1 is defined as k-ring 0 and all neighboring indexes, and so on.
Output is inserted into the array in order of increasing distance from the origin.
63 64 65 66 67 68 69 |
# File 'lib/h3/traversal.rb', line 63 def hex_range(origin, k) max_hexagons = max_kring_size(k) hexagons = FFI::MemoryPointer.new(:ulong_long, max_hexagons) pentagonal_distortion = Bindings::Private.hex_range(origin, k, hexagons) raise(ArgumentError, "Specified hexagon range contains a pentagon") if pentagonal_distortion hexagons.read_array_of_ulong_long(max_hexagons).reject(&:zero?) end |
#hex_range_distances(origin, k) ⇒ Hash
Derives the hex range for the given origin at k distance, sub-grouped by distance.
220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/h3/traversal.rb', line 220 def hex_range_distances(origin, k) max_out_size = max_kring_size(k) out = FFI::MemoryPointer.new(H3_INDEX, max_out_size) distances = FFI::MemoryPointer.new(:int, max_out_size) pentagonal_distortion = Bindings::Private.hex_range_distances(origin, k, out, distances) raise(ArgumentError, "Specified hexagon range contains a pentagon") if pentagonal_distortion hexagons = out.read_array_of_ulong_long(max_out_size) distances = distances.read_array_of_int(max_out_size) Hash[ distances.zip(hexagons).group_by(&:first).map { |d, hs| [d, hs.map(&:last)] } ] end |
#hex_ranges(h3_set, k, grouped: true) ⇒ Hash
Derives H3 indexes within k distance for each H3 index in the set.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/h3/traversal.rb', line 180 def hex_ranges(h3_set, k, grouped: true) h3_range_indexes = hex_ranges_ungrouped(h3_set, k) return h3_range_indexes unless grouped out = {} h3_range_indexes.each_slice(max_kring_size(k)).each do |indexes| h3_index = indexes.first out[h3_index] = 0.upto(k).map do |j| start = j == 0 ? 0 : max_kring_size(j-1) length = max_hex_ring_size(j) indexes.slice(start, length) end end out end |
#hex_ring(origin, k) ⇒ Array<Integer>
Derives the hollow hexagonal ring centered at origin with sides of length k.
An error is raised when one of the indexes returned is a pentagon or is in the pentagon distortion area.
117 118 119 120 121 122 123 |
# File 'lib/h3/traversal.rb', line 117 def hex_ring(origin, k) max_hexagons = max_hex_ring_size(k) hexagons = FFI::MemoryPointer.new(:ulong_long, max_hexagons) pentagonal_distortion = Bindings::Private.hex_ring(origin, k, hexagons) raise(ArgumentError, "The hex ring contains a pentagon") if pentagonal_distortion hexagons.read_array_of_ulong_long(max_hexagons).reject(&:zero?) end |
#k_ring(origin, k) ⇒ Array<Integer>
Derives H3 indexes within k distance of the origin H3 index.
k-ring 0 is defined as the origin index, k-ring 1 is defined as k-ring 0 and all neighboring indexes, and so on.
92 93 94 95 96 97 |
# File 'lib/h3/traversal.rb', line 92 def k_ring(origin, k) max_hexagons = max_kring_size(k) hexagons = FFI::MemoryPointer.new(:ulong_long, max_hexagons) Bindings::Private.k_ring(origin, k, hexagons) hexagons.read_array_of_ulong_long(max_hexagons).reject(&:zero?) end |
#k_ring_distances(origin, k) ⇒ Hash
Derives the k-ring for the given origin at k distance, sub-grouped by distance.
257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/h3/traversal.rb', line 257 def k_ring_distances(origin, k) max_out_size = max_kring_size(k) out = FFI::MemoryPointer.new(H3_INDEX, max_out_size) distances = FFI::MemoryPointer.new(:int, max_out_size) Bindings::Private.k_ring_distances(origin, k, out, distances) hexagons = out.read_array_of_ulong_long(max_out_size) distances = distances.read_array_of_int(max_out_size) Hash[ distances.zip(hexagons).group_by(&:first).map { |d, hs| [d, hs.map(&:last)] } ] end |
#max_hex_ring_size(k) ⇒ Integer
Derive the maximum hex ring size for a given distance k.
NOTE: This method is not part of the H3 API and is added to this binding for convenience.
136 137 138 |
# File 'lib/h3/traversal.rb', line 136 def max_hex_ring_size(k) k.zero? ? 1 : 6 * k end |
#max_kring_size(k) ⇒ Integer
Derive the maximum k-ring size for distance k.
19 |
# File 'lib/h3/traversal.rb', line 19 attach_function :max_kring_size, :maxKringSize, [ :int ], :int |