h3 icon indicating copy to clipboard operation
h3 copied to clipboard

"polygonToCells" fails near poles

Open benguild opened this issue 2 months ago • 0 comments

Possibly related to #1029, #1046, and #1048… both calls below returnE_FAILED codes.

Demo

#include <h3/h3api.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
    LatLng verts[] = {
        {.lat = degsToRads(-89.99993255000588), .lng = degsToRads(180.0)},
        {.lat = degsToRads(-89.99993255000588), .lng = degsToRads(174.374766472271318)},
        {.lat = degsToRads(-89.99993255000588), .lng = degsToRads(168.749766472271205)},
        {.lat = degsToRads(-88.021978021978015), .lng = degsToRads(180.0)}
    };

    GeoLoop geoloop = {
        .numVerts = 4,
        .verts = verts
    };

    GeoPolygon polygon = {
        .geoloop = geoloop,
        .numHoles = 0,
        .holes = NULL
    };

    int resolution = 3;
    int64_t maxCells = 87000000;

    int64_t numCells1;
    H3Error err1 = maxPolygonToCellsSizeExperimental(
        &polygon, resolution, CONTAINMENT_FULL, &numCells1
    );

    if (err1 == E_SUCCESS) {
        H3Index *cells1 = calloc(numCells1, sizeof(H3Index));
        err1 = polygonToCellsExperimental( // error=5 (E_FAILED)
            &polygon, resolution, CONTAINMENT_FULL, cells1
        );
        printf("polygonToCellsExperimental: error=%d\n", err1);
        free(cells1);
    }

    int64_t numCells2;
    H3Error err2 = maxPolygonToCellsSize(&polygon, resolution, 0, &numCells2);

    if (err2 == E_SUCCESS) {
        H3Index *cells2 = calloc(numCells2, sizeof(H3Index));
        err2 = polygonToCells(&polygon, resolution, 0, cells2); // error=5 (E_FAILED)
        printf("polygonToCells: error=%d\n", err2);
        free(cells2);
    }

    return 0;
}

benguild avatar Oct 03 '25 17:10 benguild