Module: H3::Indexing
- Included in:
- H3
- Defined in:
- lib/h3/indexing.rb
Overview
Indexing functions.
Coordinates are returned in degrees, in the form
[latitude, longitude]
Instance Method Summary collapse
-
#geo_to_h3(coords, resolution) ⇒ Integer
Derive H3 index for the given set of coordinates.
-
#h3_to_geo(h3_index) ⇒ Array<Integer>
Derive coordinates for a given H3 index.
-
#h3_to_geo_boundary(h3_index) ⇒ Array<Array<Integer>>
Derive the geographical boundary as coordinates for a given H3 index.
Instance Method Details
#geo_to_h3(coords, resolution) ⇒ Integer
Derive H3 index for the given set of coordinates.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/h3/indexing.rb', line 22 def geo_to_h3(coords, resolution) raise ArgumentError unless coords.is_a?(Array) && coords.count == 2 lat, lon = coords if lat > 90 || lat < -90 || lon > 180 || lon < -180 raise(ArgumentError, "Invalid coordinates") end coords = Bindings::Structs::GeoCoord.new coords[:lat] = degs_to_rads(lat) coords[:lon] = degs_to_rads(lon) Bindings::Private.geo_to_h3(coords, resolution) end |
#h3_to_geo(h3_index) ⇒ Array<Integer>
Derive coordinates for a given H3 index.
The coordinates map to the centre of the hexagon at the given index.
48 49 50 51 52 |
# File 'lib/h3/indexing.rb', line 48 def h3_to_geo(h3_index) coords = Bindings::Structs::GeoCoord.new Bindings::Private.h3_to_geo(h3_index, coords) [rads_to_degs(coords[:lat]), rads_to_degs(coords[:lon])] end |
#h3_to_geo_boundary(h3_index) ⇒ Array<Array<Integer>>
Derive the geographical boundary as coordinates for a given H3 index.
This will be a set of 6 coordinate pairs matching the vertexes of the hexagon represented by the given H3 index.
If the H3 index is a pentagon, there will be only 5 coordinate pairs returned.
72 73 74 75 76 77 78 |
# File 'lib/h3/indexing.rb', line 72 def h3_to_geo_boundary(h3_index) geo_boundary = Bindings::Structs::GeoBoundary.new Bindings::Private.h3_to_geo_boundary(h3_index, geo_boundary) geo_boundary[:verts].take(geo_boundary[:num_verts] * 2).map do |d| rads_to_degs(d) end.each_slice(2).to_a end |