Skip to content

h3_polygon_to_cells: Invalid geometries can return millions of Hex IDs #157

@jmealo

Description

@jmealo

Issue:

I've found that calling h3_polygon_to_cells with invalid geometries can result in as many as 2.3 million hex_ids being returned at a resolution of 7. This is not a bug in h3-pg but, rather how the h3 library handles invalid geometries. It's unclear whether we can guard against this, or should at the very least provide documentation/warnings about the issues and workarounds to mitigate them.

Cause:

  • All of the geometries that caused these issues fail ST_IsValid().
  • Using ST_MakeValid() turns them in bow ties.

Test case:

{
  "type": "Polygon",
  "coordinates": [
    [
      [-148.5, 29.1],
      [-148.5, 63.9],
      [-72.5, 29.1],
      [-72.5, 63.9],
      [-148.5, 29.1]
    ]
  ]
}
WITH h3_cells AS (
  SELECT h3_polygon_to_cells(
    ST_GeomFromGeoJSON('{
      "type": "Polygon",
      "coordinates": [
        [
          [-148.5, 29.1],
          [-148.5, 63.9],
          [-72.5, 29.1],
          [-72.5, 63.9],
          [-148.5, 29.1]
        ]
      ]
    }'),
    7
  ) AS cells
)
SELECT COUNT(1) 
FROM h3_cells;
# count = 2,364,092

Solution:

  • It might make sense to update the documentation or examples to include a call to ST_IsValid() ... or a warning that invalid geometries could return millions of hex ids?

Upstream issue:

uber/h3#926

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions