Skip to content

Conversation

@saket0187
Copy link
Contributor

Fixes #6232

Problem Description

The v.profile module only supported plain text output with pipe-delimited or custom separators. We were required to add JSON support as well.

Solution

Implemented comprehensive output format support with three options:

  1. Plain format (default): Traditional CSV-style output with quoted string values. Maintains full backward compatibility with existing scripts.

  2. CSV format: Unquoted delimiter-separated values optimized for spreadsheet applications. Uses the same separator parameter as plain format but omits quotes for cleaner parsing.

  3. JSON format: Structured output with proper data type handling. Each feature is represented as a JSON object containing:

    • category: Feature category number (always present, even without database)
    • distance: Distance along profile line
    • attributes

Implementation details:

  • Uses GRASS GJSON API (G_json_* functions) for JSON serialization with proper NULL value handling
  • Added comprehensive error checking with G_warning() for database operation failures
  • Works correctly with and without database connections (Fi != NULL check)
  • Updated format option to include "json" in descriptions

Testing

Automated Tests (test_v_profile.py):
Added testJsonFormat() that validates:

  • JSON syntax correctness (parses without errors)
  • Array structure with expected item count
  • Required fields present (category, distance, attributes)
  • Data type validation (category as int, distance as float)
  • Value accuracy against expected results
  • Attribute field completeness

All 6 test suite tests pass, including the new JSON format test.

Manual Testing with NC Basic dataset:

# Plain format (backward compatible)
v.profile input=poi_names_wake profile_map=roadsmajor buffer=200 profile_where="cat=354"

# CSV format
v.profile input=poi_names_wake profile_map=roadsmajor buffer=200 profile_where="cat=354" \
  format=csv separator=comma

# JSON format
v.profile input=poi_names_wake profile_map=roadsmajor buffer=200 profile_where="cat=354" \
  format=json

JSON Output example :-

[
    {
        "category": 572,
        "distance": 19537.972502200733,
        "attributes": {
            "cat": 572,
            "feature_id": "986138",
            "featurenam": "Greshams Lake",
            "class": "Reservoir",
            ...
        }
    }
]

@github-actions github-actions bot added vector Related to vector data processing Python Related code is in Python C Related code is in C module docs markdown Related to markdown, markdown files tests Related to Test Suite labels Jan 5, 2026
Copy link
Contributor

@petrasovaa petrasovaa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution, please read our contributing guidelines, specifically pre-commit to get the formatting correct.

@github-actions github-actions bot added the CMake label Jan 7, 2026
@saket0187 saket0187 requested a review from petrasovaa January 7, 2026 14:36
@saket0187
Copy link
Contributor Author

@petrasovaa

I've addressed your feedback:

  • All original comments have been added.
  • Removed PLAIN format (was similar to CSV)
  • Kept original pipe (|) delimiter as default.
  • Added JSON format with proper type handling
  • Updated documentation with format examples

Please review the updated main.c and v.profile.md files and let me know if any other changes are needed.

Thanks!

@saket0187 saket0187 requested a review from petrasovaa January 14, 2026 17:20
@petrasovaa
Copy link
Contributor

@petrasovaa

I've addressed your feedback:

  • All original comments have been added.
  • Removed PLAIN format (was similar to CSV)
  • Kept original pipe (|) delimiter as default.
  • Added JSON format with proper type handling
  • Updated documentation with format examples

Please review the updated main.c and v.profile.md files and let me know if any other changes are needed.

Thanks!

Please keep the plain mode there for backwards compatibility with | as separator and CSV needs to have comma. All I was trying to avoid is too much code duplication.

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@saket0187
Copy link
Contributor Author

@petrasovaa
I've implemented the changes as discussed:

  1. Added plain format - This is the old CSV behavior (pipe separator) and is default for backward compatibility
  2. Updated csv format - Now uses comma separator as expected for proper CSV files
  3. Eliminated code duplication - Both plain and CSV share the same output code via switch fall-through

The separator logic respects user overrides via the separator= option while providing format-appropriate defaults (pipe for plain, comma for CSV).

Let me know if you'd like any adjustments!

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 CMake docs markdown Related to markdown, markdown files module Python Related code is in Python tests Related to Test Suite vector Related to vector data processing

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants