Skip to content

Conversation

@cvvergara
Copy link
Member

@cvvergara cvvergara commented Apr 19, 2025

Fixes #2850

Changes proposed in this pull request:

  • Use pgr_separateCrossing and pgr_separateTouching as the back bone of the calculation
  • Leave indexes responsibility to the user
    • No check indexes
    • No create indexes
  • pgtap tests where done on many open issues

@pgRouting/admins

Summary by CodeRabbit

  • Documentation

    • Updated release notes and changelog to highlight SQL code enhancements, specifically the rewrite of the node network function.
    • Improved documentation for the node network function, detailing new behaviors and data type changes.
  • Bug Fixes

    • Adjusted geometric precision in query results and improved detection of close edges in node network processing.
    • Expanded logic for handling touching geometries in utility functions.
  • Tests

    • Added multiple new tests to validate node network functionality, including edge and vertex counts, and behavior on custom datasets.
    • Updated existing tests to reflect changes in expected edge and vertex counts after processing.
  • Refactor

    • Streamlined internal logic for node network processing, enhancing modularity and error handling.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 19, 2025

Walkthrough

This update introduces significant changes to the pgr_nodeNetwork function and its supporting utilities in pgRouting. The function was refactored for modularity, using helper functions for geometry processing and improved error handling. The SQL code for detecting and handling touching geometries was enhanced to include more cases. Documentation and release notes were updated to reflect these changes, and several new pgTAP test scripts were added to validate the behavior of the revised pgr_nodeNetwork function and related utilities. Minor adjustments were made to geometric precision in test results and expected test outcomes.

Changes

File(s) Change Summary
NEWS.md, doc/src/release_notes.rst Added a "SQL code enhancements" section for pgRouting 3.8.0, documenting the rewrite of pgr_nodeNetwork and referencing issue #2850.
doc/topology/pgr_nodeNetwork.rst Updated documentation for pgr_nodeNetwork to describe version 3.8.0 changes, including data type updates and function behavior.
docqueries/topology/nodeNetwork.result Adjusted geometric precision in test results and added a new row for close edge detection.
docqueries/utilities/separateTouching.result, sql/utilities/separateTouching.sql Expanded SQL logic and test results to include additional touching geometry cases in the touchings CTE.
pgtap/topology/nodeNetwork/issue_1074.pg, pgtap/utilities/separateTouching/issue_623.pg Increased test plan counts and updated expected values for edge and vertex counts in tests.
pgtap/topology/nodeNetwork/issue_1882.pg, pgtap/topology/nodeNetwork/issue_2628.pg, pgtap/topology/nodeNetwork/issue_280.pg, pgtap/topology/nodeNetwork/issue_623.pg Added new pgTAP test scripts to validate pgr_nodeNetwork and related utilities on various datasets and scenarios.
sql/topology/nodeNetwork.sql Refactored and modularized the pgr_nodeNetwork function, improved error handling, and replaced manual intersection logic with helper functions.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant pgr_nodeNetwork
    participant pgr_separateCrossing
    participant pgr_separateTouching
    participant OutputTable

    User->>pgr_nodeNetwork: Call with edge table and parameters
    pgr_nodeNetwork->>pgr_separateCrossing: Pass edge data and tolerance
    pgr_separateCrossing-->>pgr_nodeNetwork: Return split crossing edges
    pgr_nodeNetwork->>pgr_separateTouching: Pass edge data and tolerance
    pgr_separateTouching-->>pgr_nodeNetwork: Return split touching edges
    pgr_nodeNetwork->>OutputTable: Insert results
    pgr_nodeNetwork->>User: Return output table name
Loading

Poem

🐇
The network’s nodes are now refined,
With crossing, touching, all aligned.
Tests abound and docs are bright,
Edges split just right tonight.
SQL’s enhanced, the code is neat—
A rabbit’s hop makes routing sweet!
🗺️✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Free

📥 Commits

Reviewing files that changed from the base of the PR and between 8ff43b8 and 5be4230.

📒 Files selected for processing (13)
  • NEWS.md (1 hunks)
  • doc/src/release_notes.rst (1 hunks)
  • doc/topology/pgr_nodeNetwork.rst (2 hunks)
  • docqueries/topology/nodeNetwork.result (2 hunks)
  • docqueries/utilities/separateTouching.result (1 hunks)
  • pgtap/topology/nodeNetwork/issue_1074.pg (2 hunks)
  • pgtap/topology/nodeNetwork/issue_1882.pg (1 hunks)
  • pgtap/topology/nodeNetwork/issue_2628.pg (1 hunks)
  • pgtap/topology/nodeNetwork/issue_280.pg (1 hunks)
  • pgtap/topology/nodeNetwork/issue_623.pg (1 hunks)
  • pgtap/utilities/separateTouching/issue_623.pg (3 hunks)
  • sql/topology/nodeNetwork.sql (3 hunks)
  • sql/utilities/separateTouching.sql (1 hunks)

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8ff43b8 and 0cf144c.

