-
-
Notifications
You must be signed in to change notification settings - Fork 291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
r.report: add JSON support #3935
base: main
Are you sure you want to change the base?
Conversation
Welcome to Codecov 🎉Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests. Thanks for integrating Codecov - We've got you covered ☂️ |
raster/r.report/prt_json.c
Outdated
JSON_Object *object = json_object(object_value); | ||
|
||
CELL *cats = Gstats[ns].cats; | ||
char *cp = construct_cat_label(nl, cats[nl]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How should ranged categories be represented?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could represent continuous datasets similarly to how it is already represented in the shell output to make things easier, and just add an additional range object.
For example here is the basic output for the elevation
dataset.
r.report map=elevation@PERMANENT nsteps=3
+-----------------------------------------------------------------------------+
| RASTER MAP CATEGORY REPORT |
|PROJECT: nc_spm_08_grass7 Mon Jul 1 11:51:57 2024|
|-----------------------------------------------------------------------------|
| north: 228500 east: 645000 |
|REGION south: 215000 west: 630000 |
| res: 10 res: 10 |
|-----------------------------------------------------------------------------|
|MASK: none |
|-----------------------------------------------------------------------------|
|MAP: South-West Wake county: Elevation NED 10m (elevation@PERMANENT in PERMAN|
|-----------------------------------------------------------------------------|
| Category Information | cell| % |
| #|description | count| cover|
|-----------------------------------------------------------------------------|
| 55.578793-89.162483|from to . . . . . . . . . . . . . . . .| 329003| 16.25|
| 89.162483-122.746174|from to . . . . . . . . . . . . . . . .|1075075| 53.09|
|122.746174-156.329865|from to . . . . . . . . . . . . . . . .| 620922| 30.66|
|-----------------------------------------------------------------------------|
|TOTAL |2025000|100.00|
+-----------------------------------------------------------------------------+
And here is the suggested json output.
{
"category": "55.578793-89.162483"
"description": "from to",
"range": {
"from": 55.578793,
"to": 89.162483
}
}
If the user uses the -c
flag the category will be -nan--nan
and the description will be no data
.
Considering the following: {
"mask": "none",
"description": "no data"
} We should not output special strings for "null" values. Python uses >>> import json
>>> json.dumps({"max": None})
'{"max": null}' |
@wenzeslaus I agree that we should not output special strings for "null" values. |
Hi @wenzeslaus and @cwhite911, I have made the requested changes but the code segfaults when Thread 1 "r.report" received signal SIGSEGV, Segmentation fault.
Rast_quant_get_ith_rule (q=0x555555594808, i=-2147483648, dLow=0x7fffffffd100, dHigh=0x7fffffffd108, cLow=0x7fffffffd0d4, cHigh=0x7fffffffd0d4) at quant.c:330
330 *dLow = q->table[i].dLow;
(gdb) bt
#0 Rast_quant_get_ith_rule (q=0x555555594808, i=-2147483648, dLow=0x7fffffffd100, dHigh=0x7fffffffd108, cLow=0x7fffffffd0d4, cHigh=0x7fffffffd0d4) at quant.c:330
#1 0x00007ffff7f9041e in Rast_get_ith_d_cat (pcats=0x5555555947e0, i=-2147483648, rast1=0x7fffffffd100, rast2=0x7fffffffd108) at cats.c:1039
#2 0x000055555555a30c in make_category (ns=211, nl=0, sub_categories=0x0) at prt_json.c:75
#3 0x000055555555a68a in make_categories (start=0, end=212, level=0) at prt_json.c:110
#4 0x000055555555adfb in print_json () at prt_json.c:215
#5 0x000055555555c9bd in report () at report.c:9
#6 0x0000555555558b1e in main (argc=4, argv=0x7fffffffd598) at main.c:77 But I am not sure how to fix this issue. Can you please help with it? |
Next time, please, provide the command you used to ease reproduction of the issue. I ran the code but it did not crash. Looking at the gdb bt output, value of |
Hi @marisn! Sorry I forgot to mention the command. It was |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kritibirda26 Nice job on the revision. We still have a couple of items to clean up, but it's almost there.
"ew_res": 10, | ||
"ns_res": 10, | ||
}, | ||
"mask": "none", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The mask values should be None
return units_value; | ||
} | ||
|
||
JSON_Value *make_category(int ns, int nl, JSON_Value *sub_categories) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The values for the description
key are not displaying correctly for layers with cat labels. Instead the cat values are getting set for both the category
and description
.
char str[500]; | ||
|
||
if (!is_fp[nl] || as_int) | ||
json_object_set_number(object, "description", cats[nl]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets change the key name from description
to label
.
DMIN[nl]; | ||
dHigh = (DMAX[nl] - DMIN[nl]) / (double)nsteps * (double)cats[nl] + | ||
DMIN[nl]; | ||
char *from = Rast_get_d_cat(&dLow, &layers[nl].labels); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The string "from to" in this case is the label. I can't think of any cases where the an additional category label will be provide, so you can remove
char *from = Rast_get_d_cat(&dLow, &layers[nl].labels);
char *to = Rast_get_d_cat(&dHigh, &layers[nl].labels);
and just set the string to "from to"
DCELL dLow, dHigh; | ||
char str[500]; | ||
|
||
if (!is_fp[nl] || as_int) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can address the comment above by with the following code.
if (!is_fp[nl] || as_int)
json_object_set_string(object, "description", Rast_get_c_cat(&cats[nl], &layers[nl].labels));
for (int i = 0; i < nunits; i++) { | ||
switch (unit[i].type) { | ||
case CELL_COUNTS: | ||
unit[i].label[0] = " cell"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move the constants to global.h so we don't duplicate the labels and factors here and in prt_report.c.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add additional tests mixing continuous an discrete layers, printing all stats, restricting null, etc..
r.report -n -a map=towns,elevation units=miles,meters,kilometers,acres,hectares,cells,percent nsteps=2 format=json
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a GRASS Jupyter example displaying the results of the following command.
g.region raster=landuse96_28m res=30 -ap
r.report towns,landuse96_28m unit=c,h,p format=json
Using parson, add JSON output support to r.report module.
Sample JSON output is according to the discussion in #3033, example: