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