Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r.what: Add JSON output #3528

Merged
merged 5 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion raster/r.what/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ MODULE_TOPDIR = ../..

PGM = r.what

LIBES = $(RASTERLIB) $(GISLIB) $(VECTORLIB)
LIBES = $(PARSONLIB) $(RASTERLIB) $(GISLIB) $(VECTORLIB)
DEPENDENCIES = $(RASTERDEP) $(GISDEP) $(VECTORDEP)
EXTRA_INC = $(VECT_INC)
EXTRA_CFLAGS = $(VECT_CFLAGS)
Expand Down
176 changes: 140 additions & 36 deletions raster/r.what/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <grass/raster.h>
#include <grass/vector.h>
#include <grass/glocale.h>
#include <grass/parson.h>

struct order {
int point;
Expand Down Expand Up @@ -74,7 +75,8 @@ int main(int argc, char *argv[])
char buffer[1024];
char **ptr;
struct _opt {
struct Option *input, *cache, *null, *coords, *fs, *points, *output;
struct Option *input, *cache, *null, *coords, *fs, *points, *output,
*format;
} opt;
struct _flg {
struct Flag *label, *cache, *cat_int, *color, *header, *cat;
Expand All @@ -93,6 +95,11 @@ int main(int argc, char *argv[])
int red, green, blue;
struct GModule *module;

JSON_Value *root_value = NULL, *point_value, *layer_value;
JSON_Array *root_array;
JSON_Object *point_object, *layer_object;
char **names;

G_gisinit(argv[0]);

/* Set description */
Expand Down Expand Up @@ -130,6 +137,17 @@ int main(int argc, char *argv[])
opt.fs = G_define_standard_option(G_OPT_F_SEP);
opt.fs->guisection = _("Print");

opt.format = G_define_option();
opt.format->key = "format";
opt.format->type = TYPE_STRING;
opt.format->required = NO;
opt.format->label = _("Output format");
opt.format->options = "plain,json";
opt.format->descriptions = "plain;Plain text output;"
"json;JSON (JavaScript Object Notation);";
opt.format->answer = "plain";
opt.format->guisection = _("Output settings");
kritibirda26 marked this conversation as resolved.
Show resolved Hide resolved

opt.cache = G_define_option();
opt.cache->key = "cache";
opt.cache->type = TYPE_INTEGER;
Expand Down Expand Up @@ -255,8 +273,16 @@ int main(int argc, char *argv[])
Cats = Vect_new_cats_struct();
G_get_window(&window);

if (strcmp(opt.format->answer, "json") == 0) {
kritibirda26 marked this conversation as resolved.
Show resolved Hide resolved
root_value = json_value_init_array();
kritibirda26 marked this conversation as resolved.
Show resolved Hide resolved
if (root_value == NULL) {
G_fatal_error(_("Failed to initialize JSON array. Out of memory?"));
}
root_array = json_array(root_value);
}

/* print header row */
if (flg.header->answer) {
if (strcmp(opt.format->answer, "plain") == 0 && flg.header->answer) {
if (flg.cat->answer) {
fprintf(stdout, "cat%s", fs);
}
Expand All @@ -278,6 +304,17 @@ int main(int argc, char *argv[])

fprintf(stdout, "\n");
}
else if (strcmp(opt.format->answer, "json") == 0) {
i = 0;
names = G_malloc(nfiles * sizeof(char *));

ptr = opt.input->answers;
for (; *ptr != NULL; ptr++) {
names[i] = G_malloc(GNAME_MAX);
strcpy(names[i], *ptr);
i++;
}
kritibirda26 marked this conversation as resolved.
Show resolved Hide resolved
}

line = 0;
if (!opt.coords->answers && !opt.points->answers && tty)
Expand Down Expand Up @@ -472,50 +509,106 @@ int main(int argc, char *argv[])
G_debug(1, "%s|%s at col %d, row %d\n", cache[point].east_buf,
cache[point].north_buf, cache[point].col, cache[point].row);

if (flg.cat->answer) {
fprintf(stdout, "%d%s", cache[point].cat, fs);
if (strcmp(opt.format->answer, "plain") == 0) {
kritibirda26 marked this conversation as resolved.
Show resolved Hide resolved

if (flg.cat->answer) {
fprintf(stdout, "%d%s", cache[point].cat, fs);
}
fprintf(stdout, "%s%s%s%s%s", cache[point].east_buf, fs,
cache[point].north_buf, fs, cache[point].lab_buf);

for (i = 0; i < nfiles; i++) {
if (out_type[i] == CELL_TYPE) {
if (Rast_is_c_null_value(&cache[point].value[i])) {
fprintf(stdout, "%s%s", fs, null_str);
if (flg.label->answer)
fprintf(stdout, "%s", fs);
if (flg.color->answer)
fprintf(stdout, "%s", fs);
continue;
}
fprintf(stdout, "%s%ld", fs,
(long)cache[point].value[i]);
cache[point].dvalue[i] = cache[point].value[i];
}
else { /* FCELL or DCELL */

if (Rast_is_d_null_value(&cache[point].dvalue[i])) {
fprintf(stdout, "%s%s", fs, null_str);
if (flg.label->answer)
fprintf(stdout, "%s", fs);
if (flg.color->answer)
fprintf(stdout, "%s", fs);
continue;
}
if (out_type[i] == FCELL_TYPE)
sprintf(tmp_buf, "%.7g", cache[point].dvalue[i]);
else /* DCELL */
sprintf(tmp_buf, "%.15g", cache[point].dvalue[i]);
G_trim_decimal(tmp_buf); /* not needed with %g? */
fprintf(stdout, "%s%s", fs, tmp_buf);
}
if (flg.label->answer)
fprintf(stdout, "%s%s", fs,
Rast_get_d_cat(&(cache[point].dvalue[i]),
&cats[i]));
if (flg.color->answer)
fprintf(stdout, "%s%s", fs, cache[point].clr_buf[i]);
}
fprintf(stdout, "\n");
}
fprintf(stdout, "%s%s%s%s%s", cache[point].east_buf, fs,
cache[point].north_buf, fs, cache[point].lab_buf);
else {
point_value = json_value_init_object();
point_object = json_object(point_value);

for (i = 0; i < nfiles; i++) {
if (out_type[i] == CELL_TYPE) {
if (Rast_is_c_null_value(&cache[point].value[i])) {
fprintf(stdout, "%s%s", fs, null_str);
if (flg.cat->answer) {
json_object_set_number(point_object, "cat",
cache[point].cat);
}

json_object_set_string(point_object, "easting",
cache[point].east_buf);
kritibirda26 marked this conversation as resolved.
Show resolved Hide resolved
json_object_set_string(point_object, "northing",
cache[point].north_buf);
json_object_set_string(point_object, "site_name",
cache[point].lab_buf);

for (i = 0; i < nfiles; i++) {
layer_value = json_value_init_object();
layer_object = json_object(layer_value);

if (Rast_is_c_null_value(&cache[point].value[i]) ||
Rast_is_d_null_value(&cache[point].dvalue[i])) {
json_object_set_null(layer_object, "value");
if (flg.label->answer)
fprintf(stdout, "%s", fs);
json_object_set_null(layer_object, "label");
if (flg.color->answer)
fprintf(stdout, "%s", fs);
continue;
json_object_set_null(layer_object, "color");
}
fprintf(stdout, "%s%ld", fs, (long)cache[point].value[i]);
cache[point].dvalue[i] = cache[point].value[i];
}
else { /* FCELL or DCELL */

if (Rast_is_d_null_value(&cache[point].dvalue[i])) {
fprintf(stdout, "%s%s", fs, null_str);
else {
if (out_type[i] == CELL_TYPE) {
json_object_set_number(layer_object, "value",
(long)cache[point].value[i]);
cache[point].dvalue[i] = cache[point].value[i];
}
else { /* FCELL or DCELL */
json_object_set_number(layer_object, "value",
cache[point].dvalue[i]);
}
if (flg.label->answer)
fprintf(stdout, "%s", fs);
json_object_set_string(
layer_object, "label",
Rast_get_d_cat(&(cache[point].dvalue[i]),
&cats[i]));
if (flg.color->answer)
fprintf(stdout, "%s", fs);
continue;
json_object_set_string(layer_object, "color",
cache[point].clr_buf[i]);
}
if (out_type[i] == FCELL_TYPE)
sprintf(tmp_buf, "%.7g", cache[point].dvalue[i]);
else /* DCELL */
sprintf(tmp_buf, "%.15g", cache[point].dvalue[i]);
G_trim_decimal(tmp_buf); /* not needed with %g? */
fprintf(stdout, "%s%s", fs, tmp_buf);

json_object_set_value(point_object, names[i], layer_value);
}
if (flg.label->answer)
fprintf(
stdout, "%s%s", fs,
Rast_get_d_cat(&(cache[point].dvalue[i]), &cats[i]));
if (flg.color->answer)
fprintf(stdout, "%s%s", fs, cache[point].clr_buf[i]);
json_array_append_value(root_array, point_value);
}
fprintf(stdout, "\n");
}

if (cache_report & !tty)
Expand All @@ -527,6 +620,17 @@ int main(int argc, char *argv[])
cache_hit = cache_miss = 0;
}

if (strcmp(opt.format->answer, "json") == 0) {
char *serialized_string = NULL;
serialized_string = json_serialize_to_string_pretty(root_value);
if (serialized_string == NULL) {
G_fatal_error(_("Failed to initialize pretty JSON string."));
}
puts(serialized_string);
json_free_serialized_string(serialized_string);
json_value_free(root_value);
}

if (!opt.coords->answers && !opt.points->answers && tty)
fprintf(stderr, "\n");
if (cache_report & !tty)
Expand Down
46 changes: 46 additions & 0 deletions raster/r.what/testsuite/test_r_what.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from grass.gunittest.case import TestCase
from grass.gunittest.gmodules import SimpleModule
import os
import json


class TestRasterWhat(TestCase):
Expand Down Expand Up @@ -437,6 +438,25 @@ class TestRasterWhat(TestCase):
332533.5941495|242831.139883875||121
"""

@staticmethod
def convert_plain_to_json(plain):
data = []
lines = plain.split("\n")
for line in lines:
line = line.strip()
if line:
parts = line.split("|")
item = {
"easting": parts[0],
"northing": parts[1],
"site_name": parts[2],
"boundary_county_500m": {"value": int(parts[3])},
}
if len(parts) == 5:
item["boundary_county_500m"]["color"] = parts[4]
data.append(item)
return data

@classmethod
def setUpClass(cls):
cls.use_temp_region()
Expand Down Expand Up @@ -541,6 +561,32 @@ def test_raster_what_cache(self):
msg="test_raster_what_cats did't run successfully",
)

def test_raster_what_json(self):
"""Testing r.what runs successfully with input coordinates given as a vector points map and JSON output"""
reference = self.convert_plain_to_json(self.refrence_points)
module = SimpleModule(
"r.what", map=self.map1, points=self.points, format="json"
)
module.run()
self.assertListEqual(
json.loads(str(module.outputs.stdout)),
reference,
"test_raster_what_points did't run successfully",
)

def test_raster_what_points_flag_r_json(self):
"""Testing r.what runs successfully with flag r and json output"""
reference = self.convert_plain_to_json(self.refrence_flag_r)
module = SimpleModule(
"r.what", map=self.map1, points=self.points, flags="r", format="json"
)
module.run()
self.assertListEqual(
json.loads(str(module.outputs.stdout)),
reference,
"test_raster_what_cats did't run successfully",
)


if __name__ == "__main__":
from grass.gunittest.main import test
Expand Down
Loading