8000 Argument parsing for h3ToGeo, h3ToGeoBoundary, geoToH3 by isaacbrodsky · Pull Request #227 · uber/h3 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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 7 commits into from
May 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/apps/applib/include/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ typedef struct {

int parseArgs(int argc, char* argv[], int numArgs, Arg* args[],
const Arg* helpArg, const char* helpText);
void printHelp(FILE* out, const char* programName, const char* helpText,
int numArgs, Arg* args[], const char* errorMessage,
const char* errorDetails);

void error(const char* msg);
void h3Print(H3Index h); // prints as integer
Expand All @@ -108,8 +111,5 @@ void iterateAllIndexesAtResPartial(int res, void (*callback)(H3Index),
int _parseArgsList(int argc, char* argv[], int numArgs, Arg* args[],
const Arg* helpArg, const char** errorMessage,
const char** errorDetail);
void _printHelp(FILE* out, const char* programName, const char* helpText,
int numArgs, Arg* args[], const char* errorMessage,
const char* errorDetails);

#endif
14 changes: 7 additions & 7 deletions src/apps/applib/lib/utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ int parseArgs(int argc, char* argv[], int numArgs, Arg* args[],
const char* errorMessage = NULL;
const char* errorDetails = NULL;

int failed = _parseArgsList(argc, argv, 4, args, helpArg, &errorMessage,
Copy link
Collaborator

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?

Copy link
Collaborator Author
@isaacbrodsky isaacbrodsky May 2, 2019

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.

&errorDetails);
int failed = _parseArgsList(argc, argv, numArgs, args, helpArg,
&errorMessage, &errorDetails);

if (failed || helpArg->found) {
_printHelp(helpArg->found ? stdout : stderr, argv[0], helpText, numArgs,
args, errorMessage, errorDetails);
printHelp(helpArg->found ? stdout : stderr, argv[0], helpText, numArgs,
args, errorMessage, errorDetails);
return failed != PARSE_ARGS_SUCCESS ? failed : PARSE_ARGS_HELP;
}
return PARSE_ARGS_SUCCESS;
Expand Down Expand Up @@ -188,9 +188,9 @@ int _parseArgsList(int argc, char* argv[], int numArgs, Arg* args[],
* @param errorMessage Error message, or null
* @param errorDetails Additional error detail message, or null
*/
void _printHelp(FILE* out, const char* programName, const char* helpText,
int numArgs, Arg* args[], const char* errorMessage,
const char* errorDetails) {
void printHelp(FILE* out, const char* programName, const char* helpText,
int numArgs, Arg* args[], const char* errorMessage,
const char* errorDetails) {
if (errorMessage != NULL) {
fprintf(out, "%s: %s", programName, errorMessage);
if (errorDetails != NULL) {
Expand Down
100 changes: 73 additions & 27 deletions src/apps/filters/geoToH3.c
8000
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.
Expand All @@ -17,7 +17,7 @@
* @brief stdin/stdout filter that converts from lat/lon coordinates to integer
* H3 indexes
*
* usage: `geoToH3 resolution`
* usage: `geoToH3 --resolution res [--latitude lat --longitude lon]`
*
* The program reads lat/lon pairs from stdin until EOF is encountered. For
* each lat/lon the program outputs to stdout the integer H3 index of the
Expand All @@ -34,40 +34,86 @@

#include <stdio.h>
#include <stdlib.h>
#include "coordijk.h"
#include "h3Index.h"
#include "utility.h"

/**
* Convert coordinates to cell and print it.
*
* @param lat Degrees latitude
* @param lon Degrees longitude
* @param res Resolution
*/
void doCoords(double lat, double lon, int res) {
GeoCoord g;
setGeoDegs(&g, lat, lon);

H3Index h = H3_EXPORT(geoToH3)(&g, res);

h3Println(h);
}

int main(int argc, char* argv[]) {
// get the command line argument resolution
if (argc != 2) {
fprintf(stderr, "usage: %s resolution\n", argv[0]);
exit(1);
}
int res = 0;
double lat = 0;
double lon = 0;

int res;
if (!sscanf(argv[1], "%d", &res)) error("parsing resolution");
Arg helpArg = {.names = {"-h", "--help"},
.helpText = "Show this help message."};
Arg resArg = {.names = {"-r", "--resolution"},
.required = true,
.scanFormat = "%d",
.valueName = "res",
.value = &res,
.helpText = "Resolution, 0-15 inclusive."};
Arg latArg = {.names = {"--lat", "--latitude"},
.scanFormat = "%lf",
.valueName = "lat",
.value = &lat,
.helpText =
"Latitude in degrees. If not specified, \"latitude "
"longitude\" pairs will be read from stdin."};
Arg lonArg = {.names = {"--lon", "--longitude"},
.scanFormat = "%lf",
.valueName = "lon",
.value = &lon,
.helpText = "Longitude in degrees."};

// process the lat/lon's on stdin
char buff[BUFF_SIZE];
double lat, lon;
while (1) {
// get a lat/lon from stdin
if (!fgets(buff, BUFF_SIZE, stdin)) {
if (feof(stdin))
break;
else
error("reading lat/lon");
}
Arg* args[] = {&helpArg, &resArg, &latArg, &lonArg};

const char* helpText =
"Convert degrees latitude/longitude coordinates to H3 indexes.";

if (parseArgs(argc, argv, 4, args, &helpArg, helpText)) {
return helpArg.found ? 0 : 1;
}

if (sscanf(buff, "%lf %lf", &lat, &lon) != 2) error("parsing lat/lon");
if (latArg.found != lonArg.found) {
// One is found but the other is not.
printHelp(stderr, argv[0], helpText, 4, args,
"Latitude and longitude must both be specified.", NULL);
return 1;
}

// convert to H3
GeoCoord g;
setGeoDegs(&g, lat, lon);
if (latArg.found) {
doCoords(lat, lon, res);
} else {
// process the lat/lon's on stdin
char buff[BUFF_SIZE];
while (1) {
// get a lat/lon from stdin
if (!fgets(buff, BUFF_SIZE, stdin)) {
if (feof(stdin))
break;
else
error("reading lat/lon");
}

H3Index h = H3_EXPORT(geoToH3)(&g, res);
if (sscanf(buff, "%lf %lf", &lat, &lon) != 2)
error("parsing lat/lon");

h3Println(h);
// convert to H3
doCoords(lat, lon, res);
}
}
}
110 changes: 63 additions & 47 deletions src/apps/filters/h3ToGeo.c
F438
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.
Expand All @@ -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:
*
* `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;
Expand All @@ -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"},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I might have gone for -n and -d, but that's just bikeshedding.

.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();
}
Loading
0