From a0be16c9a054dbbb2b3c4a4b144562f76f5ecb9c Mon Sep 17 00:00:00 2001 From: Kriti Birda Date: Thu, 18 Jul 2024 14:45:17 +0530 Subject: [PATCH] address PR feedback --- raster/r.report/global.h | 1 + raster/r.report/prt_json.c | 69 +- raster/r.report/prt_report.c | 67 +- raster/r.report/r.report.html | 1388 +++++++------------- raster/r.report/testsuite/test_r_report.py | 466 ++++++- 5 files changed, 909 insertions(+), 1082 deletions(-) diff --git a/raster/r.report/global.h b/raster/r.report/global.h index a64b32e578d..f776705ecd7 100644 --- a/raster/r.report/global.h +++ b/raster/r.report/global.h @@ -109,6 +109,7 @@ int parse_layer(char *); int match(char *, char *, int); /* prt_report.c */ +int compute_unit_format(int, int, enum OutputFormat); int print_report(int, int); int construct_val_str(int, CELL *, char *); char *construct_cat_label(int, CELL); diff --git a/raster/r.report/prt_json.c b/raster/r.report/prt_json.c index 0f9b8ceb2b8..f56f0f382a7 100644 --- a/raster/r.report/prt_json.c +++ b/raster/r.report/prt_json.c @@ -63,16 +63,16 @@ JSON_Value *make_category(int ns, int nl, JSON_Value *sub_categories) json_object_set_number(object, "category", cats[nl]); DCELL dLow, dHigh; - char str[500]; if (!is_fp[nl] || as_int) - json_object_set_number(object, "description", cats[nl]); + json_object_set_string(object, "label", + Rast_get_c_cat(&cats[nl], &layers[nl].labels)); else { /* find or construct the label for floating point range to print */ if (Rast_is_c_null_value(&cats[nl])) - json_object_set_null(object, "description"); + json_object_set_null(object, "label"); else if (cat_ranges) { - json_object_set_string(object, "description", + json_object_set_string(object, "label", Rast_get_ith_d_cat(&layers[nl].labels, cats[nl], &dLow, &dHigh)); } @@ -82,10 +82,8 @@ JSON_Value *make_category(int ns, int nl, JSON_Value *sub_categories) DMIN[nl]; dHigh = (DMAX[nl] - DMIN[nl]) / (double)nsteps * (double)cats[nl] + DMIN[nl]; - char *from = Rast_get_d_cat(&dLow, &layers[nl].labels); - char *to = Rast_get_d_cat(&dHigh, &layers[nl].labels); - sprintf(str, "from %s to %s", from, to); - json_object_set_string(object, "description", str); + + json_object_set_string(object, "label", "from to"); JSON_Value *range_value = json_value_init_object(); JSON_Object *range_object = json_object(range_value); @@ -132,53 +130,8 @@ JSON_Value *make_categories(int start, int end, int level) void print_json() { - for (int i = 0; i < nunits; i++) { - switch (unit[i].type) { - case CELL_COUNTS: - unit[i].label[0] = " cell"; - unit[i].label[1] = "count"; - break; - - case PERCENT_COVER: - unit[i].label[0] = " % "; - unit[i].label[1] = "cover"; - break; - - case SQ_METERS: - unit[i].label[0] = "square"; - unit[i].label[1] = "meters"; - unit[i].factor = 1.0; - break; - - case SQ_KILOMETERS: - unit[i].label[0] = " square "; - unit[i].label[1] = "kilometers"; - unit[i].factor = 1.0e-6; - break; - - case ACRES: - unit[i].label[0] = ""; - unit[i].label[1] = "acres"; - unit[i].factor = 2.47105381467165e-4; /* 640 acres in a sq mile */ - break; - - case HECTARES: - unit[i].label[0] = ""; - unit[i].label[1] = "hectares"; - unit[i].factor = 1.0e-4; - break; - - case SQ_MILES: - unit[i].label[0] = "square"; - unit[i].label[1] = " miles"; - unit[i].factor = 3.86102158542446e-7; /* 1 / ( (0.0254m/in * 12in/ft - * 5280ft/mi)^2 ) */ - break; - - default: - G_fatal_error("Unit %d not yet supported", unit[i].type); - } - } + compute_unit_format(0, nunits - 1, JSON); + JSON_Value *root_value = json_value_init_object(); JSON_Object *root_object = json_object(root_value); @@ -200,7 +153,7 @@ void print_json() json_object_set_null(root_object, "mask"); } else { - json_object_set_string(root_object, "mask", maskinfo()); + json_object_set_string(root_object, "mask", mask); } JSON_Value *maps_value = json_value_init_array(); @@ -208,10 +161,10 @@ void print_json() for (int i = 0; i < nlayers; i++) { JSON_Value *map_value = json_value_init_object(); JSON_Object *map_object = json_object(map_value); - json_object_set_string(map_object, "name", layers[i].name); // fixme + json_object_set_string(map_object, "name", layers[i].name); json_object_set_string(map_object, "description", layers[i].mapset); json_object_set_string(map_object, "layer", layers[i].name); - json_object_set_string(map_object, "type", "raster"); // fixme + json_object_set_string(map_object, "type", "raster"); json_array_append_value(maps_array, map_value); } json_object_set_value(root_object, "maps", maps_value); diff --git a/raster/r.report/prt_report.c b/raster/r.report/prt_report.c index 8edb0ded88b..48ab4003c5c 100644 --- a/raster/r.report/prt_report.c +++ b/raster/r.report/prt_report.c @@ -3,48 +3,49 @@ #include #include "global.h" -int print_report(int unit1, int unit2) +int compute_unit_format(int unit1, int unit2, enum OutputFormat format) { - int ns, nl, nx; + int i, ns, len; char num[100]; - int len, new; - CELL *cats, *prev; - int first; - int i; - int divider_level; - int after_header; int need_format; - int with_stats; - char *cp; - int spacing; - char dot; /* examine units, determine output format */ for (i = unit1; i <= unit2; i++) { - need_format = 1; + if (format == PLAIN) { + need_format = 1; + } + else { + need_format = 0; + } unit[i].label[0] = ""; unit[i].label[1] = ""; switch (unit[i].type) { case CELL_COUNTS: - need_format = 0; - unit[i].len = 5; unit[i].label[0] = " cell"; unit[i].label[1] = "count"; - ns = 0; - sprintf(num, "%ld", count_sum(&ns, -1)); - len = strlen(num); - if (len > unit[i].len) - unit[i].len = len; + + if (need_format) { + need_format = 0; + unit[i].len = 5; + ns = 0; + sprintf(num, "%ld", count_sum(&ns, -1)); + len = strlen(num); + if (len > unit[i].len) + unit[i].len = len; + } break; case PERCENT_COVER: - need_format = 0; - unit[i].dp = 2; - unit[i].len = 6; unit[i].label[0] = " % "; unit[i].label[1] = "cover"; - unit[i].eformat = 0; + + if (need_format) { + need_format = 0; + unit[i].dp = 2; + unit[i].len = 6; + unit[i].eformat = 0; + } break; case SQ_METERS: @@ -90,6 +91,24 @@ int print_report(int unit1, int unit2) &unit[i].dp, &(unit[i].eformat), e_format); } } +} + +int print_report(int unit1, int unit2) +{ + int ns, nl, nx; + char num[100]; + int len, new; + CELL *cats, *prev; + int first; + int i; + int divider_level; + int after_header; + int with_stats; + char *cp; + int spacing; + char dot; + + compute_unit_format(unit1, unit2, PLAIN); /* figure out how big the category numbers are when printed */ for (nl = 0; nl < nlayers; nl++) diff --git a/raster/r.report/r.report.html b/raster/r.report/r.report.html index 7f1873a9331..7a27978aadb 100644 --- a/raster/r.report/r.report.html +++ b/raster/r.report/r.report.html @@ -135,1234 +135,704 @@

EXAMPLE

The output from r.report can be output in JSON by passing the format=json option.
-r.report map=zipcodes@PERMANENT,landclass96@PERMANENT units=h,p format=json
+r.report -n -a map=towns,elevation units=miles,meters,kilometers,acres,hectares,cells,percent nsteps=2 format=json
 
 {
     "location": "nc_spm_08_grass7",
-    "created": "Fri Jun 28 18:41:29 2024",
+    "created": "Thu Jul 18 13:23:49 2024",
     "region": {
-        "north": 228500,
-        "south": 215000,
-        "east": 645000,
-        "west": 630000,
-        "ew_res": 10,
-        "ns_res": 10
+        "north": 320000,
+        "south": 10000,
+        "east": 935000,
+        "west": 120000,
+        "ew_res": 500,
+        "ns_res": 500
     },
-    "mask": "none",
+    "mask": null,
     "maps": [
         {
-            "name": "zipcodes@PERMANENT",
+            "name": "towns",
             "description": "PERMANENT",
-            "layer": "zipcodes@PERMANENT",
+            "layer": "towns",
             "type": "raster"
         },
         {
-            "name": "landclass96@PERMANENT",
+            "name": "elevation",
             "description": "PERMANENT",
-            "layer": "landclass96@PERMANENT",
+            "layer": "elevation",
             "type": "raster"
         }
     ],
     "categories": [
         {
-            "category": 27511,
-            "description": "CARY",
+            "category": 1,
+            "label": "CARY",
             "units": [
                 {
-                    "unit": "hectares",
-                    "value": 1058
-                },
-                {
-                    "unit": "% cover",
-                    "value": 5.2246913580246916
-                }
-            ],
-            "categories": [
-                {
-                    "category": 1,
-                    "description": "developed",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 197.94
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 18.708884688090738
-                        }
-                    ]
-                },
-                {
-                    "category": 3,
-                    "description": "herbaceous",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 24.440000000000001
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 2.3100189035916823
-                        }
-                    ]
-                },
-                {
-                    "category": 4,
-                    "description": "shrubland",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 58.080000000000005
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 5.4896030245746692
-                        }
-                    ]
-                },
-                {
-                    "category": 5,
-                    "description": "forest",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 775.91000000000008
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 73.337429111531193
-                        }
-                    ]
-                },
-                {
-                    "category": 6,
-                    "description": "water",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 1.54
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 0.14555765595463138
-                        }
-                    ]
-                },
-                {
-                    "category": -2147483648,
-                    "description": "no data",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 0.090000000000000011
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 0.0085066162570888466
-                        }
-                    ]
-                }
-            ]
-        },
-        {
-            "category": 27513,
-            "description": "CARY",
-            "units": [
-                {
-                    "unit": "hectares",
-                    "value": 205.30000000000001
-                },
-                {
-                    "unit": "% cover",
-                    "value": 1.0138271604938272
-                }
-            ],
-            "categories": [
-                {
-                    "category": 1,
-                    "description": "developed",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 32.579999999999998
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 15.869459327812956
-                        }
-                    ]
-                },
-                {
-                    "category": 3,
-                    "description": "herbaceous",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 5.9300000000000006
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 2.8884559181685336
-                        }
-                    ]
-                },
-                {
-                    "category": 4,
-                    "description": "shrubland",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 52.030000000000001
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 25.343399902581588
-                        }
-                    ]
-                },
-                {
-                    "category": 5,
-                    "description": "forest",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 114.76000000000001
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 55.898684851436919
-                        }
-                    ]
-                }
-            ]
-        },
-        {
-            "category": 27518,
-            "description": "CARY",
-            "units": [
-                {
-                    "unit": "hectares",
-                    "value": 1345.1900000000001
-                },
-                {
-                    "unit": "% cover",
-                    "value": 6.6429135802469137
-                }
-            ],
-            "categories": [
-                {
-                    "category": 1,
-                    "description": "developed",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 106.46000000000001
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 7.9141236553944054
-                        }
-                    ]
-                },
-                {
-                    "category": 2,
-                    "description": "agriculture",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 7.5
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 0.55754205725585237
-                        }
-                    ]
-                },
-                {
-                    "category": 3,
-                    "description": "herbaceous",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 84.620000000000005
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 6.2905611846653633
-                        }
-                    ]
-                },
-                {
-                    "category": 4,
-                    "description": "shrubland",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 179.63
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 13.353503965982501
-                        }
-                    ]
-                },
-                {
-                    "category": 5,
-                    "description": "forest",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 919.35000000000002
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 68.343505378422378
-                        }
-                    ]
-                },
-                {
-                    "category": 6,
-                    "description": "water",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 47.630000000000003
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 3.5407637582794997
-                        }
-                    ]
-                }
-            ]
-        },
-        {
-            "category": 27529,
-            "description": "GARNER",
-            "units": [
-                {
-                    "unit": "hectares",
-                    "value": 1415.72
-                },
-                {
-                    "unit": "% cover",
-                    "value": 6.99120987654321
-                }
-            ],
-            "categories": [
-                {
-                    "category": 1,
-                    "description": "developed",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 614.97000000000003
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 43.438674314129912
-                        }
-                    ]
-                },
-                {
-                    "category": 2,
-                    "description": "agriculture",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 7.25
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 0.51210691379651341
-                        }
-                    ]
-                },
-                {
-                    "category": 3,
-                    "description": "herbaceous",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 35.960000000000001
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 2.5400502924307067
-                        }
-                    ]
-                },
-                {
-                    "category": 4,
-                    "description": "shrubland",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 12.890000000000001
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 0.91049077501200804
-                        }
-                    ]
-                },
-                {
-                    "category": 5,
-                    "description": "forest",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 719.13
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 50.796061368067129
-                        }
-                    ]
+                    "unit": "square miles",
+                    "value": 10.231707201374819
                 },
                 {
-                    "category": 6,
-                    "description": "water",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 4.1699999999999999
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 0.29454976972847741
-                        }
-                    ]
-                },
-                {
-                    "category": -2147483648,
-                    "description": "no data",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 21.350000000000001
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 1.50806656683525
-                        }
-                    ]
-                }
-            ]
-        },
-        {
-            "category": 27539,
-            "description": "APEX",
-            "units": [
-                {
-                    "unit": "hectares",
-                    "value": 254.44000000000003
+                    "unit": "square meters",
+                    "value": 26500000
                 },
                 {
-                    "unit": "% cover",
-                    "value": 1.2564938271604937
-                }
-            ],
-            "categories": [
-                {
-                    "category": 1,
-                    "description": "developed",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 2.46
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 0.96682911491903789
-                        }
-                    ]
+                    "unit": "square kilometers",
+                    "value": 26.5
                 },
                 {
-                    "category": 2,
-                    "description": "agriculture",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 28.720000000000002
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 11.28753340669706
-                        }
-                    ]
+                    "unit": "acres",
+                    "value": 6548.2926088798722
                 },
