Sf: Please consider implementing st_makevalid

Created on 10 Nov 2016  路  10Comments  路  Source: r-spatial/sf

I've searched many times over the years for an R version of postgis' st_makevalid() function _(via_ liblwgeom) and wonder if this would be a good time and place for it. Thanks for all your hard work!

Most helpful comment

st_make_valid() is in lwgeom on CRAN, including Windows and OSX binaries.

All 10 comments

I'll second that.

I have frequently resorted to pre-processing spatial data in GRASS (via rgrass7) because there isn't a good R implementation of makevalid. While this usually resolves my issue, the installation and setup of GRASS is non-trivial (esp. for Mac users) and it detracts from the reproducibility of an analysis.

It's definitely out of scope for this project, and would require additional packages on all CRAN check serves. But surely a nice idea!

Does cleangeo go in that direction? Maybe helping Emmanuel with enabling or duplicating this for sf is a better way forward than making sf too big?

More realistically, @onehundredpercenttexan , where is the stand-alone lwgeom library to which one could possibly link? Your link seems to points to a component of PostGIS.

I was under the impression that there were changes made to lwgeom that allow for somewhat standalone use:
https://github.com/postgis/postgis/pull/6

Is the RT Topology Library what you had in mind?

Some good news to be reported here: we have sf now autodeting lwgeom, and if present provide a working st_make_valid:

> library(sf)
Linking to GEOS 3.5.0, GDAL 2.1.3, proj.4 4.9.2, lwgeom 2.2.1 r14555
> x = st_sfc(st_polygon(list(rbind(c(0,0),c(0.5,0),c(0.5,0.5),c(0.5,0),c(1,0),c(1,1),c(0,1),c(0,0)))))
> st_is_valid(x)
[1] FALSE
Warning message:
In eval(substitute(expr), envir, enclos) :
  Self-intersection at or near point 0.5 0
> y = st_make_valid(x)
> st_is_valid(y)
[1] TRUE
> y
Geometry set for 1 feature 
geometry type:  GEOMETRYCOLLECTION
dimension:      XY
bbox:           xmin: 0 ymin: 0 xmax: 1 ymax: 1
epsg (SRID):    NA
proj4string:    NA
GEOMETRYCOLLECTION(POLYGON((0.5 0, 0 0, 0 1, 1 ...
> y %>% st_cast()
Geometry set for 2 features 
geometry type:  GEOMETRY
dimension:      XY
bbox:           xmin: 0 ymin: 0 xmax: 1 ymax: 1
epsg (SRID):    NA
proj4string:    NA
POLYGON((0.5 0, 0 0, 0 1, 1 1, 1 0, 0.5 0))
LINESTRING(0.5 0, 0.5 0.5)
> 

I render it quite unlikely that there will be support for lwgeom on CRAN, i.e. in the binary versions distributed there for Windows and Mac, any time soon. If there are other things in lwgeom useful for sf, let me know.

@edzer Somehow I don't get if this is actualy a wontfix, or if it is fixed.
I just installed the latest version via devtools::install_github("r-spatial/sf"), but st_make_valid doesn't seem to be a part of the package.
Any way I can get it?

Thanks!

st_make_valid() is in lwgeom on CRAN, including Windows and OSX binaries.

@rsbivand so it is actually a desired behaviour that although I have lwgeom and r-spatial/lwgeom sf doesn't automatically connect to it? [in the example above provided by @edzer it does]

> library(sf)
Linking to GEOS 3.5.1, GDAL 2.2.2, proj.4 4.9.2
> library(lwgeom)
Linking to liblwgeom 2.4.2 r16113, GEOS 3.5.1, proj.4 4.9.2

Anyways, it is working, thanks a lot! Especially for a super quick reply

Was this page helpful?
0 / 5 - 0 ratings