Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

r.report: add JSON support #3935

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

kritibirda26
Copy link
Contributor

Using parson, add JSON output support to r.report module.

Sample JSON output is according to the discussion in #3033, example:

{
    "location": "nc_spm_08_grass7",
    "created": "Fri Jun 28 18:41:29 2024",
    "region": {
        "north": 228500,
        "south": 215000,
        "east": 645000,
        "west": 630000,
        "ew_res": 10,
        "ns_res": 10
    },
    "mask": "none",
    "maps": [
        {
            "name": "zipcodes@PERMANENT",
            "description": "PERMANENT",
            "layer": "zipcodes@PERMANENT",
            "type": "raster"
        },
        {
            "name": "landclass96@PERMANENT",
            "description": "PERMANENT",
            "layer": "landclass96@PERMANENT",
            "type": "raster"
        }
    ],
    "categories": [
        {
            "category": 27511,
            "description": "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
                        }
                    ]
                },
                {
                    "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": "% cover",
                    "value": 1.2564938271604937
                }
            ],
            "categories": [
                {
                    "category": 1,
                    "description": "developed",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 2.46
                        },
                        {
                            "unit": "% cover",
                            "value": 0.96682911491903789
                        }
                    ]
                },
                {
                    "category": 2,
                    "description": "agriculture",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 28.720000000000002
                        },
                        {
                            "unit": "% cover",
                            "value": 11.28753340669706
                        }
                    ]
                },
                {
                    "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
                },
                {
                    "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": "% cover",
                    "value": 21.194024691358024
                }
            ],
            "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",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 216.73000000000002
                        },
                        {
                            "unit": "% cover",
                            "value": 5.0498742948746331
                        }
                    ]
                },
                {
                    "category": 5,
                    "description": "forest",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 2036.2900000000002
                        },
                        {
                            "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": "hectares",
                            "value": 338.04000000000002
                        },
                        {
                            "unit": "% cover",
                            "value": 71.333009770199837
                        }
                    ]
                },
                {
                    "category": 3,
                    "description": "herbaceous",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 49.710000000000001
                        },
                        {
                            "unit": "% cover",
                            "value": 10.489776108379582
                        }
                    ]
                },
                {
                    "category": 5,
                    "description": "forest",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 83.77000000000001
                        },
                        {
                            "unit": "% cover",
                            "value": 17.677098060731392
                        }
                    ]
                },
                {
                    "category": -2147483648,
                    "description": "no data",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 2.3700000000000001
                        },
                        {
                            "unit": "% cover",
                            "value": 0.50011606068918946
                        }
                    ]
                }
            ]
        },
        {
            "category": 27605,
            "description": "RALEIGH",
            "units": [
                {
                    "unit": "hectares",
                    "value": 236.77000000000001
                },
                {
                    "unit": "% cover",
                    "value": 1.1692345679012346
                }
            ],
            "categories": [
                {
                    "category": 1,
                    "description": "developed",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 187.45000000000002
                        },
                        {
                            "unit": "% cover",
                            "value": 79.169658318199097
                        }
                    ]
                },
                {
                    "category": 3,
                    "description": "herbaceous",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 5.71
                        },
                        {
                            "unit": "% cover",
                            "value": 2.4116230941419943
                        }
                    ]
                },
                {
                    "category": 4,
                    "description": "shrubland",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 6.6000000000000005
                        },
                        {
                            "unit": "% cover",
                            "value": 2.7875153102166661
                        }
                    ]
                },
                {
                    "category": 5,
                    "description": "forest",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 37.010000000000005
                        },
                        {
                            "unit": "% cover",
                            "value": 15.631203277442243
                        }
                    ]
                }
            ]
        },
        {
            "category": 27606,
            "description": "RALEIGH",
            "units": [
                {
                    "unit": "hectares",
                    "value": 6626.4200000000001
                },
                {
                    "unit": "% cover",
                    "value": 32.723061728395059
                }
            ],
            "categories": [
                {
                    "category": 1,
                    "description": "developed",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 1030.6200000000001
                        },
                        {
                            "unit": "% cover",
                            "value": 15.553194636017638
                        }
                    ]
                },
                {
                    "category": 2,
                    "description": "agriculture",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 115.23
                        },
                        {
                            "unit": "% cover",
                            "value": 1.7389480292525978
                        }
                    ]
                },
                {
                    "category": 3,
                    "description": "herbaceous",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 631.49000000000001
                        },
                        {
                            "unit": "% cover",
                            "value": 9.5298818970122632
                        }
                    ]
                },
                {
                    "category": 4,
                    "description": "shrubland",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 682.66000000000008
                        },
                        {
                            "unit": "% cover",
                            "value": 10.302093739907823
                        }
                    ]
                },
                {
                    "category": 5,
                    "description": "forest",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 3807.6200000000003
                        },
                        {
                            "unit": "% cover",
                            "value": 57.461193223490213
                        }
                    ]
                },
                {
                    "category": 6,
                    "description": "water",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 349.17000000000002
                        },
                        {
                            "unit": "% cover",
                            "value": 5.2693611331608921
                        }
                    ]
                },
                {
                    "category": -2147483648,
                    "description": "no data",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 9.6300000000000008
                        },
                        {
                            "unit": "% cover",
                            "value": 0.14532734115857432
                        }
                    ]
                }
            ]
        },
        {
            "category": 27607,
            "description": "RALEIGH",
            "units": [
                {
                    "unit": "hectares",
                    "value": 2089.8900000000003
                },
                {
                    "unit": "% cover",
                    "value": 10.320444444444444
                }
            ],
            "categories": [
                {
                    "category": 1,
                    "description": "developed",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 727.16000000000008
                        },
                        {
                            "unit": "% cover",
                            "value": 34.794175770016601
                        }
                    ]
                },
                {
                    "category": 3,
                    "description": "herbaceous",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 329.22000000000003
                        },
                        {
                            "unit": "% cover",
                            "value": 15.752982214374919
                        }
                    ]
                },
                {
                    "category": 4,
                    "description": "shrubland",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 126.89
                        },
                        {
                            "unit": "% cover",
                            "value": 6.0716114245247361
                        }
                    ]
                },
                {
                    "category": 5,
                    "description": "forest",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 894.04000000000008
                        },
                        {
                            "unit": "% cover",
                            "value": 42.77928503414055
                        }
                    ]
                },
                {
                    "category": 6,
                    "description": "water",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 7.1400000000000006
                        },
                        {
                            "unit": "% cover",
                            "value": 0.34164477556235018
                        }
                    ]
                },
                {
                    "category": 7,
                    "description": "sediment",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 5.4400000000000004
                        },
                        {
                            "unit": "% cover",
                            "value": 0.26030078138083823
                        }
                    ]
                }
            ]
        },
        {
            "category": 27608,
            "description": "RALEIGH",
            "units": [
                {
                    "unit": "hectares",
                    "value": 439.80000000000001
                },
                {
                    "unit": "% cover",
                    "value": 2.1718518518518519
                }
            ],
            "categories": [
                {
                    "category": 1,
                    "description": "developed",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 307.54000000000002
                        },
                        {
                            "unit": "% cover",
                            "value": 69.927239654388359
                        }
                    ]
                },
                {
                    "category": 3,
                    "description": "herbaceous",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 1.72
                        },
                        {
                            "unit": "% cover",
                            "value": 0.39108685766257389
                        }
                    ]
                },
                {
                    "category": 5,
                    "description": "forest",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 130.54000000000002
                        },
                        {
                            "unit": "% cover",
                            "value": 29.681673487949066
                        }
                    ]
                }
            ]
        },
        {
            "category": 27610,
            "description": "RALEIGH",
            "units": [
                {
                    "unit": "hectares",
                    "value": 1358.1100000000001
                },
                {
                    "unit": "% cover",
                    "value": 6.7067160493827158
                }
            ],
            "categories": [
                {
                    "category": 1,
                    "description": "developed",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 757.76999999999998
                        },
                        {
                            "unit": "% cover",
                            "value": 55.79592227433713
                        }
                    ]
                },
                {
                    "category": 3,
                    "description": "herbaceous",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 68.219999999999999
                        },
                        {
                            "unit": "% cover",
                            "value": 5.0231571816715874
                        }
                    ]
                },
                {
                    "category": 4,
                    "description": "shrubland",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 5.8700000000000001
                        },
                        {
                            "unit": "% cover",
                            "value": 0.43221830337748784
                        }
                    ]
                },
                {
                    "category": 5,
                    "description": "forest",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 500.04000000000002
                        },
                        {
                            "unit": "% cover",
                            "value": 36.81881438175111
                        }
                    ]
                },
                {
                    "category": 6,
                    "description": "water",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 1.9200000000000002
                        },
                        {
                            "unit": "% cover",
                            "value": 0.14137293739093298
                        }
                    ]
                },
                {
                    "category": -2147483648,
                    "description": "no data",
                    "units": [
                        {
                            "unit": "hectares",
                            "value": 24.290000000000003
                        },
                        {
                            "unit": "% cover",
                            "value": 1.7885149214717513
                        }
                    ]
                }
            ]
        }
    ],
    "totals": [
        {
            "unit": "hectares",
            "value": 20250
        },
        {
            "unit": "% cover",
            "value": 100
        }
    ]
}
@github-actions github-actions bot added raster Related to raster data processing Python Related code is in Python C Related code is in C HTML Related code is in HTML module docs tests Related to Test Suite labels Jun 28, 2024
Copy link