📒 Files selected for processing (15)
  • NEWS.md (1 hunks)
  • doc/src/release_notes.rst (1 hunks)
  • doc/topology/pgr_nodeNetwork.rst (2 hunks)
  • docqueries/topology/nodeNetwork.result (2 hunks)
  • docqueries/utilities/separateTouching.result (1 hunks)
  • locale/en/LC_MESSAGES/pgrouting_doc_strings.po (4 hunks)
  • locale/pot/pgrouting_doc_strings.pot (4 hunks)
  • pgtap/topology/nodeNetwork/issue_1074.pg (2 hunks)
  • pgtap/topology/nodeNetwork/issue_1882.pg (1 hunks)
  • pgtap/topology/nodeNetwork/issue_2628.pg (1 hunks)
  • pgtap/topology/nodeNetwork/issue_280.pg (1 hunks)
  • pgtap/topology/nodeNetwork/issue_623.pg (1 hunks)
  • pgtap/utilities/separateTouching/issue_623.pg (3 hunks)
  • sql/topology/nodeNetwork.sql (3 hunks)
  • sql/utilities/separateTouching.sql (1 hunks)
👮 Files not reviewed due to content moderation or server errors (1)
  • locale/pot/pgrouting_doc_strings.pot
🧰 Additional context used
🧠 Learnings (3)
NEWS.md (1)
Learnt from: cvvergara
PR: pgRouting/pgrouting#2744
File: doc/src/release_notes.rst:94-94
Timestamp: 2025-02-06T20:50:07.967Z
Learning: In pgRouting, changes to functions can happen at two levels:
1. SQL level - removal/addition of function signatures
2. C/C++ level - deprecation/changes to the underlying implementation
These are documented separately in the release notes.
pgtap/utilities/separateTouching/issue_623.pg (1)
Learnt from: cvvergara
PR: pgRouting/pgrouting#2725
File: src/coloring/edgeColoring_driver.cpp:87-87
Timestamp: 2025-01-19T16:17:53.496Z
Learning: Changes to error message handling in pgRouting functions require corresponding updates to pgtap tests to maintain test coverage.
doc/src/release_notes.rst (1)
Learnt from: cvvergara
PR: pgRouting/pgrouting#2744
File: doc/src/release_notes.rst:94-94
Timestamp: 2025-02-06T20:50:07.967Z
Learning: In pgRouting, changes to functions can happen at two levels:
1. SQL level - removal/addition of function signatures
2. C/C++ level - deprecation/changes to the underlying implementation
These are documented separately in the release notes.
🪛 markdownlint-cli2 (0.17.2)
NEWS.md

65-65: Emphasis used instead of a heading
null

(MD036, no-emphasis-as-heading)

⏰ Context from checks skipped due to timeout of 90000ms (16)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Ubuntu Boost (84)
  • GitHub Check: Ubuntu Boost (78)
  • GitHub Check: Ubuntu Boost (80)
  • GitHub Check: Ubuntu Boost (56)
  • GitHub Check: Ubuntu Boost (83)
  • GitHub Check: Ubuntu Boost (86)
  • GitHub Check: Ubuntu Boost (79)
  • GitHub Check: Ubuntu Boost (68)
  • GitHub Check: Ubuntu Boost (76)
  • GitHub Check: Ubuntu Boost (77)
  • GitHub Check: Ubuntu Boost (75)
  • GitHub Check: Check queries
  • GitHub Check: macos (macos-latest)
  • GitHub Check: Ubuntu clang
  • GitHub Check: macos (macos-14)
🔇 Additional comments (34)
sql/utilities/separateTouching.sql (1)

76-79: Enhanced filtering criteria for touching geometries.

The filtering condition in the touchings CTE has been expanded to better identify cases where geometries touch. Previously, rows where geom equaled g1 were excluded. Now, the condition also handles geometries that touch (ST_touches(g1, g2)) but don't intersect at the endpoints of g1. This should improve edge splitting at touching points that aren't at the endpoints.

NEWS.md (1)

65-69: Appropriate documentation of the pgr_nodeNetwork rewrite.

