Use ST_MakeValid with method=structure instead of ST_Buffer with radius 0#306
Merged
Merged
Conversation
Contributor
Author
|
The failures with PostGIS < 3.2 are expected (see explanation above). I couldn't find mention of a minimum PostGIS version in the docs, but I guess now I know 🙂 Could the minimum version be increased? Otherwise, including a GEOS version check inside the SQL fragment would probably be the best solution. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes a problem we faced when generalizing this polygon: https://www.openstreetmap.org/relation/13319209. At one point, the following error occurred:
The root cause is a problem in
ST_SimplifyPreserveTopologywhich leaves a hole outside the outer shell, andST_Buffercan't handle it (at least using GEOS 3.12.1 that came with PostGIS 3.4 in our installation).Since GEOS 3.10 / PostGIS 3.2, the recommended approach to repair polygons is
ST_MakeValidwithmethod=structure(see PostGIS docs and also this post: https://www.crunchydata.com/blog/waiting-for-postgis-3.2-st_makevalid). This should yield the same results as anST_Bufferwith 0 radius, but in a more robust way. For this PR, I replaced the use ofST_Bufferaccordingly, and the generalization now works for us.An alternative approach for older versions would be to call
ST_MakeValid(without the additional parameter) afterST_SimplifyPreserveTopology. This seems to get rid of "outer holes", and the result can be used as input forST_Buffer(to fix the remaining problems that a simpleST_MakeValidcan't handle). We could even add a version check to use different calls depending on the GEOS version, but that seems like overkill? PostGIS 3.2 was released in 2021, and looking at Ubuntu as an example, it's part of the main packages since 22.04 LTS. So simply replacingST_Buffershould be fine nowadays?