From 3ae0e638f697a9ef65025c5e5585a00ee5e30291 Mon Sep 17 00:00:00 2001 From: Kriti Birda Date: Sun, 24 Mar 2024 19:33:32 +0530 Subject: [PATCH 1/5] [feat] Add JSON output to r.what Added an option format which accepts the value "plain" for the current output format and "json" for the JSON output. The newly added parson library is being used here. Also, added a couple of python tests to ensure JSON output works as expected. --- raster/r.what/Makefile | 2 +- raster/r.what/main.c | 180 +++++++++++++++++----- raster/r.what/testsuite/test_r_what.py | 199 +++++++++++++++++++++++++ 3 files changed, 340 insertions(+), 41 deletions(-) diff --git a/raster/r.what/Makefile b/raster/r.what/Makefile index 8cf21eb01bf..e30ba7d7003 100644 --- a/raster/r.what/Makefile +++ b/raster/r.what/Makefile @@ -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) diff --git a/raster/r.what/main.c b/raster/r.what/main.c index 7bb50721e5d..49bfb9d606c 100644 --- a/raster/r.what/main.c +++ b/raster/r.what/main.c @@ -29,6 +29,7 @@ #include #include #include +#include struct order { int point; @@ -74,7 +75,7 @@ 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; @@ -93,6 +94,11 @@ int main(int argc, char *argv[]) int red, green, blue; struct GModule *module; + JSON_Value *root_value, *point_value; + JSON_Array *root_array; + JSON_Object *point_object; + char **names, **label_names, **color_names; + G_gisinit(argv[0]); /* Set description */ @@ -130,6 +136,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"); + opt.cache = G_define_option(); opt.cache->key = "cache"; opt.cache->type = TYPE_INTEGER; @@ -255,8 +272,13 @@ int main(int argc, char *argv[]) Cats = Vect_new_cats_struct(); G_get_window(&window); + if (strcmp(opt.format->answer, "json") == 0) { + root_value = json_value_init_array(); + 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); } @@ -277,6 +299,27 @@ 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 *)); + label_names = G_malloc(nfiles * sizeof(char *)); + color_names = G_malloc(nfiles * sizeof(char *)); + + ptr = opt.input->answers; + for (; *ptr != NULL; ptr++) { + names[i] = G_malloc(GNAME_MAX); + label_names[i] = G_malloc(GNAME_MAX + 6 /* 6 for _label suffix */); + color_names[i] = G_malloc(GNAME_MAX + 6 /* 6 for _color suffix */); + + strcpy(names[i], *ptr); + + if (flg.label->answer) + sprintf(label_names[i], "%s_label", *ptr); + + if (flg.color->answer) + sprintf(color_names[i], "%s_color", *ptr); + i++; + } } line = 0; @@ -472,50 +515,102 @@ 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); - } - fprintf(stdout, "%s%s%s%s%s", cache[point].east_buf, fs, - cache[point].north_buf, fs, cache[point].lab_buf); + if (strcmp(opt.format->answer, "plain") == 0) { - 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; + 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); } - fprintf(stdout, "%s%ld", fs, (long)cache[point].value[i]); - cache[point].dvalue[i] = cache[point].value[i]; + 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"); + } + else { + point_value = json_value_init_object(); + point_object = json_object(point_value); + + if (flg.cat->answer) { + json_object_set_number(point_object, "cat", cache[point].cat); } - 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; + + json_object_set_string(point_object, "easting", cache[point].east_buf); + 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++) { + if (out_type[i] == CELL_TYPE) { + if (Rast_is_c_null_value(&cache[point].value[i])) { + json_object_set_null(point_object, names[i]); + if (flg.label->answer) + json_object_set_null(point_object, label_names[i]); + if (flg.color->answer) + json_object_set_null(point_object, color_names[i]); + continue; + } + json_object_set_number(point_object, names[i], + (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])) { + json_object_set_null(point_object, names[i]); + if (flg.label->answer) + json_object_set_null(point_object, label_names[i]); + if (flg.color->answer) + json_object_set_null(point_object, color_names[i]); + continue; + } + json_object_set_number(point_object, names[i], cache[point].dvalue[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); + if (flg.label->answer) + json_object_set_string( + point_object, label_names[i], + Rast_get_d_cat(&(cache[point].dvalue[i]), &cats[i]) + ); + if (flg.color->answer) + json_object_set_string(point_object, color_names[i], + cache[point].clr_buf[i]); } - 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) @@ -527,6 +622,11 @@ int main(int argc, char *argv[]) cache_hit = cache_miss = 0; } + if (strcmp(opt.format->answer, "json") == 0) { + fprintf(stdout, "%s\n", json_serialize_to_string(root_value)); + json_value_free(root_value); + } + if (!opt.coords->answers && !opt.points->answers && tty) fprintf(stderr, "\n"); if (cache_report & !tty) diff --git a/raster/r.what/testsuite/test_r_what.py b/raster/r.what/testsuite/test_r_what.py index 12a8be2f90d..a05fdcbde1a 100644 --- a/raster/r.what/testsuite/test_r_what.py +++ b/raster/r.what/testsuite/test_r_what.py @@ -12,6 +12,7 @@ from grass.gunittest.case import TestCase from grass.gunittest.gmodules import SimpleModule import os +import json class TestRasterWhat(TestCase): @@ -78,6 +79,66 @@ class TestRasterWhat(TestCase): 511235.1561495|135372.358883875||7 332533.5941495|242831.139883875||121 """ + reference_points_json = [ + {"easting": "145096.8591495", "northing": "154534.264883875", "site_name": "", "boundary_county_500m": 39}, + {"easting": "616341.4371495", "northing": "146049.750883875", "site_name": "", "boundary_county_500m": 51}, + {"easting": "410595.7191495", "northing": "174301.828883875", "site_name": "", "boundary_county_500m": 71}, + {"easting": "734153.6871495", "northing": "169168.437883875", "site_name": "", "boundary_county_500m": 107}, + {"easting": "706338.2501495", "northing": "54889.417883875", "site_name": "", "boundary_county_500m": 129}, + {"easting": "758009.7501495", "northing": "112019.898883875", "site_name": "", "boundary_county_500m": 133}, + {"easting": "754002.7501495", "northing": "200902.234883875", "site_name": "", "boundary_county_500m": 147}, + {"easting": "704771.7501495", "northing": "183364.484883875", "site_name": "", "boundary_county_500m": 191}, + {"easting": "399187.0631495", "northing": "220018.859883875", "site_name": "", "boundary_county_500m": 35}, + {"easting": "685098.9371495", "northing": "33282.089883875", "site_name": "", "boundary_county_500m": 19}, + {"easting": "577750.8131495", "northing": "257153.109883875", "site_name": "", "boundary_county_500m": 1}, + {"easting": "794095.5621495", "northing": "199742.671883875", "site_name": "", "boundary_county_500m": 13}, + {"easting": "634688.2501495", "northing": "100629.616883875", "site_name": "", "boundary_county_500m": 17}, + {"easting": "287638.7811495", "northing": "207582.624883875", "site_name": "", "boundary_county_500m": 21}, + {"easting": "366218.5321495", "northing": "222940.625883875", "site_name": "", "boundary_county_500m": 23}, + {"easting": "385212.4371495", "northing": "236593.109883875", "site_name": "", "boundary_county_500m": 27}, + {"easting": "628137.4371495", "northing": "63995.550883875", "site_name": "", "boundary_county_500m": 47}, + {"easting": "782600.5631495", "northing": "152698.890883875", "site_name": "", "boundary_county_500m": 49}, + {"easting": "502813.9381495", "northing": "235232.577883875", "site_name": "", "boundary_county_500m": 57}, + {"easting": "705922.6251495", "northing": "136589.359883875", "site_name": "", "boundary_county_500m": 61}, + {"easting": "620397.8131495", "northing": "246847.640883875", "site_name": "", "boundary_county_500m": 63}, + {"easting": "738465.3751495", "northing": "237233.983883875", "site_name": "", "boundary_county_500m": 65}, + {"easting": "708944.7501495", "northing": "247632.296883875", "site_name": "", "boundary_county_500m": 127}, + {"easting": "526666.6871495", "northing": "249780.312883875", "site_name": "", "boundary_county_500m": 81}, + {"easting": "733439.3741495", "northing": "298005.311883875", "site_name": "", "boundary_county_500m": 83}, + {"easting": "253886.0321495", "northing": "204702.171883875", "site_name": "", "boundary_county_500m": 87}, + {"easting": "298337.5621495", "northing": "178131.233883875", "site_name": "", "boundary_county_500m": 89}, + {"easting": "788989.3121495", "northing": "284544.562883875", "site_name": "", "boundary_county_500m": 91}, + {"easting": "438378.3431495", "northing": "227157.890883875", "site_name": "", "boundary_county_500m": 97}, + {"easting": "227406.0621495", "northing": "190462.640883875", "site_name": "", "boundary_county_500m": 99}, + {"easting": "668782.8121495", "northing": "195518.718883875", "site_name": "", "boundary_county_500m": 101}, + {"easting": "596325.2501495", "northing": "190688.374883875", "site_name": "", "boundary_county_500m": 105}, + {"easting": "777034.8131495", "northing": "229280.030883875", "site_name": "", "boundary_county_500m": 117}, + {"easting": "337714.9681495", "northing": "219991.202883875", "site_name": "", "boundary_county_500m": 111}, + {"easting": "442913.7181495", "northing": "164364.608883875", "site_name": "", "boundary_county_500m": 119}, + {"easting": "528779.3741495", "northing": "179793.890883875", "site_name": "", "boundary_county_500m": 123}, + {"easting": "571926.0621495", "northing": "162885.202883875", "site_name": "", "boundary_county_500m": 125}, + {"easting": "801852.0621495", "northing": "154744.077883875", "site_name": "", "boundary_county_500m": 137}, + {"easting": "611371.9381495", "northing": "295204.467883875", "site_name": "", "boundary_county_500m": 145}, + {"easting": "534602.9371495", "northing": "213605.812883875", "site_name": "", "boundary_county_500m": 151}, + {"easting": "608960.3121495", "northing": "102612.546883875", "site_name": "", "boundary_county_500m": 155}, + {"easting": "541667.1251495", "northing": "293002.124883875", "site_name": "", "boundary_county_500m": 157}, + {"easting": "475819.8131495", "northing": "210685.359883875", "site_name": "", "boundary_county_500m": 159}, + {"easting": "343685.3441495", "northing": "182222.358883875", "site_name": "", "boundary_county_500m": 161}, + {"easting": "670160.3121495", "northing": "140070.249883875", "site_name": "", "boundary_county_500m": 163}, + {"easting": "497214.4691495", "northing": "177053.639883875", "site_name": "", "boundary_county_500m": 167}, + {"easting": "455564.7811495", "northing": "293568.937883875", "site_name": "", "boundary_county_500m": 171}, + {"easting": "636209.2501495", "northing": "210858.014883875", "site_name": "", "boundary_county_500m": 183}, + {"easting": "413565.1871495", "northing": "267534.562883875", "site_name": "", "boundary_county_500m": 193}, + {"easting": "709485.0001495", "northing": "220209.202883875", "site_name": "", "boundary_county_500m": 195}, + {"easting": "815182.1881495", "northing": "110335.429883875", "site_name": "", "boundary_county_500m": 31}, + {"easting": "495151.3131495", "northing": "258002.374883875", "site_name": "", "boundary_county_500m": 67}, + {"easting": "859129.4371495", "northing": "288849.968883875", "site_name": "", "boundary_county_500m": 139}, + {"easting": "545200.0621495", "northing": "127391.304883875", "site_name": "", "boundary_county_500m": 153}, + {"easting": "655627.6871495", "northing": "285597.342883875", "site_name": "", "boundary_county_500m": 181}, + {"easting": "382174.7811495", "northing": "171687.280883875", "site_name": "", "boundary_county_500m": 45}, + {"easting": "511235.1561495", "northing": "135372.358883875", "site_name": "", "boundary_county_500m": 7}, + {"easting": "332533.5941495", "northing": "242831.139883875", "site_name": "", "boundary_county_500m": 121} + ] refrence_coordinates = """633614.08|224125.12||2|209.5939 632972.36|225382.87||15|140.7571 """ @@ -377,6 +438,124 @@ class TestRasterWhat(TestCase): 511235.1561495|135372.358883875||7|211:255:000 332533.5941495|242831.139883875||121|010:000:255 """ + reference_flag_r_json = [ + {"easting": "145096.8591495", "northing": "154534.264883875", "site_name": '', "boundary_county_500m": 39, + "boundary_county_500m_color": "006:255:000"}, + {"easting": "616341.4371495", "northing": "146049.750883875", "site_name": '', "boundary_county_500m": 51, + "boundary_county_500m_color": "000:255:071"}, + {"easting": "410595.7191495", "northing": "174301.828883875", "site_name": '', "boundary_county_500m": 71, + "boundary_county_500m_color": "000:255:199"}, + {"easting": "734153.6871495", "northing": "169168.437883875", "site_name": '', "boundary_county_500m": 107, + "boundary_county_500m_color": "000:080:255"}, + {"easting": "706338.2501495", "northing": "54889.417883875", "site_name": '', "boundary_county_500m": 129, + "boundary_county_500m_color": "061:000:255"}, + {"easting": "758009.7501495", "northing": "112019.898883875", "site_name": '', "boundary_county_500m": 133, + "boundary_county_500m_color": "087:000:255"}, + {"easting": "754002.7501495", "northing": "200902.234883875", "site_name": '', "boundary_county_500m": 147, + "boundary_county_500m_color": "176:000:255"}, + {"easting": "704771.7501495", "northing": "183364.484883875", "site_name": '', "boundary_county_500m": 191, + "boundary_county_500m_color": "255:000:052"}, + {"easting": "399187.0631495", "northing": "220018.859883875", "site_name": '', "boundary_county_500m": 35, + "boundary_county_500m_color": "031:255:000"}, + {"easting": "685098.9371495", "northing": "33282.089883875", "site_name": '', "boundary_county_500m": 19, + "boundary_county_500m_color": "134:255:000"}, + {"easting": "577750.8131495", "northing": "257153.109883875", "site_name": '', "boundary_county_500m": 1, + "boundary_county_500m_color": "249:255:000"}, + {"easting": "794095.5621495", "northing": "199742.671883875", "site_name": '', "boundary_county_500m": 13, + "boundary_county_500m_color": "172:255:000"}, + {"easting": "634688.2501495", "northing": "100629.616883875", "site_name": '', "boundary_county_500m": 17, + "boundary_county_500m_color": "147:255:000"}, + {"easting": "287638.7811495", "northing": "207582.624883875", "site_name": '', "boundary_county_500m": 21, + "boundary_county_500m_color": "121:255:000"}, + {"easting": "366218.5321495", "northing": "222940.625883875", "site_name": '', "boundary_county_500m": 23, + "boundary_county_500m_color": "108:255:000"}, + {"easting": "385212.4371495", "northing": "236593.109883875", "site_name": '', "boundary_county_500m": 27, + "boundary_county_500m_color": "083:255:000"}, + {"easting": "628137.4371495", "northing": "63995.550883875", "site_name": '', "boundary_county_500m": 47, + "boundary_county_500m_color": "000:255:046"}, + {"easting": "782600.5631495", "northing": "152698.890883875", "site_name": '', "boundary_county_500m": 49, + "boundary_county_500m_color": "000:255:058"}, + {"easting": "502813.9381495", "northing": "235232.577883875", "site_name": '', "boundary_county_500m": 57, + "boundary_county_500m_color": "000:255:110"}, + {"easting": "705922.6251495", "northing": "136589.359883875", "site_name": '', "boundary_county_500m": 61, + "boundary_county_500m_color": "000:255:135"}, + {"easting": "620397.8131495", "northing": "246847.640883875", "site_name": '', "boundary_county_500m": 63, + "boundary_county_500m_color": "000:255:148"}, + {"easting": "738465.3751495", "northing": "237233.983883875", "site_name": '', "boundary_county_500m": 65, + "boundary_county_500m_color": "000:255:161"}, + {"easting": "708944.7501495", "northing": "247632.296883875", "site_name": '', "boundary_county_500m": 127, + "boundary_county_500m_color": "048:000:255"}, + {"easting": "526666.6871495", "northing": "249780.312883875", "site_name": '', "boundary_county_500m": 81, + "boundary_county_500m_color": "000:247:255"}, + {"easting": "733439.3741495", "northing": "298005.311883875", "site_name": '', "boundary_county_500m": 83, + "boundary_county_500m_color": "000:234:255"}, + {"easting": "253886.0321495", "northing": "204702.171883875", "site_name": '', "boundary_county_500m": 87, + "boundary_county_500m_color": "000:208:255"}, + {"easting": "298337.5621495", "northing": "178131.233883875", "site_name": '', "boundary_county_500m": 89, + "boundary_county_500m_color": "000:195:255"}, + {"easting": "788989.3121495", "northing": "284544.562883875", "site_name": '', "boundary_county_500m": 91, + "boundary_county_500m_color": "000:182:255"}, + {"easting": "438378.3431495", "northing": "227157.890883875", "site_name": '', "boundary_county_500m": 97, + "boundary_county_500m_color": "000:144:255"}, + {"easting": "227406.0621495", "northing": "190462.640883875", "site_name": '', "boundary_county_500m": 99, + "boundary_county_500m_color": "000:131:255"}, + {"easting": "668782.8121495", "northing": "195518.718883875", "site_name": '', "boundary_county_500m": 101, + "boundary_county_500m_color": "000:118:255"}, + {"easting": "596325.2501495", "northing": "190688.374883875", "site_name": '', "boundary_county_500m": 105, + "boundary_county_500m_color": "000:093:255"}, + {"easting": "777034.8131495", "northing": "229280.030883875", "site_name": '', "boundary_county_500m": 117, + "boundary_county_500m_color": "000:016:255"}, + {"easting": "337714.9681495", "northing": "219991.202883875", "site_name": '', "boundary_county_500m": 111, + "boundary_county_500m_color": "000:054:255"}, + {"easting": "442913.7181495", "northing": "164364.608883875", "site_name": '', "boundary_county_500m": 119, + "boundary_county_500m_color": "000:003:255"}, + {"easting": "528779.3741495", "northing": "179793.890883875", "site_name": '', "boundary_county_500m": 123, + "boundary_county_500m_color": "023:000:255"}, + {"easting": "571926.0621495", "northing": "162885.202883875", "site_name": '', "boundary_county_500m": 125, + "boundary_county_500m_color": "035:000:255"}, + {"easting": "801852.0621495", "northing": "154744.077883875", "site_name": '', "boundary_county_500m": 137, + "boundary_county_500m_color": "112:000:255"}, + {"easting": "611371.9381495", "northing": "295204.467883875", "site_name": '', "boundary_county_500m": 145, + "boundary_county_500m_color": "164:000:255"}, + {"easting": "534602.9371495", "northing": "213605.812883875", "site_name": '', "boundary_county_500m": 151, + "boundary_county_500m_color": "202:000:255"}, + {"easting": "608960.3121495", "northing": "102612.546883875", "site_name": '', "boundary_county_500m": 155, + "boundary_county_500m_color": "228:000:255"}, + {"easting": "541667.1251495", "northing": "293002.124883875", "site_name": '', "boundary_county_500m": 157, + "boundary_county_500m_color": "240:000:255"}, + {"easting": "475819.8131495", "northing": "210685.359883875", "site_name": '', "boundary_county_500m": 159, + "boundary_county_500m_color": "253:000:255"}, + {"easting": "343685.3441495", "northing": "182222.358883875", "site_name": '', "boundary_county_500m": 161, + "boundary_county_500m_color": "255:000:244"}, + {"easting": "670160.3121495", "northing": "140070.249883875", "site_name": '', "boundary_county_500m": 163, + "boundary_county_500m_color": "255:000:231"}, + {"easting": "497214.4691495", "northing": "177053.639883875", "site_name": '', "boundary_county_500m": 167, + "boundary_county_500m_color": "255:000:206"}, + {"easting": "455564.7811495", "northing": "293568.937883875", "site_name": '', "boundary_county_500m": 171, + "boundary_county_500m_color": "255:000:180"}, + {"easting": "636209.2501495", "northing": "210858.014883875", "site_name": '', "boundary_county_500m": 183, + "boundary_county_500m_color": "255:000:103"}, + {"easting": "413565.1871495", "northing": "267534.562883875", "site_name": '', "boundary_county_500m": 193, + "boundary_county_500m_color": "255:000:039"}, + {"easting": "709485.0001495", "northing": "220209.202883875", "site_name": '', "boundary_county_500m": 195, + "boundary_county_500m_color": "255:000:026"}, + {"easting": "815182.1881495", "northing": "110335.429883875", "site_name": '', "boundary_county_500m": 31, + "boundary_county_500m_color": "057:255:000"}, + {"easting": "495151.3131495", "northing": "258002.374883875", "site_name": '', "boundary_county_500m": 67, + "boundary_county_500m_color": "000:255:174"}, + {"easting": "859129.4371495", "northing": "288849.968883875", "site_name": '', "boundary_county_500m": 139, + "boundary_county_500m_color": "125:000:255"}, + {"easting": "545200.0621495", "northing": "127391.304883875", "site_name": '', "boundary_county_500m": 153, + "boundary_county_500m_color": "215:000:255"}, + {"easting": "655627.6871495", "northing": "285597.342883875", "site_name": '', "boundary_county_500m": 181, + "boundary_county_500m_color": "255:000:116"}, + {"easting": "382174.7811495", "northing": "171687.280883875", "site_name": '', "boundary_county_500m": 45, + "boundary_county_500m_color": "000:255:033"}, + {"easting": "511235.1561495", "northing": "135372.358883875", "site_name": '', "boundary_county_500m": 7, + "boundary_county_500m_color": "211:255:000"}, + {"easting": "332533.5941495", "northing": "242831.139883875", "site_name": '', "boundary_county_500m": 121, + "boundary_county_500m_color": "010:000:255"} + ] refrence_cache = """145096.8591495|154534.264883875||39 616341.4371495|146049.750883875||51 410595.7191495|174301.828883875||71 @@ -541,6 +720,26 @@ 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 """ + module = SimpleModule("r.what", map=self.map1, points=self.points, format="json") + module.run() + self.assertListEqual( + json.loads(str(module.outputs.stdout)), + self.reference_points_json, + "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""" + module = SimpleModule("r.what", map=self.map1, points=self.points, flags="r", format="json") + module.run() + self.assertListEqual( + json.loads(str(module.outputs.stdout)), + self.reference_flag_r_json, + "test_raster_what_cats did't run successfully", + ) + if __name__ == "__main__": from grass.gunittest.main import test From 42b2196a739a3d02de71081addb2c71d7f7f5766 Mon Sep 17 00:00:00 2001 From: Kriti Birda Date: Sun, 24 Mar 2024 19:55:07 +0530 Subject: [PATCH 2/5] fix clang format --- raster/r.what/main.c | 46 ++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/raster/r.what/main.c b/raster/r.what/main.c index 49bfb9d606c..747d8b791c3 100644 --- a/raster/r.what/main.c +++ b/raster/r.what/main.c @@ -75,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, *format; + struct Option *input, *cache, *null, *coords, *fs, *points, *output, + *format; } opt; struct _flg { struct Flag *label, *cache, *cat_int, *color, *header, *cat; @@ -299,7 +300,8 @@ int main(int argc, char *argv[]) } fprintf(stdout, "\n"); - } else if (strcmp(opt.format->answer, "json") == 0) { + } + else if (strcmp(opt.format->answer, "json") == 0) { i = 0; names = G_malloc(nfiles * sizeof(char *)); label_names = G_malloc(nfiles * sizeof(char *)); @@ -533,7 +535,8 @@ int main(int argc, char *argv[]) fprintf(stdout, "%s", fs); continue; } - fprintf(stdout, "%s%ld", fs, (long)cache[point].value[i]); + fprintf(stdout, "%s%ld", fs, + (long)cache[point].value[i]); cache[point].dvalue[i] = cache[point].value[i]; } else { /* FCELL or DCELL */ @@ -554,9 +557,9 @@ int main(int argc, char *argv[]) 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])); + 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]); } @@ -567,21 +570,27 @@ int main(int argc, char *argv[]) point_object = json_object(point_value); if (flg.cat->answer) { - json_object_set_number(point_object, "cat", cache[point].cat); + json_object_set_number(point_object, "cat", + cache[point].cat); } - json_object_set_string(point_object, "easting", cache[point].east_buf); - json_object_set_string(point_object, "northing", cache[point].north_buf); - json_object_set_string(point_object, "site_name", cache[point].lab_buf); + json_object_set_string(point_object, "easting", + cache[point].east_buf); + 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++) { if (out_type[i] == CELL_TYPE) { if (Rast_is_c_null_value(&cache[point].value[i])) { json_object_set_null(point_object, names[i]); if (flg.label->answer) - json_object_set_null(point_object, label_names[i]); + json_object_set_null(point_object, + label_names[i]); if (flg.color->answer) - json_object_set_null(point_object, color_names[i]); + json_object_set_null(point_object, + color_names[i]); continue; } json_object_set_number(point_object, names[i], @@ -592,18 +601,21 @@ int main(int argc, char *argv[]) if (Rast_is_d_null_value(&cache[point].dvalue[i])) { json_object_set_null(point_object, names[i]); if (flg.label->answer) - json_object_set_null(point_object, label_names[i]); + json_object_set_null(point_object, + label_names[i]); if (flg.color->answer) - json_object_set_null(point_object, color_names[i]); + json_object_set_null(point_object, + color_names[i]); continue; } - json_object_set_number(point_object, names[i], cache[point].dvalue[i]); + json_object_set_number(point_object, names[i], + cache[point].dvalue[i]); } if (flg.label->answer) json_object_set_string( point_object, label_names[i], - Rast_get_d_cat(&(cache[point].dvalue[i]), &cats[i]) - ); + Rast_get_d_cat(&(cache[point].dvalue[i]), + &cats[i])); if (flg.color->answer) json_object_set_string(point_object, color_names[i], cache[point].clr_buf[i]); From 80426d414285179298819898fdd6f86fbbe31868 Mon Sep 17 00:00:00 2001 From: Kriti Birda Date: Sun, 24 Mar 2024 20:11:11 +0530 Subject: [PATCH 3/5] fix python black formatting --- raster/r.what/testsuite/test_r_what.py | 938 ++++++++++++++++++++----- 1 file changed, 761 insertions(+), 177 deletions(-) diff --git a/raster/r.what/testsuite/test_r_what.py b/raster/r.what/testsuite/test_r_what.py index a05fdcbde1a..9394de54e36 100644 --- a/raster/r.what/testsuite/test_r_what.py +++ b/raster/r.what/testsuite/test_r_what.py @@ -80,64 +80,354 @@ class TestRasterWhat(TestCase): 332533.5941495|242831.139883875||121 """ reference_points_json = [ - {"easting": "145096.8591495", "northing": "154534.264883875", "site_name": "", "boundary_county_500m": 39}, - {"easting": "616341.4371495", "northing": "146049.750883875", "site_name": "", "boundary_county_500m": 51}, - {"easting": "410595.7191495", "northing": "174301.828883875", "site_name": "", "boundary_county_500m": 71}, - {"easting": "734153.6871495", "northing": "169168.437883875", "site_name": "", "boundary_county_500m": 107}, - {"easting": "706338.2501495", "northing": "54889.417883875", "site_name": "", "boundary_county_500m": 129}, - {"easting": "758009.7501495", "northing": "112019.898883875", "site_name": "", "boundary_county_500m": 133}, - {"easting": "754002.7501495", "northing": "200902.234883875", "site_name": "", "boundary_county_500m": 147}, - {"easting": "704771.7501495", "northing": "183364.484883875", "site_name": "", "boundary_county_500m": 191}, - {"easting": "399187.0631495", "northing": "220018.859883875", "site_name": "", "boundary_county_500m": 35}, - {"easting": "685098.9371495", "northing": "33282.089883875", "site_name": "", "boundary_county_500m": 19}, - {"easting": "577750.8131495", "northing": "257153.109883875", "site_name": "", "boundary_county_500m": 1}, - {"easting": "794095.5621495", "northing": "199742.671883875", "site_name": "", "boundary_county_500m": 13}, - {"easting": "634688.2501495", "northing": "100629.616883875", "site_name": "", "boundary_county_500m": 17}, - {"easting": "287638.7811495", "northing": "207582.624883875", "site_name": "", "boundary_county_500m": 21}, - {"easting": "366218.5321495", "northing": "222940.625883875", "site_name": "", "boundary_county_500m": 23}, - {"easting": "385212.4371495", "northing": "236593.109883875", "site_name": "", "boundary_county_500m": 27}, - {"easting": "628137.4371495", "northing": "63995.550883875", "site_name": "", "boundary_county_500m": 47}, - {"easting": "782600.5631495", "northing": "152698.890883875", "site_name": "", "boundary_county_500m": 49}, - {"easting": "502813.9381495", "northing": "235232.577883875", "site_name": "", "boundary_county_500m": 57}, - {"easting": "705922.6251495", "northing": "136589.359883875", "site_name": "", "boundary_county_500m": 61}, - {"easting": "620397.8131495", "northing": "246847.640883875", "site_name": "", "boundary_county_500m": 63}, - {"easting": "738465.3751495", "northing": "237233.983883875", "site_name": "", "boundary_county_500m": 65}, - {"easting": "708944.7501495", "northing": "247632.296883875", "site_name": "", "boundary_county_500m": 127}, - {"easting": "526666.6871495", "northing": "249780.312883875", "site_name": "", "boundary_county_500m": 81}, - {"easting": "733439.3741495", "northing": "298005.311883875", "site_name": "", "boundary_county_500m": 83}, - {"easting": "253886.0321495", "northing": "204702.171883875", "site_name": "", "boundary_county_500m": 87}, - {"easting": "298337.5621495", "northing": "178131.233883875", "site_name": "", "boundary_county_500m": 89}, - {"easting": "788989.3121495", "northing": "284544.562883875", "site_name": "", "boundary_county_500m": 91}, - {"easting": "438378.3431495", "northing": "227157.890883875", "site_name": "", "boundary_county_500m": 97}, - {"easting": "227406.0621495", "northing": "190462.640883875", "site_name": "", "boundary_county_500m": 99}, - {"easting": "668782.8121495", "northing": "195518.718883875", "site_name": "", "boundary_county_500m": 101}, - {"easting": "596325.2501495", "northing": "190688.374883875", "site_name": "", "boundary_county_500m": 105}, - {"easting": "777034.8131495", "northing": "229280.030883875", "site_name": "", "boundary_county_500m": 117}, - {"easting": "337714.9681495", "northing": "219991.202883875", "site_name": "", "boundary_county_500m": 111}, - {"easting": "442913.7181495", "northing": "164364.608883875", "site_name": "", "boundary_county_500m": 119}, - {"easting": "528779.3741495", "northing": "179793.890883875", "site_name": "", "boundary_county_500m": 123}, - {"easting": "571926.0621495", "northing": "162885.202883875", "site_name": "", "boundary_county_500m": 125}, - {"easting": "801852.0621495", "northing": "154744.077883875", "site_name": "", "boundary_county_500m": 137}, - {"easting": "611371.9381495", "northing": "295204.467883875", "site_name": "", "boundary_county_500m": 145}, - {"easting": "534602.9371495", "northing": "213605.812883875", "site_name": "", "boundary_county_500m": 151}, - {"easting": "608960.3121495", "northing": "102612.546883875", "site_name": "", "boundary_county_500m": 155}, - {"easting": "541667.1251495", "northing": "293002.124883875", "site_name": "", "boundary_county_500m": 157}, - {"easting": "475819.8131495", "northing": "210685.359883875", "site_name": "", "boundary_county_500m": 159}, - {"easting": "343685.3441495", "northing": "182222.358883875", "site_name": "", "boundary_county_500m": 161}, - {"easting": "670160.3121495", "northing": "140070.249883875", "site_name": "", "boundary_county_500m": 163}, - {"easting": "497214.4691495", "northing": "177053.639883875", "site_name": "", "boundary_county_500m": 167}, - {"easting": "455564.7811495", "northing": "293568.937883875", "site_name": "", "boundary_county_500m": 171}, - {"easting": "636209.2501495", "northing": "210858.014883875", "site_name": "", "boundary_county_500m": 183}, - {"easting": "413565.1871495", "northing": "267534.562883875", "site_name": "", "boundary_county_500m": 193}, - {"easting": "709485.0001495", "northing": "220209.202883875", "site_name": "", "boundary_county_500m": 195}, - {"easting": "815182.1881495", "northing": "110335.429883875", "site_name": "", "boundary_county_500m": 31}, - {"easting": "495151.3131495", "northing": "258002.374883875", "site_name": "", "boundary_county_500m": 67}, - {"easting": "859129.4371495", "northing": "288849.968883875", "site_name": "", "boundary_county_500m": 139}, - {"easting": "545200.0621495", "northing": "127391.304883875", "site_name": "", "boundary_county_500m": 153}, - {"easting": "655627.6871495", "northing": "285597.342883875", "site_name": "", "boundary_county_500m": 181}, - {"easting": "382174.7811495", "northing": "171687.280883875", "site_name": "", "boundary_county_500m": 45}, - {"easting": "511235.1561495", "northing": "135372.358883875", "site_name": "", "boundary_county_500m": 7}, - {"easting": "332533.5941495", "northing": "242831.139883875", "site_name": "", "boundary_county_500m": 121} + { + "easting": "145096.8591495", + "northing": "154534.264883875", + "site_name": "", + "boundary_county_500m": 39, + }, + { + "easting": "616341.4371495", + "northing": "146049.750883875", + "site_name": "", + "boundary_county_500m": 51, + }, + { + "easting": "410595.7191495", + "northing": "174301.828883875", + "site_name": "", + "boundary_county_500m": 71, + }, + { + "easting": "734153.6871495", + "northing": "169168.437883875", + "site_name": "", + "boundary_county_500m": 107, + }, + { + "easting": "706338.2501495", + "northing": "54889.417883875", + "site_name": "", + "boundary_county_500m": 129, + }, + { + "easting": "758009.7501495", + "northing": "112019.898883875", + "site_name": "", + "boundary_county_500m": 133, + }, + { + "easting": "754002.7501495", + "northing": "200902.234883875", + "site_name": "", + "boundary_county_500m": 147, + }, + { + "easting": "704771.7501495", + "northing": "183364.484883875", + "site_name": "", + "boundary_county_500m": 191, + }, + { + "easting": "399187.0631495", + "northing": "220018.859883875", + "site_name": "", + "boundary_county_500m": 35, + }, + { + "easting": "685098.9371495", + "northing": "33282.089883875", + "site_name": "", + "boundary_county_500m": 19, + }, + { + "easting": "577750.8131495", + "northing": "257153.109883875", + "site_name": "", + "boundary_county_500m": 1, + }, + { + "easting": "794095.5621495", + "northing": "199742.671883875", + "site_name": "", + "boundary_county_500m": 13, + }, + { + "easting": "634688.2501495", + "northing": "100629.616883875", + "site_name": "", + "boundary_county_500m": 17, + }, + { + "easting": "287638.7811495", + "northing": "207582.624883875", + "site_name": "", + "boundary_county_500m": 21, + }, + { + "easting": "366218.5321495", + "northing": "222940.625883875", + "site_name": "", + "boundary_county_500m": 23, + }, + { + "easting": "385212.4371495", + "northing": "236593.109883875", + "site_name": "", + "boundary_county_500m": 27, + }, + { + "easting": "628137.4371495", + "northing": "63995.550883875", + "site_name": "", + "boundary_county_500m": 47, + }, + { + "easting": "782600.5631495", + "northing": "152698.890883875", + "site_name": "", + "boundary_county_500m": 49, + }, + { + "easting": "502813.9381495", + "northing": "235232.577883875", + "site_name": "", + "boundary_county_500m": 57, + }, + { + "easting": "705922.6251495", + "northing": "136589.359883875", + "site_name": "", + "boundary_county_500m": 61, + }, + { + "easting": "620397.8131495", + "northing": "246847.640883875", + "site_name": "", + "boundary_county_500m": 63, + }, + { + "easting": "738465.3751495", + "northing": "237233.983883875", + "site_name": "", + "boundary_county_500m": 65, + }, + { + "easting": "708944.7501495", + "northing": "247632.296883875", + "site_name": "", + "boundary_county_500m": 127, + }, + { + "easting": "526666.6871495", + "northing": "249780.312883875", + "site_name": "", + "boundary_county_500m": 81, + }, + { + "easting": "733439.3741495", + "northing": "298005.311883875", + "site_name": "", + "boundary_county_500m": 83, + }, + { + "easting": "253886.0321495", + "northing": "204702.171883875", + "site_name": "", + "boundary_county_500m": 87, + }, + { + "easting": "298337.5621495", + "northing": "178131.233883875", + "site_name": "", + "boundary_county_500m": 89, + }, + { + "easting": "788989.3121495", + "northing": "284544.562883875", + "site_name": "", + "boundary_county_500m": 91, + }, + { + "easting": "438378.3431495", + "northing": "227157.890883875", + "site_name": "", + "boundary_county_500m": 97, + }, + { + "easting": "227406.0621495", + "northing": "190462.640883875", + "site_name": "", + "boundary_county_500m": 99, + }, + { + "easting": "668782.8121495", + "northing": "195518.718883875", + "site_name": "", + "boundary_county_500m": 101, + }, + { + "easting": "596325.2501495", + "northing": "190688.374883875", + "site_name": "", + "boundary_county_500m": 105, + }, + { + "easting": "777034.8131495", + "northing": "229280.030883875", + "site_name": "", + "boundary_county_500m": 117, + }, + { + "easting": "337714.9681495", + "northing": "219991.202883875", + "site_name": "", + "boundary_county_500m": 111, + }, + { + "easting": "442913.7181495", + "northing": "164364.608883875", + "site_name": "", + "boundary_county_500m": 119, + }, + { + "easting": "528779.3741495", + "northing": "179793.890883875", + "site_name": "", + "boundary_county_500m": 123, + }, + { + "easting": "571926.0621495", + "northing": "162885.202883875", + "site_name": "", + "boundary_county_500m": 125, + }, + { + "easting": "801852.0621495", + "northing": "154744.077883875", + "site_name": "", + "boundary_county_500m": 137, + }, + { + "easting": "611371.9381495", + "northing": "295204.467883875", + "site_name": "", + "boundary_county_500m": 145, + }, + { + "easting": "534602.9371495", + "northing": "213605.812883875", + "site_name": "", + "boundary_county_500m": 151, + }, + { + "easting": "608960.3121495", + "northing": "102612.546883875", + "site_name": "", + "boundary_county_500m": 155, + }, + { + "easting": "541667.1251495", + "northing": "293002.124883875", + "site_name": "", + "boundary_county_500m": 157, + }, + { + "easting": "475819.8131495", + "northing": "210685.359883875", + "site_name": "", + "boundary_county_500m": 159, + }, + { + "easting": "343685.3441495", + "northing": "182222.358883875", + "site_name": "", + "boundary_county_500m": 161, + }, + { + "easting": "670160.3121495", + "northing": "140070.249883875", + "site_name": "", + "boundary_county_500m": 163, + }, + { + "easting": "497214.4691495", + "northing": "177053.639883875", + "site_name": "", + "boundary_county_500m": 167, + }, + { + "easting": "455564.7811495", + "northing": "293568.937883875", + "site_name": "", + "boundary_county_500m": 171, + }, + { + "easting": "636209.2501495", + "northing": "210858.014883875", + "site_name": "", + "boundary_county_500m": 183, + }, + { + "easting": "413565.1871495", + "northing": "267534.562883875", + "site_name": "", + "boundary_county_500m": 193, + }, + { + "easting": "709485.0001495", + "northing": "220209.202883875", + "site_name": "", + "boundary_county_500m": 195, + }, + { + "easting": "815182.1881495", + "northing": "110335.429883875", + "site_name": "", + "boundary_county_500m": 31, + }, + { + "easting": "495151.3131495", + "northing": "258002.374883875", + "site_name": "", + "boundary_county_500m": 67, + }, + { + "easting": "859129.4371495", + "northing": "288849.968883875", + "site_name": "", + "boundary_county_500m": 139, + }, + { + "easting": "545200.0621495", + "northing": "127391.304883875", + "site_name": "", + "boundary_county_500m": 153, + }, + { + "easting": "655627.6871495", + "northing": "285597.342883875", + "site_name": "", + "boundary_county_500m": 181, + }, + { + "easting": "382174.7811495", + "northing": "171687.280883875", + "site_name": "", + "boundary_county_500m": 45, + }, + { + "easting": "511235.1561495", + "northing": "135372.358883875", + "site_name": "", + "boundary_county_500m": 7, + }, + { + "easting": "332533.5941495", + "northing": "242831.139883875", + "site_name": "", + "boundary_county_500m": 121, + }, ] refrence_coordinates = """633614.08|224125.12||2|209.5939 632972.36|225382.87||15|140.7571 @@ -439,122 +729,412 @@ class TestRasterWhat(TestCase): 332533.5941495|242831.139883875||121|010:000:255 """ reference_flag_r_json = [ - {"easting": "145096.8591495", "northing": "154534.264883875", "site_name": '', "boundary_county_500m": 39, - "boundary_county_500m_color": "006:255:000"}, - {"easting": "616341.4371495", "northing": "146049.750883875", "site_name": '', "boundary_county_500m": 51, - "boundary_county_500m_color": "000:255:071"}, - {"easting": "410595.7191495", "northing": "174301.828883875", "site_name": '', "boundary_county_500m": 71, - "boundary_county_500m_color": "000:255:199"}, - {"easting": "734153.6871495", "northing": "169168.437883875", "site_name": '', "boundary_county_500m": 107, - "boundary_county_500m_color": "000:080:255"}, - {"easting": "706338.2501495", "northing": "54889.417883875", "site_name": '', "boundary_county_500m": 129, - "boundary_county_500m_color": "061:000:255"}, - {"easting": "758009.7501495", "northing": "112019.898883875", "site_name": '', "boundary_county_500m": 133, - "boundary_county_500m_color": "087:000:255"}, - {"easting": "754002.7501495", "northing": "200902.234883875", "site_name": '', "boundary_county_500m": 147, - "boundary_county_500m_color": "176:000:255"}, - {"easting": "704771.7501495", "northing": "183364.484883875", "site_name": '', "boundary_county_500m": 191, - "boundary_county_500m_color": "255:000:052"}, - {"easting": "399187.0631495", "northing": "220018.859883875", "site_name": '', "boundary_county_500m": 35, - "boundary_county_500m_color": "031:255:000"}, - {"easting": "685098.9371495", "northing": "33282.089883875", "site_name": '', "boundary_county_500m": 19, - "boundary_county_500m_color": "134:255:000"}, - {"easting": "577750.8131495", "northing": "257153.109883875", "site_name": '', "boundary_county_500m": 1, - "boundary_county_500m_color": "249:255:000"}, - {"easting": "794095.5621495", "northing": "199742.671883875", "site_name": '', "boundary_county_500m": 13, - "boundary_county_500m_color": "172:255:000"}, - {"easting": "634688.2501495", "northing": "100629.616883875", "site_name": '', "boundary_county_500m": 17, - "boundary_county_500m_color": "147:255:000"}, - {"easting": "287638.7811495", "northing": "207582.624883875", "site_name": '', "boundary_county_500m": 21, - "boundary_county_500m_color": "121:255:000"}, - {"easting": "366218.5321495", "northing": "222940.625883875", "site_name": '', "boundary_county_500m": 23, - "boundary_county_500m_color": "108:255:000"}, - {"easting": "385212.4371495", "northing": "236593.109883875", "site_name": '', "boundary_county_500m": 27, - "boundary_county_500m_color": "083:255:000"}, - {"easting": "628137.4371495", "northing": "63995.550883875", "site_name": '', "boundary_county_500m": 47, - "boundary_county_500m_color": "000:255:046"}, - {"easting": "782600.5631495", "northing": "152698.890883875", "site_name": '', "boundary_county_500m": 49, - "boundary_county_500m_color": "000:255:058"}, - {"easting": "502813.9381495", "northing": "235232.577883875", "site_name": '', "boundary_county_500m": 57, - "boundary_county_500m_color": "000:255:110"}, - {"easting": "705922.6251495", "northing": "136589.359883875", "site_name": '', "boundary_county_500m": 61, - "boundary_county_500m_color": "000:255:135"}, - {"easting": "620397.8131495", "northing": "246847.640883875", "site_name": '', "boundary_county_500m": 63, - "boundary_county_500m_color": "000:255:148"}, - {"easting": "738465.3751495", "northing": "237233.983883875", "site_name": '', "boundary_county_500m": 65, - "boundary_county_500m_color": "000:255:161"}, - {"easting": "708944.7501495", "northing": "247632.296883875", "site_name": '', "boundary_county_500m": 127, - "boundary_county_500m_color": "048:000:255"}, - {"easting": "526666.6871495", "northing": "249780.312883875", "site_name": '', "boundary_county_500m": 81, - "boundary_county_500m_color": "000:247:255"}, - {"easting": "733439.3741495", "northing": "298005.311883875", "site_name": '', "boundary_county_500m": 83, - "boundary_county_500m_color": "000:234:255"}, - {"easting": "253886.0321495", "northing": "204702.171883875", "site_name": '', "boundary_county_500m": 87, - "boundary_county_500m_color": "000:208:255"}, - {"easting": "298337.5621495", "northing": "178131.233883875", "site_name": '', "boundary_county_500m": 89, - "boundary_county_500m_color": "000:195:255"}, - {"easting": "788989.3121495", "northing": "284544.562883875", "site_name": '', "boundary_county_500m": 91, - "boundary_county_500m_color": "000:182:255"}, - {"easting": "438378.3431495", "northing": "227157.890883875", "site_name": '', "boundary_county_500m": 97, - "boundary_county_500m_color": "000:144:255"}, - {"easting": "227406.0621495", "northing": "190462.640883875", "site_name": '', "boundary_county_500m": 99, - "boundary_county_500m_color": "000:131:255"}, - {"easting": "668782.8121495", "northing": "195518.718883875", "site_name": '', "boundary_county_500m": 101, - "boundary_county_500m_color": "000:118:255"}, - {"easting": "596325.2501495", "northing": "190688.374883875", "site_name": '', "boundary_county_500m": 105, - "boundary_county_500m_color": "000:093:255"}, - {"easting": "777034.8131495", "northing": "229280.030883875", "site_name": '', "boundary_county_500m": 117, - "boundary_county_500m_color": "000:016:255"}, - {"easting": "337714.9681495", "northing": "219991.202883875", "site_name": '', "boundary_county_500m": 111, - "boundary_county_500m_color": "000:054:255"}, - {"easting": "442913.7181495", "northing": "164364.608883875", "site_name": '', "boundary_county_500m": 119, - "boundary_county_500m_color": "000:003:255"}, - {"easting": "528779.3741495", "northing": "179793.890883875", "site_name": '', "boundary_county_500m": 123, - "boundary_county_500m_color": "023:000:255"}, - {"easting": "571926.0621495", "northing": "162885.202883875", "site_name": '', "boundary_county_500m": 125, - "boundary_county_500m_color": "035:000:255"}, - {"easting": "801852.0621495", "northing": "154744.077883875", "site_name": '', "boundary_county_500m": 137, - "boundary_county_500m_color": "112:000:255"}, - {"easting": "611371.9381495", "northing": "295204.467883875", "site_name": '', "boundary_county_500m": 145, - "boundary_county_500m_color": "164:000:255"}, - {"easting": "534602.9371495", "northing": "213605.812883875", "site_name": '', "boundary_county_500m": 151, - "boundary_county_500m_color": "202:000:255"}, - {"easting": "608960.3121495", "northing": "102612.546883875", "site_name": '', "boundary_county_500m": 155, - "boundary_county_500m_color": "228:000:255"}, - {"easting": "541667.1251495", "northing": "293002.124883875", "site_name": '', "boundary_county_500m": 157, - "boundary_county_500m_color": "240:000:255"}, - {"easting": "475819.8131495", "northing": "210685.359883875", "site_name": '', "boundary_county_500m": 159, - "boundary_county_500m_color": "253:000:255"}, - {"easting": "343685.3441495", "northing": "182222.358883875", "site_name": '', "boundary_county_500m": 161, - "boundary_county_500m_color": "255:000:244"}, - {"easting": "670160.3121495", "northing": "140070.249883875", "site_name": '', "boundary_county_500m": 163, - "boundary_county_500m_color": "255:000:231"}, - {"easting": "497214.4691495", "northing": "177053.639883875", "site_name": '', "boundary_county_500m": 167, - "boundary_county_500m_color": "255:000:206"}, - {"easting": "455564.7811495", "northing": "293568.937883875", "site_name": '', "boundary_county_500m": 171, - "boundary_county_500m_color": "255:000:180"}, - {"easting": "636209.2501495", "northing": "210858.014883875", "site_name": '', "boundary_county_500m": 183, - "boundary_county_500m_color": "255:000:103"}, - {"easting": "413565.1871495", "northing": "267534.562883875", "site_name": '', "boundary_county_500m": 193, - "boundary_county_500m_color": "255:000:039"}, - {"easting": "709485.0001495", "northing": "220209.202883875", "site_name": '', "boundary_county_500m": 195, - "boundary_county_500m_color": "255:000:026"}, - {"easting": "815182.1881495", "northing": "110335.429883875", "site_name": '', "boundary_county_500m": 31, - "boundary_county_500m_color": "057:255:000"}, - {"easting": "495151.3131495", "northing": "258002.374883875", "site_name": '', "boundary_county_500m": 67, - "boundary_county_500m_color": "000:255:174"}, - {"easting": "859129.4371495", "northing": "288849.968883875", "site_name": '', "boundary_county_500m": 139, - "boundary_county_500m_color": "125:000:255"}, - {"easting": "545200.0621495", "northing": "127391.304883875", "site_name": '', "boundary_county_500m": 153, - "boundary_county_500m_color": "215:000:255"}, - {"easting": "655627.6871495", "northing": "285597.342883875", "site_name": '', "boundary_county_500m": 181, - "boundary_county_500m_color": "255:000:116"}, - {"easting": "382174.7811495", "northing": "171687.280883875", "site_name": '', "boundary_county_500m": 45, - "boundary_county_500m_color": "000:255:033"}, - {"easting": "511235.1561495", "northing": "135372.358883875", "site_name": '', "boundary_county_500m": 7, - "boundary_county_500m_color": "211:255:000"}, - {"easting": "332533.5941495", "northing": "242831.139883875", "site_name": '', "boundary_county_500m": 121, - "boundary_county_500m_color": "010:000:255"} + { + "easting": "145096.8591495", + "northing": "154534.264883875", + "site_name": "", + "boundary_county_500m": 39, + "boundary_county_500m_color": "006:255:000", + }, + { + "easting": "616341.4371495", + "northing": "146049.750883875", + "site_name": "", + "boundary_county_500m": 51, + "boundary_county_500m_color": "000:255:071", + }, + { + "easting": "410595.7191495", + "northing": "174301.828883875", + "site_name": "", + "boundary_county_500m": 71, + "boundary_county_500m_color": "000:255:199", + }, + { + "easting": "734153.6871495", + "northing": "169168.437883875", + "site_name": "", + "boundary_county_500m": 107, + "boundary_county_500m_color": "000:080:255", + }, + { + "easting": "706338.2501495", + "northing": "54889.417883875", + "site_name": "", + "boundary_county_500m": 129, + "boundary_county_500m_color": "061:000:255", + }, + { + "easting": "758009.7501495", + "northing": "112019.898883875", + "site_name": "", + "boundary_county_500m": 133, + "boundary_county_500m_color": "087:000:255", + }, + { + "easting": "754002.7501495", + "northing": "200902.234883875", + "site_name": "", + "boundary_county_500m": 147, + "boundary_county_500m_color": "176:000:255", + }, + { + "easting": "704771.7501495", + "northing": "183364.484883875", + "site_name": "", + "boundary_county_500m": 191, + "boundary_county_500m_color": "255:000:052", + }, + { + "easting": "399187.0631495", + "northing": "220018.859883875", + "site_name": "", + "boundary_county_500m": 35, + "boundary_county_500m_color": "031:255:000", + }, + { + "easting": "685098.9371495", + "northing": "33282.089883875", + "site_name": "", + "boundary_county_500m": 19, + "boundary_county_500m_color": "134:255:000", + }, + { + "easting": "577750.8131495", + "northing": "257153.109883875", + "site_name": "", + "boundary_county_500m": 1, + "boundary_county_500m_color": "249:255:000", + }, + { + "easting": "794095.5621495", + "northing": "199742.671883875", + "site_name": "", + "boundary_county_500m": 13, + "boundary_county_500m_color": "172:255:000", + }, + { + "easting": "634688.2501495", + "northing": "100629.616883875", + "site_name": "", + "boundary_county_500m": 17, + "boundary_county_500m_color": "147:255:000", + }, + { + "easting": "287638.7811495", + "northing": "207582.624883875", + "site_name": "", + "boundary_county_500m": 21, + "boundary_county_500m_color": "121:255:000", + }, + { + "easting": "366218.5321495", + "northing": "222940.625883875", + "site_name": "", + "boundary_county_500m": 23, + "boundary_county_500m_color": "108:255:000", + }, + { + "easting": "385212.4371495", + "northing": "236593.109883875", + "site_name": "", + "boundary_county_500m": 27, + "boundary_county_500m_color": "083:255:000", + }, + { + "easting": "628137.4371495", + "northing": "63995.550883875", + "site_name": "", + "boundary_county_500m": 47, + "boundary_county_500m_color": "000:255:046", + }, + { + "easting": "782600.5631495", + "northing": "152698.890883875", + "site_name": "", + "boundary_county_500m": 49, + "boundary_county_500m_color": "000:255:058", + }, + { + "easting": "502813.9381495", + "northing": "235232.577883875", + "site_name": "", + "boundary_county_500m": 57, + "boundary_county_500m_color": "000:255:110", + }, + { + "easting": "705922.6251495", + "northing": "136589.359883875", + "site_name": "", + "boundary_county_500m": 61, + "boundary_county_500m_color": "000:255:135", + }, + { + "easting": "620397.8131495", + "northing": "246847.640883875", + "site_name": "", + "boundary_county_500m": 63, + "boundary_county_500m_color": "000:255:148", + }, + { + "easting": "738465.3751495", + "northing": "237233.983883875", + "site_name": "", + "boundary_county_500m": 65, + "boundary_county_500m_color": "000:255:161", + }, + { + "easting": "708944.7501495", + "northing": "247632.296883875", + "site_name": "", + "boundary_county_500m": 127, + "boundary_county_500m_color": "048:000:255", + }, + { + "easting": "526666.6871495", + "northing": "249780.312883875", + "site_name": "", + "boundary_county_500m": 81, + "boundary_county_500m_color": "000:247:255", + }, + { + "easting": "733439.3741495", + "northing": "298005.311883875", + "site_name": "", + "boundary_county_500m": 83, + "boundary_county_500m_color": "000:234:255", + }, + { + "easting": "253886.0321495", + "northing": "204702.171883875", + "site_name": "", + "boundary_county_500m": 87, + "boundary_county_500m_color": "000:208:255", + }, + { + "easting": "298337.5621495", + "northing": "178131.233883875", + "site_name": "", + "boundary_county_500m": 89, + "boundary_county_500m_color": "000:195:255", + }, + { + "easting": "788989.3121495", + "northing": "284544.562883875", + "site_name": "", + "boundary_county_500m": 91, + "boundary_county_500m_color": "000:182:255", + }, + { + "easting": "438378.3431495", + "northing": "227157.890883875", + "site_name": "", + "boundary_county_500m": 97, + "boundary_county_500m_color": "000:144:255", + }, + { + "easting": "227406.0621495", + "northing": "190462.640883875", + "site_name": "", + "boundary_county_500m": 99, + "boundary_county_500m_color": "000:131:255", + }, + { + "easting": "668782.8121495", + "northing": "195518.718883875", + "site_name": "", + "boundary_county_500m": 101, + "boundary_county_500m_color": "000:118:255", + }, + { + "easting": "596325.2501495", + "northing": "190688.374883875", + "site_name": "", + "boundary_county_500m": 105, + "boundary_county_500m_color": "000:093:255", + }, + { + "easting": "777034.8131495", + "northing": "229280.030883875", + "site_name": "", + "boundary_county_500m": 117, + "boundary_county_500m_color": "000:016:255", + }, + { + "easting": "337714.9681495", + "northing": "219991.202883875", + "site_name": "", + "boundary_county_500m": 111, + "boundary_county_500m_color": "000:054:255", + }, + { + "easting": "442913.7181495", + "northing": "164364.608883875", + "site_name": "", + "boundary_county_500m": 119, + "boundary_county_500m_color": "000:003:255", + }, + { + "easting": "528779.3741495", + "northing": "179793.890883875", + "site_name": "", + "boundary_county_500m": 123, + "boundary_county_500m_color": "023:000:255", + }, + { + "easting": "571926.0621495", + "northing": "162885.202883875", + "site_name": "", + "boundary_county_500m": 125, + "boundary_county_500m_color": "035:000:255", + }, + { + "easting": "801852.0621495", + "northing": "154744.077883875", + "site_name": "", + "boundary_county_500m": 137, + "boundary_county_500m_color": "112:000:255", + }, + { + "easting": "611371.9381495", + "northing": "295204.467883875", + "site_name": "", + "boundary_county_500m": 145, + "boundary_county_500m_color": "164:000:255", + }, + { + "easting": "534602.9371495", + "northing": "213605.812883875", + "site_name": "", + "boundary_county_500m": 151, + "boundary_county_500m_color": "202:000:255", + }, + { + "easting": "608960.3121495", + "northing": "102612.546883875", + "site_name": "", + "boundary_county_500m": 155, + "boundary_county_500m_color": "228:000:255", + }, + { + "easting": "541667.1251495", + "northing": "293002.124883875", + "site_name": "", + "boundary_county_500m": 157, + "boundary_county_500m_color": "240:000:255", + }, + { + "easting": "475819.8131495", + "northing": "210685.359883875", + "site_name": "", + "boundary_county_500m": 159, + "boundary_county_500m_color": "253:000:255", + }, + { + "easting": "343685.3441495", + "northing": "182222.358883875", + "site_name": "", + "boundary_county_500m": 161, + "boundary_county_500m_color": "255:000:244", + }, + { + "easting": "670160.3121495", + "northing": "140070.249883875", + "site_name": "", + "boundary_county_500m": 163, + "boundary_county_500m_color": "255:000:231", + }, + { + "easting": "497214.4691495", + "northing": "177053.639883875", + "site_name": "", + "boundary_county_500m": 167, + "boundary_county_500m_color": "255:000:206", + }, + { + "easting": "455564.7811495", + "northing": "293568.937883875", + "site_name": "", + "boundary_county_500m": 171, + "boundary_county_500m_color": "255:000:180", + }, + { + "easting": "636209.2501495", + "northing": "210858.014883875", + "site_name": "", + "boundary_county_500m": 183, + "boundary_county_500m_color": "255:000:103", + }, + { + "easting": "413565.1871495", + "northing": "267534.562883875", + "site_name": "", + "boundary_county_500m": 193, + "boundary_county_500m_color": "255:000:039", + }, + { + "easting": "709485.0001495", + "northing": "220209.202883875", + "site_name": "", + "boundary_county_500m": 195, + "boundary_county_500m_color": "255:000:026", + }, + { + "easting": "815182.1881495", + "northing": "110335.429883875", + "site_name": "", + "boundary_county_500m": 31, + "boundary_county_500m_color": "057:255:000", + }, + { + "easting": "495151.3131495", + "northing": "258002.374883875", + "site_name": "", + "boundary_county_500m": 67, + "boundary_county_500m_color": "000:255:174", + }, + { + "easting": "859129.4371495", + "northing": "288849.968883875", + "site_name": "", + "boundary_county_500m": 139, + "boundary_county_500m_color": "125:000:255", + }, + { + "easting": "545200.0621495", + "northing": "127391.304883875", + "site_name": "", + "boundary_county_500m": 153, + "boundary_county_500m_color": "215:000:255", + }, + { + "easting": "655627.6871495", + "northing": "285597.342883875", + "site_name": "", + "boundary_county_500m": 181, + "boundary_county_500m_color": "255:000:116", + }, + { + "easting": "382174.7811495", + "northing": "171687.280883875", + "site_name": "", + "boundary_county_500m": 45, + "boundary_county_500m_color": "000:255:033", + }, + { + "easting": "511235.1561495", + "northing": "135372.358883875", + "site_name": "", + "boundary_county_500m": 7, + "boundary_county_500m_color": "211:255:000", + }, + { + "easting": "332533.5941495", + "northing": "242831.139883875", + "site_name": "", + "boundary_county_500m": 121, + "boundary_county_500m_color": "010:000:255", + }, ] refrence_cache = """145096.8591495|154534.264883875||39 616341.4371495|146049.750883875||51 @@ -721,8 +1301,10 @@ def test_raster_what_cache(self): ) def test_raster_what_json(self): - """Testing r.what runs successfully with input coordinates given as a vector points map and JSON output """ - module = SimpleModule("r.what", map=self.map1, points=self.points, format="json") + """Testing r.what runs successfully with input coordinates given as a vector points map and JSON output""" + module = SimpleModule( + "r.what", map=self.map1, points=self.points, format="json" + ) module.run() self.assertListEqual( json.loads(str(module.outputs.stdout)), @@ -732,7 +1314,9 @@ def test_raster_what_json(self): def test_raster_what_points_flag_r_json(self): """Testing r.what runs successfully with flag r and json output""" - module = SimpleModule("r.what", map=self.map1, points=self.points, flags="r", format="json") + module = SimpleModule( + "r.what", map=self.map1, points=self.points, flags="r", format="json" + ) module.run() self.assertListEqual( json.loads(str(module.outputs.stdout)), From d39e18aa404bb7a910768924b7bf609951b7ac9d Mon Sep 17 00:00:00 2001 From: Kriti Birda Date: Tue, 26 Mar 2024 21:50:43 +0530 Subject: [PATCH 4/5] fix changes from code review --- raster/r.what/main.c | 92 ++- raster/r.what/testsuite/test_r_what.py | 783 +------------------------ 2 files changed, 65 insertions(+), 810 deletions(-) diff --git a/raster/r.what/main.c b/raster/r.what/main.c index 747d8b791c3..375a4e1b721 100644 --- a/raster/r.what/main.c +++ b/raster/r.what/main.c @@ -95,10 +95,10 @@ int main(int argc, char *argv[]) int red, green, blue; struct GModule *module; - JSON_Value *root_value, *point_value; + JSON_Value *root_value = NULL, *point_value, *layer_value; JSON_Array *root_array; - JSON_Object *point_object; - char **names, **label_names, **color_names; + JSON_Object *point_object, *layer_object; + char **names; G_gisinit(argv[0]); @@ -275,6 +275,9 @@ int main(int argc, char *argv[]) if (strcmp(opt.format->answer, "json") == 0) { root_value = json_value_init_array(); + if (root_value == NULL) { + G_fatal_error(_("Failed to initialize JSON array. Out of memory?")); + } root_array = json_array(root_value); } @@ -304,22 +307,11 @@ int main(int argc, char *argv[]) else if (strcmp(opt.format->answer, "json") == 0) { i = 0; names = G_malloc(nfiles * sizeof(char *)); - label_names = G_malloc(nfiles * sizeof(char *)); - color_names = G_malloc(nfiles * sizeof(char *)); ptr = opt.input->answers; for (; *ptr != NULL; ptr++) { names[i] = G_malloc(GNAME_MAX); - label_names[i] = G_malloc(GNAME_MAX + 6 /* 6 for _label suffix */); - color_names[i] = G_malloc(GNAME_MAX + 6 /* 6 for _color suffix */); - strcpy(names[i], *ptr); - - if (flg.label->answer) - sprintf(label_names[i], "%s_label", *ptr); - - if (flg.color->answer) - sprintf(color_names[i], "%s_color", *ptr); i++; } } @@ -582,45 +574,39 @@ int main(int argc, char *argv[]) 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])) { - json_object_set_null(point_object, names[i]); - if (flg.label->answer) - json_object_set_null(point_object, - label_names[i]); - if (flg.color->answer) - json_object_set_null(point_object, - color_names[i]); - continue; - } - json_object_set_number(point_object, names[i], - (long)cache[point].value[i]); - cache[point].dvalue[i] = cache[point].value[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) + json_object_set_null(layer_object, "label"); + if (flg.color->answer) + json_object_set_null(layer_object, "color"); } - else { /* FCELL or DCELL */ - if (Rast_is_d_null_value(&cache[point].dvalue[i])) { - json_object_set_null(point_object, names[i]); - if (flg.label->answer) - json_object_set_null(point_object, - label_names[i]); - if (flg.color->answer) - json_object_set_null(point_object, - color_names[i]); - continue; + 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]); } - json_object_set_number(point_object, names[i], - cache[point].dvalue[i]); + if (flg.label->answer) + json_object_set_string( + layer_object, "label", + Rast_get_d_cat(&(cache[point].dvalue[i]), + &cats[i])); + if (flg.color->answer) + json_object_set_string(layer_object, "color", + cache[point].clr_buf[i]); } - if (flg.label->answer) - json_object_set_string( - point_object, label_names[i], - Rast_get_d_cat(&(cache[point].dvalue[i]), - &cats[i])); - if (flg.color->answer) - json_object_set_string(point_object, color_names[i], - cache[point].clr_buf[i]); - } + json_object_set_value(point_object, names[i], layer_value); + } json_array_append_value(root_array, point_value); } } @@ -635,7 +621,13 @@ int main(int argc, char *argv[]) } if (strcmp(opt.format->answer, "json") == 0) { - fprintf(stdout, "%s\n", json_serialize_to_string(root_value)); + 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); } diff --git a/raster/r.what/testsuite/test_r_what.py b/raster/r.what/testsuite/test_r_what.py index 9394de54e36..e418bd8b6e7 100644 --- a/raster/r.what/testsuite/test_r_what.py +++ b/raster/r.what/testsuite/test_r_what.py @@ -79,356 +79,6 @@ class TestRasterWhat(TestCase): 511235.1561495|135372.358883875||7 332533.5941495|242831.139883875||121 """ - reference_points_json = [ - { - "easting": "145096.8591495", - "northing": "154534.264883875", - "site_name": "", - "boundary_county_500m": 39, - }, - { - "easting": "616341.4371495", - "northing": "146049.750883875", - "site_name": "", - "boundary_county_500m": 51, - }, - { - "easting": "410595.7191495", - "northing": "174301.828883875", - "site_name": "", - "boundary_county_500m": 71, - }, - { - "easting": "734153.6871495", - "northing": "169168.437883875", - "site_name": "", - "boundary_county_500m": 107, - }, - { - "easting": "706338.2501495", - "northing": "54889.417883875", - "site_name": "", - "boundary_county_500m": 129, - }, - { - "easting": "758009.7501495", - "northing": "112019.898883875", - "site_name": "", - "boundary_county_500m": 133, - }, - { - "easting": "754002.7501495", - "northing": "200902.234883875", - "site_name": "", - "boundary_county_500m": 147, - }, - { - "easting": "704771.7501495", - "northing": "183364.484883875", - "site_name": "", - "boundary_county_500m": 191, - }, - { - "easting": "399187.0631495", - "northing": "220018.859883875", - "site_name": "", - "boundary_county_500m": 35, - }, - { - "easting": "685098.9371495", - "northing": "33282.089883875", - "site_name": "", - "boundary_county_500m": 19, - }, - { - "easting": "577750.8131495", - "northing": "257153.109883875", - "site_name": "", - "boundary_county_500m": 1, - }, - { - "easting": "794095.5621495", - "northing": "199742.671883875", - "site_name": "", - "boundary_county_500m": 13, - }, - { - "easting": "634688.2501495", - "northing": "100629.616883875", - "site_name": "", - "boundary_county_500m": 17, - }, - { - "easting": "287638.7811495", - "northing": "207582.624883875", - "site_name": "", - "boundary_county_500m": 21, - }, - { - "easting": "366218.5321495", - "northing": "222940.625883875", - "site_name": "", - "boundary_county_500m": 23, - }, - { - "easting": "385212.4371495", - "northing": "236593.109883875", - "site_name": "", - "boundary_county_500m": 27, - }, - { - "easting": "628137.4371495", - "northing": "63995.550883875", - "site_name": "", - "boundary_county_500m": 47, - }, - { - "easting": "782600.5631495", - "northing": "152698.890883875", - "site_name": "", - "boundary_county_500m": 49, - }, - { - "easting": "502813.9381495", - "northing": "235232.577883875", - "site_name": "", - "boundary_county_500m": 57, - }, - { - "easting": "705922.6251495", - "northing": "136589.359883875", - "site_name": "", - "boundary_county_500m": 61, - }, - { - "easting": "620397.8131495", - "northing": "246847.640883875", - "site_name": "", - "boundary_county_500m": 63, - }, - { - "easting": "738465.3751495", - "northing": "237233.983883875", - "site_name": "", - "boundary_county_500m": 65, - }, - { - "easting": "708944.7501495", - "northing": "247632.296883875", - "site_name": "", - "boundary_county_500m": 127, - }, - { - "easting": "526666.6871495", - "northing": "249780.312883875", - "site_name": "", - "boundary_county_500m": 81, - }, - { - "easting": "733439.3741495", - "northing": "298005.311883875", - "site_name": "", - "boundary_county_500m": 83, - }, - { - "easting": "253886.0321495", - "northing": "204702.171883875", - "site_name": "", - "boundary_county_500m": 87, - }, - { - "easting": "298337.5621495", - "northing": "178131.233883875", - "site_name": "", - "boundary_county_500m": 89, - }, - { - "easting": "788989.3121495", - "northing": "284544.562883875", - "site_name": "", - "boundary_county_500m": 91, - }, - { - "easting": "438378.3431495", - "northing": "227157.890883875", - "site_name": "", - "boundary_county_500m": 97, - }, - { - "easting": "227406.0621495", - "northing": "190462.640883875", - "site_name": "", - "boundary_county_500m": 99, - }, - { - "easting": "668782.8121495", - "northing": "195518.718883875", - "site_name": "", - "boundary_county_500m": 101, - }, - { - "easting": "596325.2501495", - "northing": "190688.374883875", - "site_name": "", - "boundary_county_500m": 105, - }, - { - "easting": "777034.8131495", - "northing": "229280.030883875", - "site_name": "", - "boundary_county_500m": 117, - }, - { - "easting": "337714.9681495", - "northing": "219991.202883875", - "site_name": "", - "boundary_county_500m": 111, - }, - { - "easting": "442913.7181495", - "northing": "164364.608883875", - "site_name": "", - "boundary_county_500m": 119, - }, - { - "easting": "528779.3741495", - "northing": "179793.890883875", - "site_name": "", - "boundary_county_500m": 123, - }, - { - "easting": "571926.0621495", - "northing": "162885.202883875", - "site_name": "", - "boundary_county_500m": 125, - }, - { - "easting": "801852.0621495", - "northing": "154744.077883875", - "site_name": "", - "boundary_county_500m": 137, - }, - { - "easting": "611371.9381495", - "northing": "295204.467883875", - "site_name": "", - "boundary_county_500m": 145, - }, - { - "easting": "534602.9371495", - "northing": "213605.812883875", - "site_name": "", - "boundary_county_500m": 151, - }, - { - "easting": "608960.3121495", - "northing": "102612.546883875", - "site_name": "", - "boundary_county_500m": 155, - }, - { - "easting": "541667.1251495", - "northing": "293002.124883875", - "site_name": "", - "boundary_county_500m": 157, - }, - { - "easting": "475819.8131495", - "northing": "210685.359883875", - "site_name": "", - "boundary_county_500m": 159, - }, - { - "easting": "343685.3441495", - "northing": "182222.358883875", - "site_name": "", - "boundary_county_500m": 161, - }, - { - "easting": "670160.3121495", - "northing": "140070.249883875", - "site_name": "", - "boundary_county_500m": 163, - }, - { - "easting": "497214.4691495", - "northing": "177053.639883875", - "site_name": "", - "boundary_county_500m": 167, - }, - { - "easting": "455564.7811495", - "northing": "293568.937883875", - "site_name": "", - "boundary_county_500m": 171, - }, - { - "easting": "636209.2501495", - "northing": "210858.014883875", - "site_name": "", - "boundary_county_500m": 183, - }, - { - "easting": "413565.1871495", - "northing": "267534.562883875", - "site_name": "", - "boundary_county_500m": 193, - }, - { - "easting": "709485.0001495", - "northing": "220209.202883875", - "site_name": "", - "boundary_county_500m": 195, - }, - { - "easting": "815182.1881495", - "northing": "110335.429883875", - "site_name": "", - "boundary_county_500m": 31, - }, - { - "easting": "495151.3131495", - "northing": "258002.374883875", - "site_name": "", - "boundary_county_500m": 67, - }, - { - "easting": "859129.4371495", - "northing": "288849.968883875", - "site_name": "", - "boundary_county_500m": 139, - }, - { - "easting": "545200.0621495", - "northing": "127391.304883875", - "site_name": "", - "boundary_county_500m": 153, - }, - { - "easting": "655627.6871495", - "northing": "285597.342883875", - "site_name": "", - "boundary_county_500m": 181, - }, - { - "easting": "382174.7811495", - "northing": "171687.280883875", - "site_name": "", - "boundary_county_500m": 45, - }, - { - "easting": "511235.1561495", - "northing": "135372.358883875", - "site_name": "", - "boundary_county_500m": 7, - }, - { - "easting": "332533.5941495", - "northing": "242831.139883875", - "site_name": "", - "boundary_county_500m": 121, - }, - ] refrence_coordinates = """633614.08|224125.12||2|209.5939 632972.36|225382.87||15|140.7571 """ @@ -728,414 +378,6 @@ class TestRasterWhat(TestCase): 511235.1561495|135372.358883875||7|211:255:000 332533.5941495|242831.139883875||121|010:000:255 """ - reference_flag_r_json = [ - { - "easting": "145096.8591495", - "northing": "154534.264883875", - "site_name": "", - "boundary_county_500m": 39, - "boundary_county_500m_color": "006:255:000", - }, - { - "easting": "616341.4371495", - "northing": "146049.750883875", - "site_name": "", - "boundary_county_500m": 51, - "boundary_county_500m_color": "000:255:071", - }, - { - "easting": "410595.7191495", - "northing": "174301.828883875", - "site_name": "", - "boundary_county_500m": 71, - "boundary_county_500m_color": "000:255:199", - }, - { - "easting": "734153.6871495", - "northing": "169168.437883875", - "site_name": "", - "boundary_county_500m": 107, - "boundary_county_500m_color": "000:080:255", - }, - { - "easting": "706338.2501495", - "northing": "54889.417883875", - "site_name": "", - "boundary_county_500m": 129, - "boundary_county_500m_color": "061:000:255", - }, - { - "easting": "758009.7501495", - "northing": "112019.898883875", - "site_name": "", - "boundary_county_500m": 133, - "boundary_county_500m_color": "087:000:255", - }, - { - "easting": "754002.7501495", - "northing": "200902.234883875", - "site_name": "", - "boundary_county_500m": 147, - "boundary_county_500m_color": "176:000:255", - }, - { - "easting": "704771.7501495", - "northing": "183364.484883875", - "site_name": "", - "boundary_county_500m": 191, - "boundary_county_500m_color": "255:000:052", - }, - { - "easting": "399187.0631495", - "northing": "220018.859883875", - "site_name": "", - "boundary_county_500m": 35, - "boundary_county_500m_color": "031:255:000", - }, - { - "easting": "685098.9371495", - "northing": "33282.089883875", - "site_name": "", - "boundary_county_500m": 19, - "boundary_county_500m_color": "134:255:000", - }, - { - "easting": "577750.8131495", - "northing": "257153.109883875", - "site_name": "", - "boundary_county_500m": 1, - "boundary_county_500m_color": "249:255:000", - }, - { - "easting": "794095.5621495", - "northing": "199742.671883875", - "site_name": "", - "boundary_county_500m": 13, - "boundary_county_500m_color": "172:255:000", - }, - { - "easting": "634688.2501495", - "northing": "100629.616883875", - "site_name": "", - "boundary_county_500m": 17, - "boundary_county_500m_color": "147:255:000", - }, - { - "easting": "287638.7811495", - "northing": "207582.624883875", - "site_name": "", - "boundary_county_500m": 21, - "boundary_county_500m_color": "121:255:000", - }, - { - "easting": "366218.5321495", - "northing": "222940.625883875", - "site_name": "", - "boundary_county_500m": 23, - "boundary_county_500m_color": "108:255:000", - }, - { - "easting": "385212.4371495", - "northing": "236593.109883875", - "site_name": "", - "boundary_county_500m": 27, - "boundary_county_500m_color": "083:255:000", - }, - { - "easting": "628137.4371495", - "northing": "63995.550883875", - "site_name": "", - "boundary_county_500m": 47, - "boundary_county_500m_color": "000:255:046", - }, - { - "easting": "782600.5631495", - "northing": "152698.890883875", - "site_name": "", - "boundary_county_500m": 49, - "boundary_county_500m_color": "000:255:058", - }, - { - "easting": "502813.9381495", - "northing": "235232.577883875", - "site_name": "", - "boundary_county_500m": 57, - "boundary_county_500m_color": "000:255:110", - }, - { - "easting": "705922.6251495", - "northing": "136589.359883875", - "site_name": "", - "boundary_county_500m": 61, - "boundary_county_500m_color": "000:255:135", - }, - { - "easting": "620397.8131495", - "northing": "246847.640883875", - "site_name": "", - "boundary_county_500m": 63, - "boundary_county_500m_color": "000:255:148", - }, - { - "easting": "738465.3751495", - "northing": "237233.983883875", - "site_name": "", - "boundary_county_500m": 65, - "boundary_county_500m_color": "000:255:161", - }, - { - "easting": "708944.7501495", - "northing": "247632.296883875", - "site_name": "", - "boundary_county_500m": 127, - "boundary_county_500m_color": "048:000:255", - }, - { - "easting": "526666.6871495", - "northing": "249780.312883875", - "site_name": "", - "boundary_county_500m": 81, - "boundary_county_500m_color": "000:247:255", - }, - { - "easting": "733439.3741495", - "northing": "298005.311883875", - "site_name": "", - "boundary_county_500m": 83, - "boundary_county_500m_color": "000:234:255", - }, - { - "easting": "253886.0321495", - "northing": "204702.171883875", - "site_name": "", - "boundary_county_500m": 87, - "boundary_county_500m_color": "000:208:255", - }, - { - "easting": "298337.5621495", - "northing": "178131.233883875", - "site_name": "", - "boundary_county_500m": 89, - "boundary_county_500m_color": "000:195:255", - }, - { - "easting": "788989.3121495", - "northing": "284544.562883875", - "site_name": "", - "boundary_county_500m": 91, - "boundary_county_500m_color": "000:182:255", - }, - { - "easting": "438378.3431495", - "northing": "227157.890883875", - "site_name": "", - "boundary_county_500m": 97, - "boundary_county_500m_color": "000:144:255", - }, - { - "easting": "227406.0621495", - "northing": "190462.640883875", - "site_name": "", - "boundary_county_500m": 99, - "boundary_county_500m_color": "000:131:255", - }, - { - "easting": "668782.8121495", - "northing": "195518.718883875", - "site_name": "", - "boundary_county_500m": 101, - "boundary_county_500m_color": "000:118:255", - }, - { - "easting": "596325.2501495", - "northing": "190688.374883875", - "site_name": "", - "boundary_county_500m": 105, - "boundary_county_500m_color": "000:093:255", - }, - { - "easting": "777034.8131495", - "northing": "229280.030883875", - "site_name": "", - "boundary_county_500m": 117, - "boundary_county_500m_color": "000:016:255", - }, - { - "easting": "337714.9681495", - "northing": "219991.202883875", - "site_name": "", - "boundary_county_500m": 111, - "boundary_county_500m_color": "000:054:255", - }, - { - "easting": "442913.7181495", - "northing": "164364.608883875", - "site_name": "", - "boundary_county_500m": 119, - "boundary_county_500m_color": "000:003:255", - }, - { - "easting": "528779.3741495", - "northing": "179793.890883875", - "site_name": "", - "boundary_county_500m": 123, - "boundary_county_500m_color": "023:000:255", - }, - { - "easting": "571926.0621495", - "northing": "162885.202883875", - "site_name": "", - "boundary_county_500m": 125, - "boundary_county_500m_color": "035:000:255", - }, - { - "easting": "801852.0621495", - "northing": "154744.077883875", - "site_name": "", - "boundary_county_500m": 137, - "boundary_county_500m_color": "112:000:255", - }, - { - "easting": "611371.9381495", - "northing": "295204.467883875", - "site_name": "", - "boundary_county_500m": 145, - "boundary_county_500m_color": "164:000:255", - }, - { - "easting": "534602.9371495", - "northing": "213605.812883875", - "site_name": "", - "boundary_county_500m": 151, - "boundary_county_500m_color": "202:000:255", - }, - { - "easting": "608960.3121495", - "northing": "102612.546883875", - "site_name": "", - "boundary_county_500m": 155, - "boundary_county_500m_color": "228:000:255", - }, - { - "easting": "541667.1251495", - "northing": "293002.124883875", - "site_name": "", - "boundary_county_500m": 157, - "boundary_county_500m_color": "240:000:255", - }, - { - "easting": "475819.8131495", - "northing": "210685.359883875", - "site_name": "", - "boundary_county_500m": 159, - "boundary_county_500m_color": "253:000:255", - }, - { - "easting": "343685.3441495", - "northing": "182222.358883875", - "site_name": "", - "boundary_county_500m": 161, - "boundary_county_500m_color": "255:000:244", - }, - { - "easting": "670160.3121495", - "northing": "140070.249883875", - "site_name": "", - "boundary_county_500m": 163, - "boundary_county_500m_color": "255:000:231", - }, - { - "easting": "497214.4691495", - "northing": "177053.639883875", - "site_name": "", - "boundary_county_500m": 167, - "boundary_county_500m_color": "255:000:206", - }, - { - "easting": "455564.7811495", - "northing": "293568.937883875", - "site_name": "", - "boundary_county_500m": 171, - "boundary_county_500m_color": "255:000:180", - }, - { - "easting": "636209.2501495", - "northing": "210858.014883875", - "site_name": "", - "boundary_county_500m": 183, - "boundary_county_500m_color": "255:000:103", - }, - { - "easting": "413565.1871495", - "northing": "267534.562883875", - "site_name": "", - "boundary_county_500m": 193, - "boundary_county_500m_color": "255:000:039", - }, - { - "easting": "709485.0001495", - "northing": "220209.202883875", - "site_name": "", - "boundary_county_500m": 195, - "boundary_county_500m_color": "255:000:026", - }, - { - "easting": "815182.1881495", - "northing": "110335.429883875", - "site_name": "", - "boundary_county_500m": 31, - "boundary_county_500m_color": "057:255:000", - }, - { - "easting": "495151.3131495", - "northing": "258002.374883875", - "site_name": "", - "boundary_county_500m": 67, - "boundary_county_500m_color": "000:255:174", - }, - { - "easting": "859129.4371495", - "northing": "288849.968883875", - "site_name": "", - "boundary_county_500m": 139, - "boundary_county_500m_color": "125:000:255", - }, - { - "easting": "545200.0621495", - "northing": "127391.304883875", - "site_name": "", - "boundary_county_500m": 153, - "boundary_county_500m_color": "215:000:255", - }, - { - "easting": "655627.6871495", - "northing": "285597.342883875", - "site_name": "", - "boundary_county_500m": 181, - "boundary_county_500m_color": "255:000:116", - }, - { - "easting": "382174.7811495", - "northing": "171687.280883875", - "site_name": "", - "boundary_county_500m": 45, - "boundary_county_500m_color": "000:255:033", - }, - { - "easting": "511235.1561495", - "northing": "135372.358883875", - "site_name": "", - "boundary_county_500m": 7, - "boundary_county_500m_color": "211:255:000", - }, - { - "easting": "332533.5941495", - "northing": "242831.139883875", - "site_name": "", - "boundary_county_500m": 121, - "boundary_county_500m_color": "010:000:255", - }, - ] refrence_cache = """145096.8591495|154534.264883875||39 616341.4371495|146049.750883875||51 410595.7191495|174301.828883875||71 @@ -1196,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() @@ -1302,25 +563,27 @@ def test_raster_what_cache(self): 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)), - self.reference_points_json, + 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)), - self.reference_flag_r_json, + reference, "test_raster_what_cats did't run successfully", ) From b1a6c9a419e90530c7880611c49adcb7eee4a680 Mon Sep 17 00:00:00 2001 From: Kriti Birda Date: Wed, 27 Mar 2024 16:53:00 +0530 Subject: [PATCH 5/5] fix changes from code review - 2 --- raster/r.what/main.c | 43 ++++++++++++-------------- raster/r.what/testsuite/test_r_what.py | 4 +-- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/raster/r.what/main.c b/raster/r.what/main.c index 375a4e1b721..0698b6ffbd6 100644 --- a/raster/r.what/main.c +++ b/raster/r.what/main.c @@ -50,6 +50,8 @@ static int by_point(const void *, const void *); static int tty = 0; +enum OutputFormat { PLAIN, JSON }; + int main(int argc, char *argv[]) { int i, j; @@ -98,7 +100,8 @@ int main(int argc, char *argv[]) JSON_Value *root_value = NULL, *point_value, *layer_value; JSON_Array *root_array; JSON_Object *point_object, *layer_object; - char **names; + + enum OutputFormat format; G_gisinit(argv[0]); @@ -146,7 +149,7 @@ int main(int argc, char *argv[]) opt.format->descriptions = "plain;Plain text output;" "json;JSON (JavaScript Object Notation);"; opt.format->answer = "plain"; - opt.format->guisection = _("Output settings"); + opt.format->guisection = _("Print"); opt.cache = G_define_option(); opt.cache->key = "cache"; @@ -273,7 +276,12 @@ int main(int argc, char *argv[]) Cats = Vect_new_cats_struct(); G_get_window(&window); - if (strcmp(opt.format->answer, "json") == 0) { + if (strcmp(opt.format->answer, "json") == 0) + format = JSON; + else + format = PLAIN; + + if (format == JSON) { root_value = json_value_init_array(); if (root_value == NULL) { G_fatal_error(_("Failed to initialize JSON array. Out of memory?")); @@ -282,7 +290,7 @@ int main(int argc, char *argv[]) } /* print header row */ - if (strcmp(opt.format->answer, "plain") == 0 && flg.header->answer) { + if (format == PLAIN && flg.header->answer) { if (flg.cat->answer) { fprintf(stdout, "cat%s", fs); } @@ -304,17 +312,6 @@ 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++; - } - } line = 0; if (!opt.coords->answers && !opt.points->answers && tty) @@ -503,13 +500,12 @@ int main(int argc, char *argv[]) qsort(cache, point_cnt, sizeof(struct order), by_point); /* report data from re-ordered cache */ - for (point = 0; point < point_cnt; point++) { 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 (strcmp(opt.format->answer, "plain") == 0) { + if (format == PLAIN) { if (flg.cat->answer) { fprintf(stdout, "%d%s", cache[point].cat, fs); @@ -566,10 +562,10 @@ int main(int argc, char *argv[]) cache[point].cat); } - json_object_set_string(point_object, "easting", - cache[point].east_buf); - json_object_set_string(point_object, "northing", - cache[point].north_buf); + json_object_set_number(point_object, "easting", + atof(cache[point].east_buf)); + json_object_set_number(point_object, "northing", + atof(cache[point].north_buf)); json_object_set_string(point_object, "site_name", cache[point].lab_buf); @@ -605,7 +601,8 @@ int main(int argc, char *argv[]) cache[point].clr_buf[i]); } - json_object_set_value(point_object, names[i], layer_value); + json_object_set_value(point_object, opt.input->answers[i], + layer_value); } json_array_append_value(root_array, point_value); } @@ -620,7 +617,7 @@ int main(int argc, char *argv[]) cache_hit = cache_miss = 0; } - if (strcmp(opt.format->answer, "json") == 0) { + if (format == JSON) { char *serialized_string = NULL; serialized_string = json_serialize_to_string_pretty(root_value); if (serialized_string == NULL) { diff --git a/raster/r.what/testsuite/test_r_what.py b/raster/r.what/testsuite/test_r_what.py index e418bd8b6e7..fcc312e6284 100644 --- a/raster/r.what/testsuite/test_r_what.py +++ b/raster/r.what/testsuite/test_r_what.py @@ -447,8 +447,8 @@ def convert_plain_to_json(plain): if line: parts = line.split("|") item = { - "easting": parts[0], - "northing": parts[1], + "easting": float(parts[0]), + "northing": float(parts[1]), "site_name": parts[2], "boundary_county_500m": {"value": int(parts[3])}, }