Skip to content

Commit

Permalink
v.to.db: add JSON support
Browse files Browse the repository at this point in the history
  • Loading branch information
kritibirda26 committed Jul 11, 2024
1 parent ff2da11 commit 378c2f3
Show file tree
Hide file tree
Showing 5 changed files with 420 additions and 139 deletions.
2 changes: 1 addition & 1 deletion vector/v.to.db/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ MODULE_TOPDIR = ../..

PGM=v.to.db

LIBES = $(VECTORLIB) $(DBMILIB) $(GISLIB) $(MATHLIB)
LIBES = $(VECTORLIB) $(DBMILIB) $(GISLIB) $(MATHLIB) $(PARSONLIB)
DEPENDENCIES = $(VECTORDEP) $(DBMIDEP) $(GISDEP)
EXTRA_INC = $(VECT_INC)
EXTRA_CFLAGS = $(VECT_CFLAGS)
Expand Down
6 changes: 5 additions & 1 deletion vector/v.to.db/global.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include <grass/gis.h>
#include <grass/vector.h>
#include <grass/parson.h>

enum OutputFormat { PLAIN, JSON };

struct value {
int cat; /* category */
Expand Down Expand Up @@ -33,6 +36,7 @@ struct options {
int units;
int qfield; /* query field */
char *fs;
enum OutputFormat format;
};

extern struct options options;
Expand Down Expand Up @@ -96,7 +100,7 @@ int parse_command_line(int, char *[]);
int query(struct Map_info *);

/* report.c */
int report(void);
int report(enum OutputFormat format);
int print_stat(void);

/* units.c */
Expand Down
2 changes: 1 addition & 1 deletion vector/v.to.db/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ int main(int argc, char *argv[])
conv_units();

if (options.print || options.total) {
report();
report(options.format);
}
else {
update(&Map);
Expand Down
11 changes: 11 additions & 0 deletions vector/v.to.db/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ int parse_command_line(int argc, char *argv[])
struct Option *units;
struct Option *qcol;
struct Option *fs;
struct Option *format;
} parms;
struct {
struct Flag *h, *p, *s, *t;
Expand Down Expand Up @@ -116,6 +117,9 @@ int parse_command_line(int argc, char *argv[])
parms.fs->label = _("Field separator for print mode");
parms.fs->guisection = _("Print");

parms.format = G_define_standard_option(G_OPT_F_FORMAT);
parms.format->guisection = _("Print");

flags.p = G_define_flag();
flags.p->key = 'p';
flags.p->description = _("Print only");
Expand Down Expand Up @@ -168,6 +172,13 @@ int parse_command_line(int argc, char *argv[])

options.fs = G_option_to_separator(parms.fs);

if (strcmp(parms.format->answer, "json") == 0) {
options.format = JSON;
}
else {
options.format = PLAIN;
}

/* Check number of columns */
ncols = 0;
options.col[0] = NULL;
Expand Down
Loading

0 comments on commit 378c2f3

Please sign in to comment.