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

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

希望在 R 中做两个 LINESTRING 的相交。

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

以上步骤成功,可以在R中加载shapefile

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.

那么,如何解决呢?谢谢。

我检查了 shapefile nc1,发现每个特征只有 1 个点,即使它们是 LINESTRING

去掉这1个点LINESTRINGst_intersects操作即可成功

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

这里 cnt2 是新创建的 data.frame 列,用于在 geometry 列中存储 "," 的数量。这可以表示每个特征的点数。

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

我们可以这样做:

res= st_intersects(nc3, nc2)

或:

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

我也遇到了这个问题。如果您将对象显式转换为正确的类型,它通常可以解决任何奇怪的几何问题。

nc1 <- st_cast(nc1,  "LINESTRING")

也总是值得一看 st_is_valid 和 st_make_valid