ext/h3/src/src/apps/testapps/mkRandGeoBoundary.c in h3-3.4.4 vs ext/h3/src/src/apps/testapps/mkRandGeoBoundary.c in h3-3.5.0
- old
+ new
@@ -1,7 +1,7 @@
/*
- * 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.
* You may obtain a copy of the License at
*
@@ -14,49 +14,54 @@
* limitations under the License.
*/
/** @file
* @brief generates random cell indexes and the corresponding cell boundaries
*
- * usage: `mkRandGeoBoundary numPoints resolution`
+ * See `mkRandGeoBoundary --help` for usage.
*
* The program generates `numPoints` random lat/lon coordinates and outputs
* them along with the corresponding H3Index at the specified `resolution`.
*/
#include <stdio.h>
#include <stdlib.h>
-#include <time.h>
-#include "baseCells.h"
-#include "h3Index.h"
+#include "args.h"
#include "utility.h"
-void randomGeo(GeoCoord* g) {
- static int init = 0;
- if (!init) {
- srand((unsigned int)time(0));
- init = 1;
- }
-
- g->lat = H3_EXPORT(degsToRads)(
- (((float)rand() / (float)(RAND_MAX)) * 180.0) - 90.0);
- g->lon = H3_EXPORT(degsToRads)((float)rand() / (float)(RAND_MAX)) * 360.0;
-}
-
int main(int argc, char* argv[]) {
- // check command line args
- if (argc > 3) {
- fprintf(stderr, "usage: %s numPoints resolution\n", argv[0]);
- exit(1);
- }
-
+ int res = 0;
int numPoints = 0;
- if (!sscanf(argv[1], "%d", &numPoints))
- error("numPoints must be an integer");
- int res = 0;
- if (!sscanf(argv[2], "%d", &res)) error("resolution must be an integer");
+ Arg helpArg = ARG_HELP;
+ Arg numPointsArg = {
+ .names = {"-n", "--num-points"},
+ .required = true,
+ .scanFormat = "%d",
+ .valueName = "num",
+ .value = &numPoints,
+ .helpText = "Number of random lat/lon pairs to generate."};
+ Arg resArg = {.names = {"-r", "--resolution"},
+ .required = true,
+ .scanFormat = "%d",
+ .valueName = "res",
+ .value = &res,
+ .helpText = "Resolution, 0-15 inclusive."};
- if (res > MAX_H3_RES) error("specified resolution exceeds max resolution");
+ Arg* args[] = {&helpArg, &numPointsArg, &resArg};
+ const int numArgs = 3;
+ const char* helpText =
+ "Generates random cell indexes and cell boundaries at the specified "
+ "resolution.";
+
+ if (parseArgs(argc, argv, numArgs, args, &helpArg, helpText)) {
+ return helpArg.found ? 0 : 1;
+ }
+
+ if (res > MAX_H3_RES) {
+ printHelp(stderr, argv[0], helpText, numArgs, args,
+ "Resolution exceeds maximum resolution.", NULL);
+ return 1;
+ }
for (int i = 0; i < numPoints; i++) {
GeoCoord g;
randomGeo(&g);