h3 3.5.0 → 3.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (180) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +45 -0
  3. data/Gemfile.lock +9 -7
  4. data/README.md +8 -8
  5. data/ext/h3/src/.github/workflows/test-linux.yml +118 -0
  6. data/ext/h3/src/.github/workflows/test-macos.yml +42 -0
  7. data/ext/h3/src/.github/workflows/test-website.yml +32 -0
  8. data/ext/h3/src/.github/workflows/test-windows.yml +44 -0
  9. data/ext/h3/src/.gitignore +5 -0
  10. data/ext/h3/src/.travis.yml +21 -32
  11. data/ext/h3/src/CHANGELOG.md +60 -0
  12. data/ext/h3/src/CMakeLists.txt +150 -33
  13. data/ext/h3/src/CONTRIBUTING.md +1 -1
  14. data/ext/h3/src/README.md +65 -16
  15. data/ext/h3/src/RELEASE.md +3 -1
  16. data/ext/h3/src/VERSION +1 -1
  17. data/ext/h3/src/dev-docs/RFCs/rfc-template.md +21 -0
  18. data/ext/h3/src/dev-docs/RFCs/v4.0.0/error-handling-rfc.md +21 -0
  19. data/ext/h3/src/dev-docs/RFCs/v4.0.0/names_for_concepts_types_functions.md +276 -0
  20. data/ext/h3/src/dev-docs/RFCs/v4.0.0/overrideable-allocators-rfc.md +141 -0
  21. data/ext/h3/src/dev-docs/RFCs/v4.0.0/polyfill-modes-rfc.md +21 -0
  22. data/ext/h3/src/dev-docs/RFCs/v4.0.0/vertex-mode-rfc.md +50 -0
  23. data/ext/h3/src/dev-docs/build_windows.md +6 -1
  24. data/ext/h3/src/dev-docs/creating_bindings.md +3 -3
  25. data/ext/h3/src/dev-docs/custom_alloc.md +27 -0
  26. data/ext/h3/src/docs/{overview/mainpage.md → README.md} +2 -3
  27. data/ext/h3/src/docs/api/hierarchy.md +8 -0
  28. data/ext/h3/src/docs/api/misc.md +94 -0
  29. data/ext/h3/src/docs/community/applications.md +1 -0
  30. data/ext/h3/src/docs/community/bindings.md +10 -0
  31. data/ext/h3/src/docs/community/tutorials.md +8 -3
  32. data/ext/h3/src/docs/core-library/coordsystems.md +5 -4
  33. data/ext/h3/src/docs/core-library/filters.md +8 -9
  34. data/ext/h3/src/docs/core-library/geoToH3desc.md +2 -3
  35. data/ext/h3/src/docs/core-library/h3ToGeoBoundaryDesc.md +4 -5
  36. data/ext/h3/src/docs/core-library/h3ToGeoDesc.md +3 -4
  37. data/ext/h3/src/docs/core-library/h3indexing.md +26 -17
  38. data/ext/h3/src/docs/core-library/overview.md +2 -3
  39. data/ext/h3/src/docs/core-library/restable.md +1 -2
  40. data/ext/h3/src/docs/core-library/usage.md +1 -2
  41. data/ext/h3/src/docs/table-of-contents.json +47 -0
  42. data/ext/h3/src/docs/{overview/usecases.md → usecases.md} +6 -11
  43. data/ext/h3/src/scripts/binding_functions.sh +1 -1
  44. data/ext/h3/src/scripts/coverage.sh.in +8 -4
  45. data/ext/h3/src/scripts/update_version.sh +2 -2
  46. data/ext/h3/src/src/apps/applib/include/args.h +1 -0
  47. data/ext/h3/src/src/apps/applib/include/test.h +1 -0
  48. data/ext/h3/src/src/apps/applib/include/utility.h +7 -1
  49. data/ext/h3/src/src/apps/applib/lib/args.c +2 -0
  50. data/ext/h3/src/src/apps/applib/lib/kml.c +2 -0
  51. data/ext/h3/src/src/apps/applib/lib/test.c +1 -0
  52. data/ext/h3/src/src/apps/applib/lib/utility.c +133 -2
  53. data/ext/h3/src/src/apps/benchmarks/benchmarkH3Api.c +1 -1
  54. data/ext/h3/src/{website/html.config.js → src/apps/benchmarks/benchmarkH3UniEdge.c} +15 -12
  55. data/ext/h3/src/src/apps/filters/h3ToComponents.c +1 -0
  56. data/ext/h3/src/src/apps/filters/h3ToGeo.c +1 -0
  57. data/ext/h3/src/src/apps/filters/h3ToGeoBoundary.c +1 -0
  58. data/ext/h3/src/src/apps/filters/h3ToLocalIj.c +1 -0
  59. data/ext/h3/src/src/apps/filters/hexRange.c +1 -0
  60. data/ext/h3/src/src/apps/filters/kRing.c +1 -0
  61. data/ext/h3/src/src/apps/filters/localIjToH3.c +1 -0
  62. data/ext/h3/src/src/apps/miscapps/generateBaseCellNeighbors.c +2 -2
  63. data/ext/h3/src/src/apps/miscapps/generateFaceCenterPoint.c +1 -0
  64. data/ext/h3/src/src/apps/miscapps/generateNumHexagons.c +1 -2
  65. data/ext/h3/src/src/apps/miscapps/generatePentagonDirectionFaces.c +67 -0
  66. data/ext/h3/src/src/apps/miscapps/h3ToGeoBoundaryHier.c +1 -0
  67. data/ext/h3/src/src/apps/miscapps/h3ToGeoHier.c +1 -0
  68. data/ext/h3/src/src/apps/miscapps/h3ToHier.c +1 -0
  69. data/ext/h3/src/src/apps/testapps/mkRandGeo.c +1 -0
  70. data/ext/h3/src/src/apps/testapps/mkRandGeoBoundary.c +1 -0
  71. data/ext/h3/src/src/apps/testapps/testBBox.c +1 -0
  72. data/ext/h3/src/src/apps/testapps/testBaseCells.c +15 -1
  73. data/ext/h3/src/src/apps/testapps/testCompact.c +121 -2
  74. data/ext/h3/src/src/apps/testapps/testCoordIj.c +1 -0
  75. data/ext/h3/src/src/apps/testapps/testGeoCoord.c +47 -8
  76. data/ext/h3/src/src/apps/testapps/testGeoToH3.c +1 -0
  77. data/ext/h3/src/src/apps/testapps/testH3Api.c +1 -0
  78. data/ext/h3/src/src/apps/testapps/testH3CellArea.c +47 -0
  79. data/ext/h3/src/src/apps/testapps/testH3CellAreaExhaustive.c +180 -0
  80. data/ext/h3/src/src/apps/testapps/testH3Distance.c +2 -50
  81. data/ext/h3/src/src/apps/testapps/testH3DistanceExhaustive.c +84 -0
  82. data/ext/h3/src/src/apps/testapps/testH3GetFaces.c +1 -0
  83. data/ext/h3/src/src/apps/testapps/testH3Index.c +33 -3
  84. data/ext/h3/src/src/apps/testapps/testH3Line.c +2 -84
  85. data/ext/h3/src/src/apps/testapps/testH3LineExhaustive.c +115 -0
  86. data/ext/h3/src/src/apps/testapps/testH3Memory.c +175 -0
  87. data/ext/h3/src/src/apps/testapps/testH3NeighborRotations.c +1 -0
  88. data/ext/h3/src/src/apps/testapps/testH3SetToLinkedGeo.c +1 -0
  89. data/ext/h3/src/src/apps/testapps/testH3SetToVertexGraph.c +1 -0
  90. data/ext/h3/src/src/apps/testapps/testH3ToCenterChild.c +68 -0
  91. data/ext/h3/src/src/apps/testapps/testH3ToChildren.c +15 -2
  92. data/ext/h3/src/src/apps/testapps/testH3ToGeo.c +1 -0
  93. data/ext/h3/src/src/apps/testapps/testH3ToGeoBoundary.c +1 -0
  94. data/ext/h3/src/src/apps/testapps/testH3ToLocalIj.c +24 -236
  95. data/ext/h3/src/src/apps/testapps/testH3ToLocalIjExhaustive.c +265 -0
  96. data/ext/h3/src/src/apps/testapps/testH3ToParent.c +1 -0
  97. data/ext/h3/src/src/apps/testapps/testH3UniEdge.c +45 -16
  98. data/ext/h3/src/src/apps/testapps/testH3UniEdgeExhaustive.c +111 -0
  99. data/ext/h3/src/src/apps/testapps/testHexRanges.c +1 -0
  100. data/ext/h3/src/src/apps/testapps/testHexRing.c +1 -0
  101. data/ext/h3/src/src/apps/testapps/testKRing.c +1 -0
  102. data/ext/h3/src/src/apps/testapps/testLinkedGeo.c +1 -0
  103. data/ext/h3/src/src/apps/testapps/testMaxH3ToChildrenSize.c +1 -0
  104. data/ext/h3/src/src/apps/testapps/testPentagonIndexes.c +58 -0
  105. data/ext/h3/src/src/apps/testapps/testPolyfill.c +72 -9
  106. data/ext/h3/src/src/apps/testapps/testPolyfillReported.c +157 -0
  107. data/ext/h3/src/src/apps/testapps/testPolygon.c +27 -1
  108. data/ext/h3/src/src/apps/testapps/testVec2d.c +1 -0
  109. data/ext/h3/src/src/apps/testapps/testVec3d.c +1 -0
  110. data/ext/h3/src/src/apps/testapps/testVertex.c +66 -0
  111. data/ext/h3/src/src/apps/testapps/testVertexGraph.c +1 -0
  112. data/ext/h3/src/src/h3lib/include/algos.h +8 -0
  113. data/ext/h3/src/src/h3lib/include/alloc.h +40 -0
  114. data/ext/h3/src/src/h3lib/include/baseCells.h +4 -0
  115. data/ext/h3/src/src/h3lib/include/bbox.h +4 -1
  116. data/ext/h3/src/src/h3lib/include/constants.h +2 -0
  117. data/ext/h3/src/src/h3lib/include/faceijk.h +3 -2
  118. data/ext/h3/src/src/h3lib/include/geoCoord.h +2 -3
  119. data/ext/h3/src/src/h3lib/include/h3Index.h +37 -4
  120. data/ext/h3/src/src/h3lib/include/h3api.h.in +85 -17
  121. data/ext/h3/src/src/h3lib/include/linkedGeo.h +1 -0
  122. data/ext/h3/src/src/h3lib/include/polygon.h +1 -0
  123. data/ext/h3/src/src/h3lib/include/polygonAlgos.h +1 -0
  124. data/ext/h3/src/src/h3lib/include/vertex.h +44 -0
  125. data/ext/h3/src/src/h3lib/include/vertexGraph.h +1 -0
  126. data/ext/h3/src/src/h3lib/lib/algos.c +305 -80
  127. data/ext/h3/src/src/h3lib/lib/baseCells.c +26 -4
  128. data/ext/h3/src/src/h3lib/lib/bbox.c +56 -27
  129. data/ext/h3/src/src/h3lib/lib/coordijk.c +2 -0
  130. data/ext/h3/src/src/h3lib/lib/faceijk.c +35 -24
  131. data/ext/h3/src/src/h3lib/lib/geoCoord.c +162 -44
  132. data/ext/h3/src/src/h3lib/lib/h3Index.c +150 -46
  133. data/ext/h3/src/src/h3lib/lib/h3UniEdge.c +42 -57
  134. data/ext/h3/src/src/h3lib/lib/linkedGeo.c +20 -15
  135. data/ext/h3/src/src/h3lib/lib/localij.c +5 -5
  136. data/ext/h3/src/src/h3lib/lib/polygon.c +3 -2
  137. data/ext/h3/src/src/h3lib/lib/vec2d.c +1 -0
  138. data/ext/h3/src/src/h3lib/lib/vec3d.c +1 -0
  139. data/ext/h3/src/src/h3lib/lib/vertex.c +134 -0
  140. data/ext/h3/src/src/h3lib/lib/vertexGraph.c +8 -5
  141. data/ext/h3/src/website/.eslintignore +2 -0
  142. data/ext/h3/src/website/.gitignore +57 -0
  143. data/ext/h3/src/website/.nvmrc +1 -0
  144. data/ext/h3/src/website/README.md +8 -6
  145. data/ext/h3/src/website/gatsby-config.js +83 -0
  146. data/ext/h3/src/website/package.json +20 -12
  147. data/ext/h3/src/website/scripts/build-to-gh-pages.sh +7 -5
  148. data/ext/h3/src/website/src/.gitkeep +0 -0
  149. data/ext/h3/src/website/templates/documentation.jsx +129 -0
  150. data/ext/h3/src/website/yarn.lock +13723 -0
  151. data/h3.gemspec +1 -0
  152. data/lib/h3.rb +8 -23
  153. data/lib/h3/bindings/base.rb +15 -4
  154. data/lib/h3/bindings/private.rb +13 -9
  155. data/lib/h3/geo_json.rb +1 -1
  156. data/lib/h3/hierarchy.rb +24 -9
  157. data/lib/h3/indexing.rb +7 -7
  158. data/lib/h3/inspection.rb +22 -26
  159. data/lib/h3/miscellaneous.rb +157 -9
  160. data/lib/h3/regions.rb +3 -0
  161. data/lib/h3/traversal.rb +9 -9
  162. data/lib/h3/unidirectional_edges.rb +18 -18
  163. data/lib/h3/version.rb +1 -1
  164. data/spec/geo_json_spec.rb +8 -0
  165. data/spec/hierarchy_spec.rb +23 -13
  166. data/spec/indexing_spec.rb +15 -15
  167. data/spec/inspection_spec.rb +17 -17
  168. data/spec/miscellaneous_spec.rb +151 -6
  169. data/spec/{region_spec.rb → regions_spec.rb} +1 -1
  170. data/spec/traversal_spec.rb +6 -6
  171. data/spec/unidirectional_edges_spec.rb +18 -18
  172. metadata +55 -15
  173. data/ext/h3/src/.ycm_extra_conf.py +0 -92
  174. data/ext/h3/src/appveyor.yml +0 -50
  175. data/ext/h3/src/website/src/config.js +0 -46
  176. data/ext/h3/src/website/src/mdRoutes.js +0 -151
  177. data/ext/h3/src/website/src/styles/_variables.scss +0 -16
  178. data/ext/h3/src/website/src/styles/index.scss +0 -3
  179. data/ext/h3/src/website/static/index.html +0 -15
  180. data/lib/h3/bindings.rb +0 -12
