Sf: st_split leads to crashing r session

Created on 28 Sep 2017  路  8Comments  路  Source: r-spatial/sf

Hi all,

st_split can be used to split a linestring with points, but is it possible to split a polygon with a line?

A simple example:

library(sf)
polygon <- st_polygon(list(cbind(c(0,0,1,1,0),c(0,1,1,0,0))))
plot(polygon)

blade_line <- st_linestring(matrix(c(runif(1), runif(1), runif(1), runif(1)),2))
plot(blade_line, add = TRUE)

... already leads to a crashing R session in my case. Is there anything wrong with sf, liblwgeom or whatever installation, or is just not possible to do that with sf?

According to this:

https://www.gaia-gis.it/fossil/libspatialite/wiki?name=liblwgeom-4.0

... it should be possible.

> session_info()
Session info --------------------------------------------
 setting  value                       
 version  R version 3.4.1 (2017-06-30)
 system   x86_64, linux-gnu           
 ui       RStudio (1.1.331)           
 language (EN)                        
 collate  en_US.UTF-8                 
 tz       Europe/Berlin               
 date     2017-09-28                  

Packages --------------------------------------------
 package   * version date       source        
 base      * 3.4.1   2017-07-08 local         
 compiler    3.4.1   2017-07-08 local         
 datasets  * 3.4.1   2017-07-08 local         
 DBI         0.7     2017-06-18 CRAN (R 3.4.0)
 devtools  * 1.13.3  2017-08-02 CRAN (R 3.4.1)
 digest      0.6.12  2017-01-27 CRAN (R 3.3.3)
 graphics  * 3.4.1   2017-07-08 local         
 grDevices * 3.4.1   2017-07-08 local         
 grid        3.4.1   2017-07-08 local         
 magrittr    1.5     2014-11-22 CRAN (R 3.3.3)
 memoise     1.1.0   2017-04-21 CRAN (R 3.4.0)
 methods   * 3.4.1   2017-07-08 local         
 Rcpp        0.12.12 2017-07-15 CRAN (R 3.4.1)
 sf        * 0.5-4   2017-08-28 CRAN (R 3.4.1)
 stats     * 3.4.1   2017-07-08 local         
 tools       3.4.1   2017-07-08 local         
 udunits2    0.13    2016-11-17 CRAN (R 3.4.1)
 units       0.4-6   2017-08-27 CRAN (R 3.4.1)
 utils     * 3.4.1   2017-07-08 local         
 withr       2.0.0   2017-07-28 CRAN (R 3.4.1)
 yaml        2.1.14  2016-11-12 CRAN (R 3.4.0)

Most helpful comment

People finding this page through search engines should be aware that st_split was moved to the lwgeom package: https://github.com/r-spatial/lwgeom

All 8 comments

This is not a reprex for anything; crash is defined in the R FAQ. When using randon numbers, always set the seed. Nothing unusual for me. If you want to contribute code, please do so, use a polygon to divide a polygon.

I also cannot reproduce this. What do you see when you try

> sf::sf_extSoftVersion()
          GEOS           GDAL         proj.4         lwgeom 
       "3.5.1"        "2.2.0"        "4.9.2" "2.3.3 r15473" 

?

Please reopen in case you can provide a reproducible example.

I'd like to reopen the issue as I experience the same problem.
But first:


Session Info

> sf::sf_extSoftVersion()
          GEOS           GDAL         proj.4         lwgeom 
       "3.5.1"        "2.1.3"        "4.9.2" "2.4.0 r15853" 

```r

sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.3 LTS

Matrix products: default
BLAS: /usr/lib/openblas-base/libblas.so.3
LAPACK: /usr/lib/libopenblasp-r0.2.18.so

locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8
[4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] sf_0.5-5

loaded via a namespace (and not attached):
[1] compiler_3.4.2 magrittr_1.5 DBI_0.7 tools_3.4.2 units_0.4-6 yaml_2.1.14
[7] Rcpp_0.12.13 udunits2_0.13 grid_3.4.2

</details>

## Reproducible example

```r
pol <- rbind(c(-3,3), c(-3,4), c(-2,4), c(-2,3), c(-3,3))
pol <- st_polygon(list(pol))
blade <- rbind(c(-2.5, 3), c(-2.6, 4))
blade <- st_linestring(blade)
st_split(pol, blade)

This crashes the R session. Could it come from the GDAL version?

This also crashes my session and I'm on GDAL 2.2.1

screenshot at 2017-10-03 10 42 27

When running R in the terminal it also crashes but at least it tells me that 'Splitting a Polygon by a MultiLineString is unsupported'

screenshot at 2017-10-03 10 46 14

Well it is strange for two reasons:

  • It is not a multiline (or is it?).
  • You've got GDAL 2.2.1 and according to this page: https://postgis.net/docs/ST_Split.html,
    > The function supports splitting a line by (multi)point, (multi)line or (multi)polygon boundary, a (multi)polygon by line. The returned geometry is always a collection.
    > Availability: 2.0.0

Thanks! It doesn't crash R, it stops it, with a message, as Tim shows.

Splitting a Polygon by a MultiLineString is unsupported

The error is the call to st_combine(), the following works:

> st_sfc(sf:::CPL_split(st_geometry(pol), st_geometry(blade)))
Geometry set for 1 feature 
geometry type:  GEOMETRYCOLLECTION
dimension:      XY
bbox:           xmin: -3 ymin: 3 xmax: -2 ymax: 4
epsg (SRID):    NA
proj4string:    NA
GEOMETRYCOLLECTION (POLYGON ((-3 3, -3 4, -2.6 ...

with this commit, I get:

> st_split(pol, blade) %>% st_collection_extract("POLYGON")
Geometry set for 2 features 
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: -3 ymin: 3 xmax: -2 ymax: 4
epsg (SRID):    NA
proj4string:    NA
POLYGON ((-3 3, -3 4, -2.6 4, -2.5 3, -3 3))
POLYGON ((-2.6 4, -2 4, -2 3, -2.5 3, -2.6 4))

People finding this page through search engines should be aware that st_split was moved to the lwgeom package: https://github.com/r-spatial/lwgeom

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jsta picture jsta  路  4Comments

tiernanmartin picture tiernanmartin  路  3Comments

jmsigner picture jmsigner  路  4Comments

kendonB picture kendonB  路  3Comments

gregmacfarlane picture gregmacfarlane  路  4Comments