Skip to content

Commit

Permalink
Actually get polygon file reading working and add the polygon tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dfellis committed Oct 7, 2024
1 parent 24b0c6e commit c994043
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 17 deletions.
42 changes: 25 additions & 17 deletions src/apps/filters/h3.c
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,7 @@ H3Error polygonStringToGeoPolygon(FILE *fp, char *polygonString,
int strPos = 0;
while (polygonString[strPos] != 0) {
// Load more of the file if we've hit our buffer limit
if (strPos == 1500 && fp != 0) {
if (strPos >= 1500 && fp != 0) {
int res = fread(polygonString, 1, 1500, fp);
strPos = 0;
// If we didn't get any data from the file, we're done.
Expand Down Expand Up @@ -1260,9 +1260,12 @@ H3Error polygonStringToGeoPolygon(FILE *fp, char *polygonString,
// value, then store the lat, lng pair
double lat, lng;
int count;
int res = sscanf(polygonString + strPos, "%lf%*[, \n]%lf%n", &lat, &lng,
&count);
if (count > 0 && res != 0) {
// Must grab the closing ] or we might accidentally parse a partial
// float across buffer boundaries
char closeBracket[2] = "]";
int res = sscanf(polygonString + strPos, "%lf%*[, \n]%lf%1[]]%n", &lat,
&lng, &closeBracket[0], &count);
if (count > 0 && res == 3) {
verts[curVert].lat = H3_EXPORT(degsToRads)(lat);
verts[curVert].lng = H3_EXPORT(degsToRads)(lng);
curVert++;
Expand All @@ -1279,6 +1282,7 @@ H3Error polygonStringToGeoPolygon(FILE *fp, char *polygonString,
numVerts *= 2;
}
strPos += count;
curDepth--;
continue;
}
// Check for whitespace and skip it if we reach this point.
Expand All @@ -1304,26 +1308,30 @@ H3Error polygonStringToGeoPolygon(FILE *fp, char *polygonString,
} else {
// Move the end of this buffer to the beginning
for (int i = strPos; i <= 1500; i++) {
polygonString[i - strPos] = polygonString[strPos];
polygonString[i - strPos] = polygonString[i];
}
// Set the string position to the end of the buffer
strPos = 1500 - strPos;
// Grab up to the remaining size of the buffer and fill it
// into the file
int res =
fread(polygonString + strPos, 1, 1500 - strPos, fp);
// Did you know that if the amount you want to read from
// the file buffer is larger than the buffer itself,
// fread can choose to give you squat and not the
// remainder of the file as you'd expect from the
// documentation? This was a surprise to me and a
// significant amount of wasted time to figure out how
// to tackle. C leaves *way* too much as undefined
// behavior to be nice to work with...
int64_t i = 0;
int res;
do {
res = fread(polygonString + strPos + i, 1, 1, fp);
i++;
} while (i < 1500 - strPos && res != 0);
if (res == 0) {
// If we got nothing new from the file, just treat this
// as a character to skip over and write an explicit 0
// to the old end.
polygonString[strPos] = 0;
strPos = 1;
continue;
} else {
// If we got something new, reset the position to 0 and
// try again.
strPos = 0;
polygonString[strPos + i - 1] = 0;
}
strPos = 0;
}
} else {
strPos++;
Expand Down
8 changes: 8 additions & 0 deletions tests/cli/maxPolygonToCellsSize.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
add_h3_cli_test(testCliMaxPolygonToCellsSizeFile1 "maxPolygonToCellsSize -r 7 -f ${PROJECT_SOURCE_DIR}/tests/inputfiles/polygon_test1.txt" "100")
add_h3_cli_test(testCliMaxPolygonToCellsSizeFile2 "maxPolygonToCellsSize -r 7 -f ${PROJECT_SOURCE_DIR}/tests/inputfiles/polygon_test2.txt" "23")
add_h3_cli_test(testCliMaxPolygonToCellsSizeFile3 "maxPolygonToCellsSize -r 4 -f ${PROJECT_SOURCE_DIR}/tests/inputfiles/polygon_test3.txt" "3484")
add_h3_cli_test(testCliMaxPolygonToCellsSizeStdin1 "maxPolygonToCellsSize -r 7 -f -- < ${PROJECT_SOURCE_DIR}/tests/inputfiles/polygon_test1.txt" "100")
add_h3_cli_test(testCliMaxPolygonToCellsSizeStdin2 "maxPolygonToCellsSize -r 7 -f -- < ${PROJECT_SOURCE_DIR}/tests/inputfiles/polygon_test2.txt" "23")
add_h3_cli_test(testCliMaxPolygonToCellsSizeStdin3 "maxPolygonToCellsSize -r 4 -f -- < ${PROJECT_SOURCE_DIR}/tests/inputfiles/polygon_test3.txt" "3484")
add_h3_cli_test(testCliMaxPolygonToCellsSizeArg1 "maxPolygonToCellsSize -r 7 -p \"\\\\`cat ${PROJECT_SOURCE_DIR}/tests/inputfiles/polygon_test1.txt\\\\`\"" "100")
add_h3_cli_test(testCliMaxPolygonToCellsSizeArg2 "maxPolygonToCellsSize -r 7 -p \"\\\\`cat ${PROJECT_SOURCE_DIR}/tests/inputfiles/polygon_test2.txt\\\\`\"" "23")
8 changes: 8 additions & 0 deletions tests/cli/polygonToCells.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
add_h3_cli_test(testCliPolygonToCellsFile1 "polygonToCells -r 7 -f ${PROJECT_SOURCE_DIR}/tests/inputfiles/polygon_test1.txt | tr -s '\\\\r\\\\n' ',' | tr -s '\\\\n' ','" "87283082bffffff,872830870ffffff,872830820ffffff,87283082effffff,872830828ffffff,87283082affffff,872830876ffffff,")
add_h3_cli_test(testCliPolygonToCellsFile2 "polygonToCells -r 7 -f ${PROJECT_SOURCE_DIR}/tests/inputfiles/polygon_test2.txt | tr -s '\\\\r\\\\n' ',' | tr -s '\\\\n' ','" "872830828ffffff,87283082effffff,")
add_h3_cli_test(testCliPolygonToCellsFile3 "polygonToCells -r 4 -f ${PROJECT_SOURCE_DIR}/tests/inputfiles/polygon_test3.txt | tr -s '\\\\r\\\\n' ',' | tr -s '\\\\n' ','" "84f05a3ffffffff,84e720bffffffff,84e7257ffffffff,84e725bffffffff,")
add_h3_cli_test(testCliPolygonToCellsStdin1 "polygonToCells -r 7 -f -- < ${PROJECT_SOURCE_DIR}/tests/inputfiles/polygon_test1.txt | tr -s '\\\\r\\\\n' ',' | tr -s '\\\\n' ','" "87283082bffffff,872830870ffffff,872830820ffffff,87283082effffff,872830828ffffff,87283082affffff,872830876ffffff,")
add_h3_cli_test(testCliPolygonToCellsStdin2 "polygonToCells -r 7 -f -- < ${PROJECT_SOURCE_DIR}/tests/inputfiles/polygon_test2.txt | tr -s '\\\\r\\\\n' ',' | tr -s '\\\\n' ','" "872830828ffffff,87283082effffff,")
add_h3_cli_test(testCliPolygonToCellsStdin3 "polygonToCells -r 4 -f -- < ${PROJECT_SOURCE_DIR}/tests/inputfiles/polygon_test3.txt | tr -s '\\\\r\\\\n' ',' | tr -s '\\\\n' ','" "84f05a3ffffffff,84e720bffffffff,84e7257ffffffff,84e725bffffffff,")
add_h3_cli_test(testCliPolygonToCellsArg1 "polygonToCells -r 7 -p \"\\\\`cat ${PROJECT_SOURCE_DIR}/tests/inputfiles/polygon_test1.txt\\\\`\" | tr -s '\\\\r\\\\n' ',' | tr -s '\\\\n' ','" "87283082bffffff,872830870ffffff,872830820ffffff,87283082effffff,872830828ffffff,87283082affffff,872830876ffffff,")
add_h3_cli_test(testCliPolygonToCellsArg2 "polygonToCells -r 7 -p \"\\\\`cat ${PROJECT_SOURCE_DIR}/tests/inputfiles/polygon_test2.txt\\\\`\" | tr -s '\\\\r\\\\n' ',' | tr -s '\\\\n' ','" "872830828ffffff,87283082effffff,")
1 change: 1 addition & 0 deletions tests/inputfiles/polygon_test1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[37.813318999983238, -122.4089866999972145],[37.7198061999978478, -122.3544736999993603],[37.8151571999998453, -122.4798767000009008]]
1 change: 1 addition & 0 deletions tests/inputfiles/polygon_test2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[[[37.784046, -122.427089], [37.772267, -122.434586], [37.761736, -122.425769], [37.762982, -122.409455], [37.752446, -122.400640], [37.753689, -122.384324], [37.765468, -122.376819], [37.776004, -122.385635], [37.774761, -122.401954], [37.785293, -122.410771]]]]
1 change: 1 addition & 0 deletions tests/inputfiles/polygon_test3.txt

Large diffs are not rendered by default.

0 comments on commit c994043

Please sign in to comment.