-                {
-                    "category": 3,
-                    "description": "herbaceous",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 4.04
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 1.5878006602735419
-                        }
-                    ]
-                },
-                {
-                    "category": 4,
-                    "description": "shrubland",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 18.560000000000002
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 7.294450558088351
-                        }
-                    ]
-                },
-                {
-                    "category": 5,
-                    "description": "forest",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 192.37
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 75.605250746737937
-                        }
-                    ]
-                },
-                {
-                    "category": 6,
-                    "description": "water",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 0.87
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 0.34192736991039147
-                        }
-                    ]
-                },
-                {
-                    "category": -2147483648,
-                    "description": "no data",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 7.4199999999999999
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 2.9162081433736833
-                        }
-                    ]
-                }
-            ]
-        },
-        {
-            "category": 27601,
-            "description": "RALEIGH",
-            "units": [
                 {
                     "unit": "hectares",
-                    "value": 454.68000000000001
+                    "value": 2650
                 },
                 {
-                    "unit": "% cover",
-                    "value": 2.2453333333333334
-                }
-            ],
-            "categories": [
-                {
-                    "category": 1,
-                    "description": "developed",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 436.65000000000003
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 96.034573766165209
-                        }
-                    ]
-                },
-                {
-                    "category": 3,
-                    "description": "herbaceous",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 11.610000000000001
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 2.5534441805225652
-                        }
-                    ]
-                },
-                {
-                    "category": 5,
-                    "description": "forest",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 6.4199999999999999
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 1.4119820533122196
-                        }
-                    ]
-                }
-            ]
-        },
-        {
-            "category": 27603,
-            "description": "RALEIGH",
-            "units": [
-                {
-                    "unit": "hectares",
-                    "value": 4291.79
+                    "unit": "cell counts",
+                    "value": 106
                 },
                 {
                     "unit": "% cover",
-                    "value": 21.194024691358024
+                    "value": 13.086419753086419
                 }
             ],
             "categories": [
                 {
                     "category": 1,
-                    "description": "developed",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 1183.24
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 27.569848478140823
-                        }
-                    ]
-                },
-                {
-                    "category": 2,
-                    "description": "agriculture",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 14.770000000000001
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 0.34414544980066591
-                        }
-                    ]
-                },
-                {
-                    "category": 3,
-                    "description": "herbaceous",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 808.62
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 18.841089615288727
-                        }
-                    ]
-                },
-                {
-                    "category": 4,
-                    "description": "shrubland",
+                    "label": "from to",
+                    "range": {
+                        "from": 55.578792572021484,
+                        "to": 105.9543285369873
+                    },
                     "units": [
                         {
-                            "unit": "hectares",
-                            "value": 216.73000000000002
+                            "unit": "square miles",
+                            "value": 0.8687298567205034
                         },
                         {
-                            "unit": "% cover",
-                            "value": 5.0498742948746331
-                        }
-                    ]
-                },
-                {
-                    "category": 5,
-                    "description": "forest",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 2036.2900000000002
+                            "unit": "square meters",
+                            "value": 2250000
                         },
                         {
-                            "unit": "% cover",
-                            "value": 47.446170478984293
-                        }
-                    ]
-                },
-                {
-                    "category": 6,
-                    "description": "water",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 16.100000000000001
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 0.37513485049361689
-                        }
-                    ]
-                },
-                {
-                    "category": 7,
-                    "description": "sediment",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 10.66
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 0.24838121156906559
-                        }
-                    ]
-                },
-                {
-                    "category": -2147483648,
-                    "description": "no data",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 5.3799999999999999
-                        },
-                        {
-                            "unit": "% cover",
-                            "value": 0.12535562084817756
-                        }
-                    ]
-                }
-            ]
-        },
-        {
-            "category": 27604,
-            "description": "RALEIGH",
-            "units": [
-                {
-                    "unit": "hectares",
-                    "value": 473.89000000000004
-                },
-                {
-                    "unit": "% cover",
-                    "value": 2.3401975308641974
-                }
-            ],
-            "categories": [
-                {
-                    "category": 1,
-                    "description": "developed",
-                    "units": [
+                            "unit": "square kilometers",
+                            "value": 2.25
+                        },
+                        {
+                            "unit": "acres",
+                            "value": 555.98710830112122
+                        },
                         {
                             "unit": "hectares",
-                            "value": 338.04000000000002
+                            "value": 225
+                        },
+                        {
+                            "unit": "cell counts",
+                            "value": 9
                         },
                         {
                             "unit": "% cover",
-                            "value": 71.333009770199837
+                            "value": 8.4905660377358494
                         }
                     ]
                 },
                 {
-                    "category": 3,
-                    "description": "herbaceous",
+                    "category": 2,
+                    "label": "from to",
+                    "range": {
+                        "from": 105.9543285369873,
+                        "to": 156.32986450195312
+                    },
                     "units": [
                         {
-                            "unit": "hectares",
-                            "value": 49.710000000000001
+                            "unit": "square miles",
+                            "value": 9.3629773446543147
                         },
                         {
-                            "unit": "% cover",
-                            "value": 10.489776108379582
-                        }
-                    ]
-                },
-                {
-                    "category": 5,
-                    "description": "forest",
-                    "units": [
+                            "unit": "square meters",
+                            "value": 24250000
+                        },
                         {
-                            "unit": "hectares",
-                            "value": 83.77000000000001
+                            "unit": "square kilometers",
+                            "value": 24.25
                         },
                         {
-                            "unit": "% cover",
-                            "value": 17.677098060731392
-                        }
-                    ]
-                },
-                {
-                    "category": -2147483648,
-                    "description": "no data",
-                    "units": [
+                            "unit": "acres",
+                            "value": 5992.305500578751
+                        },
                         {
                             "unit": "hectares",
-                            "value": 2.3700000000000001
+                            "value": 2425
+                        },
+                        {
+                            "unit": "cell counts",
+                            "value": 97
                         },
                         {
                             "unit": "% cover",
-                            "value": 0.50011606068918946
+                            "value": 91.509433962264154
                         }
                     ]
                 }
             ]
         },
         {
-            "category": 27605,
-            "description": "RALEIGH",
+            "category": 2,
+            "label": "GARNER",
             "units": [
+                {
+                    "unit": "square miles",
+                    "value": 5.5019557592298556
+                },
+                {
+                    "unit": "square meters",
+                    "value": 14250000
+                },
+                {
+                    "unit": "square kilometers",
+                    "value": 14.25
+                },
+                {
+                    "unit": "acres",
+                    "value": 3521.2516859071011
+                },
                 {
                     "unit": "hectares",
-                    "value": 236.77000000000001
+                    "value": 1425
+                },
+                {
+                    "unit": "cell counts",
+                    "value": 57
                 },
                 {
                     "unit": "% cover",
-                    "value": 1.1692345679012346
+                    "value": 7.0370370370370372
                 }
             ],
             "categories": [
                 {
                     "category": 1,
-                    "description": "developed",
+                    "label": "from to",
+                    "range": {
+                        "from": 55.578792572021484,
+                        "to": 105.9543285369873
+                    },
                     "units": [
                         {
-                            "unit": "hectares",
-                            "value": 187.45000000000002
+                            "unit": "square miles",
+                            "value": 4.3436492836025176
                         },
                         {
-                            "unit": "% cover",
-                            "value": 79.169658318199097
-                        }
-                    ]
-                },
-                {
-                    "category": 3,
-                    "description": "herbaceous",
-                    "units": [
+                            "unit": "square meters",
+                            "value": 11250000
+                        },
                         {
-                            "unit": "hectares",
-                            "value": 5.71
+                            "unit": "square kilometers",
+                            "value": 11.25
                         },
                         {
-                            "unit": "% cover",
-                            "value": 2.4116230941419943
-                        }
-                    ]
-                },
-                {
-                    "category": 4,
-                    "description": "shrubland",
-                    "units": [
+                            "unit": "acres",
+                            "value": 2779.9355415056061
+                        },
                         {
                             "unit": "hectares",
-                            "value": 6.6000000000000005
+                            "value": 1125
+                        },
+                        {
+                            "unit": "cell counts",
+                            "value": 45
                         },
                         {
                             "unit": "% cover",
-                            "value": 2.7875153102166661
+                            "value": 78.94736842105263
                         }
                     ]
                 },
                 {
-                    "category": 5,
-                    "description": "forest",
+                    "category": 2,
+                    "label": "from to",
+                    "range": {
+                        "from": 105.9543285369873,
+                        "to": 156.32986450195312
+                    },
                     "units": [
+                        {
+                            "unit": "square miles",
+                            "value": 1.158306475627338
+                        },
+                        {
+                            "unit": "square meters",
+                            "value": 3000000
+                        },
+                        {
+                            "unit": "square kilometers",
+                            "value": 3
+                        },
+                        {
+                            "unit": "acres",
+                            "value": 741.31614440149497
+                        },
                         {
                             "unit": "hectares",
-                            "value": 37.010000000000005
+                            "value": 300
+                        },
+                        {
+                            "unit": "cell counts",
+                            "value": 12
                         },
                         {
                             "unit": "% cover",
-                            "value": 15.631203277442243
+                            "value": 21.05263157894737
                         }
                     ]
                 }
             ]
         },
         {
-            "category": 27606,
-            "description": "RALEIGH",
+            "category": 3,
+            "label": "APEX",
             "units": [
+                {
+                    "unit": "square miles",
+                    "value": 0.9652553963561149
+                },
+                {
+                    "unit": "square meters",
+                    "value": 2500000
+                },
+                {
+                    "unit": "square kilometers",
+                    "value": 2.5
+                },
+                {
+                    "unit": "acres",
+                    "value": 617.76345366791247
+                },
                 {
                     "unit": "hectares",
-                    "value": 6626.4200000000001
+                    "value": 250
+                },
+                {
+                    "unit": "cell counts",
+                    "value": 10
                 },
                 {
                     "unit": "% cover",
-                    "value": 32.723061728395059
+                    "value": 1.2345679012345678
                 }
             ],
             "categories": [
                 {
                     "category": 1,
-                    "description": "developed",
+                    "label": "from to",
+                    "range": {
+                        "from": 55.578792572021484,
+                        "to": 105.9543285369873
+                    },
                     "units": [
                         {
-                            "unit": "hectares",
-                            "value": 1030.6200000000001
+                            "unit": "square miles",
+                            "value": 0.096525539635611488
                         },
                         {
-                            "unit": "% cover",
-                            "value": 15.553194636017638
-                        }
-                    ]
-                },
-                {
-                    "category": 2,
-                    "description": "agriculture",
-                    "units": [
+                            "unit": "square meters",
+                            "value": 250000
+                        },
                         {
-                            "unit": "hectares",
-                            "value": 115.23
+                            "unit": "square kilometers",
+                            "value": 0.25
                         },
                         {
-                            "unit": "% cover",
-                            "value": 1.7389480292525978
-                        }
-                    ]
-                },
-                {
-                    "category": 3,
-                    "description": "herbaceous",
-                    "units": [
+                            "unit": "acres",
+                            "value": 61.776345366791247
+                        },
                         {
                             "unit": "hectares",
-                            "value": 631.49000000000001
+                            "value": 25
                         },
                         {
-                            "unit": "% cover",
-                            "value": 9.5298818970122632
-                        }
-                    ]
-                },
-                {
-                    "category": 4,
-                    "description": "shrubland",
-                    "units": [
-                        {
-                            "unit": "hectares",
-                            "value": 682.66000000000008
+                            "unit": "cell counts",
+                            "value": 1
                         },
                         {
                             "unit": "% cover",
-                            "value": 10.302093739907823
+                            "value": 10
                         }
                     ]
                 },
                 {
-                    "category": 5,
-                    "description": "forest",
+                    "category": 2,
+                    "label": "from to",
+                    "range": {
+                        "from": 105.9543285369873,
+                        "to": 156.32986450195312
+                    },
                     "units": [
                         {
-                            "unit": "hectares",
-                            "value": 3807.6200000000003
+                            "unit": "square miles",
+                            "value": 0.8687298567205034
                         },
                         {
-                            "unit": "% cover",
-                            "value": 57.461193223490213
-                        }
-                    ]
-                },
-                {
-                    "category": 6,
-                    "description": "water",
-                    "units": [
+                            "unit": "square meters",
+                            "value": 2250000
+                        },
                         {
-                            "unit": "hectares",
-                            "value": 349.17000000000002
+                            "unit": "square kilometers",
+                            "value": 2.25
                         },
                         {
-                            "unit": "% cover",
-                            "value": 5.2693611331608921
-                        }
-                    ]
-                },
-                {
-                    "category": -2147483648,
-                    "description": "no data",
-                    "units": [
+                            "unit": "acres",
+                            "value": 555.98710830112122
+                        },
                         {
                             "unit": "hectares",
-                            "value": 9.6300000000000008
+                            "value": 225
+                        },
+                        {
+                            "unit": "cell counts",
+                            "value": 9
                         },
                         {
                             "unit": "% cover",
-                            "value": 0.14532734115857432
+                            "value": 90
                         }
                     ]
                 }
             ]
         },
         {
-            "category": 27607,
-            "description": "RALEIGH",
+            "category": 4,
+            "label": "RALEIGH-CITY",
             "units": [
+                {
+                    "unit": "square miles",
+                    "value": 6.0811089970435237
+                },
+                {
+                    "unit": "square meters",
+                    "value": 15750000
+                },
+                {
+                    "unit": "square kilometers",
+                    "value": 15.75
+                },
+                {
+                    "unit": "acres",
+                    "value": 3891.9097581078486
+                },
                 {
                     "unit": "hectares",
-                    "value": 2089.8900000000003
+                    "value": 1575
+                },
+                {
+                    "unit": "cell counts",
+                    "value": 63
                 },
                 {
                     "unit": "% cover",
-                    "value": 10.320444444444444
+                    "value": 7.7777777777777777
                 }
             ],
             "categories": [
                 {
                     "category": 1,
-                    "description": "developed",
+                    "label": "from to",
+                    "range": {
+                        "from": 55.578792572021484,
+                        "to": 105.9543285369873
+                    },
                     "units": [
                         {
-                            "unit": "hectares",
-                            "value": 727.16000000000008
+                            "unit": "square miles",
+                            "value": 5.3089046799586326
                         },
                         {
-                            "unit": "% cover",
-                            "value": 34.794175770016601
-                        }
-                    ]
-                },
-                {
-                    "category": 3,
-                    "description": "herbaceous",
-                    "units": [
+                            "unit": "square meters",
+                            "value": 13750000
+                        },
                         {
-                            "unit": "hectares",
-                            "value": 329.22000000000003
+                            "unit": "square kilometers",
+                            "value": 13.75
                         },
                         {
-                            "unit": "% cover",
-                            "value": 15.752982214374919
-                        }
-                    ]
-                },
-                {
-                    "category": 4,
-                    "description": "shrubland",
-                    "units": [
+                            "unit": "acres",
+                            "value": 3397.6989951735186
+                        },
                         {
                             "unit": "hectares",
-                            "value": 126.89
+                            "value": 1375
+                        },
+                        {
+                            "unit": "cell counts",
+                            "value": 55
                         },
                         {
                             "unit": "% cover",
-                            "value": 6.0716114245247361
+                            "value": 87.301587301587304
                         }
                     ]
                 },
                 {
-                    "category": 5,
-                    "description": "forest",
+                    "category": 2,
+                    "label": "from to",
+                    "range": {
+                        "from": 105.9543285369873,
+                        "to": 156.32986450195312
+                    },
                     "units": [
                         {
-                            "unit": "hectares",
-                            "value": 894.04000000000008
+                            "unit": "square miles",
+                            "value": 0.7722043170848919
                         },
                         {
-                            "unit": "% cover",
-                            "value": 42.77928503414055
-                        }
-                    ]
-                },
-                {
-                    "category": 6,
-                    "description": "water",
-                    "units": [
+                            "unit": "square meters",
+                            "value": 2000000
+                        },
                         {
-                            "unit": "hectares",
-                            "value": 7.1400000000000006
+                            "unit": "square kilometers",
+                            "value": 2
                         },
                         {
-                            "unit": "% cover",
-                            "value": 0.34164477556235018
-                        }
-                    ]
-                },
-                {
-                    "category": 7,
-                    "description": "sediment",
-                    "units": [
+                            "unit": "acres",
+                            "value": 494.21076293432998
+                        },
                         {
                             "unit": "hectares",
-                            "value": 5.4400000000000004
+                            "value": 200
+                        },
+                        {
+                            "unit": "cell counts",
+                            "value": 8
                         },
                         {
                             "unit": "% cover",
-                            "value": 0.26030078138083823
+                            "value": 12.698412698412698
                         }
                     ]
                 }
             ]
         },
         {
-            "category": 27608,
-            "description": "RALEIGH",
+            "category": 5,
+            "label": "RALEIGH-SOUTH",
             "units": [
+                {
+                    "unit": "square miles",
+                    "value": 47.394039961085241
+                },
+                {
+                    "unit": "square meters",
+                    "value": 122750000
+                },
+                {
+                    "unit": "square kilometers",
+                    "value": 122.75
+                },
+                {
+                    "unit": "acres",
+                    "value": 30332.185575094503
+                },
                 {
                     "unit": "hectares",
-                    "value": 439.80000000000001
+                    "value": 12275
+                },
+                {
+                    "unit": "cell counts",
+                    "value": 491
                 },
                 {
                     "unit": "% cover",
-                    "value": 2.1718518518518519
+                    "value": 60.617283950617285
                 }
             ],
             "categories": [
                 {
                     "category": 1,
-                    "description": "developed",
+                    "label": "from to",
+                    "range": {
+                        "from": 55.578792572021484,
+                        "to": 105.9543285369873
+                    },
                     "units": [
                         {
-                            "unit": "hectares",
-                            "value": 307.54000000000002
+                            "unit": "square miles",
+                            "value": 25.579268003437047
                         },
                         {
-                            "unit": "% cover",
-                            "value": 69.927239654388359
-                        }
-                    ]
-                },
-                {
-                    "category": 3,
-                    "description": "herbaceous",
-                    "units": [
+                            "unit": "square meters",
+                            "value": 66250000
+                        },
+                        {
+                            "unit": "square kilometers",
+                            "value": 66.25
+                        },
+                        {
+                            "unit": "acres",
+                            "value": 16370.731522199681
+                        },
                         {
                             "unit": "hectares",
-                            "value": 1.72
+                            "value": 6625
+                        },
+                        {
+                            "unit": "cell counts",
+                            "value": 265
                         },
                         {
                             "unit": "% cover",
-                            "value": 0.39108685766257389
+                            "value": 53.971486761710793
                         }
                     ]
                 },
                 {
-                    "category": 5,
-                    "description": "forest",
+                    "category": 2,
+                    "label": "from to",
+                    "range": {
+                        "from": 105.9543285369873,
+                        "to": 156.32986450195312
+                    },
                     "units": [
+                        {
+                            "unit": "square miles",
+                            "value": 21.814771957648198
+                        },
+                        {
+                            "unit": "square meters",
+                            "value": 56500000
+                        },
+                        {
+                            "unit": "square kilometers",
+                            "value": 56.5
+                        },
+                        {
+                            "unit": "acres",
+                            "value": 13961.454052894822
+                        },
                         {
                             "unit": "hectares",
-                            "value": 130.54000000000002
+                            "value": 5650
+                        },
+                        {
+                            "unit": "cell counts",
+                            "value": 226
                         },
                         {
                             "unit": "% cover",
-                            "value": 29.681673487949066
+                            "value": 46.028513238289207
                         }
                     ]
                 }
             ]
         },
         {
-            "category": 27610,
-            "description": "RALEIGH",
+            "category": 6,
+            "label": "RALEIGH-WEST",
             "units": [
+                {
+                    "unit": "square miles",
+                    "value": 8.0116197897557537
+                },
+                {
+                    "unit": "square meters",
+                    "value": 20750000
+                },
+                {
+                    "unit": "square kilometers",
+                    "value": 20.75
+                },
+                {
+                    "unit": "acres",
+                    "value": 5127.4366654436735
+                },
                 {
                     "unit": "hectares",
-                    "value": 1358.1100000000001
+                    "value": 2075
+                },
+                {
+                    "unit": "cell counts",
+                    "value": 83
                 },
                 {
                     "unit": "% cover",
-                    "value": 6.7067160493827158
+                    "value": 10.246913580246913
                 }
             ],
             "categories": [
                 {
                     "category": 1,
-                    "description": "developed",
+                    "label": "from to",
+                    "range": {
+                        "from": 55.578792572021484,
+                        "to": 105.9543285369873
+                    },
                     "units": [
                         {
-                            "unit": "hectares",
-                            "value": 757.76999999999998
+                            "unit": "square miles",
+                            "value": 0.096525539635611488
                         },
                         {
-                            "unit": "% cover",
-                            "value": 55.79592227433713
-                        }
-                    ]
-                },
-                {
-                    "category": 3,
-                    "description": "herbaceous",
-                    "units": [
+                            "unit": "square meters",
+                            "value": 250000
+                        },
                         {
-                            "unit": "hectares",
-                            "value": 68.219999999999999
+                            "unit": "square kilometers",
+                            "value": 0.25
                         },
                         {
-                            "unit": "% cover",
-                            "value": 5.0231571816715874
-                        }
-                    ]
-                },
-                {
-                    "category": 4,
-                    "description": "shrubland",
-                    "units": [
+                            "unit": "acres",
+                            "value": 61.776345366791247
+                        },
                         {
                             "unit": "hectares",
-                            "value": 5.8700000000000001
+                            "value": 25
+                        },
+                        {
+                            "unit": "cell counts",
+                            "value": 1
                         },
                         {
                             "unit": "% cover",
-                            "value": 0.43221830337748784
+                            "value": 1.2048192771084338
                         }
                     ]
                 },
                 {
-                    "category": 5,
-                    "description": "forest",
+                    "category": 2,
+                    "label": "from to",
+                    "range": {
+                        "from": 105.9543285369873,
+                        "to": 156.32986450195312
+                    },
                     "units": [
                         {
-                            "unit": "hectares",
-                            "value": 500.04000000000002
+                            "unit": "square miles",
+                            "value": 7.9150942501201422
                         },
                         {
-                            "unit": "% cover",
-                            "value": 36.81881438175111
-                        }
-                    ]
-                },
-                {
-                    "category": 6,
-                    "description": "water",
-                    "units": [
+                            "unit": "square meters",
+                            "value": 20500000
+                        },
                         {
-                            "unit": "hectares",
-                            "value": 1.9200000000000002
+                            "unit": "square kilometers",
+                            "value": 20.5
                         },
                         {
-                            "unit": "% cover",
-                            "value": 0.14137293739093298
-                        }
-                    ]
-                },
-                {
-                    "category": -2147483648,
-                    "description": "no data",
-                    "units": [
+                            "unit": "acres",
+                            "value": 5065.6603200768823
+                        },
                         {
                             "unit": "hectares",
-                            "value": 24.290000000000003
+                            "value": 2050
+                        },
+                        {
+                            "unit": "cell counts",
+                            "value": 82
                         },
                         {
                             "unit": "% cover",
-                            "value": 1.7885149214717513
+                            "value": 98.795180722891573
                         }
                     ]
                 }
