Skip to content

Commit

Permalink
v.what: new option to select columns (OSGeo#2458)
Browse files Browse the repository at this point in the history
  • Loading branch information
metzm authored Jun 30, 2022
1 parent 2af9d79 commit 6c1c13c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
32 changes: 29 additions & 3 deletions vector/v.what/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int main(int argc, char **argv)
struct Flag *print, *topo, *shell, *json, *multiple;
} flag;
struct {
struct Option *map, *field, *coords, *maxdist, *type;
struct Option *map, *field, *coords, *maxdist, *type, *cols;
} opt;
struct Cell_head window;
struct GModule *module;
Expand All @@ -49,6 +49,7 @@ int main(int argc, char **argv)
char buf[2000];
int i, level, ret, type;
int *field;
char *columns;
double xval, yval, xres, yres, maxd, x;
double EW_DIST1, EW_DIST2, NS_DIST1, NS_DIST2;
char nsres[30], ewres[30];
Expand Down Expand Up @@ -85,6 +86,10 @@ int main(int argc, char **argv)
opt.maxdist->description = _("Query threshold distance");
opt.maxdist->guisection = _("Threshold");

opt.cols = G_define_standard_option(G_OPT_DB_COLUMNS);
opt.cols->label = _("Name of attribute column(s)");
opt.cols->description = _("Default: all columns");

flag.topo = G_define_flag();
flag.topo->key = 'd';
flag.topo->description = _("Print topological information (debugging)");
Expand Down Expand Up @@ -126,6 +131,27 @@ int main(int argc, char **argv)
else
G_fatal_error(_("No input vector maps!"));

/* get specified column names */
if (opt.cols->answers && opt.cols->answers[0]) {
int col_alloc;
int new_len;

col_alloc = 1024;
columns = (char *)G_calloc(col_alloc, sizeof(char));
for (i = 0; opt.cols->answers[i]; i++) {
new_len = strlen(columns) + strlen(opt.cols->answers[i]) + 1;
if (new_len >= col_alloc) {
col_alloc = new_len + 1024;
columns = G_realloc(columns, col_alloc);
}
if (i > 0)
strcat(columns, ",");
strcat(columns, opt.cols->answers[i]);
}
}
else
columns = "*";

maxd = atof(opt.maxdist->answer);
type = Vect_option_to_types(opt.type);
output = flag.shell->answer ? OUTPUT_SCRIPT :
Expand Down Expand Up @@ -196,7 +222,7 @@ int main(int argc, char **argv)
if (ret == 3 && (ch == ',' || ch == ' ' || ch == '\t')) {
what(Map, nvects, vect, xval, yval, maxd, type,
flag.topo->answer, flag.print->answer, output,
flag.multiple->answer, field);
flag.multiple->answer, field, columns);
}
else {
G_warning(_("Unknown input format, skipping: '%s'"), buf);
Expand All @@ -210,7 +236,7 @@ int main(int argc, char **argv)
xval = atof(opt.coords->answers[i]);
yval = atof(opt.coords->answers[i + 1]);
what(Map, nvects, vect, xval, yval, maxd, type, flag.topo->answer,
flag.print->answer, output, flag.multiple->answer, field);
flag.print->answer, output, flag.multiple->answer, field, columns);
}
}

Expand Down
14 changes: 7 additions & 7 deletions vector/v.what/what.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ static int nlines = 50;

static void F_generate(const char *drvname, const char *dbname,
const char *tblname, const char *key, int keyval,
int output, char **form)
int output, char **form, char *columns)
{
int col, ncols, sqltype, more;
char buf[5000];
Expand Down Expand Up @@ -59,7 +59,7 @@ static void F_generate(const char *drvname, const char *dbname,
* application before F_generate() is called, because it may be correct
* (connection defined in DB but table does not exist) */

sprintf(buf, "select * from %s where %s = %d", tblname, key, keyval);
sprintf(buf, "select %s from %s where %s = %d", columns, tblname, key, keyval);
G_debug(2, "%s", buf);
db_set_string(&sql, buf);
if (db_open_select_cursor(driver, &sql, &cursor, DB_SEQUENTIAL) != DB_OK)
Expand Down Expand Up @@ -145,7 +145,7 @@ void coord2bbox(double east, double north, double maxdist,
}

void write_cats(struct Map_info *Map, int field, struct line_cats *Cats,
int showextra, int output)
int showextra, int output, char *columns)
{
int i, j;
char *formbuf1;
Expand Down Expand Up @@ -204,7 +204,7 @@ void write_cats(struct Map_info *Map, int field, struct line_cats *Cats,
break;
}
F_generate(Fi->driver, Fi->database, Fi->table,
Fi->key, Cats->cat[i], output, &form);
Fi->key, Cats->cat[i], output, &form, columns);

switch (output) {
case OUTPUT_SCRIPT:
Expand Down Expand Up @@ -232,7 +232,7 @@ void write_cats(struct Map_info *Map, int field, struct line_cats *Cats,

void what(struct Map_info *Map, int nvects, char **vect, double east,
double north, double maxdist, int qtype, int topo, int showextra,
int output, int multiple, int *field)
int output, int multiple, int *field, char *columns)
{
struct line_pnts *Points;
struct line_cats *Cats;
Expand Down Expand Up @@ -625,7 +625,7 @@ void what(struct Map_info *Map, int nvects, char **vect, double east,
}
} /* if height */

write_cats(&Map[i], field[i], Cats, showextra, output);
write_cats(&Map[i], field[i], Cats, showextra, output, columns);

if (output == OUTPUT_JSON && multiple)
fprintf(stdout, "}");
Expand Down Expand Up @@ -770,7 +770,7 @@ void what(struct Map_info *Map, int nvects, char **vect, double east,
if (centroid > 0)
Vect_read_line(&Map[i], Points, Cats, centroid);

write_cats(&Map[i], field[i], Cats, showextra, output);
write_cats(&Map[i], field[i], Cats, showextra, output, columns);

if (output == OUTPUT_JSON && multiple)
fprintf(stdout, "}");
Expand Down
2 changes: 1 addition & 1 deletion vector/v.what/what.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

/* what.c */
void what(struct Map_info *, int, char **,
double, double, double, int, int, int, int, int, int *);
double, double, double, int, int, int, int, int, int *, char *);

#endif

0 comments on commit 6c1c13c

Please sign in to comment.