v.net: Add JSON output support for nodes operation#6999
v.net: Add JSON output support for nodes operation#6999tangelll wants to merge 10 commits intoOSGeo:mainfrom
Conversation
vector/v.net/args.c
Outdated
| opt->tucfield->required = NO; | ||
| opt->tucfield->guisection = _("Turntable"); | ||
|
|
||
| opt->format = G_define_option(); |
There was a problem hiding this comment.
Use G_OPT_F_FORMAT standard option.
vector/v.net/main.c
Outdated
| G_message(_("Copying attributes...")); | ||
| if (Vect_copy_tables(In, Out, 0)) | ||
| if (root_object) { | ||
| G_json_object_set_string(root_object, "current_step", |
There was a problem hiding this comment.
These are messages that go to stderr, they are not expected to be in output JSON.
petrasovaa
left a comment
There was a problem hiding this comment.
To simplify review, could you please post the examples and the output as it was and with the changes you made? Edit the PR description.
I’ve updated the PR and reverted the changes in the man folder. Please note that some minor formatting was automatically adjusted by my local pre-commit hooks. |
petrasovaa
left a comment
There was a problem hiding this comment.
Thank you, several things to address:
Please fix the problems in compilation:
main.c: In function ‘main’:
main.c:62:5: warning: ‘output_format’ may be used uninitialized [-Wmaybe-uninitialized]
62 | parse_arguments(&opt, &afield, &nfield, &thresh, &act, output_format);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.c:30:33: note: ‘output_format’ was declared here
30 | enum { PLAIN, SHELL, JSON } output_format;
| ^~~~~~~~~~~~~
Remove the reporting of new nodes, keep the json output for operation=report and nreport only.
Drop the shell format, instead you can add CSV.
Remove any unrelated changes (e.g. you removed several empty lines).
The test misses the report/nreport case.
Thank you!
| opt->format = G_define_standard_option(G_OPT_F_FORMAT); | ||
| opt->format->key = "format"; | ||
| opt->format->type = TYPE_STRING; | ||
| opt->format->required = NO; | ||
| opt->format->options = "plain,shell,json"; | ||
| opt->format->answer = "plain"; | ||
| opt->format->description = _("Output"); |
There was a problem hiding this comment.
These are already part of the standard option definition.
| opt->format = G_define_standard_option(G_OPT_F_FORMAT); | |
| opt->format->key = "format"; | |
| opt->format->type = TYPE_STRING; | |
| opt->format->required = NO; | |
| opt->format->options = "plain,shell,json"; | |
| opt->format->answer = "plain"; | |
| opt->format->description = _("Output"); | |
| opt->format = G_define_standard_option(G_OPT_F_FORMAT); | |
| opt->format->required = NO; | |
| opt->format->options = "plain,shell,json"; | |
| opt->format->answer = "plain"; | |
| opt->format->description = _("Output"); |
…porting from stdout. Cleaned up redundant code and added validation tests.
| } | ||
| fprintf(stdout, "\n"); | ||
| if (root_arr) { | ||
| G_json_object_set_value(G_json_value_get_object(item_val), "lines", lines_val); G_json_array_append_value(root_arr, item_val); |
There was a problem hiding this comment.
Use pre-commit to automatically fix the formatting.
| except json.JSONDecodeError: | ||
| self.fail(f"nreport produced invalid JSON: {output}") | ||
|
|
||
| self.assertIsInstance(data, list, "nreport output must be a JSON array") |
There was a problem hiding this comment.
The tests should be more specific that just verifying type of output data.
| Generating nodes and getting the result in JSON: | ||
|
|
||
| ```sh | ||
| v.net input=streets operation=nodes format=json -c |
There was a problem hiding this comment.
The example should also show the example output.
| def test_nreport_json(self): | ||
| """Test nreport operation JSON output""" | ||
| self.runModule("v.net", input="streets", output=self.network, operation="nodes") | ||
| output = read_command( |
There was a problem hiding this comment.
Use parse_command or the new Tools API:
https://grass.osgeo.org/grass-devel/manuals/python_intro.html#running-tools
Description
This PR introduces JSON output support for the
v.netmodule, specifically for theoperation=nodestask. This enhancement allows users to programmatically capture network statistics (such as new node counts) for use in automated workflows and testing pipelines.Changes
G_JSONsupport inmain.cto handle structured output.formatparameter to choose betweenplain(default) andjson.v.net.mdwith usage examples and guidelines for JSON output.testsuite/test_v_net.pyto ensure output consistency.How to Test
To verify the implementation, you can use the North Carolina sample dataset.
Expected JSON Output :
[
{
"line_cat": 49599,
"start_node_cat": -1,
"end_node_cat": -1
},
...
]
Copying features...
100%
Building topology for vector map test_vnet@PERMANENT...
Registering primitives...
40000
Copying attributes...
Building topology for vector map test_vnet@PERMANENT...
Registering primitives...
50000
v.net complete. 323 lines (network arcs) written to output.
.
Copying features...
100%
Building topology for vector map test_vnet@PERMANENT...
Registering primitives...
40000
Copying attributes...
Building topology for vector map test_vnet@PERMANENT...
Registering primitives...
50000
v.net complete. 156 lines (network arcs) written to output.
.
Copying attributes...
Building topology for vector map test_vnet@PERMANENT...
Registering primitives...
90000
v.net complete. 41813 new points (nodes) written to output.
.v.net complete.
.v.net complete.
.OK