data/h3.gemspec CHANGED
@@ -14,6 +14,7 @@ Gem::Specification.new do |spec|
14
14
 
15
15
  spec.add_runtime_dependency "ffi", "~> 1.9"
16
16
  spec.add_runtime_dependency "rgeo-geojson", "~> 2.1"
17
+ spec.add_runtime_dependency "zeitwerk", "~> 2.1"
17
18
 
18
19
  spec.add_development_dependency "coveralls", "~> 0.8"
19
20
  spec.add_development_dependency "rake", "~> 12.3"
data/lib/h3.rb CHANGED
@@ -1,15 +1,8 @@
1
1
  require "ffi"
2
2
  require "rgeo/geo_json"
3
+ require "zeitwerk"
3
4
 
4
- require "h3/bindings"
5
- require "h3/geo_json"
6
- require "h3/hierarchy"
7
- require "h3/indexing"
8
- require "h3/inspection"
9
- require "h3/miscellaneous"
10
- require "h3/regions"
11
- require "h3/traversal"
12
- require "h3/unidirectional_edges"
5
+ Zeitwerk::Loader.for_gem.setup
13
6
 
14
7
  # The main H3 namespace.
15
8
  #
@@ -17,7 +10,7 @@ require "h3/unidirectional_edges"
17
10
  #
18
11
  # @see https://uber.github.io/h3/#/documentation/overview/introduction
19
12
  module H3
20
- extend GeoJSON
13
+ extend GeoJson
21
14
  extend Hierarchy
22
15
  extend Miscellaneous
23
16
  extend Indexing
@@ -26,17 +19,9 @@ module H3
26
19
  extend Traversal
27
20
  extend UnidirectionalEdges
28
21
 
29
- PREDICATES = %i[h3_indexes_neighbors h3_pentagon h3_res_class_3
30
- h3_unidirectional_edge_valid h3_valid].freeze
31
- private_constant :PREDICATES
32
-
33
- class << self
34
- # FFI's attach_function doesn't allow method names ending with a
35
- # question mark. This works around the issue by dynamically
36
- # renaming those methods afterwards.
37
- PREDICATES.each do |predicate|
38
- alias_method "#{predicate}?", predicate
39
- undef_method predicate
40
- end
41
- end
22
+ # Internal bindings related modules and classes.
23
+ #
24
+ # These are intended to be used by the library's public methods
25
+ # and not to be used directly by client code.
26
+ module Bindings; end
42
27
  end
@@ -7,15 +7,26 @@ module H3
7
7
  def self.extended(base)
8
8
  lib_path = File.expand_path(__dir__ + "/../../../ext/h3/src/lib")
