Description
Here is an unusual outcome of the st_make_valid
function applied to a real-life polygon:
> Pol <- st_read("https://raw.githubusercontent.com/jldupouey/DataGitJLD/main/weirdpol.gpkg")
Reading layer `weirdpol' from data source
`https://raw.githubusercontent.com/jldupouey/DataGitJLD/main/weirdpol.gpkg' using driver `GPKG'
Simple feature collection with 1 feature and 0 fields
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: 952404.6 ymin: 6451236 xmax: 953294.4 ymax: 6451311
Projected CRS: Undefined Cartesian SRS with unknown unit
> plot(Pol)
> st_is_valid(Pol, reason=TRUE)
[1] "Self-intersection[953233.287955463 6451236.21683465]"
> Polv <- st_make_valid(Pol)
> plot(Polv)
Only the right-hand side of this invalid polygon has been retained.
1 - I understand that the self-intersection is what makes this polygon invalid. But could someone please explain why the left side was removed during the repair process? I suppose it is linked to an unusual configuration of this polygon. However, I do not understand why such a simple self-intersection would cause this issue during the repair process.
This is all the more surprising given that the following polygon, which has the same topology, doesn't pose this problem:
> Pol2 <- matrix(c(0,0,0,10,10,10,10,0,11,0,11,1,12,1,12,0,0,0),ncol=2,byrow=TRUE)
> Pol2 <- st_polygon(list(Pol2))
> Pol2 <- st_sfc(Pol2)
> plot(Pol2,axes=TRUE)
> st_is_valid(Pol2,reason=TRUE)
[1] "Ring Self-intersection[11 0]"
> Pol2v <- st_make_valid(Pol2)
> plot(Pol2v,axes=TRUE)
> st_is_valid(Pol2v)
[1] TRUE
2 - Is there a recommended way to avoid this issue?
Setting the geos_method
parameter to valid_linework
in st_make_valid
allows a correct result:
> Polv2 <- st_make_valid(Pol, geos_method="valid_linework")
> st_is_valid(Polv2, reason=TRUE)
[1] "Valid Geometry"
> plot(Polv2)
However, the valid_structure
method is generally preferable to the valid linework
method. Is there a way to use the valid structure method in general while avoiding this particular issue?
If not, large parts of polygons can be lost during the repair process, which is problematic.