diff --git a/ocat.c b/ocat.c index e32f684..5b62f39 100644 --- a/ocat.c +++ b/ocat.c @@ -108,6 +108,7 @@ void usage(char *prog) printf(" --format json -f output format (default: JSON)\n"); printf(" csv (overrides $OCAT_FORMAT\n"); printf(" geojson\n"); + printf(" gpx\n"); printf(" raw\n"); printf(" --fields tst,lat,lon,... Choose fields for CSV. (dflt: ALL)\n"); printf(" --last -L JSON object with last users\n"); @@ -205,6 +206,8 @@ int main(int argc, char **argv) otype = GEOJSON; else if (!strcmp(optarg, "raw")) otype = RAW; + else if (!strcmp(optarg, "gpx")) + otype = GPX; else if (!strcmp(optarg, "csv")) otype = CSV; else { @@ -388,6 +391,11 @@ int main(int argc, char **argv) } json_delete(geojson); } + } else if (otype == GPX) { + char *xml = gpx_string(locs); + + if (xml) + printf("%s\n", xml); } json_delete(obj); diff --git a/storage.c b/storage.c index 30e8cad..c254f7b 100644 --- a/storage.c +++ b/storage.c @@ -673,6 +673,46 @@ JsonNode *geo_json(JsonNode *location_array) return (fcollection); } +/* + * Turn our JSON location array into a GPX XML string. + */ + +char *gpx_string(JsonNode *location_array) +{ + JsonNode *one; + static UT_string *xml = NULL; + + if (location_array->tag != JSON_ARRAY) + return (NULL); + + utstring_renew(xml); + + utstring_printf(xml, "%s", "\n\ +\n\ + \n\ + \n"); + + // + + json_foreach(one, location_array) { + double lat = 0.0, lon = 0.0; + JsonNode *j; + + if ((j = json_find_member(one, "lat")) != NULL) { + lat = j->number_; + } + + if ((j = json_find_member(one, "lon")) != NULL) { + lon = j->number_; + } + + utstring_printf(xml, "\n", lat, lon); + } + + utstring_printf(xml, "%s", " \n\n\n"); + return (utstring_body(xml)); +} + /* * Remove all data for a user's device. Return a JSON object with a status * and an array of deleted files. diff --git a/storage.h b/storage.h index 2aba3be..330cea8 100644 --- a/storage.h +++ b/storage.h @@ -9,6 +9,7 @@ typedef enum { CSV, JSON, RAW, + GPX, } output_type; JsonNode *lister(char *username, char *device, time_t s_lo, time_t s_hi, int reverse); @@ -17,5 +18,6 @@ int make_times(char *time_from, time_t *s_lo, char *time_to, time_t *s_to); JsonNode *geo_json(JsonNode *json); JsonNode *kill_datastore(char *username, char *device); JsonNode *last_users(); +char *gpx_string(JsonNode *json); #endif