9
9
  base.extend FFI::Library
10
+ base.extend Gem::Deprecate
10
11
  base.include Structs
11
12
  base.include Types
12
13
  base.ffi_lib ["#{lib_path}/libh3.dylib", "#{lib_path}/libh3.so"]
13
14
  base.typedef :ulong_long, :h3_index
14
- base.typedef :int, :size
15
15
  base.typedef :int, :k_distance
16
- base.typedef :pointer, :h3_set
17
- base.typedef :pointer, :output_buffer
18
- base.const_set("H3_INDEX", :ulong_long)
16
+ end
17
+
18
+ def attach_predicate_function(name, *args)
19
+ stripped_name = name.to_s.gsub("?", "")
20
+ attach_function(stripped_name, *args).tap do
21
+ rename_function stripped_name, name
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def rename_function(from, to)
28
+ alias_method to, from
29
+ undef_method from
19
30
  end
20
31
  end
21
32
  end
@@ -7,10 +7,11 @@ module H3
7
7
  module Private
8
8
  extend H3::Bindings::Base
9
9
 
10
- attach_function :compact, [H3IndexesIn, H3IndexesOut, :size], :bool
10
+ attach_function :compact, [H3IndexesIn, H3IndexesOut, :size_t], :bool
11
11
  attach_function :destroy_linked_polygon, :destroyLinkedPolygon, [LinkedGeoPolygon], :void
12
12
  attach_function :geo_to_h3, :geoToH3, [GeoCoord, Resolution], :h3_index
13
- attach_function :h3_faces, :h3GetFaces, %i[h3_index output_buffer], :void
13
+ attach_function :get_pentagon_indexes, :getPentagonIndexes, [:int, H3IndexesOut], :void
14
+ attach_function :h3_faces, :h3GetFaces, %i[h3_index buffer_out], :void
14
15
  attach_function :h3_indexes_from_unidirectional_edge,
15
16
  :getH3IndexesFromUnidirectionalEdge,
16
17
  [:h3_index, H3IndexesOut], :void
@@ -20,11 +21,11 @@ module H3
20
21
  [:h3_index, H3IndexesOut], :void
21
22
  attach_function :h3_set_to_linked_geo,
22
23
  :h3SetToLinkedGeo,
23
- [H3IndexesIn, :size, LinkedGeoPolygon],
24
+ [H3IndexesIn, :size_t, LinkedGeoPolygon],
24
25
  :void
25
26
  attach_function :h3_to_children, :h3ToChildren, [:h3_index, Resolution, H3IndexesOut], :void
26
27
  attach_function :h3_to_geo, :h3ToGeo, [:h3_index, GeoCoord], :void
27
- attach_function :h3_to_string, :h3ToString, %i[h3_index output_buffer size], :void
28
+ attach_function :h3_to_string, :h3ToString, %i[h3_index buffer_out size_t], :void
28
29
  attach_function :h3_to_geo_boundary,
29
30
  :h3ToGeoBoundary,
30
31
  [:h3_index, GeoBoundary],
@@ -35,25 +36,28 @@ module H3
35
36
  attach_function :hex_range, :hexRange, [:h3_index, :k_distance, H3IndexesOut], :bool
36
37
  attach_function :hex_range_distances,
37
38
  :hexRangeDistances,
38
- [:h3_index, :k_distance, H3IndexesOut, :output_buffer], :bool
39
+ [:h3_index, :k_distance, H3IndexesOut, :buffer_out], :bool
39
40
  attach_function :hex_ranges,
40
41
  :hexRanges,
41
- [H3IndexesIn, :size, :k_distance, H3IndexesOut],
42
+ [H3IndexesIn, :size_t, :k_distance, H3IndexesOut],
42
43
  :bool
43
44
  attach_function :hex_ring, :hexRing, [:h3_index, :k_distance, H3IndexesOut], :bool
44
45
  attach_function :k_ring, :kRing, [:h3_index, :k_distance, H3IndexesOut], :void
45
46
  attach_function :k_ring_distances,
46
47
  :kRingDistances,
47
- [:h3_index, :k_distance, H3IndexesOut, :output_buffer],
48
+ [:h3_index, :k_distance, H3IndexesOut, :buffer_out],
48
49
  :bool
49
50
  attach_function :max_polyfill_size,
50
51
  :maxPolyfillSize,
51
52
  [GeoPolygon, Resolution],
52
53
  :int
53
- attach_function :max_uncompact_size, :maxUncompactSize, [H3IndexesIn, :size, Resolution], :int
54
+ attach_function :max_uncompact_size, :maxUncompactSize, [H3IndexesIn, :size_t, Resolution], :int
55
+ attach_function :point_distance_rads, :pointDistRads, [GeoCoord, GeoCoord], :double
56
+ attach_function :point_distance_km, :pointDistKm, [GeoCoord, GeoCoord], :double
57
+ attach_function :point_distance_m, :pointDistM, [GeoCoord, GeoCoord], :double
54
58
  attach_function :polyfill, [GeoPolygon, Resolution, H3IndexesOut], :void
55
59
  attach_function :res_0_indexes, :getRes0Indexes, [H3IndexesOut], :void
