2025-09-16 14:41:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
module Maps
|
|
|
|
|
class HexagonPolygonGenerator
|
2025-09-19 13:55:27 -04:00
|
|
|
def initialize(h3_index:)
|
2025-09-18 18:23:12 -04:00
|
|
|
@h3_index = h3_index
|
2025-09-16 14:41:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def call
|
2025-09-19 13:55:27 -04:00
|
|
|
# Parse H3 index from hex string if needed
|
|
|
|
|
index = h3_index.is_a?(String) ? h3_index.to_i(16) : h3_index
|
2025-09-16 19:55:42 -04:00
|
|
|
|
|
|
|
|
# Get the boundary coordinates for this H3 hexagon
|
2025-09-19 13:55:27 -04:00
|
|
|
boundary_coordinates = H3.to_boundary(index)
|
2025-09-16 19:55:42 -04:00
|
|
|
|
|
|
|
|
# Convert to GeoJSON polygon format (lng, lat)
|
2025-09-18 16:23:47 -04:00
|
|
|
polygon_coordinates = boundary_coordinates.map { [_2, _1] }
|
2025-09-16 19:55:42 -04:00
|
|
|
|
|
|
|
|
# Close the polygon by adding the first point at the end
|
|
|
|
|
polygon_coordinates << polygon_coordinates.first
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
'type' => 'Polygon',
|
|
|
|
|
'coordinates' => [polygon_coordinates]
|
|
|
|
|
}
|
|
|
|
|
end
|
2025-09-19 13:55:27 -04:00
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
attr_reader :h3_index
|
2025-09-16 14:41:53 -04:00
|
|
|
end
|
2025-09-16 19:55:42 -04:00
|
|
|
end
|