Method: H3::Traversal#hex_range

Defined in:
lib/h3/traversal.rb

#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.

Examples:

Derive the hex range for a given H3 index with k of 0.

H3.hex_range(617700169983721471, 0)
[617700169983721471]

Derive the hex range for a given H3 index with k of 1.

H3.hex_range(617700169983721471, 1)
[
  617700169983721471, 617700170047946751, 617700169984245759,
  617700169982672895, 617700169983983615, 617700170044276735,
  617700170044014591
]

Parameters:

  • origin (Integer)

    Origin H3 index

  • k (Integer)

    K distance.

Returns:

  • (Array<Integer>)

    Array of H3 indexes within the k-range.

Raises:

  • (ArgumentError)

    Raised if the range contains a pentagon.



82
83
84
85
86
87
88
# File 'lib/h3/traversal.rb', line 82

def hex_range(origin, k)
  max_hexagons = max_kring_size(k)
  out = H3Indexes.of_size(max_hexagons)
  pentagonal_distortion = Bindings::Private.hex_range(origin, k, out)
  raise(ArgumentError, "Specified hexagon range contains a pentagon") if pentagonal_distortion
  out.read
end