56
- attach_function :uncompact, [H3IndexesIn, :size, H3IndexesOut, :size, Resolution], :bool
60
+ attach_function :uncompact, [H3IndexesIn, :size_t, H3IndexesOut, :size_t, Resolution], :bool
57
61
  end
58
62
  end
59
63
  end
@@ -27,7 +27,7 @@ module H3
27
27
  #
28
28
  # @see http://geojson.io geojson.io - A tool to see GeoJSON data rendered on a world map.
29
29
  # @see https://tools.ietf.org/html/rfc7946 The GeoJSON RFC standard.
30
- module GeoJSON
30
+ module GeoJson
31
31
  # Convert a GeoJSON document to a nested array of coordinates.
32
32
  #
33
33
  # @param [String] input The GeoJSON document. This can be a feature collection, feature,
@@ -5,7 +5,7 @@ module H3
5
5
  module Hierarchy
6
6
  extend H3::Bindings::Base
7
7
 
8
- # @!method h3_to_parent(h3_index, parent_resolution)
8
+ # @!method parent(h3_index, parent_resolution)
9
9
  #
10
10
  # Derive the parent hexagon which contains the hexagon at the given H3 index.
11
11
  #
@@ -13,13 +13,13 @@ module H3
13
13
  # @param [Integer] parent_resoluton The desired resolution of the parent hexagon.
14
14
  #
15
15
  # @example Find the parent hexagon for a H3 index.
16
- # H3.h3_to_parent(613196570357137407, 6)
16
+ # H3.parent(613196570357137407, 6)
17
17
  # 604189371209351167
18
18
  #
19
19
  # @return [Integer] H3 index of parent hexagon.
20
- attach_function :h3_to_parent, :h3ToParent, [:h3_index, Resolution], :h3_index
20
+ attach_function :parent, :h3ToParent, [:h3_index, Resolution], :h3_index
21
21
 
22
- # @!method max_h3_to_children_size(h3_index, child_resolution)
22
+ # @!method max_children(h3_index, child_resolution)
23
23
  #
24
24
  # Derive maximum number of child hexagons possible at given resolution.
25
25
  #
@@ -27,11 +27,26 @@ module H3
27
27
  # @param [Integer] child_resoluton The desired resolution of the child hexagons.
28
28
  #
29
29
  # @example Derive maximum number of child hexagons.
30
- # H3.max_h3_to_children_size(613196570357137407, 10)
30
+ # H3.max_children(613196570357137407, 10)
31
31
  # 49
32
32
  #
33
33
  # @return [Integer] Maximum number of child hexagons possible at given resolution.
34
- attach_function :max_h3_to_children_size, :maxH3ToChildrenSize, [:h3_index, Resolution], :int
34
+ attach_function :max_children, :maxH3ToChildrenSize, [:h3_index, Resolution], :int
35
+
36
+ # @!method center_child(h3_index, child_resolution)
37
+ #
38
+ # Returns the center child (finer) index contained by the given index
39
+ # at the given resolution.
40
+ #
41
+ # @param [Integer] h3_index A valid H3 index.
42
+ # @param [Integer] child_resoluton The desired resolution of the center child hexagon.
43
+ #
44
+ # @example Find center child hexagon.
45
+ # H3.center_child(613196570357137407, 10)
46
+ # 622203769609814015
47
+ #
48
+ # @return [Integer] H3 index of center child hexagon.
49
+ attach_function :center_child, :h3ToCenterChild, [:h3_index, Resolution], :h3_index
35
50
 
36
51
  # Derive child hexagons contained within the hexagon at the given H3 index.
37
52
  #
@@ -39,15 +54,15 @@ module H3
39
54
  # @param [Integer] child_resolution The desired resolution of hexagons returned.
40
55
  #
41
56
  # @example Find the child hexagons for a H3 index.
42
- # H3.h3_to_children(613196570357137407, 9)
57
+ # H3.children(613196570357137407, 9)
43
58
  # [
44
59
  # 617700169982672895, 617700169982935039, 617700169983197183, 617700169983459327,
45
60
  # 617700169983721471, 617700169983983615, 617700169984245759
46
61
  # ]
47
62
  #
48
63
  # @return [Array<Integer>] H3 indexes of child hexagons.
49
- def h3_to_children(h3_index, child_resolution)
50
- max_children = max_h3_to_children_size(h3_index, child_resolution)
64
+ def children(h3_index, child_resolution)
65
+ max_children = max_children(h3_index, child_resolution)
51
66
  out = H3Indexes.of_size(max_children)
52
67
  Bindings::Private.h3_to_children(h3_index, child_resolution, out)
53
68
  out.read
@@ -7,20 +7,20 @@ module H3
7
7
  #
8
8
  # @see https://uber.github.io/h3/#/documentation/api-reference/indexing
9
9
  module Indexing
10
- include Bindings::Structs
10
+ extend H3::Bindings::Base
11
11
  # Derive H3 index for the given set of coordinates.
12
12
  #
13
13
  # @param [Array<Integer>] coords A coordinate pair.
14
14
  # @param [Integer] resolution The desired resolution of the H3 index.
15
15
  #
16
16
  # @example Derive the H3 index for the given coordinates.
17
- # H3.geo_to_h3([52.24630137198303, -1.7358398437499998], 9)
17
+ # H3.from_geo_coordinates([52.24630137198303, -1.7358398437499998], 9)
18
18
  # 617439284584775679
19
19
  #
20
20
  # @raise [ArgumentError] If coordinates are invalid.
21
21
  #
22
22
  # @return [Integer] H3 index.
23
- def geo_to_h3(coords, resolution)
23
+ def from_geo_coordinates(coords, resolution)
24
24
  raise ArgumentError unless coords.is_a?(Array) && coords.count == 2
