Skip to content

Commit

Permalink
update r.describe json output
Browse files Browse the repository at this point in the history
Signed-off-by: Nishant Bansal <[email protected]>
  • Loading branch information
NishantBansal2003 committed Jan 16, 2025
1 parent 28623de commit 4556a7e
Show file tree
Hide file tree
Showing 2 changed files with 390 additions and 177 deletions.
161 changes: 88 additions & 73 deletions raster/r.describe/dumplist.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,21 @@

#include "local_proto.h"

static int show(CELL, CELL, int *, DCELL, DCELL, RASTER_MAP_TYPE, int,
enum OutputFormat, JSON_Array *);
static void initialize_json_object(JSON_Value **, JSON_Object **);
static void initialize_json_array(JSON_Value **, JSON_Array **);
static void json_append_category_value(JSON_Array *, const char *);
static void append_category_ranges(JSON_Array *range_array, long min, long max);
static void output_pretty_json(JSON_Value *);
static int show(CELL, CELL, int *, DCELL, DCELL, RASTER_MAP_TYPE, int,
enum OutputFormat, JSON_Array *);

void initialize_json_object(JSON_Value **root_value, JSON_Object **root_object)
{
*root_value = json_value_init_object();
if (*root_value == NULL) {
G_fatal_error(_("Failed to initialize JSON object. Out of memory?"));
}
*root_object = json_object(*root_value);
}

void initialize_json_array(JSON_Value **root_value, JSON_Array **root_array)
{
Expand All @@ -37,19 +47,16 @@ void initialize_json_array(JSON_Value **root_value, JSON_Array **root_array)
*root_array = json_array(*root_value);
}

void json_append_category_value(JSON_Array *root_array, const char *cat_str)
void append_category_ranges(JSON_Array *range_array, long min, long max)
{
JSON_Value *category_value;
JSON_Object *category;
JSON_Object *cat_object;
JSON_Value *cat_value;
initialize_json_object(&cat_value, &cat_object);

category_value = json_value_init_object();
if (category_value == NULL) {
G_fatal_error(_("Failed to initialize JSON array. Out of memory?"));
}
category = json_object(category_value);
json_object_set_number(cat_object, "min", min);
json_object_set_number(cat_object, "max", max);

json_object_set_string(category, "value", cat_str);
json_array_append_value(root_array, category_value);
json_array_append_value(range_array, cat_value);
}

void output_pretty_json(JSON_Value *root_value)
Expand All @@ -72,11 +79,13 @@ int long_list(struct Cell_stats *statf, DCELL dmin, DCELL dmax,
CELL cat;
long count; /* not used, but required by cell stats call */
char cat_str[MAX_STR_LEN];
JSON_Value *root_value;
JSON_Array *root_array;
JSON_Value *root_value, *range_value;
JSON_Object *root_object;
JSON_Array *range_array;

if (format == JSON) {
initialize_json_array(&root_value, &root_array);
initialize_json_object(&root_value, &root_object);
initialize_json_array(&range_value, &range_array);
}

Rast_get_stats_for_null_value(&count, statf);
Expand All @@ -86,7 +95,7 @@ int long_list(struct Cell_stats *statf, DCELL dmin, DCELL dmax,
fprintf(stdout, "%s\n", no_data_str);
break;
case JSON:
json_append_category_value(root_array, no_data_str);
json_object_set_string(root_object, "null_value", no_data_str);
break;
}
}
Expand All @@ -103,19 +112,35 @@ int long_list(struct Cell_stats *statf, DCELL dmin, DCELL dmax,
break;
case JSON:
if (map_type != CELL_TYPE) {
snprintf(cat_str, sizeof(cat_str), "%f-%f",
dmin + (double)(cat - 1) * (dmax - dmin) / nsteps,
dmin + (double)cat * (dmax - dmin) / nsteps);
JSON_Object *cat_object;
JSON_Value *cat_value;
initialize_json_object(&cat_value, &cat_object);

json_object_set_number(cat_object, "min",
dmin + (double)(cat - 1) *
(dmax - dmin) / nsteps);
json_object_set_number(cat_object, "max",
dmin + (double)cat * (dmax - dmin) /
nsteps);

json_array_append_value(range_array, cat_value);
}
else {
snprintf(cat_str, sizeof(cat_str), "%ld", (long)cat);
json_array_append_number(range_array, (long)cat);
}
json_append_category_value(root_array, cat_str);

break;
}
}

