GRASS GSoC 2025 Add JSON output to different tools in C
Student Name | Nishant Bansal |
Organization | OSGeo - Open Source Geospatial Foundation |
Mentor Name | Anna Petrasova, Corey White, Vaclav Petras |
GitHub Fork | View Repo |
GSoC proposal | View Proposal |
Abstract
Currently, most GRASS tools produce data in plain text by default. This means that users who want to process the output in Python need to create custom parsing code. To make this easier, several modules could be improved by offering JSON as an alternative output format. Recently, the Parson library was added to the GRASS codebase to support JSON output in addition to plain text. Building on the advancements made during the last GSoC, my project aims to expand JSON output capabilities to more tools using the Parson library. I will also work on making the JSON structure introduced in the previous GSoC more useful and consistent. Additionally, I plan to add support for SHELL and CSV output formats to the relevant tools where appropriate.
Goal
- Working with mentors to finalize the structure of the JSON output.
- Adding an option to choose the output format (plain text or JSON) for each updated tool.
- Make the JSON structure introduced during the last GSoC more useful and consistent across tools.
- Developing Python test cases to ensure that the JSON output works correctly and to avoid future issues.
- Providing basic documentation and example JSON outputs for each modified tool.
- Adding support for SHELL and CSV output formats to the relevant tools where appropriate.
- Develop simple examples demonstrating how to use JSON output in a data science workflow, such as reading JSON data into pandas or python.
Timeline
Period | Timeline | Tasks | Status |
---|---|---|---|
Bonding Period |
May 8 - June 1 |
|
OK |
Official Coding Period | |||
Week 1 (June 2 - June 8) |
OK | ||
Week 2 (June 9 - June 15) |
OK | ||
Week 3 (June 16 - June 22) |
OK | ||
Week 4 (June 23 - June 29) |
OK | ||
Week 5 (June 30 - July 6) |
OK | ||
Week 6 (July 7 - July 13) |
OK | ||
Week 7 (July 14 - July 20) |
OK | ||
Week 8 (July 21 - July 27) |
OK | ||
Week 9 (July 28 - August 3) |
OK | ||
Week 10 (August 4 - August 10) |
|
OK | |
Week 11 (August 11 - August 17) |
OK | ||
Week 12 (August 18 - August 24) |
|
OK | |
Evaluation Period | |||
August 25 - September 1 |
|
OK |
Reports
Community Bonding Period
What did I get done this week?
- Introduced myself on the OSGeo Discourse mailing list.
- Created both my personal Wiki page and the project Wiki page, adding all necessary project-related information.
- Read GRASS documentation to support the addition of Python and JSON examples.
- Worked on the following pull requests:
What do I plan on doing next week?
- Add JSON output support to the g.mapset and r.what.color modules.
- If time permits, I will begin improving the r.info interface: Issue #5793
Am I blocked on anything?
- No, I am not currently blocked on anything.
Project Discussions and Meetings
- 14 May 2025: Introductory meeting discussing JSON support and overall project milestones.
- 21 May 2025: Introduction with fellow developers. Discussed priority tools, adding Python/Pandas examples, and approaches for finalizing the JSON schema for each module.
Week-1
What did I get done this week?
- g.mapset: Added JSON output support, now you can use -p and -l flags to get JSON-formatted results.
- r.univar: Fixed a bug causing an uninitialized zone_value.
- r.what.color: Implemented JSON output support. (See PR #5822)
- r.info (regression tests): To maintain backward compatibility while updating its interface, I added strong regression tests.
- r.info (interface improvements): Began improving the module’s interface. (See PR #5859)
What do I plan on doing next week?
- Address review comments on my open PRs.
- Add JSON output support to g.version (see issue #5816).
- Add JSON output support to i.group (see issue #5815).
- If time permits, write tests for r.stats (see issue #5814).
Am I blocked on anything?
- No, I am not currently blocked on anything.
Week-2
What did I get done this week?
- g.version: Implemented JSON output support. (See PR #5863)
- i.group: Implemented JSON output support. (See PR #5870)
- r.stats (regression tests): To maintain backward compatibility while adding JSON format, I added strong regression tests.
- r.stats: Added tests for JSON output; this uncovered a bug, which was fixed alongside.
What do I plan on doing next week?
- Address review comments on my open PRs.
- Update JSON output format of v.distance (see issue #5846).
- Add JSON output support to g.list (see issue #5848).
- If time permits, fix the JSON output format of g.region (see issue #5828).
Am I blocked on anything?
- No, I am not currently blocked on anything.
Week-3
What did I get done this week?
- v.distance: Updated the JSON output schema. (See PR #5898)
- r.stats: Fixed a bug causing an uninitialized
window
variable. - g.list (regression tests): To maintain backward compatibility while adding JSON format, I added strong regression tests.
- g.list: Implemented JSON output support. (See PR #5921)
- g.region: Updated the JSON output schema. (See PR #5929)
What do I plan on doing next week?
- Address review comments on my open PRs.
- Update JSON output format of v.to.db (see issue #5711).
- Update JSON output format and documentation of r.profile (see issue #5834).
- If time permits, add shell and csv output formats to r.univar (see issue #5824).
Am I blocked on anything?
- No, I am not currently blocked on anything.
Week-4
What did I get done this week?
- v.to.db: Updated the JSON output schema. (See PR #5946)
- r.profile: Updated the JSON output schema. (See PR #5962)
- r.univar: Implemented SHELL and CSV output support. (See PR #5966)
What do I plan on doing next week?
- Add JSON output support to r.regression.line (see issue #5950).
- Add JSON output support to r.stats.quantile (see issue #5951).
- Add JSON output support to r.volume (see issue #5949).
Am I blocked on anything?
- No, I am not currently blocked on anything.
Week-5
What did I get done this week?
- r.regression.line (regression tests): To maintain backward compatibility while adding JSON format, I added strong regression tests.
- r.regression.line: Implemented JSON output support. (See PR #5993)
- r.stats.quantile (regression tests): To maintain backward compatibility while adding JSON format, I added strong regression tests.
- r.stats.quantile: Implemented JSON output support. (See PR #6007)
- r.volume (regression tests): To maintain backward compatibility while adding JSON format, I added strong regression tests.
- r.volume: Implemented JSON output support. (See PR #6036)
What do I plan on doing next week?
- Add JSON output support to m.measure (see issue #6006).
- Add JSON output support to db.columns (see issue #6008).
- Add JSON output support to db.connect (see issue #6009).
Am I blocked on anything?
- No, I am not currently blocked on anything.
Week-6
What did I get done this week?
- db.columns: Implemented JSON output support. (See PR #6042)
- m.measure (regression tests): To maintain backward compatibility while adding JSON format, I added strong regression tests.
- db.connect (regression tests): To maintain backward compatibility while adding JSON format, I added strong regression tests.
- m.measure: Implemented JSON output support. (See PR #6053)
- db.connect: Implemented JSON output support. (See PR #6059)
What do I plan on doing next week?
- Add JSON output support to v.db.connect (see issue #6014).
- Add JSON output support to r.regression.multi (see issue #6010).
- If time permits, add JSON output support to g.findfile (see issue #6005).
- If time permits, add JSON output support to r3.info (see issue #5995).
Am I blocked on anything?
- No, I am not currently blocked on anything.
Week-7
What did I get done this week?
- r.regression.multi (regression tests): To maintain backward compatibility while adding JSON format, I added strong regression tests.
- v.db.connect (regression tests): To maintain backward compatibility while adding JSON format, I added strong regression tests.
- r.regression.multi: Implemented JSON output support. (See PR #6075)
- v.db.connect: Implemented JSON output support. (See PR #6077)
What do I plan on doing next week?
- Address review comments on my open PRs.
- Add JSON output support to g.findfile (see issue #6005).
- Add JSON output support to r3.info (see issue #5995).
- If time permits, add JSON output support to v.what.rast (see issue #6024).
Am I blocked on anything?
- No, I am not currently blocked on anything.
Week-8
What did I get done this week?
- g.findfile: Implemented JSON output support. (See PR #6091)
- r3.info (regression tests): Added regression tests to ensure backward compatibility with new JSON support and fixed a flag-handling bug.
- r3.info: Implemented JSON output support. (See PR #6103)
- v.what.rast (regression tests): Added regression tests to ensure backward compatibility with new JSON support and fixed a sorting bug.
What do I plan on doing next week?
- Address review comments on my open PRs.
- Add JSON output support to v.what.rast (see issue #6024).
- Update format to CSV instead of shell in v.category (see issue #6025).
- Add CSV output support to r.stats (see issue #5835).
- If time permits, add CSV output support to r.profile (see issue #5958).
- If time permits, add CSV headers to r.volume (see issue #6049).
Am I blocked on anything?
- No, I don’t have any blockers this week. However, I’ll be a little busy with some personal work from August 3 to August 10, so I’ll try to complete next week’s tasks this week itself.
Week-9
What did I get done this week?
- v.category: Updated format to CSV instead of shell. (See PR #6132)
- r.stats: Implemented CSV output support. (See PR #6133)
- r.volume: Implemented CSV column headers support. (See PR #6134)
- v.what.rast: Implemented JSON output support. (See PR #6140)
- r.profile: Implemented CSV output support. (See PR #6145)
- r.distance (regression tests): Updated regression tests to ensure backward compatibility with new JSON support. (See PR #6150)
What do I plan on doing next week?
- I’ll be a bit busy with some personal work from August 3 to 10, so I completed next week’s tasks last week itself. I’ve already informed my mentors about my limited availability.
Am I blocked on anything?
- Yes, I’ll be a bit busy with some personal work from August 3 to 10.
Week-10
What did I get done this week?
- This week I was busy with some personal work, so I did not complete any additional tasks. However, I had already finished this week’s tasks last week. This week, I mostly focused on handling reviews for those PRs.
What do I plan on doing next week?
- Add JSON output support to r.distance (see issue #6135).
- Add JSON output support to v.class (see issue #6136).
Am I blocked on anything?
- No, I am not currently blocked on anything.
Week-11
What did I get done this week?
- r.distance: Implemented JSON and CSV output support. (See PR #6204)
- v.class: Updated regression tests to ensure backward compatibility with new JSON support and fixed a out-of-bounds bug. (See PR #6205)
- v.class: Implemented JSON and CSV output support. (See PR #6210)
What do I plan on doing next week?
- Address review comments on my open PRs.
- Discuss next week’s tasks with my mentors, if no issues are assigned, I’ll identify tools that currently lack JSON support and evaluate which ones are feasible to add it to.
- Write the final GSoC report summarizing the work completed, changes merged, and next steps.
Am I blocked on anything?
- No, I am not currently blocked on anything.
Week-12
What did I get done this week?
- v.what: Updated JSON output format. (See PR #6252)
- Addressed review comments on my open PRs.
What do I plan on doing next week?
- Address review comments on my open PRs.
- Write the final GSoC report summarizing the work completed, changes merged, and next steps.
Am I blocked on anything?
- No, I am not currently blocked on anything.
Final Report
Title | Add JSON output to different tools in C for GRASS |
Organization | OSGeo (Open Source Geospatial Foundation) |
Mentor Name | Anna Petrasova, Corey White, Vaclav Petras |
Program | Google Summer of Code 2025 |
Contributions | Pull Requests |
Abstract
During my GSoC project, I addressed the challenge that most GRASS tools produced only plain text output, forcing users to write custom parsing code for Python-based workflows. The task was to make these tools more data-science friendly by adding structured output formats. To achieve this, I extended the use of the Parson library to implement consistent JSON output across multiple modules, refined the JSON structure introduced in the previous GSoC, and added optional support for SHELL and CSV outputs where appropriate. I developed comprehensive Python test cases, ensured backward compatibility, and provided documentation with practical examples such as loading JSON into pandas. As a result, GRASS users can now directly consume structured outputs in modern workflows, reducing parsing overhead, improving interoperability, and paving the way for broader adoption of GRASS in data science contexts.
The state of integration BEFORE the start of GSoC
Before the start of GSoC, the integration of GRASS with data science workflows was limited due to several challenges. The JSON outputs introduced in the previous GSoC contained deeply nested hierarchies, making them difficult to use directly with libraries such as pandas or in broader data science pipelines. While some tools already supported JSON (e.g., r.univar
, r.info
, v.distance
, g.region
, v.to.db
, r.profile
, v.what
), many important and highly demanded modules lacked JSON support, including r3.info
, g.mapset
, r.what.color
, g.version
, i.group
, g.list
, r.regression.line
, r.stats.quantile
, r.volume
, m.measure
and many more. This lack of consistency limited the seamless adoption of GRASS in analytics and machine learning contexts. Moreover, there was no clear documentation of the JSON schema or guidance on how to parse and utilize these outputs effectively in python or pandas, which made it difficult for users to understand the use cases and integrate GRASS outputs into modern data science workflows.
The state of integration AFTER GSoC
After GSoC, the integration state of JSON and alternative output formats in GRASS has significantly improved. I refactored the JSON structures for tools such as r.univar
, r.info
, v.distance
, g.region
, v.to.db
, r.profile
, v.what
, and others, removing unnecessary nesting introduced during the previous GSoC. This makes their outputs much easier to consume directly in Python or with pandas. In addition, JSON support has been extended to many essential and frequently used modules, including r3.info
, g.mapset
, r.what.color
, g.version
, i.group
, g.list
, r.regression.line
, r.stats.quantile
, r.volume
, m.measure
, and many more. I also added detailed documentation of the JSON schema and provided examples showing how to parse and use these outputs effectively with Python and pandas. Together, these improvements make the adoption of GRASS in modern data science workflows far more seamless and accessible.
Enhancements made per tool
r.univar
: Simplified JSON structure for univariate statistics, removed unnecessary hierarchy, and added new output format options (CSV, shell).r.what.color
: Added JSON support for querying raster map colors, with a Python parsing example.g.mapset
: Added JSON support for reporting the current mapset, with a Python example.r.info
: Updated JSON structure for raster map metadata, removing hierarchy for easier parsing. Additionally, added PLAIN and SHELL output formats for easier parsing and integration.g.region
: Simplified JSON output for geographic region boundaries, with examples.g.version
: Added JSON support for GRASS version information, with examples.i.group
: Added JSON support for listing imagery groups, with examples.r.stats
: Added JSON and CSV support for raster area statistics, with Python and CLI examples.v.distance
: Refined JSON schema for nearest element queries, with pandas integration example.g.list
: Added JSON support for listing GRASS database files, with Python example.v.to.db
: Updated JSON schema for printing vector attribute values, with Python example.r.profile
: Simplified JSON output, added CSV support, and included a matplotlib example for profile visualization.r.regression.line
: Updated JSON schema for linear regression between rasters, with Python parsing example.r.stats.quantile
: Updated JSON schema for category quantiles, with Python example.r.volume
: Added JSON support for raster volume calculations, fixed missing headers in CSV output, and provided pandas examples.db.columns
: Added JSON output for listing database table columns, with Python example.m.measure
: Added JSON output for measuring lengths and areas, with Python example.db.connect
: Added JSON output for DB connection info, with Python example.r.regression.multi
: Added JSON output for multiple regression between rasters, with Python example.v.db.connect
: Added JSON output for vector map attribute connections, with Python example.g.findfile
: Added JSON output for GRASS database file search, with Python example.r3.info
: Added JSON output for 3D raster map info, with Python example. Additionally, added PLAIN and SHELL output formats for easier parsing and integration.v.what.rast
: Added JSON output for extracting raster values at vector points, with pandas integration example.r.distance
: Added JSON and CSV support for locating closest points between raster objects, with pandas and CLI examples.v.class
: Added JSON and CSV support for attribute data, with pandas and CLI examples.v.what
: Migrated from manually written JSON to Parson-based JSON output, simplified schema, and provided Python parsing examples.
All these changes were accompanied by regression tests to ensure that new JSON/CSV/SHELL/PLAIN output options do not break existing functionality. Additionally, I introduced comprehensive Python test cases for these outputs, making the enhancements reliable and future-proof. During this work, I also fixed several bugs in modules such as r.stats
, r3.info
, v.what.rast
, and v.class
, strengthening the stability of GRASS as a whole.
Conclusion
To sum up, I’m glad to have met the three core objectives of my project with the constant support of my mentors:
- Brought JSON output to several important and long-requested modules, added regression tests, and ensured reliable future test coverage.
- Enhanced the JSON schema introduced in last year’s GSoC by reducing unnecessary hierarchy, making it more consistent across tools, and providing Python/Pandas examples for practical use.
- Expanded CSV and SHELL output formats to additional tools where they could bring value.
This journey has been both rewarding and educational, and I’m grateful for the opportunity to contribute to GRASS through GSoC 2025. I especially want to thank Anna Petrasova and the GRASS Development Team for their guidance and support. I’m excited to keep building on this work and further extend JSON and other features in the future.
Future Work
There is significant scope to extend structured output support across GRASS tools. In particular, future efforts should focus on:
- Expanding JSON output to additional modules, with details tracked here: GRASS JSON Outputs Project.
- Refining and standardizing the JSON schema to better support complex workflows and integrations.
- Exploring and implementing other output formats (e.g., SHELL, CSV, PLAIN) where they can improve usability and automation.
I also plan to continue contributing to GRASS by extending JSON support to more modules and engaging in community discussions around schema design and format integration.
Log of Pull Requests
Below is a list of pull requests submitted to the GRASS repository during GSoC 2025:
Pull Request | Description | Date | Status |
---|---|---|---|
#5688 | r.univar: Fix JSON structure | May 20, 2025 | Merged |
#5773 | r.what.color: Add test file | May 25, 2025 | Merged |
#5775 | g.mapset: Add test file | May 26, 2025 | Merged |
#5784 | r.info: Add Python example | May 28, 2025 | Closed |
#5785 | g.region: Add Python example | May 28, 2025 | Merged |
#5810 | g.mapset: Add JSON support | June 2, 2025 | Merged |
#5821 | r.univar: Fix uninitialized zone_value | June 3, 2025 | Merged |
#5822 | r.what.color: Add JSON support | June 3, 2025 | Merged |
#5842 | r.info: Update test file | June 5, 2025 | Merged |
#5859 | r.info: Improve interface | June 7, 2025 | Merged |
#5863 | g.version: Add JSON support | June 9, 2025 | Merged |
#5870 | i.group: Add JSON support | June 10, 2025 | Merged |
#5879 | r.stats: Add test file | June 12, 2025 | Merged |
#5892 | r.stats: Add tests for JSON output | June 14, 2025 | Merged |
#5898 | v.distance: Update JSON format | June 16, 2025 | Merged |
#5900 | r.stats: fix uninitialised window value | June 17, 2025 | Merged |
#5906 | g.list: Add test file | June 17, 2025 | Merged |
#5921 | g.list: Add JSON support | June 20, 2025 | Merged |
#5929 | g.region: Update JSON format | June 21, 2025 | Merged |
#5946 | v.to.db: Update JSON format | June 23, 2025 | Merged |
#5962 | r.profile: Update JSON format | June 25, 2025 | Merged |
#5966 | r.univar: Add shell and CSV format | June 25, 2025 | Merged |
#5990 | r.regression.line: Add test file | June 30, 2025 | Merged |
#5993 | r.regression.line: Add JSON support | June 30, 2025 | Merged |
#6002 | r.stats.quantile: Add test file | July 1, 2025 | Merged |
#6007 | r.stats.quantile: Add JSON support | July 1, 2025 | Merged |
#6027 | r.volume: Add test file | July 4, 2025 | Merged |
#6036 | r.volume: Add JSON support | July 6, 2025 | Merged |
#6042 | db.columns: Add JSON support | July 7, 2025 | Merged |
#6051 | m.measure: Add test file | July 10, 2025 | Merged |
#6052 | db.connect: Add test file | July 10, 2025 | Merged |
#6053 | m.measure: Add JSON support | July 11, 2025 | Merged |
#6059 | db.connect: Add JSON support | July 12, 2025 | Merged |
#6066 | r.regression.multi: Add test file | July 15, 2025 | Merged |
#6067 | v.db.connect: Add test file | July 15, 2025 | Merged |
#6075 | r.regression.multi: Add JSON support | July 18, 2025 | Merged |
#6077 | v.db.connect: Add JSON support | July 19, 2025 | Merged |
#6091 | g.findfile: Add JSON support | July 21, 2025 | Merged |
#6098 | r3.info: Add test file | July 22, 2025 | Merged |
#6103 | r3.info: Add JSON support | July 23, 2025 | Merged |
#6109 | v.what.rast: Add test file | July 24, 2025 | Merged |
#6132 | v.category: Use CSV format instead of shell format | July 28, 2025 | Merged |
#6133 | r.stats: add CSV support | July 28, 2025 | Merged |
#6134 | r.volume: add csv column headers | July 28, 2025 | Merged |
#6140 | v.what.rast: Add JSON support | July 29, 2025 | Merged |
#6145 | r.profile: Add CSV support | July 30, 2025 | Merged |
#6150 | r.distance: Update existing test cases | August 1, 2025 | Merged |
#6204 | r.distance: Add JSON and CSV support | August 12, 2025 | Merged |
#6205 | v.class: Add more test cases and fix out-of-bounds bug | August 12, 2025 | Merged |
#6210 | v.class: Add JSON and CSV support | August 15, 2025 | Merged |
#6252 | v.what: Update JSON output format | August 23, 2025 | Merged |
Additional Links
Ttile | Link |
---|---|
Project Wiki Page | Add JSON output to different tools in C for GRASS |
Github Fork | GitHub Fork |
GSoC Page | Link |
Examples & Images
Below is a list of some of the tasks I completed during my GSoC. Please note that this is not a complete list. For more details on these and other tools, please check out the GRASS documentation from the PR links provided above.
Using r.profile
JSON output in Python
The following example demonstrates how the JSON output of r.profile
can be directly loaded into a pandas DataFrame and used for plotting with matplotlib.
import grass.script as gs
import pandas as pd
import matplotlib.pyplot as plt
# Run r.profile command
elevation = gs.read_command(
"r.profile",
input="elevation",
coordinates="641712,226095,641546,224138,641546,222048,641049,221186",
format="json",
flags="gc",
)
# Load the JSON data into a dataframe
df = pd.read_json(elevation)
print(df)
# Create the scatter plot
plt.figure(figsize=(10, 6))
plt.scatter(df["distance"], df["value"], c=df["color"], marker="o")
plt.title("Profile of Distance vs. Elevation with Color Coding")
plt.xlabel("Distance (meters)")
plt.ylabel("Elevation")
plt.grid(True)
plt.show()
Sample DataFrame Output (first 10 rows)
easting northing distance value color
0 641712.000000 226095.000000 0.000000 84.661507 #71FF00
1 641711.154826 226085.036106 9.999675 84.511765 #6FFF00
2 641710.309651 226075.072213 19.999349 84.241806 #6BFF00
3 641709.464477 226065.108319 29.999024 84.304214 #6CFF00
4 641708.619302 226055.144426 39.998699 84.174400 #6AFF00
5 641707.774128 226045.180532 49.998374 84.112534 #6AFF00
6 641706.928954 226035.216639 59.998048 84.011208 #68FF00
7 641706.083779 226025.252745 69.997723 83.834084 #66FF00
8 641705.238605 226015.288851 79.997398 83.870193 #67FF00
9 641704.393430 226005.324958 89.997073 84.194908 #6BFF00
Visualization
Matplotlib scatter plot generated from the r.profile
JSON output:
Using v.class
JSON output with Pandas
The following example demonstrates how the JSON output of v.class
can be directly loaded into a pandas DataFrame.
import pandas as pd
import grass.script as gs
data = gs.parse_command(
"v.class",
map="bridges",
column="YEAR_BUILT",
algorithm="std",
nbclasses=5,
format="json",
)
df = pd.DataFrame(data["intervals"])
print(df)
Sample DataFrame output (intervals)
from to frequency
0 1891.000000 1938.829689 750
1 1938.829689 1954.722836 1841
2 1954.722836 1970.615983 5556
3 1970.615983 1986.509130 1788
4 1986.509130 1997.000000 1003
Using v.what.rast
JSON output with Pandas
The following example demonstrates how the JSON output of v.what.rast
can be directly loaded into a Pandas DataFrame to display categories and values.
import grass.script as gs
import pandas as pd
data = gs.parse_command(
"v.what.rast", map="bridges", raster="elevation", flags="p", format="json"
)
df = pd.DataFrame(data)
print(df)
Sample DataFrame output
category value
0 4635 86.851212
1 4648 87.883255
2 4688 103.382004
3 4709 67.387856
4 4986 90.972832
5 4991 86.550629
6 5027 84.256355
7 5060 79.500443
8 5071 78.732864
9 5089 85.703636
10 5096 115.967323
11 5130 81.055870
12 5150 92.812927
13 5166 88.149483
14 5184 76.426331
Test Suite Update
Added 6 tests to the test suite, including both regression tests and future-proof tests to ensure functionality remains reliable in future updates.
Using r.distance
JSON output with Pandas
The following example demonstrates how the JSON output of r.distance
can be directly loaded into a Pandas DataFrame to locate closest points.
import pandas as pd
import grass.script as gs
result = gs.parse_command(
"r.distance", map=["zipcodes", "lakes"], flags="l", format="json"
)
df = pd.json_normalize(result)
print(df)
Sample DataFrame output (first 10 rows, truncated)
distance from_cell.category from_cell.easting ... to_cell.easting to_cell.northing to_cell.label
0 11158.507466 27511 632604.951740 ... 640584.676990 215499.220687 Dam/Weir
1 1037.271198 27511 631734.981693 ... 632314.961724 221839.026534 Lake/Pond
2 2276.988836 27511 630015.040913 ... 630765.015090 219459.099418 Reservoir
3 14614.155031 27513 631055.005106 ... 640584.676990 215499.220687 Dam/Weir
4 3635.876453 27513 631674.983759 ... 635164.863600 227198.862392 Lake/Pond
5 6971.335517 27513 630005.041257 ... 630765.015090 219459.099418 Reservoir
6 8978.927486 27518 632134.967922 ... 640424.682499 215209.229568 Dam/Weir
7 0.000000 27518 630005.041257 ... 630005.041257 220199.076757 Lake/Pond
8 699.975899 27518 630005.041257 ... 630705.017156 219379.101868 Reservoir
9 509.197766 27529 641154.657366 ... 640674.673892 215249.228343 Dam/Weir
[10 rows x 9 columns]
Using v.distance
JSON output with Pandas
The following example demonstrates how the JSON output of v.distance
can be directly loaded into a Pandas DataFrame.
import grass.script as gs
import pandas as pd
data = gs.parse_command(
"v.distance",
flags="p",
from_="busroute_a",
to="busstopsall",
upload="dist,to_attr",
to_column="routes",
format="json",
)
df = pd.DataFrame(data)
print(df)
Sample DataFrame output
from_cat to_cat dist to_attr
0 1 112 0.142812 8,A
1 2 44 0.102327 9,A
Using r.volume
JSON output with Pandas
The following example demonstrates how the JSON output of r.volume
can be directly loaded into a Pandas DataFrame.
import grass.script as gs
import pandas as pd
data = gs.parse_command(
"r.volume", input="elevation", clump="geology_30m", flags="p", format="json"
)
df = pd.DataFrame(data)
print(df)
Sample DataFrame output
category average sum cells volume easting northing
0 217 118.93 86288827.99 725562 8628882798.63 635325 221535
1 262 108.97 21650560.37 198684 2165056037.02 638935 222495
2 270 92.23 63578874.44 689373 6357887443.53 642405 221485
3 405 132.96 33732662.09 253710 3373266208.59 631835 224095
4 583 139.35 3011288.22 21609 301128821.55 630205 224665
5 720 124.30 599618.16 4824 59961816.06 634075 227995
6 766 132.43 936791.20 7074 93679120.08 631425 227845
7 862 118.31 7302317.47 61722 730231746.74 630505 218885
8 910 94.20 4235816.13 44964 423581613.11 639215 216365
9 921 135.22 1693985.23 12528 169398523.05 630755 215445
10 945 127.24 1145.12 9 114512.03 630015 215015
11 946 89.91 365748.34 4068 36574833.85 639085 215255
12 948 129.02 112631.82 873 11263181.57 630185 215115
Test Suite Update
Added 4 tests to the test suite, including both regression tests and future-proof tests to ensure functionality remains reliable in future updates.