25
25
 
26
26
  lat, lon = coords
@@ -42,11 +42,11 @@ module H3
42
42
  # @param [Integer] h3_index A valid H3 index.
43
43
  #
44
44
  # @example Derive the central coordinates for the given H3 index.
45
- # H3.h3_to_geo(617439284584775679)
45
+ # H3.to_geo_coordinates(617439284584775679)
46
46
  # [52.245519061399506, -1.7363137757391423]
47
47
  #
48
48
  # @return [Array<Integer>] A coordinate pair.
49
- def h3_to_geo(h3_index)
49
+ def to_geo_coordinates(h3_index)
50
50
  coords = GeoCoord.new
51
51
  Bindings::Private.h3_to_geo(h3_index, coords)
52
52
  [rads_to_degs(coords[:lat]), rads_to_degs(coords[:lon])]
@@ -62,7 +62,7 @@ module H3
62
62
  # @param [Integer] h3_index A valid H3 index.
63
63
  #
64
64
  # @example Derive the geographical boundary for the given H3 index.
65
- # H3.h3_to_geo_boundary(617439284584775679)
65
+ # H3.to_boundary(617439284584775679)
66
66
  # [
67
67
  # [52.247260929171055, -1.736809158397472], [52.24625850761068, -1.7389279144996015],
68
68
  # [52.244516619273476, -1.7384324668792375], [52.243777169245725, -1.7358184256304658],
@@ -70,7 +70,7 @@ module H3
70
70
  # ]
71
71
  #
72
72
  # @return [Array<Array<Integer>>] An array of six coordinate pairs.
73
- def h3_to_geo_boundary(h3_index)
73
+ def to_boundary(h3_index)
74
74
  geo_boundary = GeoBoundary.new
75
75
  Bindings::Private.h3_to_geo_boundary(h3_index, geo_boundary)
76
76
  geo_boundary[:verts].take(geo_boundary[:num_verts]).map do |d|
@@ -8,59 +8,59 @@ module H3
8
8
  H3_TO_STR_BUF_SIZE = 17
9
9
  private_constant :H3_TO_STR_BUF_SIZE
10
10
 
11
- # @!method h3_resolution(h3_index)
11
+ # @!method resolution(h3_index)
12
12
  #
13
13
  # Derive the resolution of a given H3 index
14
14
  #
15
15
  # @param [Integer] h3_index A valid H3 index
16
16
  #
17
17
  # @example Derive the resolution of a H3 index
18
- # H3.h3_resolution(617700440100569087)
18
+ # H3.resolution(617700440100569087)
19
19
  # 9
20
20
  #
21
21
  # @return [Integer] Resolution of H3 index
22
- attach_function :h3_resolution, :h3GetResolution, %i[h3_index], Resolution
22
+ attach_function :resolution, :h3GetResolution, %i[h3_index], Resolution
23
23
 
24
- # @!method h3_base_cell(h3_index)
24
+ # @!method base_cell(h3_index)
25
25
  #
26
26
  # Derives the base cell number of the given H3 index
27
27
  #
28
28
  # @param [Integer] h3_index A valid H3 index
29
29
  #
30
30
  # @example Derive the base cell number of a H3 index
31
- # H3.h3_base_cell(617700440100569087)
31
+ # H3.base_cell(617700440100569087)
32
32
  # 20
33
33
  #
34
34
  # @return [Integer] Base cell number
35
- attach_function :h3_base_cell, :h3GetBaseCell, %i[h3_index], :int
35
+ attach_function :base_cell, :h3GetBaseCell, %i[h3_index], :int
36
36
 
37
- # @!method string_to_h3(h3_string)
37
+ # @!method from_string(h3_string)
38
38
  #
39
39
  # Derives the H3 index for a given hexadecimal string representation.
40
40
  #
41
41
  # @param [String] h3_string A H3 index in hexadecimal form.
42
42
  #
43
43
  # @example Derive the H3 index from the given hexadecimal form.
44
- # H3.string_to_h3("8928308280fffff")
44
+ # H3.from_string("8928308280fffff")
45
45
  # 617700169958293503
46
46
  #
47
47
  # @return [Integer] H3 index
48
- attach_function :string_to_h3, :stringToH3, %i[string], :h3_index
48
+ attach_function :from_string, :stringToH3, %i[string], :h3_index
49
49
 
50
- # @!method h3_pentagon?(h3_index)
50
+ # @!method pentagon?(h3_index)
51
51
  #
52
52
  # Determine whether the given H3 index is a pentagon.
53
53
  #
54
54
  # @param [Integer] h3_index A valid H3 index.
55
55
  #
56
56
  # @example Check if H3 index is a pentagon
57
- # H3.h3_pentagon?(585961082523222015)
57
+ # H3.pentagon?(585961082523222015)
58
58
  # true
59
59
  #
60
60
  # @return [Boolean] True if the H3 index is a pentagon.
61
- attach_function :h3_pentagon, :h3IsPentagon, %i[h3_index], :bool
61
+ attach_predicate_function :pentagon?, :h3IsPentagon, %i[h3_index], :bool
62
62
 
63
- # @!method h3_res_class_3?(h3_index)
63
+ # @!method class_3_resolution?(h3_index)
64
64
  #
65
65
  # Determine whether the given H3 index has a resolution with
66
66
  # Class III orientation.
@@ -68,35 +68,35 @@ module H3
68
68
  # @param [Integer] h3_index A valid H3 index.
69
69
  #
70
70
  # @example Check if H3 index has a class III resolution.
71
- # H3.h3_res_class_3?(599686042433355775)
71
+ # H3.class_3_resolution?(599686042433355775)
72
72
  # true
