ext/h3/src/src/apps/miscapps/h3ToGeoHier.c in h3-3.4.4 vs ext/h3/src/src/apps/miscapps/h3ToGeoHier.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
*
@@ -15,53 +15,52 @@
*/
/** @file
* @brief takes an H3 index and generates cell center points for descendants a
* specified resolution.
*
- * usage: `h3ToGeoHier H3Index [resolution outputMode]`
+ * See `h3ToGeoHier --help` for usage.
*
* The program generates the cell center points in lat/lon coordinates for all
* hierarchical children of H3Index at the specified resolution. If the
* specified resolution is less than or equal to the resolution of H3Index
* the single cell H3Index is processed.
*
* `resolution` should be a positive integer. The default is 0 (i.e., only the
* specified cell H3Index would be processed).
*
- * `outputMode` indicates the type of output; currently the choices are 0 for
- * plain text output (the default) and 1 for KML output.
+ * `--kml` indicates KML output format; if not specified plain text output is
+ * the default.
*
* Examples:
* ---------
*
- * `h3ToGeoHier 836e9bfffffffff`
+ * `h3ToGeoHier --parent 836e9bfffffffff`
* - outputs the cell center point in lat/lon for cell
* `836e9bfffffffff` as plain text
*
- * `h3ToGeoHier 820ceffffffffff 4 1 > pts.kml`
+ * `h3ToGeoHier --parent 820ceffffffffff --resolution 4 --kml > pts.kml`
* - outputs the cell center points of all of the resolution 4
* descendants of cell `820ceffffffffff` as a KML file (redirected to
* `pts.kml`).
*
- * `h3ToGeoHier 86283082fffffff 9 1 > uber9pts.kml`
+ * `h3ToGeoHier --parent 86283082fffffff --resolution 9 --kml >
+ * uber9pts.kml`
* - creates a KML file containing the cell center points of all of the
* resolution 9 hexagons covering Uber HQ and the surrounding region of
* San Francisco.
*/
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include "args.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;
H3_EXPORT(h3ToGeo)(h, &g);
@@ -93,57 +92,83 @@
recursiveH3IndexToGeo(h, res + 1, isKmlOut);
}
}
}
-int main(int argc, char* argv[]) {
- // check command line args
- if (argc < 2 || argc > 5) {
- fprintf(stderr, "usage: %s H3Index [resolution outputMode]\n", argv[0]);
- exit(1);
+int main(int argc, char *argv[]) {
+ int res = 0;
+ H3Index parentIndex = 0;
+
+ Arg helpArg = ARG_HELP;
+ Arg resArg = {.names = {"-r", "--resolution"},
+ .scanFormat = "%d",
+ .valueName = "res",
+ .value = &res,
+ .helpText =
+ "Resolution, if less than the resolution of the parent "
+ "only the parent is printed. Default the resolution of "
+ "the parent."};
+ Arg parentArg = {
+ .names = {"-p", "--parent"},
+ .scanFormat = "%" PRIx64,
+ .valueName = "parent",
+ .value = &parentIndex,
+ .required = true,
+ .helpText = "Print cell centers descendent from this index."};
+ Arg kmlArg = ARG_KML;
+ DEFINE_KML_NAME_ARG(userKmlName, kmlNameArg);
+ DEFINE_KML_DESC_ARG(userKmlDesc, kmlDescArg);
+
+ Arg *args[] = {&helpArg, &resArg, &parentArg,
+ &kmlArg, &kmlNameArg, &kmlDescArg};
+ const int numArgs = 6;
+ const char *helpText =
+ "Print cell center points for descendants of an index";
+
+ if (parseArgs(argc, argv, numArgs, args, &helpArg, helpText)) {
+ return helpArg.found ? 0 : 1;
}
- H3Index rootCell = H3_EXPORT(stringToH3)(argv[1]);
- int baseCell = H3_GET_BASE_CELL(rootCell);
- int rootRes = H3_GET_RESOLUTION(rootCell);
- if (baseCell < 0 || baseCell >= NUM_BASE_CELLS) {
- error("invalid base cell number");
+ if (res > MAX_H3_RES) {
+ printHelp(stderr, argv[0], helpText, numArgs, args,
+ "Resolution exceeds maximum resolution.", NULL);
+ return 1;
}
- int res = 0;
- int isKmlOut = 0;
- if (argc > 2) {
- if (!sscanf(argv[2], "%d", &res))
- error("resolution must be an integer");
+ if (!H3_EXPORT(h3IsValid)(parentIndex)) {
+ printHelp(stderr, argv[0], helpText, numArgs, args,
+ "Parent index is invalid.", NULL);
+ return 1;
+ }
- if (res > MAX_H3_RES)
- error("specified resolution exceeds max resolution");
+ int rootRes = H3_GET_RESOLUTION(parentIndex);
- if (argc > 3) {
- if (!sscanf(argv[3], "%d", &isKmlOut))
- error("outputMode must be an integer");
+ if (kmlArg.found) {
+ char *kmlName;
+ if (kmlNameArg.found) {
+ kmlName = strdup(userKmlName);
+ } else {
+ kmlName = calloc(BUFF_SIZE, sizeof(char));
- if (isKmlOut != 0 && isKmlOut != 1)
- error("outputMode must be 0 or 1");
+ sprintf(kmlName, "Cell %" PRIx64 " Res %d", parentIndex,
+ ((res <= rootRes) ? rootRes : res));
+ }
- if (isKmlOut) {
- char name[BUFF_SIZE];
+ char *kmlDesc = "Generated by h3ToGeoHier";
+ if (kmlDescArg.found) kmlDesc = userKmlDesc;
- sprintf(name, "Cell %" PRIx64 " Res %d", rootCell,
- ((res <= rootRes) ? rootRes : res));
+ kmlBoundaryHeader(kmlName, kmlDesc);
- kmlBoundaryHeader(name, "cell center");
- }
- }
+ free(kmlName);
}
// generate the points
if (res <= rootRes) {
- doCell(rootCell, isKmlOut);
+ doCell(parentIndex, kmlArg.found);
} else {
- H3_SET_RESOLUTION(rootCell, res);
- recursiveH3IndexToGeo(rootCell, rootRes + 1, isKmlOut);
+ H3_SET_RESOLUTION(parentIndex, res);
+ recursiveH3IndexToGeo(parentIndex, rootRes + 1, kmlArg.found);
}
- if (isKmlOut) kmlBoundaryFooter();
+ if (kmlArg.found) kmlBoundaryFooter();
}