Skip to content
70 changes: 57 additions & 13 deletions general/g.parser/g.parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,28 +255,72 @@ v.in.db --interface-description
## JSON

The flag **--json** added to a GRASS command with parameters mandatorily
to be specified generates a module interface description in JSON.
Example:
to be specified generates a module interface description in JSON. The JSON
output can be used as building blocks in actinia processing chains.

actinia specific parametrization is supported for both import and export.

Import of input data is denoted with a "@" as delimiter followed by a
valid URL.

Export of raster, vector or other file-based date if invoked with a
"+" behind the file-name followed by an actinia-format name. Formats
currently supported are:

- COG
- GTiff
- GPKG
- SQLite
- GML
- GeoJSON
- ESRI_Shapefile
- CSV
- TXT
- PDF
- PostgreSQL

Many GRASS modules produce textual output to stdout and actinia allows
to export that output as well, however, the **--json** flag does not yet
allow to specify export of stdout which may look in an actinia processing
chain e.g. like this:

```json
{
...
'stdout': {'id': 'stats', 'format': 'kv', 'delimiter': '='},
}
```
Comment on lines 281 to 293
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not clear. What a user is supposed to do here? I'm missing something like "So, to do X, do A, B and C."

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the updated text...


Here is a full example for usage of the **--json** flag:

```sh
v.in.db driver=sqlite database=mysqlite.db table=pointsfile x=x y=y z=z key=idcol out=dtmpoints --json
r.slope.aspect --o --q -e \
elevation="elevation@https://storage.googleapis.com/graas-geodata/elev_ned_30m.tif" \
slope="slope+GTiff" aspect="aspect+GTiff" --json
```

```json
{
"module": "v.in.db",
"id": "v.in.db_1804289383",
"module": "r.slope.aspect",
"overwrite": true,
"quiet": true,
"id": "r.slope.aspect_1804289383",
"flags":"e",
"inputs":[
{"param": "table", "value": "pointsfile"},
{"param": "driver", "value": "sqlite"},
{"param": "database", "value": "mysqlite.db"},
{"param": "x", "value": "x"},
{"param": "y", "value": "y"},
{"param": "z", "value": "z"},
{"param": "key", "value": "idcol"}
{"import_descr": {"source":"https://storage.googleapis.com/graas-geodata/elev_ned_30m.tif", "type":"raster"},
"param": "elevation", "value": "elevation"},
{"param": "format", "value": "degrees"},
{"param": "precision", "value": "FCELL"},
{"param": "zscale", "value": "1.0"},
{"param": "min_slope", "value": "0.0"},
{"param": "nprocs", "value": "0"},
{"param": "memory", "value": "300"}
],
"outputs":[
{"param": "output", "value": "dtmpoints"}
{"export": {"format":"GTiff", "type":"raster"},
"param": "slope", "value": "slope"},
{"export": {"format":"GTiff", "type":"raster"},
"param": "aspect", "value": "aspect"}
]
}
```
Expand Down
3 changes: 3 additions & 0 deletions lib/gis/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ int G_parser(int argc, char **argv)
"both. Assuming --verbose."));
}
st->quiet = -1;
st->superquiet = -1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is suspicions as it is not initialized anywhere.

}

/* Quiet option */
Expand All @@ -568,11 +569,13 @@ int G_parser(int argc, char **argv)
"both. Assuming --quiet."));
}
st->quiet = 1; /* for passing to gui init */
st->superquiet = -1;
}

/* Super quiet option */
else if (strcmp(ptr, "--qq") == 0) {
char buff[32];
st->superquiet = 1;

/* print nothing, but errors */
st->module_info.verbose = G_verbose_min();
Expand Down
22 changes: 18 additions & 4 deletions lib/gis/parser_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ char *check_mapset_in_layer_name(char *, int);
static char *str_json_escape(const char *str);
static char *str_replace_free_buffer(char *buffer, const char old_char,
const char *new_str);

/*!
\brief This function generates actinia JSON process chain building blocks
from the command line arguments that can be used in the actinia processing
Expand Down Expand Up @@ -229,7 +228,6 @@ char *G__json(void)
/* Count input and output options */
if (st->n_opts) {
struct Option *opt;

for (opt = &st->first_option; opt; opt = opt->next_opt) {
if (opt->answer) {
if (opt->gisprompt) {
Expand All @@ -254,6 +252,22 @@ char *G__json(void)

fprintf(fp, "{\n");
fprintf(fp, " \"module\": \"%s\",\n", G_program_name());
if (st->overwrite || getenv("GRASS_OVERWRITE")) {
fprintf(fp, " \"overwrite\": true,\n");
}

if (G_verbose() == G_verbose_max()) {
fprintf(fp, " \"verbose\": true,\n");
}

if (G_verbose() == G_verbose_min() && st->superquiet < 1) {
fprintf(fp, " \"quiet\": true,\n");
}

if (st->superquiet == 1 || G_verbose() == -1) {
fprintf(fp, " \"superquiet\": true,\n");
}

fprintf(fp, " \"id\": \"%s_%i\"", G_program_name(), random_int);

if (st->n_flags && num_flags > 0) {
Expand Down Expand Up @@ -336,10 +350,10 @@ char *G__json(void)
}
}
}
fprintf(fp, " ]\n");
fprintf(fp, " ]");
}

fprintf(fp, "}\n");
fprintf(fp, "\n}\n");
fclose(fp);

/* Print the file content to stdout */
Expand Down
1 change: 1 addition & 0 deletions lib/gis/parser_local_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct state {
int n_keys_alloc;
int overwrite;
int quiet;
int superquiet;
int has_required;
int suppress_required;
int suppress_overwrite;
Expand Down
Loading