73
73
  #
74
74
  # @return [Boolean] True if the H3 index has a class III resolution.
75
- attach_function :h3_res_class_3, :h3IsResClassIII, %i[h3_index], :bool
75
+ attach_predicate_function :class_3_resolution?, :h3IsResClassIII, %i[h3_index], :bool
76
76
 
77
- # @!method h3_valid?(h3_index)
77
+ # @!method valid?(h3_index)
78
78
  #
79
79
  # Determine whether the given H3 index is valid.
80
80
  #
81
81
  # @param [Integer] h3_index A H3 index.
82
82
  #
83
83
  # @example Check if H3 index is valid
84
- # H3.h3_valid?(599686042433355775)
84
+ # H3.valid?(599686042433355775)
85
85
  # true
86
86
  #
87
87
  # @return [Boolean] True if the H3 index is valid.
88
- attach_function :h3_valid, :h3IsValid, %i[h3_index], :bool
88
+ attach_predicate_function :valid?, :h3IsValid, %i[h3_index], :bool
89
89
 
90
90
  # Derives the hexadecimal string representation for a given H3 index.
91
91
  #
92
92
  # @param [Integer] h3_index A valid H3 index.
93
93
  #
94
94
  # @example Derive the given hexadecimal form for the H3 index
95
- # H3.h3_to_string(617700169958293503)
95
+ # H3.to_string(617700169958293503)
96
96
  # "89283470dcbffff"
97
97
  #
98
98
  # @return [String] H3 index in hexadecimal form.
99
- def h3_to_string(h3_index)
99
+ def to_string(h3_index)
100
100
  h3_str = FFI::MemoryPointer.new(:char, H3_TO_STR_BUF_SIZE)
101
101
  Bindings::Private.h3_to_string(h3_index, h3_str, H3_TO_STR_BUF_SIZE)
102
102
  h3_str.read_string
@@ -115,20 +115,16 @@ module H3
115
115
  # @return [Integer] Maximum possible number of faces
116
116
  attach_function :max_face_count, :maxFaceCount, %i[h3_index], :int
117
117
 
118
- # void h3GetFaces(H3Index h, int* out);
119
-
120
- # @!method h3_faces(h3_index)
121
- #
122
118
  # Find all icosahedron faces intersected by a given H3 index.
123
119
  #
124
120
  # @param [Integer] h3_index A H3 index.
125
121
  #
126
122
  # @example Find icosahedron faces for given index
127
- # H3.h3_faces(585961082523222015)
123
+ # H3.faces(585961082523222015)
128
124
  # [1, 2, 6, 7, 11]
129
125
  #
130
126
  # @return [Array<Integer>] Faces. Faces are represented as integers from 0-19, inclusive.
131
- def h3_faces(h3_index)
127
+ def faces(h3_index)
132
128
  max_faces = max_face_count(h3_index)
133
129
  out = FFI::MemoryPointer.new(:int, max_faces)
134
130
  Bindings::Private.h3_faces(h3_index, out)
@@ -70,18 +70,18 @@ module H3
70
70
  # @return [Float] Average hexagon area in square metres.
71
71
  attach_function :hex_area_m2, :hexAreaM2, [Resolution], :double
72
72
 
73
- # @!method num_hexagons(resolution)
73
+ # @!method hexagon_count(resolution)
74
74
  #
75
75
  # Number of unique H3 indexes at the given resolution.
76
76
  #
77
77
  # @param [Integer] resolution Resolution.
78
78
  #
79
79
  # @example Find number of hexagons at resolution 6
80
- # H3.num_hexagons(6)
80
+ # H3.hexagon_count(6)
81
81
  # 14117882
82
82
  #
83
83
  # @return [Integer] Number of unique hexagons
84
- attach_function :num_hexagons, :numHexagons, [Resolution], :ulong_long
84
+ attach_function :hexagon_count, :numHexagons, [Resolution], :ulong_long
85
85
 
86
86
  # @!method rads_to_degs(rads)
87
87
  #
@@ -96,28 +96,176 @@ module H3
96
96
  # @return [Float] Value expressed in degrees.
97
97
  attach_function :rads_to_degs, :radsToDegs, %i[double], :double
98
98
 
99
- # @!method res_0_index_count
99
+ # @!method base_cell_count
100
100
  #
101
101
  # Returns the number of resolution 0 hexagons (base cells).
102
102
  #
103
103
  # @example Return the number of base cells
104
- # H3.res_0_index_count
104
+ # H3.base_cell_count
105
105
  # 122
106
106
  #
107
107
  # @return [Integer] The number of resolution 0 hexagons (base cells).
