Search code examples
rgdalr-sfr-sprgdal

error when using st_intersects, in CPL_geos_binop(st_geometry(x), st_geometry(y), op, par, pattern : Evaluation error: IllegalArgumentException


Wish to do intersect two LINESTRING in R.

nc1 = st_read(shp_file_1)
nc2 = st_read(shp_file_2)

The above steps are successful, shapefiles can be loaded in R.

res= st_intersects(nc1, nc2)
Error in CPL_geos_binop(st_geometry(x), st_geometry(y), op, par, pattern,  :
  Evaluation error: IllegalArgumentException: point array must contain 0 or >1 elements.

So, how to fix it? Thank you.


Solution

  • I checked shapefile nc1 and found features having only 1 point per feature even they're LINESTRING.

    Remove these 1-point LINESTRING, the st_intersects operation can be successful.

    nc1$cnt2 = stringr::str_count(nc1$geometry, ",")
    

    Here cnt2 is a newly created data.frame column to store number of "," in the geometry column. This can indicate the number of points per feacture.

    library('dplyr')
    nc3 = filter(nc1, cnt2>1)
    

    We can either do:

    res= st_intersects(nc3, nc2)
    

    or:

    res = st_join(nc3, nc2, join = st_intersects)