r-spatial / lwgeom

bindings to the liblwgeom library
https://r-spatial.github.io/lwgeom/
58 stars 23 forks source link

st_split error - lwgeom Error MultiLineString is Unsupported #44

Closed jknowles closed 5 years ago

jknowles commented 5 years ago

I'm getting a warning that a LINESTRING cannot split a polygon because it is in fact a MultiLineString - even though it does not appear to be to me. I get this warning message:

Splitting a Polygon by a MultiLineString is unsupported Error in CPL_split(x, st_geometry(y)) : lwgeom error

I am using the latest GitHub development release of lwgeom on a Windows 10 installation of R 3.5.3 with rtools installed:

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

other attached packages:
[1] lwgeom_0.1-6 sf_0.7-3  

Below is a REPREX of my problem - forgive me for the length of the example - I didn't know how to succinctly represent the polygon I am working with:

library(sf)
library(lwgeom)

hex <-
  structure(
    list(structure(
      list(structure(
        c(
          -88.455060600427,
          -88.0925247037421,-87.7299888070571,
          -87.7299888070571,
          -88.0925247037421,
          -88.455060600427,-88.455060600427,
          43.8085223219415,
          44.0178325194835,
          43.8085223219415,
          43.3899019268576,
          43.1805917293156,
          43.3899019268576,
          43.8085223219415
        ),
        .Dim = c(7L, 2L),
        .Dimnames = list(NULL, c("x", "y"))
      )), class = c("XY",
                    "POLYGON", "sfg")
    )),
    class = c("sfc_POLYGON", "sfc"),
    precision = 0,
    bbox = structure(
      c(
        xmin = -88.455060600427,
        ymin = 43.1805917293156,
        xmax = -87.7299888070571,
        ymax = 44.0178325194835
      ),
      class = "bbox"
    ),
    crs = structure(
      list(epsg = 4326L, proj4string = "+proj=longlat +datum=WGS84 +no_defs"),
      class = "crs"
    ),
    n_empty = 0L
  )

hex_coords <- st_coordinates(hex)
hex_coords <- hex_coords[, 1:2]
hex_centr <- st_coordinates(st_centroid(hex))
# define lines that split 
blade_coords <- rbind(hex_coords[2, ], hex_centr,
                      hex_coords[4, ], hex_centr, 
                      hex_coords[6, ], hex_centr, 
                      hex_coords[2:7, ], 
                      hex_coords[1, ], 
                      hex_coords[2, ])
blade_string <- st_linestring(blade_coords)

plot(blade_string)
plot(hex)
lwgeom::st_split(hex, blade_string)

# Simpler string
blade_coords <- rbind(hex_coords[2, ], hex_centr,
                      hex_coords[4, ], hex_centr)
blade_string <- st_linestring(blade_coords)
plot(blade_string)
lwgeom::st_split(hex, blade_string)

Am I misunderstanding something about st_split?

edzer commented 5 years ago

Try

lwgeom::st_split(hex, st_sfc(blade_string))
jknowles commented 5 years ago

Well that's embarrassing. That works perfectly. Thanks for your help!