108
- attach_function :res_0_index_count, :res0IndexCount, [], :int
108
+ attach_function :base_cell_count, :res0IndexCount, [], :int
109
+
110
+ # @!method pentagon_count
111
+ #
112
+ # Number of pentagon H3 indexes per resolution.
113
+ # This is always 12, but provided as a convenience.
114
+ #
115
+ # @example Return the number of pentagons
116
+ # H3.pentagon_count
117
+ # 12
118
+ #
119
+ # @return [Integer] The number of pentagons per resolution.
120
+ attach_function :pentagon_count, :pentagonIndexCount, [], :int
121
+
122
+ # @!method cell_area_rads2
123
+ #
124
+ # Area of a given cell expressed in radians squared
125
+ #
126
+ # @example Return the area of the cell
127
+ # H3.cell_area_rads2(617700169958293503)
128
+ # 2.6952182709835757e-09
129
+ #
130
+ # @return [Double] Area of cell in rads2
131
+ attach_function :cell_area_rads2, :cellAreaRads2, %i[h3_index], :double
132
+
133
+ # @!method cell_area_km2
134
+ #
135
+ # Area of a given cell expressed in km squared
136
+ #
137
+ # @example Return the area of the cell
138
+ # H3.cell_area_km2(617700169958293503)
139
+ # 0.10939818864648902
140
+ #
141
+ # @return [Double] Area of cell in km2
142
+ attach_function :cell_area_km2, :cellAreaKm2, %i[h3_index], :double
143
+
144
+ # @!method cell_area_m2
145
+ #
146
+ # Area of a given cell expressed in metres squared
147
+ #
148
+ # @example Return the area of the cell
149
+ # H3.cell_area_m2(617700169958293503)
150
+ # 109398.18864648901
151
+ #
152
+ # @return [Double] Area of cell in metres squared
153
+ attach_function :cell_area_m2, :cellAreaM2, %i[h3_index], :double
154
+
155
+ # @!method exact_edge_length_rads
156
+ #
157
+ # Exact length of edge in rads
158
+ #
159
+ # @example Return the edge length
160
+ # H3.exact_edge_length_rads(1266218516299644927)
161
+ # 3.287684056071637e-05
162
+ #
163
+ # @return [Double] Edge length in rads
164
+ attach_function :exact_edge_length_rads, :exactEdgeLengthRads, %i[h3_index], :double
165
+
166
+ # @!method exact_edge_length_km
167
+ #
168
+ # Exact length of edge in kilometres
169
+ #
170
+ # @example Return the edge length
171
+ # H3.exact_edge_length_km(1266218516299644927)
172
+ # 3.287684056071637e-05
173
+ #
174
+ # @return [Double] Edge length in kilometres
175
+ attach_function :exact_edge_length_km, :exactEdgeLengthKm, %i[h3_index], :double
176
+
177
+ # @!method exact_edge_length_m
178
+ #
179
+ # Exact length of edge in metres
180
+ #
181
+ # @example Return the edge length
182
+ # H3.exact_edge_length_m(1266218516299644927)
183
+ # 3.287684056071637e-05
184
+ #
185
+ # @return [Double] Edge length in metres
186
+ attach_function :exact_edge_length_m, :exactEdgeLengthM, %i[h3_index], :double
187
+
188
+ # Returns the radians distance between two points.
189
+ #
190
+ # @example Return radians distance.
191
+ # H3.point_distance_rads([41.3964809, 2.160444], [41.3870609, 2.164917])
192
+ # 0.00017453024784008713
193
+ #
194
+ # @return [Double] Radians distance between two points.
195
+ def point_distance_rads(origin, destination)
196
+ Bindings::Private.point_distance_rads(*build_geocoords(origin, destination))
197
+ end
198
+
199
+ # Returns the kilometres distance between two points.
200
+ #
201
+ # @example Return km distance.
202
+ # H3.point_distance_km([41.3964809, 2.160444], [41.3870609, 2.164917])
203
+ # 1.1119334622766763
204
+ #
205
+ # @return [Double] KM distance between two points.
206
+ def point_distance_km(origin, destination)
207
+ Bindings::Private.point_distance_km(*build_geocoords(origin, destination))
208
+ end
209
+
210
+ # Returns the metre distance between two points.
211
+ #
212
+ # @example Return metre distance.
213
+ # H3.point_distance_m([41.3964809, 2.160444], [41.3870609, 2.164917])
214
+ # 1111.9334622766764
215
+ #
216
+ # @return [Double] Metre distance between two points.
217
+ def point_distance_m(origin, destination)
218
+ Bindings::Private.point_distance_m(*build_geocoords(origin, destination))
219
+ end
109
220
 
110
221
  # Returns all resolution 0 hexagons (base cells).
111
222
  #
112
223
  # @example Return all base cells.
113
- # H3.res_0_indexes
224
+ # H3.base_cells
114
225
  # [576495936675512319, 576531121047601151, ..., 580753245698260991]
115
226
  #
116
227
  # @return [Array<Integer>] All resolution 0 hexagons (base cells).
117
- def res_0_indexes
118
- out = H3Indexes.of_size(res_0_index_count)
228
+ def base_cells
229
+ out = H3Indexes.of_size(base_cell_count)
119
230
  Bindings::Private.res_0_indexes(out)
120
231
  out.read
121
232
  end
233
+
234
+ # Returns all pentagon indexes at the given resolution.
235
+ #
236
+ # @example Return all pentagons at resolution 4.
237
+ # H3.pentagons(4)
238
+ # [594615896891195391, 594967740612083711, ..., 598591730937233407]
239
+ #
240
+ # @return [Array<Integer>] All pentagon indexes at the given resolution.
241
+ def pentagons(resolution)
242
+ out = H3Indexes.of_size(pentagon_count)
243
+ Bindings::Private.get_pentagon_indexes(resolution, out)
244
+ out.read
245
+ end
246
+
247
+ private
248
+
249
+ def build_geocoords(origin, destination)
250
+ [origin, destination].inject([]) do |acc, coords|
251
+ validate_coordinate(coords)
252
+
253
+ geo_coord = GeoCoord.new
254
+ lat, lon = coords
255
+ geo_coord[:lat] = degs_to_rads(lat)
256
+ geo_coord[:lon] = degs_to_rads(lon)
257
+ acc << geo_coord
258
+ end
259
+ end
260
+
261
+ def validate_coordinate(coords)
262
+ raise ArgumentError unless coords.is_a?(Array) && coords.count == 2
263
+
264
+ lat, lon = coords
265
+
266
+ if lat > 90 || lat < -90 || lon > 180 || lon < -180
267
+ raise(ArgumentError, "Invalid coordinates")
268
+ end
269
+ end
122
270
  end
123
271
  end