Repository navigation
SFCGAL: Add CG_Simplify - #803
lbartoletti wants to merge 3 commits into
Conversation
📝 WalkthroughWalkthroughA new geometry simplification function, Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant SQL
participant PostGIS
participant SFCGAL
User->>SQL: CALL CG_Simplify(geom, threshold, preserveTopology)
SQL->>PostGIS: Invoke sfcgal_simplify()
PostGIS->>SFCGAL: Convert geometry and call sfcgal_geometry_simplify()
SFCGAL-->>PostGIS: Return simplified geometry
PostGIS-->>SQL: Convert back and return result
SQL-->>User: Return simplified geometry
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (11)
✅ Files skipped from review due to trivial changes (4)
🚧 Files skipped from review as they are similar to previous changes (7)
⏰ Context from checks skipped due to timeout of 90000ms (4)
✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed 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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 3
♻️ Duplicate comments (1)
doc/html/images/wkt/cg_simplicity02.wkt (1)
1-1: Filename prefix mismatch duplicates earlier issue.
See the comment incg_simplicity01.wktregarding renamingcg_simplicity02.wkt→cg_simplify02.wkt.
🧹 Nitpick comments (4)
doc/reference_sfcgal.xml (4)
3577-3586: Align default parameter syntax with DocBook conventions.Other optional parameters in this file embed the default value directly in the
<parameter>tag (e.g.allow_holes = false) rather than using an<initializer>element. To maintain consistency, consider changing to:<paramdef choice="opt"> <type>boolean</type> <parameter>preserveTopology = false</parameter> </paramdef>
3588-3604: Clarify terminology and grammar.
- In the description of
threshold, “in geometry unit” should be “in geometry units.”- Consider tightening the phrase “preserving the essential features of the geometry” to “preserving essential geometric features” to match the style of other entries.
3656-3698: Ensure<programlisting>tags are balanced and captions match.The complex examples interleave
<programlisting>and<informalfigure>blocks. Please verify that each<programlisting>is properly closed before its corresponding figure, and that captions refer to the correct image files (cg_simplify00.png,cg_simplify01.png,cg_simplify02.png). Indentation tweaks could improve readability.
3701-3707: Consider enhancing “See Also” cross-references.You currently link to
ST_SimplifyandST_SimplifyPreserveTopology. As this documentsCG_Simplify, you might also cross-reference related CG_ functions or mention the SQL interface functions for 2D/3D simplification. However, the existing links are appropriate.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (11)
doc/html/images/Makefile.in(1 hunks)doc/html/images/wkt/cg_simplicity00.wkt(1 hunks)doc/html/images/wkt/cg_simplicity01.wkt(1 hunks)doc/html/images/wkt/cg_simplicity02.wkt(1 hunks)doc/reference_sfcgal.xml(2 hunks)liblwgeom/lwgeom_sfcgal.c(1 hunks)sfcgal/lwgeom_sfcgal.c(2 hunks)sfcgal/regress/simplify.sql(1 hunks)sfcgal/regress/simplify_expected(1 hunks)sfcgal/regress/tests.mk.in(1 hunks)sfcgal/sfcgal.sql.in(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
sfcgal/lwgeom_sfcgal.c (2)
sfcgal/lwgeom_sfcgal.h (3)
sfcgal_postgis_init(40-40)POSTGIS2SFCGALGeometry(28-28)SFCGALGeometry2POSTGIS(34-34)liblwgeom/gserialized.c (1)
gserialized_get_srid(155-161)
liblwgeom/lwgeom_sfcgal.c (1)
liblwgeom/lwgeom_api.c (1)
getPoint4d_p(124-179)
⏰ Context from checks skipped due to timeout of 90000ms (12)
- GitHub Check: macOS
- GitHub Check: 14.2-RELEASE
- GitHub Check: CI (pg13-geos38-gdal31-proj71, tests)
- GitHub Check: CI (pg17-geosmain-gdal39-proj94, coverage)
- GitHub Check: CI (pg12-geos38-gdal30-proj611, tests)
- GitHub Check: CI (latest, tests)
- GitHub Check: CI (pg13-clang-geos39-gdal31-proj71, usan_clang)
- GitHub Check: CI (pg15-clang-geos311-gdal35-proj90, usan_clang)
- GitHub Check: CI (pg14-geos39-gdal33-proj71, tests)
- GitHub Check: CI (pg15-geos311-gdal35-proj90, coverage)
- GitHub Check: CI (pg16-geos312-gdal37-proj921, coverage)
- GitHub Check: Analyze (c-cpp)
🔇 Additional comments (15)
doc/html/images/Makefile.in (1)
50-52: New simplification images added to GENERATED_IMAGES.
The entries forcg_simplify00.png,cg_simplify01.png, andcg_simplify02.pngcorrectly integrate the new CG_Simplify visual examples into the build.sfcgal/regress/tests.mk.in (1)
40-44: Conditionally includesimplify.sqlfor SFCGAL ≥ 2.1.0.
This block mirrors the existing version checks and will ensure the new regression tests run only when the SFCGAL backend supports CG_Simplify. The version constant20100correctly represents 2.1.0 in this scheme.sfcgal/sfcgal.sql.in (1)
598-603: SQL function definition looks good.The new
CG_Simplifyfunction follows the standard pattern of SFCGAL function definitions, with proper implementation linking, parameter definition, and function attributes. The preserveTopology parameter defaulting to false aligns with standard simplification behavior while providing flexibility.sfcgal/regress/simplify_expected (1)
1-2: Regression test expected output is correctly formatted.The expected output for both simplification operations (with and without topology preservation) is properly defined, showing the same geometries but with different topology characteristics. Both outputs correctly maintain ZM dimension information from the input.
sfcgal/regress/simplify.sql (1)
1-2: Test cases comprehensive for core functionality.Both test cases use the same complex 4D geometry collection with Z and M coordinates, properly testing the simplification with and without topology preservation. The tests provide good coverage for the function's key capabilities.
sfcgal/lwgeom_sfcgal.c (2)
80-80: Function prototype correctly added.The
sfcgal_simplifyfunction is properly declared in the prototypes section, following the pattern of other SFCGAL functions.
1732-1767: Implementation is robust with proper version checking.The function implementation:
- Properly checks for SFCGAL version 2.1.0+ compatibility
- Correctly handles parameter validation and conversion
- Follows the standard pattern of initialize → convert → process → convert back → cleanup
- Properly manages memory by deleting temporary geometries
The implementation preserves the original SRID of the input geometry when returning the simplified geometry. This is essential for maintaining spatial reference integrity.
liblwgeom/lwgeom_sfcgal.c (5)
239-262: Well-designed helper function for dimensional handling.The function
create_sfcgal_point_by_dimensionsis a good abstraction that cleanly handles different dimension combinations (XY, XYZ, XYM, XYZM) with proper version checking. The careful handling of backward compatibility for SFCGAL versions before 1.5.0 is excellent.
267-285: Good use of POINT4D for handling all dimensions.The refactored code properly extracts both Z and M dimensions from the point array using the appropriate flags. Using the
getPoint4d_pfunction to retrieve the full dimensional information and delegating to the new helper function creates cleaner, more maintainable code.
286-298: Clean implementation for linestring point handling.The linestring case is well implemented, iterating through all points and properly handling their dimensional information. This change ensures that M-values are preserved when converting from PostGIS to SFCGAL geometries.
299-328: Good version-specific triangle handling.The triangle case correctly handles measured dimensions when SFCGAL version is 1.5.0 or higher, falling back to coordinate-specific methods for older versions or non-measured cases. This ensures proper preservation of M values while maintaining backward compatibility.
329-333: Corrected error message matches function name.The error message in the default case has been correctly updated to reflect the function name
ptarray_to_SFCGALinstead of the previous incorrect reference toptarray_from_SFCGAL.doc/reference_sfcgal.xml (3)
3571-3576: Check xml:id and naming consistency.The
<refentry xml:id="CG_Simplify">and<refname>CG_Simplify</refname>align with the naming of other CG_ functions. Please ensure that the&M_support;entity used later is defined in the DocBook preamble so that the M-coordinate support macro resolves correctly.
3606-3628: Parameter descriptions are thorough.The entries for
geom,threshold, andpreserveTopologyclearly describe their purpose and defaults. Optionally, you may note thatthresholdmust be non-negative, matching the C implementation’s validation.
3630-3636: Return Value section is concise and accurate.It correctly states that Z and M values are preserved in the output, reflecting the implementation.
| @@ -0,0 +1 @@ | |||
| GEOMETRYCOLLECTION(MULTIPOLYGON(((5.96 46.13,6.81 46.39,6.8 45.78,6.33 45.69,5.83 45.94,5.96 46.13))),MULTIPOLYGON(((6.33 45.69,6.8 45.78,7.14 45.26,5.62 45.61,5.83 45.94,6.33 45.69)))) | |||
There was a problem hiding this comment.
Filename prefix mismatch: cg_simplicity01.wkt vs cg_simplify01.png.
The Makefile rule generates cg_simplify01.png from wkt/cg_simplify01.wkt, but the source WKT is named cg_simplicity01.wkt. Rename the file to cg_simplify01.wkt (and update references) so the generator can locate it.
🤖 Prompt for AI Agents (early access)
In doc/html/images/wkt/cg_simplicity01.wkt at line 1, the filename prefix does
not match the expected prefix used in the Makefile and related references.
Rename the file from cg_simplicity01.wkt to cg_simplify01.wkt and update any
references in the Makefile or code to use the new filename so the generator can
correctly locate and process the source WKT file.
| @@ -0,0 +1 @@ | |||
| GeometryCollection(MULTIPOLYGON(((5.96 46.13,6.3 46.26,6.32 46.37,6.81 46.39,6.8 46.14,7.04 45.92,6.8 45.78,6.51 45.91,6.33 45.69,6.1 45.76,5.83 45.94,5.89 46.12,5.96 46.13))),MULTIPOLYGON(((6.33 45.69,6.51 45.91,6.8 45.78,7.18 45.41,7.14 45.26,6.63 45.11,6.26 45.13,6.12 45.44,5.91 45.39,5.62 45.61,5.83 45.94,6.1 45.76,6.33 45.69)))) | |||
There was a problem hiding this comment.
Filename and WKT keyword casing inconsistencies.
- The file is named
cg_simplicity00.wktbut the Makefile expectscg_simplify00.wkt. - Inside the file,
GeometryCollectionis mixed-case, whereas other examples useGEOMETRYCOLLECTION.
Apply these fixes:
- Rename the file in the repository:
git mv doc/html/images/wkt/cg_simplicity00.wkt doc/html/images/wkt/cg_simplify00.wkt
- Update the keyword casing in the file:
-GeometryCollection(MULTIPOLYGON(... +GEOMETRYCOLLECTION(MULTIPOLYGON(...
🤖 Prompt for AI Agents (early access)
In doc/html/images/wkt/cg_simplicity00.wkt at line 1, rename the file to
cg_simplify00.wkt to match the Makefile expectation. Also, change the keyword
GeometryCollection to uppercase GEOMETRYCOLLECTION to maintain consistent WKT
keyword casing with other examples.
30c1eb1 to
a7b3e32
Compare
There was a problem hiding this comment.
Actionable comments posted: 2
♻️ Duplicate comments (1)
doc/reference_sfcgal.xml (1)
3641-3654: 🛠️ Refactor suggestionExplicitly cast geometry literals in examples
Examples that pass raw string literals forGEOMETRYCOLLECTION ZM(...)rely on implicit type inference. PostgreSQL may not infer the correct geometry type without an explicit cast. Update the two examples as follows:-- Simplify two geometries preserving Z and M values, without topology - SELECT ST_AsText(CG_Simplify('GEOMETRYCOLLECTION ZM(...)', 2, false)); + SELECT ST_AsText(CG_Simplify('GEOMETRYCOLLECTION ZM(...)'::geometry, 2, false)); -- Simplify two geometries preserving Z and M values, with topology - SELECT ST_AsText(CG_Simplify('GEOMETRYCOLLECTION ZM(...)', 2, true)); + SELECT ST_AsText(CG_Simplify('GEOMETRYCOLLECTION ZM(...)'::geometry, 2, true));
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
doc/html/images/Makefile.in(1 hunks)doc/html/images/wkt/cg_simplify00.wkt(1 hunks)doc/html/images/wkt/cg_simplify01.wkt(1 hunks)doc/html/images/wkt/cg_simplify02.wkt(1 hunks)doc/reference_sfcgal.xml(2 hunks)liblwgeom/lwgeom_sfcgal.c(1 hunks)
✅ Files skipped from review due to trivial changes (3)
- doc/html/images/wkt/cg_simplify01.wkt
- doc/html/images/wkt/cg_simplify02.wkt
- doc/html/images/wkt/cg_simplify00.wkt
🚧 Files skipped from review as they are similar to previous changes (2)
- doc/html/images/Makefile.in
- liblwgeom/lwgeom_sfcgal.c
⏰ Context from checks skipped due to timeout of 90000ms (9)
- GitHub Check: CI (pg13-geos38-gdal31-proj71, tests)
- GitHub Check: CI (pg12-geos38-gdal30-proj611, tests)
- GitHub Check: CI (pg13-clang-geos39-gdal31-proj71, usan_clang)
- GitHub Check: CI (latest, tests)
- GitHub Check: CI (pg15-geos311-gdal35-proj90, coverage)
- GitHub Check: CI (pg15-clang-geos311-gdal35-proj90, usan_clang)
- GitHub Check: CI (pg14-geos39-gdal33-proj71, tests)
- GitHub Check: Analyze (c-cpp)
- GitHub Check: macOS
a7b3e32 to
4d339b9
Compare
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
sfcgal/lwgeom_sfcgal.c (1)
1742-1742: Fix comment to match version check condition.There's a mismatch between the conditional check and the comment. The condition checks for
< 20100but the comment mentions>= 21000.-#else /* POSTGIS_SFCGAL_VERSION >= 21000 */ +#else /* POSTGIS_SFCGAL_VERSION >= 20100 */
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (11)
doc/html/images/Makefile.in(1 hunks)doc/html/images/wkt/cg_simplify00.wkt(1 hunks)doc/html/images/wkt/cg_simplify01.wkt(1 hunks)doc/html/images/wkt/cg_simplify02.wkt(1 hunks)doc/reference_sfcgal.xml(2 hunks)liblwgeom/lwgeom_sfcgal.c(1 hunks)sfcgal/lwgeom_sfcgal.c(2 hunks)sfcgal/regress/simplify.sql(1 hunks)sfcgal/regress/simplify_expected(1 hunks)sfcgal/regress/tests.mk.in(1 hunks)sfcgal/sfcgal.sql.in(1 hunks)
✅ Files skipped from review due to trivial changes (2)
- sfcgal/regress/tests.mk.in
- doc/html/images/Makefile.in
🚧 Files skipped from review as they are similar to previous changes (8)
- doc/html/images/wkt/cg_simplify02.wkt
- doc/html/images/wkt/cg_simplify01.wkt
- sfcgal/regress/simplify_expected
- doc/html/images/wkt/cg_simplify00.wkt
- sfcgal/sfcgal.sql.in
- sfcgal/regress/simplify.sql
- doc/reference_sfcgal.xml
- liblwgeom/lwgeom_sfcgal.c
🧰 Additional context used
🧬 Code Graph Analysis (1)
sfcgal/lwgeom_sfcgal.c (2)
sfcgal/lwgeom_sfcgal.h (3)
sfcgal_postgis_init(40-40)POSTGIS2SFCGALGeometry(28-28)SFCGALGeometry2POSTGIS(34-34)liblwgeom/gserialized.c (1)
gserialized_get_srid(155-161)
⏰ Context from checks skipped due to timeout of 90000ms (8)
- GitHub Check: macOS
- GitHub Check: Analyze (c-cpp)
- GitHub Check: CI (latest, tests)
- GitHub Check: CI (pg13-clang-geos39-gdal31-proj71, usan_clang)
- GitHub Check: CI (pg13-geos38-gdal31-proj71, tests)
- GitHub Check: CI (pg12-geos38-gdal30-proj611, tests)
- GitHub Check: CI (pg15-clang-geos311-gdal35-proj90, usan_clang)
- GitHub Check: CI (pg14-geos39-gdal33-proj71, tests)
🔇 Additional comments (6)
sfcgal/lwgeom_sfcgal.c (6)
80-80: Good function declaration placement.This is a well-placed function declaration alongside other similar SFCGAL functions. Adding the function prototype in the appropriate location ensures consistent organization of the code.
1732-1742: Version checking aligned with requirements.The function correctly checks for SFCGAL version 2.1.0 as specified in the PR objectives. This ensures that users are informed when trying to use this function with an incompatible SFCGAL version.
1743-1748: Good variable declarations and clear naming.The variables are well-named and clearly indicate their purpose, maintaining consistency with the rest of the codebase. The function correctly declares variables for input/output geometries, threshold, topology preservation flag, and SRID.
1749-1758: Proper initialization and parameter extraction.The function follows the established pattern for SFCGAL functions:
- Initializes the SFCGAL library
- Extracts input parameters
- Converts PostGIS geometry to SFCGAL geometry
- Frees the input geometry
This ensures consistent behavior and proper memory management.
1759-1764: Appropriate geometry processing and memory cleanup.The function correctly:
- Calls the SFCGAL simplify function with appropriate parameters
- Frees the input SFCGAL geometry
- Converts the result back to PostGIS format
- Frees the SFCGAL result geometry
This ensures proper memory management and follows the established pattern in the codebase.
1765-1767: Function correctly returns the processed geometry.The function properly returns the simplified geometry using the PostgreSQL function return mechanism, which is consistent with other functions in the file.
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
liblwgeom/cunit/cu_sfcgal.c (1)
103-125: Consider enhancing test failure messages with test case indexWhile the error message formatting has been improved, consider adding the test case index to the error message to make it easier to identify which test case failed.
- fprintf(stderr, "\nExp:\t%s\nObt:\t%s\n", expected_ewkt[i], out_ewkt); + fprintf(stderr, "\nTest case %zu failed:\nExp:\t%s\nObt:\t%s\n", i, expected_ewkt[i], out_ewkt);
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
liblwgeom/cunit/cu_sfcgal.c(3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (8)
- GitHub Check: macOS
- GitHub Check: CI (pg12-geos38-gdal30-proj611, tests)
- GitHub Check: CI (pg14-geos39-gdal33-proj71, tests)
- GitHub Check: CI (pg13-geos38-gdal31-proj71, tests)
- GitHub Check: CI (latest, tests)
- GitHub Check: CI (pg13-clang-geos39-gdal31-proj71, usan_clang)
- GitHub Check: CI (pg15-clang-geos311-gdal35-proj90, usan_clang)
- GitHub Check: Analyze (c-cpp)
🔇 Additional comments (3)
liblwgeom/cunit/cu_sfcgal.c (3)
44-63: Good addition of test cases for Z/M supportThese new test cases comprehensively cover all geometry types with Z, M, and combined ZM dimensions, which is excellent for ensuring the proper handling of geometries with additional dimensions.
82-101: Appropriate expected outputs for Z/M test casesThe expected outputs correctly reflect how SFCGAL handles different dimension types:
- For ZM geometries, the dimension indicators are dropped in output type names
- For M-only geometries, the "M" suffix is preserved
This behavior is consistent across all geometry types and properly tests the dimensional handling in the SFCGAL backend.
120-120: Improved error message formattingThe change to tab-based formatting improves readability for test failure messages, making it easier to compare expected vs obtained values during debugging.
5729dba to
366478b
Compare
366478b to
62de99f
Compare
|
Merged |
This introduces CG_Simplify, a new function for SFCGAL
backend based on sfcgal_geometry_simplify.
The algorithm is based on CGAL Simplification package and uses CDT.
The documentation and unit tests are updated accordingly.
Since our algorithm supports both Z and M coordinates, I have also enhanced the ptarray to SFCGAL conversion with proper M dimension support. This enhancement requires SFCGAL 1.5.0 or higher for M coordinate handling. This requirement is not an issue in practice, as the CG_Simplify function itself requires SFCGAL 2.1.0 or higher, which already satisfies the dependency.
SFCGAL 2.1 or above is needed.
cc @ptitjano