module

H3::GeoJSON

GeoJSON integration for H3 polygon operations.

This module provides convenience methods to convert between GeoJSON Polygon/MultiPolygon geometries and H3 polyfill operations.

This is an optional integration. To use it, consumers must add the geojson shard to their dependencies and require this file explicitly:

require "h3_crystal/h3/geojson"

@example Fill a GeoJSON Polygon with H3 cells polygon = GeoJSON::Polygon.new([ [[-122.4089866, 37.813318], [-122.3805436, 37.7866302], [-122.3544736, 37.7198061], [-122.5123436, 37.7076131], [-122.4089866, 37.813318]] ]) cells = H3.polygon_to_cells(polygon, 9)

@example Convert H3 cells to a GeoJSON MultiPolygon cells = H3.polygon_to_cells(polygon, 9) multi_polygon = H3.cells_to_geojson_multi_polygon(cells)

Instance methods

cells_to_geojson_multi_polygon(h3_set : Array(UInt64)) : ::GeoJSON::MultiPolygon

Convert a set of H3 cells to a GeoJSON::MultiPolygon geometry.

@param [Array<UInt64>] h3_set An array of valid H3 indexes.

@example Convert cells to a GeoJSON MultiPolygon. cells = H3.polygon_to_cells(polygon, 9) multi_polygon = H3.cells_to_geojson_multi_polygon(cells)

@return [::GeoJSON::MultiPolygon] A GeoJSON MultiPolygon geometry.

Source
polygon_to_cells(polygon : ::GeoJSON::Polygon, resolution : Int32, flags : UInt32 = 0_u32) : Array(UInt64)

Fill a GeoJSON::Polygon with H3 cells at the specified resolution.

The GeoJSON Polygon's exterior ring is used as the outer boundary. Any additional rings are treated as holes.

GeoJSON coordinates are in [longitude, latitude] order per RFC 7946. These are converted to the (latitude, longitude) tuples expected by H3.

@param [::GeoJSON::Polygon] polygon A GeoJSON Polygon geometry. @param [Integer] resolution The desired H3 resolution (0-15). @param [UInt32] flags Containment mode flags (default 0 = center containment).

@example Fill a GeoJSON polygon with H3 cells at resolution 9. polygon = GeoJSON::Polygon.new([ [[-122.4089866, 37.813318], [-122.3805436, 37.7866302], [-122.3544736, 37.7198061], [-122.5123436, 37.7076131], [-122.4089866, 37.813318]] ]) H3.polygon_to_cells(polygon, 9)

@return [Array<UInt64>] Array of H3 indexes filling the polygon.

Source
polygon_to_cells(multi_polygon : ::GeoJSON::MultiPolygon, resolution : Int32, flags : UInt32 = 0_u32) : Array(UInt64)

Fill a GeoJSON::MultiPolygon with H3 cells at the specified resolution.

Each sub-polygon is filled independently and the results are combined, with duplicates removed.

@param [::GeoJSON::MultiPolygon] multi_polygon A GeoJSON MultiPolygon geometry. @param [Integer] resolution The desired H3 resolution (0-15). @param [UInt32] flags Containment mode flags (default 0 = center containment).

@example Fill a GeoJSON MultiPolygon with H3 cells at resolution 7. multi_polygon = GeoJSON::MultiPolygon.new([polygon1, polygon2]) H3.polygon_to_cells(multi_polygon, 7)

@return [Array<UInt64>] Array of H3 indexes filling all polygons.

Source