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

[Feat] Add JSON output to r.describe #4903

Open
NishantBansal2003 opened this issue Jan 1, 2025 · 5 comments · May be fixed by #4918
Open

[Feat] Add JSON output to r.describe #4903

NishantBansal2003 opened this issue Jan 1, 2025 · 5 comments · May be fixed by #4918
Labels
enhancement New feature or request

Comments

@NishantBansal2003
Copy link
Contributor

Add JSON output for easy parsing of r.describe output.

Describe the solution you'd like
Add an option for format, such as format=plain for the current output format and format=json for the JSON output.

A possible JSON layout could be:

[
  {
    "values": "0.996534-8.677654"
  },
  {
    "values": "8.677654-16.358774"
  },
  {
    "values": "16.358774-24.040000"
  }
]

Describe alternatives you've considered
Alternatively(helpful for numerical operations or parsing), the JSON layout could look like this:

[
  {
    "min": "0.996534",
    "max": "8.677654"
  },
  {
    "min": "8.677654",
    "max": "16.358774"
  },
  {
    "min": "16.358774",
    "max": "24.040000"
  }
]
@NishantBansal2003 NishantBansal2003 added the enhancement New feature or request label Jan 1, 2025
@NishantBansal2003
Copy link
Contributor Author

Hey @echoix, I was trying to write tests for the above change, and I am using this raster map:

map = if(row() == 5 && col() == 5, null(), if(row() % 2 == 0, col() * 0.5, -col() * 0.5))

When I run the following command:

r.describe -1 map

I get the following output:

*
-5.000000--4.960784
-4.529412--4.490196
-4.019608--3.980392
-3.509804--3.470588
-3.039216--3.000000
-2.529412--2.490196
-2.019608--1.980392
-1.549020--1.509804
-1.039216--1.000000
-0.529412--0.490196
0.450980-0.490196
0.960784-1.000000
1.470588-1.509804
1.941176-1.980392
2.450980-2.490196
2.960784-3.000000
3.431373-3.470588
3.941176-3.980392
4.450980-4.490196
4.960784-5.000000

Is this output format correct(for negative values)? Also, when should we use the "-" symbol and "thru" in the r.describe output?

@NishantBansal2003 NishantBansal2003 linked a pull request Jan 5, 2025 that will close this issue
@echoix
Copy link
Member

echoix commented Jan 5, 2025

Can another "expert" or someone knowing r.describe jump in? I don't know what to answer here

@neteler
Copy link
Member

neteler commented Jan 5, 2025

The thru of r.describe is understood by e.g. r.reclass and other commands. Hence, it is the official syntax (yes, looks oldish but works since ~1984).

The output of r.describe is also (to remain) machine readable:

ag --python r.describe
scripts/d.rast.leg/d.rast.leg.py
123:    cats = gs.read_command("r.describe", map=map, flags="1n")

scripts/r.grow/testsuite/test_r_grow.py
61:        shrined_range = SimpleModule("r.describe", flags="i", _map=self.mapShrunkNoNULL)

gui/wxpython/rdigit/controller.py
466:                        "r.describe", flags="1n", map=name, quiet=True

Of course, with increasing JSON support we get modern options here.

@petrasovaa
Copy link
Contributor

Sorry coming late to this, but I would like to suggest alternative schema. The JSON output should have the raw numbers. Unfortunately the r.describe output is pretty complex and depends on CELL/FCELL, so this suggestion tries to capture it:

Integer maps current behavior:

r.describe my_int_map 
* 1-3 10-12 15

r.describe my_int_map -r
1 thru 15
*

r.describe my_int_map -1
*
1
2
3
10
11
12
15

With json:

r.describe my_int_map format=json
{"ranges": [{"min": 1, "max": 3}, {"min": 10, "max": 12}, {"min": 15, "max": 15}],
"has_nulls": true}

r.describe my_int_map -r format=json
{"min": 1, "max": 15, "has_nulls": true}

r.describe my_int_map -1 format=json
{"values": [1,2,3,4,5,10,11,12,15],
"has_nulls": true}

Floating point maps current behavior:

r.describe elevation
55.578793-156.329865

r.describe elevation -r
55.578793-156.329865

r.describe elevation -1 nsteps=3
55.578793-89.162483
89.162483-122.746174
122.746174-156.329865

With json:

r.describe elevation format=json
{"ranges": [{"min": 55.578793, "max": 156.329865}],
"has_nulls": false}

r.describe elevation format=json -r
{"min": 55.578793, "max": 156.329865, "has_nulls": false}

r.describe elevation -1 nsteps=3 format=json
{"ranges": [{"min": 55.578793, "max": 89.162483}, {"min": 89.162483, "max": 122.746174}, {"min": 122.746174, "max": 156.329865}],
"has_nulls": false}

@nilason nilason moved this to In Progress in GRASS JSON Outputs Jan 15, 2025
@NishantBansal2003
Copy link
Contributor Author

Hey @petrasovaa, should we ignore the 'nv' option for null value strings in JSON?
I suggest using something like "null_value": "*". wdyt?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: In Progress
Development

Successfully merging a pull request may close this issue.

4 participants