8000 Rewrite NumGeometries and GeometryN behavior for TIN and PolyhedralSurface, add NumPatches and PatchN functions #252 by lbartoletti · Pull Request #812 · postgis/postgis · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Rewrite NumGeometries and GeometryN behavior for TIN and PolyhedralSurface, add NumPatches and PatchN functions #252 #812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
8000
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

lbartoletti
Copy link
Contributor

… to NumPatches and PatchN

TIN and PolyhedralSurface are now treated as unitary geometries:
- ST_NumGeometries() returns 1 for non-empty TIN/PolyhedralSurface
- ST_GeometryN() returns the whole geometry for index 1
- ST_NumPatches() preserves original patch counting behavior
- ST_PatchN() provides access to individual patches
- Added lwgeom_has_patches() helper function
Copy link
coderabbitai bot commented Jun 5, 2025
📝 Walkthrough

Walkthrough

The changes introduce and implement patch-level operations for TIN and PolyhedralSurface geometries in PostGIS. New C functions and SQL bindings for counting and extracting patches are added, with corresponding updates to documentation, regression tests, and geometry classification logic. The changelog is updated to reflect these breaking and feature changes.

Changes

File(s) Change Summary
NEWS Updated changelog for breaking change in handling TIN/PolyhedralSurface; minor formatting fix.
liblwgeom/liblwgeom.h.in Declared new function: lwgeom_has_patches(const LWGEOM *geom).
liblwgeom/lwgeom.c Implemented lwgeom_has_patches; updated type checks for TIN/PolyhedralSurface in geometry classification.
postgis/lwgeom_ogc.c Added LWGEOM_numpatches and LWGEOM_patchn functions; refactored geometry counting/extraction logic.
postgis/postgis.sql.in Changed ST_NumPatches and ST_PatchN from SQL to C-language implementations.
regress/core/polyhedralsurface.sql,
regress/core/polyhedralsurface_expected
Added regression tests and expected outputs for PolyhedralSurface with new patch and geometry queries.

Sequence Diagram(s)

sequenceDiagram
  participant User
  participant SQL
  participant C_Functions
  participant Geometry

  User->>SQL: CALL ST_NumPatches(geometry)
  SQL->>C_Functions: LWGEOM_numpatches(geometry)
  C_Functions->>Geometry: Check if geometry has patches
  alt Geometry supports patches
    C_Functions->>Geometry: Count patches
    Geometry-->>C_Functions: Patch count
    C_Functions-->>SQL: Return patch count
    SQL-->>User: Patch count
  else Not supported
    C_Functions-->>SQL: Error
    SQL-->>User: Error
  end

  User->>SQL: CALL ST_PatchN(geometry, n)
  SQL->>C_Functions: LWGEOM_patchn(geometry, n)
  C_Functions->>Geometry: Check if geometry has patches
  alt Geometry supports patches
    C_Functions->>Geometry: Extract nth patch
    Geometry-->>C_Functions: Patch geometry or NULL
    C_Functions-->>SQL: Return patch geometry
    SQL-->>User: Patch geometry
  else Not supported
    C_Functions-->>SQL: Error
    SQL-->>User: Error
  end
Loading

Poem

In the garden of shapes, a patchwork anew,
With PolyhedralSurface and TIN in view.
Now rabbits can count, and pluck patches with glee,
Thanks to new functions, as neat as can be!
Geometry’s puzzle, a patch at a time—
PostGIS grows clever, in logic and rhyme.
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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
@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

🧹 Nitpick comments (2)
NEWS (1)

18-19: Update the placeholder issue number.

The changelog entry correctly describes the breaking change, but "XXXXX" should be replaced with the actual issue or PR number before merging.

-  - XXXXX, ST_NumGeometries/ST_GeometryN treat TIN and PolyhedralSurface as unitary geometries,
+  - #812, ST_NumGeometries/ST_GeometryN treat TIN and PolyhedralSurface as unitary geometries,
postgis/postgis.sql.in (1)

5495-5498: Add cost hint for consistency.

The function implementation looks correct, but consider adding a cost hint for consistency with ST_NumPatches. Since this is a simple geometry extraction operation, _COST_LOW would be appropriate.

 CREATE OR REPLACE FUNCTION ST_PatchN(geometry, integer)
 	RETURNS geometry
 	AS 'MODULE_PATHNAME', 'LWGEOM_patchn'
-	LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;
+	LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE
+	_COST_LOW;
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 9630344 and 78c1f61.

📒 Files selected for processing (7)
  • NEWS (2 hunks)
  • liblwgeom/liblwgeom.h.in (1 hunks)
  • liblwgeom/lwgeom.c (3 hunks)
  • postgis/lwgeom_ogc.c (2 hunks)
  • postgis/postgis.sql.in (1 hunks)
  • regress/core/polyhedralsurface.sql (1 hunks)
  • regress/core/polyhedralsurface_expected (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (11)
  • GitHub Check: macOS
  • GitHub Check: Analyze (c-cpp)
  • GitHub Check: CI (pg14-geos39-gdal33-proj71, tests)
  • GitHub Check: CI (pg13-geos38-gdal31-proj71, tests)
  • GitHub Check: CI (latest, tests)
  • GitHub Check: CI (pg15-clang-geos311-gdal35-proj90, usan_clang)
  • GitHub Check: CI (pg12-geos38-gdal30-proj611, tests)
  • GitHub Check: CI (pg16-geos312-gdal37-proj921, coverage)
  • GitHub Check: CI (pg15-geos311-gdal35-proj90, coverage)
  • GitHub Check: CI (pg17-geosmain-gdal39-proj94, coverage)
  • GitHub Check: CI (pg13-clang-geos39-gdal31-proj71, usan_clang)
🔇 Additional comments (13)
NEWS (1)

40-40: Good formatting cleanup.

The removal of the trailing space improves consistency in the changelog formatting.

liblwgeom/liblwgeom.h.in (1)

910-913: Function declaration looks good.

The lwgeom_has_patches function declaration follows proper C conventions and PostGIS coding style. The placement between lwgeom_is_unitary and lwgeom_has_rings is logical and maintains the organizational structure of the header file.

regress/core/polyhedralsurface_expected (1)

17-23: Test expectations align with the behavioral change.

The expected results correctly reflect the new behavior where PolyhedralSurface geometries are treated as unitary:

  • Empty surfaces return 0 geometries
  • Non-empty surfaces return 1 geometry (the surface itself)
  • ST_GeometryN(surface, 1) returns the surface, other indices return empty

The format is consistent with existing test data patterns.

regress/core/polyhedralsurface.sql (1)

31-40: Comprehensive test coverage for the behavioral change.

The test cases provide excellent coverage of the new unitary geometry behavior:

  1. Empty surface testing: Verifies ST_NumGeometries returns 0 for empty PolyhedralSurface
  2. Single/multi-patch testing: Confirms both return 1 geometry (treating surface as unitary)
  3. Index boundary testing: Tests valid index (1) and invalid indices (0, 2) for ST_GeometryN

The test queries align perfectly with the expected results in polyhedralsurface_expected, demonstrating that PolyhedralSurface geometries are now consistently treated as single units rather than collections of patches.

liblwgeom/lwgeom.c (2)

1115-1117: Include PolyhedralSurface and TIN types as unitary geometries
Expands lwgeom_is_unitary() to treat these types as single‐component geometries for NumGeometries/GeometryN.


1174-1176: Classify TIN as a collection type
Adding TINTYPE to lwtype_is_collection() aligns it with other multi-geometry types, enabling collection operations (e.g., iteration) to include TIN.

postgis/postgis.sql.in (1)

5488-5492: LGTM! Well-implemented C function integration.

The migration from SQL conditional logic to a direct C function call is properly implemented with appropriate function attributes and cost hints.

postgis/lwgeom_ogc.c (6)

58-63: Function declarations look good.

The new function declarations follow the established naming conventions and documentation style.


276-314: Implementation looks correct.

Both functions properly use the helper function with appropriate flags and handle memory management correctly. The error handling for unsupported geometry types in LWGEOM_numpatches is appropriate.


316-369: Extraction logic is consistent with counting logic.

The function properly handles all geometry types and the patches option. The index bounds checking and conversion from 1-based to 0-based indexing are correct.


371-399: Refactoring maintains backward compatibility correctly.

The function properly treats TIN and PolyhedralSurface as unitary geometries and includes an optimization to avoid unnecessary copying when returning the original geometry.


401-436: New patch extraction function is well-implemented.

The function follows the same robust pattern as LWGEOM_geometryn_collection with appropriate type validation and error handling for non-patch geometries.


256-265:

❓ Verification inconclusive

Verify the fallback logic for single-patch geometries.

The code attempts to cast patch geometries to LWCOLLECTION and falls back to returning 1 if the cast fails. This assumes single-patch TIN/PolyhedralSurface geometries might not cast successfully to collections.


🏁 Script executed:

#!/bin/bash
# Description: Search for information about TIN and PolyhedralSurface structure and casting behavior

# Look for lwgeom_as_lwcollection implementation
ast-grep --pattern 'lwgeom_as_lwcollection($_) {
  $$$
}'

# Search for TIN and PolyhedralSurface type definitions
rg -A 10 "TINTYPE|POLYHEDRALSURFACETYPE" --type c

# Look for lwgeom_has_patches implementation
ast-grep --pattern 'lwgeom_has_patches($_) {
  $$$
}'

Length of output: 71008


Confirm lwgeom_as_lwcollection covers patch geometries

I wasn’t able to locate a definition of lwgeom_as_lwcollection in the repo. Before relying on the fallback (col == NULL ? 1), please verify that this cast:

  • Returns non‐NULL for both TINTYPE and POLYHEDRALSURFACETYPE, and
  • Is implemented (e.g. as an inline function or macro) so the fallback branch never executes at runtime.

• Check for its declaration/definition in headers like liblwgeom/lwgeom_inline.h.
• Ensure it handles patch types the same way lwgeom_has_patches does.

Comment on lines +1125 to +1137
int
lwgeom_has_patches(const LWGEOM *geom)
{
switch (geom->type)
{
case TINTYPE:
case POLYHEDRALSURFACETYPE:
return LW_TRUE;
break;
default:
return LW_FALSE;
}
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

New lwgeom_has_patches helper for patch‐level support
Correctly identifies TIN and PolyhedralSurface for the new NumPatches/PatchN API. Please add a doxygen‐style comment above this function and ensure it is declared in the public header (liblwgeom.h) so it’s discoverable.

🤖 Prompt for AI Agents
In liblwgeom/lwgeom.c around lines 1125 to 1137, add a doxygen-style comment
above the lwgeom_has_patches function describing its purpose and behavior. Then
declare this function in the public header file liblwgeom.h to make it
accessible and discoverable by other code using the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
0