diff --git a/raster/r.report/global.h b/raster/r.report/global.h index 1b23ab6b813..17929fd0653 100644 --- a/raster/r.report/global.h +++ b/raster/r.report/global.h @@ -66,7 +66,7 @@ extern int do_sort; extern char *stats_file; extern char *no_data_str; extern int stats_flag; -extern int nsteps, cat_ranges, as_int; +extern int nsteps, cat_ranges, as_int, as_json; extern int *is_fp; extern DCELL *DMAX, *DMIN; diff --git a/raster/r.report/header.c b/raster/r.report/header.c index 1d0437c4c27..2113b38849f 100644 --- a/raster/r.report/header.c +++ b/raster/r.report/header.c @@ -1,6 +1,7 @@ #include #include #include "global.h" +#include static int page = 0; static const char *date = NULL; diff --git a/raster/r.report/main.c b/raster/r.report/main.c index bb04db163c2..326d290e528 100644 --- a/raster/r.report/main.c +++ b/raster/r.report/main.c @@ -44,7 +44,7 @@ int do_sort = SORT_DEFAULT; char *stats_file; char *no_data_str; int stats_flag; -int nsteps, cat_ranges, as_int; +int nsteps, cat_ranges, as_int, as_json; int *is_fp; DCELL *DMAX, *DMIN; diff --git a/raster/r.report/parse.c b/raster/r.report/parse.c index 2b18491b8a0..ea140c4a487 100644 --- a/raster/r.report/parse.c +++ b/raster/r.report/parse.c @@ -30,6 +30,7 @@ int parse_command_line(int argc, char *argv[]) i.e. read it as int */ struct Flag *C; /* report for fp ranges in Cats file (fp maps only) */ + struct Flag *j; /* Return results as JSON*/ } flags; parms.cell = G_define_standard_option(G_OPT_R_MAPS); @@ -140,6 +141,11 @@ int parse_command_line(int argc, char *argv[]) _("Read floating-point map as integer (use map's quant rules)"); flags.i->guisection = _("Floating point"); + flags.j = G_define_flag(); + flags.j->key = 'j'; + flags.j->description = _("Return report as JSON"); + flags.j->guisection = _("Formatting"); + /* hidden feature. * if first arg is >file just run r.stats into this file and quit * if first arg is answer; cat_ranges = flags.C->answer; as_int = flags.i->answer; + as_json = flags.j->answer; for (i = 0; parms.cell->answers[i]; i++) parse_layer(parms.cell->answers[i]); diff --git a/raster/r.report/prt_json.c b/raster/r.report/prt_json.c new file mode 100644 index 00000000000..412d740aa6f --- /dev/null +++ b/raster/r.report/prt_json.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include +#include + +void serialization_example(void) +{ + + // Create root object + JSON_Value *root_value = json_value_init_object(); + // JSON_Array *maps_arr; + JSON_Object *root_object = json_value_get_object(root_value); + char *serialized_string = NULL; + json_object_set_string(root_object, "location", "nc_spm_08_grass7"); + json_object_set_string(root_object, "created", "Fri Dec 6 17:00:21 2013"); + json_object_dotset_number(root_object, "region.north", 279073.97546639); + json_object_dotset_number(root_object, "region.south", 113673.97546639); + + // Create array and append map object to array + json_object_set_value(root_object, "maps", json_value_init_array()); + // maps_arr = json_object_get_array(root_object, "maps"); + + // Loop the maps used by r.report to create object and add to array + // for (i = 0; i < nmaps; i++) { + // // Create map object + // JSON_Value *root_map_value = json_value_init_object(); + // JSON_Object *root_map_object = + // json_value_get_object(root_map_value); + // json_object_set_string(root_map_object, "name", "South-West Wake + // county"); json_object_set_string(root_map_object, "description", + // "geology derived from vector map"); + // json_object_set_string(root_map_object, "layer", "geology_30m"); + // json_object_set_string(root_map_object, "type", "raster"); + // json_array_append_value(maps_arr, root_map_value); + // } + + serialized_string = json_serialize_to_string_pretty(root_value); + puts(serialized_string); + json_free_serialized_string(serialized_string); + json_value_free(root_value); +} diff --git a/raster/r.report/testsuite/test_r_report.py b/raster/r.report/testsuite/test_r_report.py index 7d872e17ff8..67ae6ac3307 100644 --- a/raster/r.report/testsuite/test_r_report.py +++ b/raster/r.report/testsuite/test_r_report.py @@ -12,6 +12,106 @@ import os from grass.gunittest.case import TestCase +OPTION_1 = { + "location": "nc_spm_08_grass7", + "created": "Fri Dec 6 17:00:21 2013", + "region": { + "north": 279073.97546639, + "south": 113673.97546639, + "east": 798143.31179672, + "west": 595143.31179672, + "sn-res": 200, + "ew-res": 200, + }, + "mask": None, + "maps": [ + { + "name": "South-West Wake county", + "description": "geology derived from vector map", + "layer": "geology_30m", + "type": "raster", + } + ], + "category": { + "categories": [ + { + "category": 217, + "description": "CZfg", + "sqmi": 27.78, + "acres": 17781.703, + "categories": [ + { + "category": 1, + "description": "developed", + "sqmi": 18, + "acres": 17781.703, + } + ], + } + ], + "total": {"sqmi": 77.60, "acres": 49668.182}, + }, +} + +OPTION_2 = { + "location": "nc_spm_08_grass7", + "created": "Fri Dec 6 17:00:21 2013", + "region": { + "north": 279073.97546639, + "south": 113673.97546639, + "east": 798143.31179672, + "west": 595143.31179672, + "sn-res": 200, + "ew-res": 200, + }, + "mask": None, + "maps": { + "geology_30m_raster": { + "name": "South-West Wake county", + "description": "geology derived from vector map", + } + }, + "categories": { + "217": { + "description": "CZfg", + "sqmi": 27.78, + "acres": 11781.703, + "1": {"description": "developed", "sqmi": 18, "acres": 17781.703}, + }, + "total": {"sqmi": 77.60, "acres": 49668.182}, + }, +} + +OPTION_3 = { + "location": "nc_spm_08_grass7", + "created": "Fri Dec 6 17:00:21 2013", + "region": { + "north": 279073.97546639, + "south": 113673.97546639, + "east": 798143.31179672, + "west": 595143.31179672, + "sn-res": 200, + "ew-res": 200, + }, + "mask": None, + "maps": [ + { + "name": "South-West Wake county", + "description": "geology derived from vector map", + "layer": "geology_30m", + "type": "raster", + } + ], + "totals": {"sqmi": 77.60, "acres": 49668.182}, + "fields": ["description", "sqmi", "acres"], + "categories": { + "217": { + "values": ["CZfg", 27.78, 11781.703], + "categories": {"1": {"values": ["developed", 18, 17781.703]}}, + } + }, +} + class TestRasterreport(TestCase): outfile = "test_out.csv"