The addition of the "SQL code enhancements" section properly documents the rewrite of the pgr_nodeNetwork function (referencing issue #2850). This logically fits between the C/C++ code enhancements and the new proposed functions sections.

🧰 Tools
🪛 markdownlint-cli2 (0.17.2)

65-65: Emphasis used instead of a heading
null

(MD036, no-emphasis-as-heading)

doc/src/release_notes.rst (1)

95-99: Consistent documentation in release notes.

The addition of the "SQL code enhancements" section with reference to issue #2850 (rewrite of pgr_nodeNetwork) maintains consistency with the changes in NEWS.md. This properly documents the significant enhancement to this function's implementation.

pgtap/topology/nodeNetwork/issue_1074.pg (2)

25-25: Updated test plan count.

The test plan count has been increased from 15 to 16 to account for the new assertion being added (line 64). This maintains proper test accounting.


64-64: Added verification for total edge count.

This new assertion validates that the combined table original_noded contains exactly 13 edges (9 from original_roads + 4 from original_bridges). This helps ensure the node network operation produces the expected number of edges, which is important for the rewritten function.

doc/topology/pgr_nodeNetwork.rst (2)

39-44: Documentation update correctly reflects API changes.

The added availability section for version 3.8.0 clearly documents the important changes made in this release:

  1. Removing automatic index checking/creation
  2. Using the modular helper functions for geometry processing
  3. Updated data type for the output table

These changes align well with the PR objectives to rewrite the node network functionality.


87-89: Data type update from integer to bigint.

The documentation now correctly specifies that the source and target columns in the output table use bigint data type instead of integer, which is consistent with the changes mentioned in the version 3.8.0 availability section.

docqueries/utilities/separateTouching.result (1)

34-37: Enhanced geometry filtering logic.

The WHERE clause in the touchings CTE has been improved to:

  1. Keep the original condition that excludes cases where geom = g1
  2. Add a new condition that includes geometries that touch (ST_touches(g1, g2)) but don't intersect at the start or end points of g1

This change broadens the detection criteria for touching geometries, which should improve the accuracy of the node network creation.

pgtap/utilities/separateTouching/issue_623.pg (3)

21-21: Test plan count updated for both version branches.

The test plan count is correctly incremented to accommodate the new test case, maintaining proper test structure for both old and new versions of pgRouting.


121-121: Added validation for total row count.

Good addition of a row count validation test to ensure the total number of rows in the houses.simplenet table equals the expected 96 (14 original edges + 82 houses).


139-139: Updated edge and vertex count expectations.

The test now expects 102 edges and 101 vertices after running pgr_separateTouching, updated from the previous values (91, 98). This change reflects the different behavior in the rewritten node network processing.

locale/en/LC_MESSAGES/pgrouting_doc_strings.po (4)

11-11: Updated POT-Creation-Date timestamp

This is a standard update to the timestamp when translation files are regenerated.


4019-4025: Documentation updated with new SQL code enhancements section

This change properly adds new message entries that document SQL code enhancements, specifically referencing issue #2850 about the rewrite of pgr_nodeNetwork.


13397-13404: Clear documentation of pgr_nodeNetwork behavior changes

The added messages clearly explain the key changes to the pgr_nodeNetwork function:

  1. No longer checks or creates indexes
  2. Uses pgr_separateTouching and pgr_separateCrossing functions
  3. Creates tables with BIGINT data type

This provides important information to users about the new behavior they should expect.


13455-13455: Updated data type from integer to bigint

The data type for the source and target columns has been properly updated from integer to bigint in the documentation, which correctly reflects the implementation changes mentioned in the PR objectives.

Also applies to: 13458-13458

pgtap/topology/nodeNetwork/issue_280.pg (1)

1-42: Well-structured test for basic node network functionality.

This test verifies that the rewritten pgr_nodeNetwork function correctly handles intersecting linestrings by:

  1. Creating a simple network with two intersecting linestrings
  2. Running pgr_nodeNetwork with a small tolerance
  3. Verifying the resulting network has 4 edges (original edges split at intersection)
  4. Extracting vertices and confirming there are 5 vertices (4 endpoints + 1 intersection)
  5. Specifically checking that the intersection point at (5,5) exists

The test provides good coverage of the core functionality of splitting intersecting edges.

docqueries/topology/nodeNetwork.result (2)

170-172: Minor coordinate precision changes reflect the rewritten function.

The slight change in coordinate precision (now showing 1.999999999999 3.5 instead of exactly 2 3.5) reflects the internal implementation changes from using the new pgr_separateCrossing and pgr_separateTouching functions.


219-222: Result update reflects new implementation behavior.

The updated result for the pgr_findCloseEdges query shows different values for edge_id, distance and geometry compared to the previous implementation, which is expected given the function rewrite using the new helper functions.

pgtap/topology/nodeNetwork/issue_1882.pg (1)

23-78: Comprehensive test for schema-qualified tables and version-specific behavior.

This test thoroughly validates the rewritten pgr_nodeNetwork function with:

  1. Schema qualification support (using 'ferry' schema)
  2. Complex real-world ferry route geometries
  3. Proper validation of table structure in the resulting noded table
  4. Verification that each original edge is split into exactly 4 sub-edges
  5. Version-conditional testing that accounts for behavior differences in pgRouting 3.8.0+

The conditional logic on line 68-78 correctly handles version-specific testing, ensuring backward compatibility while validating the new implementation.

pgtap/topology/nodeNetwork/issue_623.pg (3)

24-107: Well-designed test for complex network with custom schema.

This test creates a realistic road network scenario with:

  1. A custom 'houses' schema
  2. A road network table with multiple road segments
  3. House locations as points
  4. Proper indexes and constraints

The test data provides good coverage for a real-world scenario where a complex network would need to be noded.


108-123: Realistic connectivity testing between houses and road network.

This section adds edges connecting each house to its nearest road segment using ST_ShortestLine, which creates a realistic network topology that will need to be properly handled by the node network function.


124-161: Thorough verification of connectivity and version-specific behavior.

The test provides comprehensive validation by:

  1. Running pgr_nodeNetwork on the complex network
  2. Extracting vertices and updating source/target relationships
  3. Conditionally checking for version-specific edge counts (190 in v3.8.0+, 189 in earlier versions)
  4. Computing connected components to verify network integrity
  5. Confirming the entire network forms a single connected component

The version-specific checks on lines 144-149 ensure compatibility with both pre-3.8.0 and post-3.8.0 behavior.

pgtap/topology/nodeNetwork/issue_2628.pg (5)

22-22: Good use of conditional test planning

The test intelligently adapts the number of tests based on version compatibility, which is a good practice for maintaining backward compatibility while testing new functionality.


46-46: Function usage validates the new implementation

The test correctly calls pgr_nodenetwork with the appropriate parameters, including the schema-qualified table name, tolerance, and column identifiers, which helps validate the refactored implementation.


54-58: Well-implemented version-specific test

The conditional check for the column type ensures that tests are version-aware, accommodating the data type changes in 3.8.0 while skipping incompatible tests in earlier versions.


83-93: Comprehensive results validation

The test block effectively verifies multiple aspects of the function's behavior, including edge subdivision counts, total edge counts, and vertex extraction. The conditional execution based on version ensures compatibility with older pgRouting versions.


1-3: 🧹 Nitpick (assertive)

Check the copyright year

The copyright year is set to 2025, which is in the future. Consider updating it to the current year.

-Copyright (c) 2025  pgRouting developers
+Copyright (c) 2024  pgRouting developers

Likely an incorrect or invalid review comment.

sql/topology/nodeNetwork.sql (7)

47-63: Improved variable declarations for schema awareness

The refactored variable declarations support proper schema handling and provide clearer naming conventions. The BIGINT type for counters ensures compatibility with large datasets.


77-87: Well-implemented schema-aware table handling

The function now properly parses the input table name into schema and table components, defaulting to the current schema if unspecified. This improves flexibility and ensures correct schema qualification in all SQL operations.


88-90: Cleaner handling of filtering conditions

The code elegantly normalizes the filtering condition based on the provided rows_where parameter and outall flag, ensuring consistent behavior regardless of input format.


91-110: Improved query construction and validation

The function now builds a SQL query string using the format function for better readability and maintainability. The error handling with detailed diagnostics helps identify issues more easily.


165-165: Appropriate data type change for old_id

Changed the data type of old_id to BIGINT to maintain consistency with the source table's ID column type, which improves compatibility with large datasets.


183-191: Modular implementation using helper functions

The core edge-splitting logic has been elegantly replaced with calls to pgr_separateCrossing and pgr_separateTouching, which significantly improves code maintainability and follows the single responsibility principle. This approach makes the code cleaner and easier to understand.


204-215: Improved handling of untouched edges

The function now uses a cleaner approach to insert untouched edges (those not requiring splitting) into the result table. The WITH clause and subqueries improve readability and performance compared to the previous implementation.

@cvvergara cvvergara requested review from iosefa and robe2 April 19, 2025 16:46
@cvvergara cvvergara force-pushed the rewrite-pgr_nodeNetwork-v3.8 branch from 0cf144c to 5be4230 Compare April 19, 2025 16:53
@cvvergara cvvergara merged commit d422a99 into pgRouting:main Apr 19, 2025
26 checks passed
@cvvergara cvvergara deleted the rewrite-pgr_nodeNetwork-v3.8 branch April 19, 2025 22:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rewrite pgr_nodeNetwork

3 participants