if (format == JSON) {
if (map_type != CELL_TYPE) {
json_object_set_value(root_object, "ranges", range_value);
}
else {
json_object_set_value(root_object, "values", range_value);
}

output_pretty_json(root_value);
}

Expand All @@ -129,11 +154,13 @@ int compact_list(struct Cell_stats *statf, DCELL dmin, DCELL dmax,
CELL cat1, cat2, temp;
int len;
long count; /* not used, but required by cell stats call */
JSON_Value *root_value;
JSON_Array *root_array;
JSON_Value *root_value, *range_value;
JSON_Object *root_object;
JSON_Array *range_array;

if (format == JSON) {
initialize_json_array(&root_value, &root_array);
initialize_json_object(&root_value, &root_object);
initialize_json_array(&range_value, &range_array);
}

len = 0;
Expand All @@ -144,7 +171,7 @@ int compact_list(struct Cell_stats *statf, DCELL dmin, DCELL dmax,
fprintf(stdout, "%s ", no_data_str);
break;
case JSON:
json_append_category_value(root_array, no_data_str);
json_object_set_string(root_object, "null_value", no_data_str);
break;
}
}
Expand All @@ -157,18 +184,19 @@ int compact_list(struct Cell_stats *statf, DCELL dmin, DCELL dmax,
while (Rast_next_cell_stat(&temp, &count, statf)) {
if (temp != cat2 + (CELL)1) {
show(cat1, cat2, &len, dmin, dmax, map_type, nsteps, format,
root_array);
range_array);
cat1 = temp;
}
cat2 = temp;
}
show(cat1, cat2, &len, dmin, dmax, map_type, nsteps, format, root_array);
show(cat1, cat2, &len, dmin, dmax, map_type, nsteps, format, range_array);

switch (format) {
case PLAIN:
fprintf(stdout, "\n");
break;
case JSON:
json_object_set_value(root_object, "ranges", range_value);
output_pretty_json(root_value);
break;
}
Expand All @@ -182,13 +210,18 @@ static int show(CELL low, CELL high, int *len, DCELL dmin, DCELL dmax,
{
char text[MAX_STR_LEN];
char xlen;
JSON_Object *cat_object;
JSON_Value *cat_value;

if (low + 1 == high) {
show(low, low, len, dmin, dmax, map_type, nsteps, format, root_array);
show(high, high, len, dmin, dmax, map_type, nsteps, format, root_array);
return 0;
}

if (format == JSON)
initialize_json_object(&cat_value, &cat_object);

if (map_type != CELL_TYPE) {
switch (format) {
case PLAIN:
Expand All @@ -197,9 +230,10 @@ static int show(CELL low, CELL high, int *len, DCELL dmin, DCELL dmax,
dmin + high * (dmax - dmin) / nsteps);
break;
case JSON:
sprintf(text, "%f%s%f", dmin + (low - 1) * (dmax - dmin) / nsteps,
dmin < 0 ? " thru " : "-",
dmin + high * (dmax - dmin) / nsteps);
json_object_set_number(cat_object, "min",
dmin + (low - 1) * (dmax - dmin) / nsteps);
json_object_set_number(cat_object, "max",
dmin + high * (dmax - dmin) / nsteps);
break;
}
}
Expand All @@ -213,11 +247,8 @@ static int show(CELL low, CELL high, int *len, DCELL dmin, DCELL dmax,
(long)high);
break;
case JSON:
if (low == high)
sprintf(text, "%ld", (long)low);
else
sprintf(text, "%ld%s%ld", (long)low, low < 0 ? " thru " : "-",
(long)high);
json_object_set_number(cat_object, "min", (long)low);
json_object_set_number(cat_object, "max", (long)high);
break;
}
}
Expand All @@ -234,7 +265,7 @@ static int show(CELL low, CELL high, int *len, DCELL dmin, DCELL dmax,
fprintf(stdout, "%s", text);
break;
case JSON:
json_append_category_value(root_array, text);
json_array_append_value(root_array, cat_value);
break;
}

Expand All @@ -247,11 +278,13 @@ int compact_range_list(CELL negmin, CELL negmax, CELL zero, CELL posmin,
int skip_nulls, enum OutputFormat format)
{
char cat_str[MAX_STR_LEN];
JSON_Value *root_value;
JSON_Array *root_array;
JSON_Value *root_value, *range_value;
JSON_Object *root_object;
JSON_Array *range_array;

if (format == JSON) {
initialize_json_array(&root_value, &root_array);
initialize_json_object(&root_value, &root_object);
initialize_json_array(&range_value, &range_array);
}

if (negmin) {
Expand All @@ -263,12 +296,7 @@ int compact_range_list(CELL negmin, CELL negmax, CELL zero, CELL posmin,
fprintf(stdout, "\n");
break;
case JSON:
snprintf(cat_str, sizeof(cat_str), "%ld", (long)negmin);
if (negmin != negmax)
snprintf(cat_str, sizeof(cat_str), "%ld thru %ld", (long)negmin,
(long)negmax);

json_append_category_value(root_array, cat_str);
append_category_ranges(range_array, (long)negmin, (long)negmax);
break;
}
}
Expand All @@ -278,7 +306,7 @@ int compact_range_list(CELL negmin, CELL negmax, CELL zero, CELL posmin,
fprintf(stdout, "0\n");
break;
case JSON:
json_append_category_value(root_array, "0");
append_category_ranges(range_array, 0, 0);
break;
}
}
Expand All @@ -291,12 +319,7 @@ int compact_range_list(CELL negmin, CELL negmax, CELL zero, CELL posmin,
fprintf(stdout, "\n");
break;
case JSON:
snprintf(cat_str, sizeof(cat_str), "%ld", (long)posmin);
if (posmin != posmax)
snprintf(cat_str, sizeof(cat_str), "%ld thru %ld", (long)posmin,
(long)posmax);

json_append_category_value(root_array, cat_str);
append_category_ranges(range_array, (long)posmin, (long)posmax);
break;
}
}
Expand All @@ -307,12 +330,13 @@ int compact_range_list(CELL negmin, CELL negmax, CELL zero, CELL posmin,
fprintf(stdout, "%s\n", no_data_str);
break;
case JSON:
json_append_category_value(root_array, no_data_str);
json_object_set_string(root_object, "null_value", no_data_str);
break;
}
}

if (format == JSON) {
json_object_set_value(root_object, "ranges", range_value);
output_pretty_json(root_value);
}

Expand All @@ -324,11 +348,13 @@ int range_list(CELL negmin, CELL negmax, CELL zero, CELL posmin, CELL posmax,
enum OutputFormat format)
{
char cat_str[MAX_STR_LEN];
JSON_Value *root_value;
JSON_Array *root_array;
JSON_Value *root_value, *range_value;
JSON_Object *root_object;
JSON_Array *range_array;

if (format == JSON) {
initialize_json_array(&root_value, &root_array);
initialize_json_object(&root_value, &root_object);
initialize_json_array(&range_value, &range_array);
}

if (negmin) {
Expand All @@ -339,13 +365,7 @@ int range_list(CELL negmin, CELL negmax, CELL zero, CELL posmin, CELL posmax,
fprintf(stdout, "%ld\n", (long)negmax);
break;
case JSON:
snprintf(cat_str, sizeof(cat_str), "%ld", (long)negmin);
json_append_category_value(root_array, cat_str);

if (negmin != negmax) {
snprintf(cat_str, sizeof(cat_str), "%ld", (long)negmax);
json_append_category_value(root_array, cat_str);
}
append_category_ranges(range_array, (long)negmin, (long)negmax);
break;
}
}
Expand All @@ -356,7 +376,7 @@ int range_list(CELL negmin, CELL negmax, CELL zero, CELL posmin, CELL posmax,
fprintf(stdout, "0\n");
break;
case JSON:
json_append_category_value(root_array, "0");
append_category_ranges(range_array, 0, 0);
break;
}
}
Expand All @@ -369,13 +389,7 @@ int range_list(CELL negmin, CELL negmax, CELL zero, CELL posmin, CELL posmax,
fprintf(stdout, "%ld\n", (long)posmax);
break;
case JSON:
snprintf(cat_str, sizeof(cat_str), "%ld", (long)posmin);
json_append_category_value(root_array, cat_str);

if (posmin != posmax) {
snprintf(cat_str, sizeof(cat_str), "%ld", (long)posmax);
json_append_category_value(root_array, cat_str);
}
append_category_ranges(range_array, (long)posmin, (long)posmax);
break;
}
}
Expand All @@ -386,12 +400,13 @@ int range_list(CELL negmin, CELL negmax, CELL zero, CELL posmin, CELL posmax,
fprintf(stdout, "%s\n", no_data_str);
break;
case JSON:
json_append_category_value(root_array, no_data_str);
json_object_set_string(root_object, "null_value", no_data_str);
break;
}
}

if (format == JSON) {
json_object_set_value(root_object, "ranges", range_value);
output_pretty_json(root_value);
}

Expand Down
Loading

0 comments on commit 4556a7e

Please sign in to comment.