codecov bot commented Jun 28, 2024

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 ☂️

JSON_Object *object = json_object(object_value);

CELL *cats = Gstats[ns].cats;
char *cp = construct_cat_label(nl, cats[nl]);
Copy link
Contributor Author

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?

Copy link
Contributor

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.

@echoix echoix added this to the 8.5.0 milestone Jun 30, 2024
@wenzeslaus
Copy link
Member

Considering the following:

{
"mask": "none",
"description": "no data"
}

We should not output special strings for "null" values. Python uses null for None:

>>> import json
>>> json.dumps({"max": None})
'{"max": null}'
@cwhite911
Copy link
Contributor

@wenzeslaus I agree that we should not output special strings for "null" values.

@kritibirda26
Copy link
Contributor Author

Hi @wenzeslaus and @cwhite911,

I have made the requested changes but the code segfaults when -c flag is used. I used gdb and found the errornous stack trace to be:

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?

@echoix echoix changed the title r.report: add json support Jul 10, 2024
@marisn
Copy link
Contributor

marisn commented Jul 11, 2024

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 i passed to Rast_get_ith_d_cat does not make sense. That would indicate on cats[nl] making no sense. Does if (!Rast_is_c_null_value(&cats[nl])) check help?

@kritibirda26
Copy link
Contributor Author

Hi @marisn! Sorry I forgot to mention the command. It was r.report -c map=elevation units=mi format=json. And thanks for the pointer, it helped me realised the issue. I already had that check but was using if instead of else if. The segfault is now fixed.

Copy link
Contributor

@cwhite911 cwhite911 left a 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",
Copy link
Contributor

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)
Copy link
Contributor

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]);
Copy link
Contributor

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);
Copy link
Contributor

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)
Copy link
Contributor

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";
Copy link
Contributor

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.

Copy link
Contributor

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
Copy link
Contributor

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C Related code is in C docs HTML Related code is in HTML module Python Related code is in Python raster Related to raster data processing tests Related to Test Suite
5 participants