-
Notifications
You must be signed in to change notification settings - Fork 511
Argument parsing for h3ToGeo, h3ToGeoBoundary, geoToH3 #227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2b5bd7b
Argument parsing for h3ToGeo, h3ToGeoBoundayr, geoToH3
aaf79ae
Print argument validation error with the help message.
0c28636
Pass the actual number of arguments
1c5cb75
Abbreviate arguments with `--` when not single character
1f53dc7
KML help text
f1c2199
Adjust arg description
7f3de95
Merge branch 'master' into args-h3-to-geo
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright 2016-2017 Uber Technologies, Inc. | ||
* Copyright 2016-2017, 2019 Uber Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -17,43 +17,37 @@ | |
* @brief stdin/stdout filter that converts from integer H3 indexes to lat/lon | ||
* cell center point | ||
* | ||
* usage: `h3ToGeo [outputMode kmlName kmlDesc]` | ||
* usage: `h3ToGeo [--index index] [--kml [--kml-name name] [--kml-description | ||
* desc]]` | ||
* | ||
* The program reads H3 indexes from stdin and outputs the corresponding | ||
* cell center points to stdout, until EOF is encountered. The H3 indexes | ||
* should be in integer form. | ||
* | ||
* `outputMode` indicates the type of output; the choices are 0 for | ||
* plain text output (the default) and 1 for KML output | ||
* | ||
* `kmlName` indicates the string for the name tag in KML output (only used | ||
* when `outputMode` == 1). The default is "geo from H3". | ||
* | ||
* `kmlName` indicates the string for the desc tag in KML output (only used | ||
* when `outputMode` == 1). The default is "generated by h3ToGeo". | ||
* `--kml` causes KML output to be printed. `--kml-name` and | ||
* `--kml-description` can be used to change the name and description in the | ||
* KML header, which default to "H3 Geometry" and "Generated by h3ToGeo" | ||
* respectively. | ||
* | ||
* Examples: | ||
F438 | * | |
* `h3ToGeo < indexes.txt` | ||
* - outputs plain text cell center points for the H3 indexes contained | ||
* in the file `indexes.txt` | ||
* | ||
* `h3ToGeo 1 "kml file" "h3 cells" < indexes.txt > cells.kml` | ||
* `h3ToGeo --kml --kml-name "kml title" --kml-description "h3 cells" < | ||
* indexes.txt > cells.kml` | ||
* - creates the KML file `cells.kml` containing the cell center points | ||
* for all of the H3 indexes contained in the file `indexes.txt`. | ||
*/ | ||
|
||
#include <inttypes.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include "baseCells.h" | ||
#include "coordijk.h" | ||
#include "geoCoord.h" | ||
#include "h3Index.h" | ||
#include "h3api.h" | ||
#include "kml.h" | ||
#include "utility.h" | ||
#include "vec2d.h" | ||
|
||
void doCell(H3Index h, int isKmlOut) { | ||
GeoCoord g; | ||
|
@@ -72,46 +66,68 @@ void doCell(H3Index h, int isKmlOut) { | |
} | ||
|
||
int main(int argc, char *argv[]) { | ||
// check command line args | ||
if (argc > 5) { | ||
fprintf(stderr, "usage: %s [outputMode kmlName kmlDesc]\n", argv[0]); | ||
exit(1); | ||
} | ||
H3Index index = 0; | ||
char userKmlName[BUFF_SIZE] = {0}; | ||
char userKmlDesc[BUFF_SIZE] = {0}; | ||
|
||
int isKmlOut = 0; | ||
if (argc > 1) { | ||
if (!sscanf(argv[1], "%d", &isKmlOut)) | ||
error("outputMode must be an integer"); | ||
Arg helpArg = {.names = {"-h", "--help"}, | ||
.helpText = "Show this help message."}; | ||
Arg indexArg = { | ||
.names = {"-i", "--index"}, | ||
.scanFormat = "%" PRIx64, | ||
.valueName = "index", | ||
.value = &index, | ||
.helpText = | ||
"Index, or not specified to read indexes from standard in."}; | ||
Arg kmlArg = {.names = {"-k", "--kml"}, | ||
.helpText = "Print output in KML format."}; | ||
Arg kmlNameArg = {.names = {"--kn", "--kml-name"}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I might have gone for |
||
.scanFormat = "%255c", // BUFF_SIZE - 1 | ||
.valueName = "name", | ||
.value = &userKmlName, | ||
.helpText = "Text for the KML name tag."}; | ||
Arg kmlDescArg = {.names = {"--kd", "--kml-description"}, | ||
.scanFormat = "%255c", // BUFF_SIZE - 1 | ||
.valueName = "description", | ||
.value = &userKmlDesc, | ||
.helpText = "Text for the KML description tag."}; | ||
|
||
if (isKmlOut != 0 && isKmlOut != 1) error("outputMode must be 0 or 1"); | ||
char *defaultKmlName = "geo from H3"; | ||
char *defaultKmlDesc = "from h3ToGeo"; | ||
Arg *args[] = {&helpArg, &indexArg, &kmlArg, &kmlNameArg, &kmlDescArg}; | ||
|
||
char *kmlName = defaultKmlName; | ||
char *kmlDesc = defaultKmlDesc; | ||
if (parseArgs(argc, argv, 5, args, &helpArg, | ||
"Converts indexes to latitude/longitude center coordinates " | ||
"in degrees")) { | ||
return helpArg.found ? 0 : 1; | ||
} | ||
|
||
if (argc > 2) { | ||
kmlName = argv[2]; | ||
if (argc > 3) kmlDesc = argv[3]; | ||
} | ||
if (kmlArg.found) { | ||
char *kmlName = "H3 Geometry"; | ||
if (kmlNameArg.found) kmlName = userKmlName; | ||
|
||
char *kmlDesc = "Generated by h3ToGeo"; | ||
if (kmlDescArg.found) kmlDesc = userKmlDesc; | ||
|
||
kmlPtsHeader(kmlName, kmlDesc); | ||
} | ||
|
||
// process the indexes on stdin | ||
char buff[BUFF_SIZE]; | ||
while (1) { | ||
// get an index from stdin | ||
if (!fgets(buff, BUFF_SIZE, stdin)) { | ||
if (feof(stdin)) | ||
break; | ||
else | ||
error("reading H3 index from stdin"); | ||
} | ||
if (indexArg.found) { | ||
doCell(index, kmlArg.found); | ||
} else { | ||
// process the indexes on stdin | ||
char buff[BUFF_SIZE]; | ||
while (1) { | ||
// get an index from stdin | ||
if (!fgets(buff, BUFF_SIZE, stdin)) { | ||
if (feof(stdin)) | ||
break; | ||
else | ||
error("reading H3 index from stdin"); | ||
} | ||
|
||
H3Index h3 = H3_EXPORT(stringToH3)(buff); | ||
doCell(h3, isKmlOut); | ||
H3Index h3 = H3_EXPORT(stringToH3)(buff); | ||
doCell(h3, kmlArg.found); | ||
} | ||
} | ||
|
||
if (isKmlOut) kmlPtsFooter(); | ||
if (kmlArg.found) kmlPtsFooter(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was a bug from the last diff?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this was a copy paste error when refactoring the code. I found it from a segfault when testing another filter with only three arguments.