parser_json.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*!
  2. \file lib/gis/parser_json.c
  3. \brief GIS Library - converts the command line arguments into actinia JSON process
  4. chain building blocks
  5. (C) 2018-2021 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Soeren Gebbert
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <grass/glocale.h>
  14. #include <grass/gis.h>
  15. #include "parser_local_proto.h"
  16. void check_create_import_opts(struct Option *, char *, FILE *);
  17. void check_create_export_opts(struct Option *, char *, FILE *);
  18. char *check_mapset_in_layer_name(char *, int);
  19. /*!
  20. \brief This function generates actinia JSON process chain building blocks
  21. from the command line arguments that can be used in the actinia processing API.
  22. The following commands will create according JSON output:
  23. r.slope.aspect elevation="elevation@https://storage.googleapis.com/graas-geodata/elev_ned_30m.tif" slope="slope+GTiff" aspect="aspect+GTiff" --json
  24. {
  25. "module": "r.slope.aspect",
  26. "id": "r.slope.aspect_1804289383",
  27. "inputs":[
  28. {"import_descr": {"source":"https://storage.googleapis.com/graas-geodata/elev_ned_30m.tif", "type":"raster"},
  29. "param": "elevation", "value": "elevation"},
  30. {"param": "format", "value": "degrees"},
  31. {"param": "precision", "value": "FCELL"},
  32. {"param": "zscale", "value": "1.0"},
  33. {"param": "min_slope", "value": "0.0"}
  34. ],
  35. "outputs":[
  36. {"export": {"format":"GTiff", "type":"raster"},
  37. "param": "slope", "value": "slope"},
  38. {"export": {"format":"GTiff", "type":"raster"},
  39. "param": "aspect", "value": "aspect"}
  40. ]
  41. }
  42. v.out.ascii input="hospitals@PERMANENT" output="myfile+TXT" --json
  43. {
  44. "module": "v.out.ascii",
  45. "id": "v.out.ascii_1804289383",
  46. "inputs":[
  47. {"param": "input", "value": "hospitals@PERMANENT"},
  48. {"param": "layer", "value": "1"},
  49. {"param": "type", "value": "point,line,boundary,centroid,area,face,kernel"},
  50. {"param": "format", "value": "point"},
  51. {"param": "separator", "value": "pipe"},
  52. {"param": "precision", "value": "8"}
  53. ],
  54. "outputs":[
  55. {"export": {"format":"TXT", "type":"file"},
  56. "param": "output", "value": "$file::myfile"}
  57. ]
  58. }
  59. v.info map="hospitals@PERMANENT" -c --json
  60. {
  61. "module": "v.info",
  62. "id": "v.info_1804289383",
  63. "flags":"c",
  64. "inputs":[
  65. {"param": "map", "value": "hospitals@PERMANENT"},
  66. {"param": "layer", "value": "1"}
  67. ]
  68. }
  69. A process chain has the following form
  70. {
  71. 'list': [{
  72. 'module': 'g.region',
  73. 'id': 'g_region_1',
  74. 'inputs': [{'import_descr': {'source': 'https://storage.googleapis.com/graas-geodata/elev_ned_30m.tif',
  75. 'type': 'raster'},
  76. 'param': 'raster',
  77. 'value': 'elev_ned_30m_new'}],
  78. 'flags': 'p'
  79. },
  80. {
  81. 'module': 'r.slope.aspect',
  82. 'id': 'r_slope_aspect_1',
  83. 'inputs': [{'param': 'elevation',
  84. 'value': 'elev_ned_30m_new'}],
  85. 'outputs': [{'export': {'format': 'GTiff',
  86. 'type': 'raster'},
  87. 'param': 'slope',
  88. 'value': 'elev_ned_30m_new_slope'}],
  89. 'flags': 'a'},
  90. {
  91. 'module': 'r.univar',
  92. 'id': 'r_univar_1',
  93. 'inputs': [{"import_descr": {"source": "LT52170762005240COA00",
  94. "type": "landsat",
  95. "landsat_atcor": "dos1"},
  96. 'param': 'map',
  97. 'value': 'LT52170762005240COA00_dos1.1'}],
  98. 'stdout': {'id': 'stats', 'format': 'kv', 'delimiter': '='},
  99. 'flags': 'a'
  100. },
  101. {
  102. 'module': 'exporter',
  103. 'id': 'exporter_1',
  104. 'outputs': [{'export': {'format': 'GTiff',
  105. 'type': 'raster'},
  106. 'param': 'map',
  107. 'value': 'LT52170762005240COA00_dos1.1'}]
  108. },
  109. {
  110. "id": "ascii_out",
  111. "module": "r.out.ascii",
  112. "inputs": [{"param": "input",
  113. "value": "elevation@PERMANENT"},
  114. {"param": "precision", "value": "0"}],
  115. "stdout": {"id": "elev_1", "format": "table", "delimiter": " "},
  116. "flags": "h"
  117. },
  118. {
  119. "id": "ascii_export",
  120. "module": "r.out.ascii",
  121. "inputs": [{"param": "input",
  122. "value": "elevation@PERMANENT"}],
  123. "outputs": [
  124. {"export": {"type": "file", "format": "TXT"},
  125. "param": "output",
  126. "value": "$file::out1"}
  127. ]
  128. },
  129. {
  130. "id": "raster_list",
  131. "module": "g.list",
  132. "inputs": [{"param": "type",
  133. "value": "raster"}],
  134. "stdout": {"id": "raster", "format": "list", "delimiter": "\n"}
  135. },
  136. {
  137. "module": "r.what",
  138. "id": "r_what_1",
  139. "verbose": True,
  140. "flags": "nfic",
  141. "inputs": [
  142. {
  143. "param": "map",
  144. "value": "landuse96_28m@PERMANENT"
  145. },
  146. {
  147. "param": "coordinates",
  148. "value": "633614.08,224125.12,632972.36,225382.87"
  149. },
  150. {
  151. "param": "null_value",
  152. "value": "null"
  153. },
  154. {
  155. "param": "separator",
  156. "value": "pipe"
  157. }
  158. ],
  159. "stdout": {"id": "sample", "format": "table", "delimiter": "|"}
  160. }
  161. ],
  162. 'webhooks': {'update': 'http://business-logic.company.com/api/v1/actinia-update-webhook',
  163. 'finished': 'http://business-logic.company.com/api/v1/actinia-finished-webhook'},
  164. 'version': '1'
  165. }
  166. */
  167. char *G__json(void)
  168. {
  169. FILE *fp = stdout;
  170. /*FILE *fp = NULL; */
  171. char *file_name = NULL;
  172. int c;
  173. int random_int = rand();
  174. int num_flags = 0;
  175. int num_inputs = 0;
  176. int num_outputs = 0;
  177. int i = 0;
  178. char age[KEYLENGTH];
  179. char element[KEYLENGTH]; /*cell, file, grid3, vector */
  180. char desc[KEYLENGTH];
  181. file_name = G_tempfile();
  182. /* fprintf(stderr, "Filename: %s\n", file_name); */
  183. fp = fopen(file_name, "w+");
  184. if (fp == NULL) {
  185. fprintf(stderr, "Unable to open temporary file <%s>\n", file_name);
  186. exit(EXIT_FAILURE);
  187. }
  188. if (st->n_flags) {
  189. struct Flag *flag;
  190. for (flag = &st->first_flag; flag; flag = flag->next_flag) {
  191. if (flag->answer)
  192. num_flags += 1;;
  193. }
  194. }
  195. /* Count input and output options */
  196. if (st->n_opts) {
  197. struct Option *opt;
  198. for (opt = &st->first_option; opt; opt = opt->next_opt) {
  199. if (opt->answer) {
  200. if (opt->gisprompt) {
  201. G__split_gisprompt(opt->gisprompt, age, element, desc);
  202. /* fprintf(stderr, "age: %s element: %s desc: %s\n", age, element, desc); */
  203. if (G_strncasecmp("new", age, 3) == 0) {
  204. /*fprintf(fp, "new: %s\n", opt->gisprompt); */
  205. num_outputs += 1;
  206. }
  207. else {
  208. /*fprintf(fp, "%s\n", opt->gisprompt); */
  209. num_inputs += 1;
  210. }
  211. }
  212. else {
  213. num_inputs += 1;
  214. }
  215. }
  216. }
  217. }
  218. fprintf(fp, "{\n");
  219. fprintf(fp, " \"module\": \"%s\",\n", G_program_name());
  220. fprintf(fp, " \"id\": \"%s_%i\"", G_program_name(), random_int);
  221. if (st->n_flags && num_flags > 0) {
  222. struct Flag *flag;
  223. fprintf(fp, ",\n");
  224. fprintf(fp, " \"flags\":\"");
  225. for (flag = &st->first_flag; flag; flag = flag->next_flag) {
  226. if (flag->answer)
  227. fprintf(fp, "%c", flag->key);
  228. }
  229. fprintf(fp, "\"");
  230. }
  231. /* Print the input options
  232. */
  233. if (st->n_opts && num_inputs > 0) {
  234. struct Option *opt;
  235. i = 0;
  236. fprintf(fp, ",\n");
  237. fprintf(fp, " \"inputs\":[\n");
  238. for (opt = &st->first_option; opt; opt = opt->next_opt) {
  239. if (opt->gisprompt) {
  240. G__split_gisprompt(opt->gisprompt, age, element, desc);
  241. if (G_strncasecmp("new", age, 3) != 0) {
  242. if (opt->answer) {
  243. check_create_import_opts(opt, element, fp);
  244. i++;
  245. if (i < num_inputs) {
  246. fprintf(fp, ",\n");
  247. }
  248. else {
  249. fprintf(fp, "\n");
  250. }
  251. }
  252. }
  253. }
  254. else if (opt->answer) {
  255. /* Check for input options */
  256. fprintf(fp, " {\"param\": \"%s\", ", opt->key);
  257. fprintf(fp, "\"value\": \"%s\"}", opt->answer);
  258. i++;
  259. if (i < num_inputs) {
  260. fprintf(fp, ",\n");
  261. }
  262. else {
  263. fprintf(fp, "\n");
  264. }
  265. }
  266. }
  267. fprintf(fp, " ]");
  268. }
  269. /* Print the output options
  270. */
  271. if (st->n_opts && num_outputs > 0) {
  272. struct Option *opt;
  273. i = 0;
  274. fprintf(fp, ",\n");
  275. fprintf(fp, " \"outputs\":[\n");
  276. for (opt = &st->first_option; opt; opt = opt->next_opt) {
  277. if (opt->gisprompt) {
  278. G__split_gisprompt(opt->gisprompt, age, element, desc);
  279. if (G_strncasecmp("new", age, 3) == 0) {
  280. if (opt->answer) {
  281. check_create_export_opts(opt, element, fp);
  282. i++;
  283. if (i < num_outputs) {
  284. fprintf(fp, ",\n");
  285. }
  286. else {
  287. fprintf(fp, "\n");
  288. }
  289. }
  290. }
  291. }
  292. }
  293. fprintf(fp, " ]\n");
  294. }
  295. fprintf(fp, "}\n");
  296. fclose(fp);
  297. /* Print the file content to stdout */
  298. fp = fopen(file_name, "r");
  299. if (fp == NULL) {
  300. fprintf(stderr, "Unable to open temporary file <%s>\n", file_name);
  301. exit(EXIT_FAILURE);
  302. }
  303. c = fgetc(fp);
  304. while (c != EOF) {
  305. fprintf(stdout, "%c", c);
  306. c = fgetc(fp);
  307. }
  308. fclose(fp);
  309. return file_name;
  310. }
  311. /* \brief Check the provided answer and generate the import statement
  312. dependent on the element type (cell, vector, grid3, file)
  313. {'import_descr': {'source': 'https://storage.googleapis.com/graas-geodata/elev_ned_30m.tif',
  314. 'type': 'raster'},
  315. 'param': 'map',
  316. 'value': 'elevation'}
  317. */
  318. void check_create_import_opts(struct Option *opt, char *element, FILE * fp)
  319. {
  320. int i = 0, urlfound = 0;
  321. int has_import = 0;
  322. char **tokens;
  323. G_debug(2, "tokenize opt string: <%s> with '@'", opt->answer);
  324. tokens = G_tokenize(opt->answer, "@");
  325. while (tokens[i]) {
  326. G_chop(tokens[i]);
  327. i++;
  328. }
  329. if (i > 2)
  330. G_fatal_error(_("Input string not understood: <%s>. Multiple '@' chars?"),
  331. opt->answer);
  332. if (i > 1) {
  333. /* check if tokens[1] starts with an URL or name@mapset */
  334. G_debug(2, "tokens[1]: <%s>", tokens[1]);
  335. if (strncmp(tokens[1], "http://", 7) == 0 ||
  336. strncmp(tokens[1], "https://", 8) == 0 ||
  337. strncmp(tokens[1], "ftp://", 6) == 0) {
  338. urlfound = 1;
  339. G_debug(2, "URL found");
  340. }
  341. else {
  342. urlfound = 0;
  343. G_debug(2, "name@mapset found");
  344. }
  345. }
  346. fprintf(fp, " {");
  347. if (i > 1 && urlfound == 1) {
  348. if (G_strncasecmp("cell", element, 4) == 0) {
  349. fprintf(fp,
  350. "\"import_descr\": {\"source\":\"%s\", \"type\":\"raster\"},\n ",
  351. tokens[1]);
  352. has_import = 1;
  353. }
  354. else if (G_strncasecmp("file", element, 4) == 0) {
  355. fprintf(fp,
  356. "\"import_descr\": {\"source\":\"%s\", \"type\":\"file\"},\n ",
  357. tokens[1]);
  358. has_import = 1;
  359. }
  360. else if (G_strncasecmp("vector", element, 4) == 0) {
  361. fprintf(fp,
  362. "\"import_descr\": {\"source\":\"%s\", \"type\":\"vector\"},\n ",
  363. tokens[1]);
  364. has_import = 1;
  365. }
  366. }
  367. fprintf(fp, "\"param\": \"%s\", ", opt->key);
  368. /* In case of import the mapset must be removed always */
  369. if (urlfound == 1) {
  370. fprintf(fp, "\"value\": \"%s\"",
  371. check_mapset_in_layer_name(tokens[0], has_import));
  372. }
  373. else {
  374. fprintf(fp, "\"value\": \"%s\"",
  375. check_mapset_in_layer_name(opt->answer, has_import));
  376. };
  377. fprintf(fp, "}");
  378. G_free_tokens(tokens);
  379. }
  380. /* \brief Check the provided answer and generate the export statement
  381. dependent on the element type (cell, vector, grid3, file)
  382. "outputs": [
  383. {"export": {"type": "file", "format": "TXT"},
  384. "param": "output",
  385. "value": "$file::out1"},
  386. {'export': {'format': 'GTiff', 'type': 'raster'},
  387. 'param': 'map',
  388. 'value': 'LT52170762005240COA00_dos1.1'}
  389. ]
  390. */
  391. void check_create_export_opts(struct Option *opt, char *element, FILE * fp)
  392. {
  393. int i = 0;
  394. int has_file_export = 0;
  395. char **tokens;
  396. tokens = G_tokenize(opt->answer, "+");
  397. while (tokens[i]) {
  398. G_chop(tokens[i]);
  399. i++;
  400. }
  401. fprintf(fp, " {");
  402. if (i > 1) {
  403. if (G_strncasecmp("cell", element, 4) == 0) {
  404. fprintf(fp,
  405. "\"export\": {\"format\":\"%s\", \"type\":\"raster\"},\n ",
  406. tokens[1]);
  407. }
  408. else if (G_strncasecmp("file", element, 4) == 0) {
  409. fprintf(fp,
  410. "\"export\": {\"format\":\"%s\", \"type\":\"file\"},\n ",
  411. tokens[1]);
  412. has_file_export = 1;
  413. }
  414. else if (G_strncasecmp("vector", element, 4) == 0) {
  415. fprintf(fp,
  416. "\"export\": {\"format\":\"%s\", \"type\":\"vector\"},\n ",
  417. tokens[1]);
  418. }
  419. }
  420. fprintf(fp, "\"param\": \"%s\", ", opt->key);
  421. if (has_file_export == 1) {
  422. fprintf(fp, "\"value\": \"$file::%s\"",
  423. check_mapset_in_layer_name(tokens[0], 1));
  424. }
  425. else {
  426. fprintf(fp, "\"value\": \"%s\"",
  427. check_mapset_in_layer_name(tokens[0], 1));
  428. }
  429. fprintf(fp, "}");
  430. G_free_tokens(tokens);
  431. }
  432. /*
  433. \brief Check if the current mapset is present in the layer name and remove it
  434. The flag always_remove tells this function to always remove all mapset names.
  435. \return pointer to the layer name without the current mapset
  436. */
  437. char *check_mapset_in_layer_name(char *layer_name, int always_remove)
  438. {
  439. int i = 0;
  440. char **tokens;
  441. const char *mapset;
  442. mapset = G_mapset();
  443. tokens = G_tokenize(layer_name, "@");
  444. while (tokens[i]) {
  445. G_chop(tokens[i]);
  446. /* fprintf(stderr, "Token %i: %s\n", i, tokens[i]); */
  447. i++;
  448. }
  449. if (always_remove == 1)
  450. return tokens[0];
  451. if (i > 1 && G_strcasecmp(mapset, tokens[1]) == 0)
  452. return tokens[0];
  453. return layer_name;
  454. }