Related to #1046, I was playing around with this library and realized that there presently seem to be some subtleties in how -180º longitude is handled compared to +180º …because those are technically the same point but entirely different numbers. I was actually able to trigger a crash because of this, but I can't remember if it crashed the calling software test or if H3 actually did.
In any case, considering how rare it may be for some software to interact with these points, it might make sense to take extra care here. A workaround from the client side is detecting and normalizing exact -180.0 coordinates to the equivalent positive range before passing them, but unless this is expected and required, it seems like the library could likely handle that better.
Example
#include <h3/h3api.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
// Polygon with exact -180.0 longitude (in radians)
LatLng vertices180[] = {
{.lat = 0.837758, .lng = -3.141593},
{.lat = 0.837758, .lng = -3.016},
{.lat = 0.610865, .lng = -3.016},
{.lat = 0.610865, .lng = -3.141593}, // -180°
{.lat = 0.837758, .lng = -3.141593} // Close
};
// Same polygon with +180.0 (normalized)
LatLng verticesNorm[] = {
{.lat = 0.837758, .lng = 3.141593},
{.lat = 0.837758, .lng = -3.016},
{.lat = 0.610865, .lng = -3.016},
{.lat = 0.610865, .lng = 3.141593}, // +180°
{.lat = 0.837758, .lng = 3.141593} // Close
};
GeoPolygon polygon180 = {
.geoloop = {.numVerts = 5, .verts = vertices180},
.numHoles = 0,
.holes = NULL
};
GeoPolygon polygonNorm = {
.geoloop = {.numVerts = 5, .verts = verticesNorm},
.numHoles = 0,
.holes = NULL
};
// Test with -180 using CONTAINMENT_OVERLAPPING
int64_t maxCells = 10000;
H3Index *cells1 = calloc(maxCells, sizeof(H3Index));
H3Error err1 = polygonToCellsExperimental(
&polygon180, 3, CONTAINMENT_OVERLAPPING, maxCells, cells1);
int count1 = 0;
if (err1 == E_SUCCESS) {
for (int64_t i = 0; i < maxCells; i++) {
if (cells1[i] != 0) count1++;
}
}
// Test with +180 (normalized)
H3Index *cells2 = calloc(maxCells, sizeof(H3Index));
H3Error err2 = polygonToCellsExperimental(
&polygonNorm, 3, CONTAINMENT_OVERLAPPING, maxCells, cells2);
int count2 = 0;
if (err2 == E_SUCCESS) {
for (int64_t i = 0; i < maxCells; i++) {
if (cells2[i] != 0) count2++;
}
}
printf("With -180.0: %d cells\n", count1); // 1 cell (incorrect)
printf("With +180.0: %d cells\n", count2); // 15 cells (correct)
free(cells1);
free(cells2);
return 0;
}
Related to #1046, I was playing around with this library and realized that there presently seem to be some subtleties in how -180º longitude is handled compared to +180º …because those are technically the same point but entirely different numbers. I was actually able to trigger a crash because of this, but I can't remember if it crashed the calling software test or if H3 actually did.
In any case, considering how rare it may be for some software to interact with these points, it might make sense to take extra care here. A workaround from the client side is detecting and normalizing exact
-180.0coordinates to the equivalent positive range before passing them, but unless this is expected and required, it seems like the library could likely handle that better.Example