@@ -1370,10 +840,30 @@ 

EXAMPLE

} ], "totals": [ + { + "unit": "square miles", + "value": 78.185687104845314 + }, + { + "unit": "square meters", + "value": 202500000 + }, + { + "unit": "square kilometers", + "value": 202.5 + }, + { + "unit": "acres", + "value": 50038.839747100916 + }, { "unit": "hectares", "value": 20250 }, + { + "unit": "cell counts", + "value": 810 + }, { "unit": "% cover", "value": 100 diff --git a/raster/r.report/testsuite/test_r_report.py b/raster/r.report/testsuite/test_r_report.py index 369a57cc99b..47b36ac1bbb 100644 --- a/raster/r.report/testsuite/test_r_report.py +++ b/raster/r.report/testsuite/test_r_report.py @@ -63,6 +63,35 @@ def test_output(self): self.assertModule("r.report", map="lakes", output=self.outfile) self.assertFileExists(self.outfile) + def _assert_report_equal(self, reference, data): + keys = ["location", "region", "mask", "maps", "totals"] + for key in keys: + self.assertEqual(reference[key], data[key]) + + for category1, category2 in zip_longest( + reference["categories"], data["categories"] + ): + self.assertEqual(category1["category"], category2["category"]) + self.assertEqual(category1["label"], category2["label"]) + + for unit1, unit2 in zip_longest(category1["units"], category2["units"]): + self.assertEqual(unit1["unit"], unit2["unit"]) + self.assertAlmostEqual(unit1["value"], unit2["value"], places=6) + + for sub_category1, sub_category2 in zip_longest( + category1["categories"], category2["categories"] + ): + self.assertEqual(sub_category1["category"], sub_category2["category"]) + self.assertEqual(sub_category1["label"], sub_category2["label"]) + + for sub_unit1, sub_unit2 in zip_longest( + sub_category1["units"], sub_category2["units"] + ): + self.assertEqual(sub_unit1["unit"], sub_unit2["unit"]) + self.assertAlmostEqual( + sub_unit1["value"], sub_unit2["value"], places=6 + ) + def test_json(self): """Test JSON format""" reference = { @@ -75,7 +104,7 @@ def test_json(self): "ew_res": 10, "ns_res": 10, }, - "mask": "none", + "mask": None, "maps": [ { "name": "towns", @@ -85,7 +114,7 @@ def test_json(self): }, { "name": "zipcodes", - "description": "PERMANENT", + "description": "landsat", "layer": "zipcodes", "type": "raster", }, @@ -93,7 +122,7 @@ def test_json(self): "categories": [ { "category": 1, - "description": "CARY", + "label": "CARY", "units": [ {"unit": "cell counts", "value": 260849}, {"unit": "% cover", "value": 12.881432098765432}, @@ -101,7 +130,7 @@ def test_json(self): "categories": [ { "category": 27511, - "description": "CARY", + "label": "CARY", "units": [ {"unit": "cell counts", "value": 105800}, {"unit": "% cover", "value": 40.559864135956055}, @@ -109,7 +138,7 @@ def test_json(self): }, { "category": 27513, - "description": "CARY", + "label": "CARY", "units": [ {"unit": "cell counts", "value": 20530}, {"unit": "% cover", "value": 7.8704537874402432}, @@ -117,7 +146,7 @@ def test_json(self): }, { "category": 27518, - "description": "CARY", + "label": "CARY", "units": [ {"unit": "cell counts", "value": 134519}, {"unit": "% cover", "value": 51.569682076603705}, @@ -127,7 +156,7 @@ def test_json(self): }, { "category": 2, - "description": "GARNER", + "label": "GARNER", "units": [ {"unit": "cell counts", "value": 141572}, {"unit": "% cover", "value": 6.99120987654321}, @@ -135,7 +164,7 @@ def test_json(self): "categories": [ { "category": 27529, - "description": "GARNER", + "label": "GARNER", "units": [ {"unit": "cell counts", "value": 141572}, {"unit": "% cover", "value": 100}, @@ -145,7 +174,7 @@ def test_json(self): }, { "category": 3, - "description": "APEX", + "label": "APEX", "units": [ {"unit": "cell counts", "value": 25444}, {"unit": "% cover", "value": 1.2564938271604937}, @@ -153,7 +182,7 @@ def test_json(self): "categories": [ { "category": 27539, - "description": "APEX", + "label": "APEX", "units": [ {"unit": "cell counts", "value": 25444}, {"unit": "% cover", "value": 100}, @@ -163,7 +192,7 @@ def test_json(self): }, { "category": 4, - "description": "RALEIGH-CITY", + "label": "RALEIGH-CITY", "units": [ {"unit": "cell counts", "value": 160514}, {"unit": "% cover", "value": 7.926617283950617}, @@ -171,7 +200,7 @@ def test_json(self): "categories": [ { "category": 27601, - "description": "RALEIGH", + "label": "RALEIGH", "units": [ {"unit": "cell counts", "value": 45468}, {"unit": "% cover", "value": 28.326501115167524}, @@ -179,7 +208,7 @@ def test_json(self): }, { "category": 27604, - "description": "RALEIGH", + "label": "RALEIGH", "units": [ {"unit": "cell counts", "value": 47389}, {"unit": "% cover", "value": 29.523281458315161}, @@ -187,7 +216,7 @@ def test_json(self): }, { "category": 27605, - "description": "RALEIGH", + "label": "RALEIGH", "units": [ {"unit": "cell counts", "value": 23677}, {"unit": "% cover", "value": 14.750738253361078}, @@ -195,7 +224,7 @@ def test_json(self): }, { "category": 27608, - "description": "RALEIGH", + "label": "RALEIGH", "units": [ {"unit": "cell counts", "value": 43980}, {"unit": "% cover", "value": 27.399479173156237}, @@ -205,7 +234,7 @@ def test_json(self): }, { "category": 5, - "description": "RALEIGH-SOUTH", + "label": "RALEIGH-SOUTH", "units": [ {"unit": "cell counts", "value": 1227632}, {"unit": "% cover", "value": 60.623802469135804}, @@ -213,7 +242,7 @@ def test_json(self): "categories": [ { "category": 27603, - "description": "RALEIGH", + "label": "RALEIGH", "units": [ {"unit": "cell counts", "value": 429179}, {"unit": "% cover", "value": 34.959906551800536}, @@ -221,7 +250,7 @@ def test_json(self): }, { "category": 27606, - "description": "RALEIGH", + "label": "RALEIGH", "units": [ {"unit": "cell counts", "value": 662642}, {"unit": "% cover", "value": 53.977250511553954}, @@ -229,7 +258,7 @@ def test_json(self): }, { "category": 27610, - "description": "RALEIGH", + "label": "RALEIGH", "units": [ {"unit": "cell counts", "value": 135811}, {"unit": "% cover", "value": 11.062842936645509}, @@ -239,7 +268,7 @@ def test_json(self): }, { "category": 6, - "description": "RALEIGH-WEST", + "label": "RALEIGH-WEST", "units": [ {"unit": "cell counts", "value": 208989}, {"unit": "% cover", "value": 10.320444444444444}, @@ -247,7 +276,7 @@ def test_json(self): "categories": [ { "category": 27607, - "description": "RALEIGH", + "label": "RALEIGH", "units": [ {"unit": "cell counts", "value": 208989}, {"unit": "% cover", "value": 100}, @@ -261,40 +290,375 @@ def test_json(self): {"unit": "% cover", "value": 100}, ], } - module = SimpleModule("r.report", map="towns,zipcodes", format="json") self.runModule(module) data = json.loads(module.outputs.stdout) + self._assert_report_equal(reference, data) - keys = ["location", "region", "mask", "maps", "totals"] - for key in keys: - self.assertEqual(reference[key], data[key]) - - for category1, category2 in zip_longest( - reference["categories"], data["categories"] - ): - self.assertEqual(category1["category"], category2["category"]) - self.assertEqual(category1["description"], category2["description"]) - - for unit1, unit2 in zip_longest(category1["units"], category2["units"]): - self.assertEqual(unit1["unit"], unit2["unit"]) - self.assertAlmostEqual(unit1["value"], unit2["value"], places=6) - - for sub_category1, sub_category2 in zip_longest( - category1["categories"], category2["categories"] - ): - self.assertEqual(sub_category1["category"], sub_category2["category"]) - self.assertEqual( - sub_category1["description"], sub_category2["description"] - ) - - for sub_unit1, sub_unit2 in zip_longest( - sub_category1["units"], sub_category2["units"] - ): - self.assertEqual(sub_unit1["unit"], sub_unit2["unit"]) - self.assertAlmostEqual( - sub_unit1["value"], sub_unit2["value"], places=6 - ) + def test_json2(self): + """Test JSON format with more options""" + reference = { + "location": "nc_spm_08_grass7", + "created": "Thu Jul 18 14:21:20 2024", + "region": { + "north": 228500, + "south": 215000, + "east": 645000, + "west": 630000, + "ew_res": 10, + "ns_res": 10, + }, + "mask": None, + "maps": [ + { + "name": "towns", + "description": "PERMANENT", + "layer": "towns", + "type": "raster", + }, + { + "name": "elevation", + "description": "PERMANENT", + "layer": "elevation", + "type": "raster", + }, + ], + "categories": [ + { + "category": 1, + "label": "CARY", + "units": [ + {"unit": "square miles", "value": 10.07143619536385}, + {"unit": "square meters", "value": 26084900}, + {"unit": "square kilometers", "value": 26.084899999999998}, + {"unit": "acres", "value": 6445.719165032852}, + {"unit": "hectares", "value": 2608.4900000000002}, + {"unit": "cell counts", "value": 260849}, + {"unit": "% cover", "value": 12.881432098765432}, + ], + "categories": [ + { + "category": 1, + "label": "from to", + "range": { + "from": 55.578792572021484, + "to": 105.9543285369873, + }, + "units": [ + {"unit": "square miles", "value": 0.9655642780829489}, + {"unit": "square meters", "value": 2500800}, + {"unit": "square kilometers", "value": 2.5008}, + {"unit": "acres", "value": 617.9611379730862}, + {"unit": "hectares", "value": 250.08}, + {"unit": "cell counts", "value": 25008}, + {"unit": "% cover", "value": 9.58715578744791}, + ], + }, + { + "category": 2, + "label": "from to", + "range": { + "from": 105.9543285369873, + "to": 156.32986450195312, + }, + "units": [ + {"unit": "square miles", "value": 9.1058719172809}, + {"unit": "square meters", "value": 23584100}, + {"unit": "square kilometers", "value": 23.5841}, + {"unit": "acres", "value": 5827.758027059766}, + {"unit": "hectares", "value": 2358.4100000000003}, + {"unit": "cell counts", "value": 235841}, + {"unit": "% cover", "value": 90.41284421255209}, + ], + }, + ], + }, + { + "category": 2, + "label": "GARNER", + "units": [ + {"unit": "square miles", "value": 5.4661254789171165}, + {"unit": "square meters", "value": 14157200}, + {"unit": "square kilometers", "value": 14.1572}, + {"unit": "acres", "value": 3498.3203065069483}, + {"unit": "hectares", "value": 1415.72}, + {"unit": "cell counts", "value": 141572}, + {"unit": "% cover", "value": 6.99120987654321}, + ], + "categories": [ + { + "category": 1, + "label": "from to", + "range": { + "from": 55.578792572021484, + "to": 105.9543285369873, + }, + "units": [ + {"unit": "square miles", "value": 4.24917008540718}, + {"unit": "square meters", "value": 11005300}, + {"unit": "square kilometers", "value": 11.0053}, + {"unit": "acres", "value": 2719.4688546605908}, + {"unit": "hectares", "value": 1100.53}, + {"unit": "cell counts", "value": 110053}, + {"unit": "% cover", "value": 77.73641680558302}, + ], + }, + { + "category": 2, + "label": "from to", + "range": { + "from": 105.9543285369873, + "to": 156.32986450195312, + }, + "units": [ + {"unit": "square miles", "value": 1.2169553935099355}, + {"unit": "square meters", "value": 3151900}, + {"unit": "square kilometers", "value": 3.1519}, + {"unit": "acres", "value": 778.8514518463573}, + {"unit": "hectares", "value": 315.19}, + {"unit": "cell counts", "value": 31519}, + {"unit": "% cover", "value": 22.263583194416974}, + ], + }, + ], + }, + { + "category": 3, + "label": "APEX", + "units": [ + {"unit": "square miles", "value": 0.9823983321953995}, + {"unit": "square meters", "value": 2544400}, + {"unit": "square kilometers", "value": 2.5444}, + {"unit": "acres", "value": 628.7349326050546}, + {"unit": "hectares", "value": 254.44000000000003}, + {"unit": "cell counts", "value": 25444}, + {"unit": "% cover", "value": 1.2564938271604937}, + ], + "categories": [ + { + "category": 1, + "label": "from to", + "range": { + "from": 55.578792572021484, + "to": 105.9543285369873, + }, + "units": [ + {"unit": "square miles", "value": 0.03262563239683668}, + {"unit": "square meters", "value": 84500}, + { + "unit": "square kilometers", + "value": 0.08449999999999999, + }, + {"unit": "acres", "value": 20.880404733975443}, + {"unit": "hectares", "value": 8.450000000000001}, + {"unit": "cell counts", "value": 845}, + {"unit": "% cover", "value": 3.321018707750354}, + ], + }, + { + "category": 2, + "label": "from to", + "range": { + "from": 105.9543285369873, + "to": 156.32986450195312, + }, + "units": [ + {"unit": "square miles", "value": 0.9497726997985628}, + {"unit": "square meters", "value": 2459900}, + { + "unit": "square kilometers", + "value": 2.4598999999999998, + }, + {"unit": "acres", "value": 607.8545278710792}, + {"unit": "hectares", "value": 245.99}, + {"unit": "cell counts", "value": 24599}, + {"unit": "% cover", "value": 96.67898129224965}, + ], + }, + ], + }, + { + "category": 4, + "label": "RALEIGH-CITY", + "units": [ + {"unit": "square miles", "value": 6.1974801876282175}, + {"unit": "square meters", "value": 16051400}, + {"unit": "square kilometers", "value": 16.0514}, + {"unit": "acres", "value": 3966.387320082052}, + {"unit": "hectares", "value": 1605.14}, + {"unit": "cell counts", "value": 160514}, + {"unit": "% cover", "value": 7.926617283950617}, + ], + "categories": [ + { + "category": 1, + "label": "from to", + "range": { + "from": 55.578792572021484, + "to": 105.9543285369873, + }, + "units": [ + {"unit": "square miles", "value": 5.062455672160989}, + {"unit": "square meters", "value": 13111700}, + { + "unit": "square kilometers", + "value": 13.111699999999999, + }, + {"unit": "acres", "value": 3239.971630183027}, + {"unit": "hectares", "value": 1311.17}, + {"unit": "cell counts", "value": 131117}, + {"unit": "% cover", "value": 81.68570965772456}, + ], + }, + { + "category": 2, + "label": "from to", + "range": { + "from": 105.9543285369873, + "to": 156.32986450195312, + }, + "units": [ + {"unit": "square miles", "value": 1.1350245154672285}, + {"unit": "square meters", "value": 2939700}, + { + "unit": "square kilometers", + "value": 2.9396999999999998, + }, + {"unit": "acres", "value": 726.415689899025}, + {"unit": "hectares", "value": 293.97}, + {"unit": "cell counts", "value": 29397}, + {"unit": "% cover", "value": 18.31429034227544}, + ], + }, + ], + }, + { + "category": 5, + "label": "RALEIGH-SOUTH", + "units": [ + {"unit": "square miles", "value": 47.39913650957801}, + {"unit": "square meters", "value": 122763200}, + {"unit": "square kilometers", "value": 122.7632}, + {"unit": "acres", "value": 30335.44736612987}, + {"unit": "hectares", "value": 12276.32}, + {"unit": "cell counts", "value": 1227632}, + {"unit": "% cover", "value": 60.6238024691358}, + ], + "categories": [ + { + "category": 1, + "label": "from to", + "range": { + "from": 55.578792572021484, + "to": 105.9543285369873, + }, + "units": [ + {"unit": "square miles", "value": 24.823086925931666}, + {"unit": "square meters", "value": 64291500}, + {"unit": "square kilometers", "value": 64.2915}, + {"unit": "acres", "value": 15886.775632596238}, + {"unit": "hectares", "value": 6429.150000000001}, + {"unit": "cell counts", "value": 642915}, + {"unit": "% cover", "value": 52.37033573579053}, + ], + }, + { + "category": 2, + "label": "from to", + "range": { + "from": 105.9543285369873, + "to": 156.32986450195312, + }, + "units": [ + {"unit": "square miles", "value": 22.576049583646338}, + {"unit": "square meters", "value": 58471700}, + {"unit": "square kilometers", "value": 58.4717}, + {"unit": "acres", "value": 14448.671733533633}, + {"unit": "hectares", "value": 5847.17}, + {"unit": "cell counts", "value": 584717}, + {"unit": "% cover", "value": 47.62966426420947}, + ], + }, + ], + }, + { + "category": 6, + "label": "RALEIGH-WEST", + "units": [ + {"unit": "square miles", "value": 8.069110401162725}, + {"unit": "square meters", "value": 20898900}, + {"unit": "square kilometers", "value": 20.898899999999998}, + {"unit": "acres", "value": 5164.230656744135}, + {"unit": "hectares", "value": 2089.8900000000003}, + {"unit": "cell counts", "value": 208989}, + {"unit": "% cover", "value": 10.320444444444444}, + ], + "categories": [ + { + "category": 1, + "label": "from to", + "range": { + "from": 55.578792572021484, + "to": 105.9543285369873, + }, + "units": [ + {"unit": "square miles", "value": 0.23822503182068916}, + {"unit": "square meters", "value": 617000}, + {"unit": "square kilometers", "value": 0.617}, + {"unit": "acres", "value": 152.4640203652408}, + {"unit": "hectares", "value": 61.7}, + {"unit": "cell counts", "value": 6170}, + {"unit": "% cover", "value": 2.9523084947054627}, + ], + }, + { + "category": 2, + "label": "from to", + "range": { + "from": 105.9543285369873, + "to": 156.32986450195312, + }, + "units": [ + {"unit": "square miles", "value": 7.8308853693420355}, + {"unit": "square meters", "value": 20281900}, + {"unit": "square kilometers", "value": 20.2819}, + {"unit": "acres", "value": 5011.766636378894}, + {"unit": "hectares", "value": 2028.19}, + {"unit": "cell counts", "value": 202819}, + {"unit": "% cover", "value": 97.04769150529454}, + ], + }, + ], + }, + ], + "totals": [ + {"unit": "square miles", "value": 78.18568710484531}, + {"unit": "square meters", "value": 202500000}, + {"unit": "square kilometers", "value": 202.5}, + {"unit": "acres", "value": 50038.839747100916}, + {"unit": "hectares", "value": 20250}, + {"unit": "cell counts", "value": 2025000}, + {"unit": "% cover", "value": 100}, + ], + } + module = SimpleModule( + "r.report", + map="towns,elevation", + units=[ + "miles", + "meters", + "kilometers", + "acres", + "hectares", + "cells", + "percent", + ], + nsteps=2, + format="json", + ) + self.runModule(module) + data = json.loads(module.outputs.stdout) + self._assert_report_equal(reference, data) if __name__ == "__main__":