无法在 R 中绘制 sf 线串:CPL_geos_is_empty(st_geometry(x)) 中的错误

Cannot plot sf linestring in R: Error in CPL_geos_is_empty(st_geometry(x))

我有飓风轨迹点,我在 QGIS 中将其转换为线:

https://i.imgur.com/PUOTpsi.png

https://i.imgur.com/5cbCdX2.png

我都保存为 shapefile 并使用 sf 包将它们加载到 R 中。这些点将使用标准 plot() 函数绘制,但线条不会。

我遇到错误:

plot(hurricane_paths)
Error in CPL_geos_is_empty(st_geometry(x)) : 
     Evaluation error: IllegalArgumentException: point array must contain 0 or >1 elements.

我在使用plot(st_geometry(hurricane_paths))

时出现同样的错误

R 肯定会加载到几何中,但是:

> hurricane_paths
Simple feature collection with 1410 features and 5 fields
geometry type:  LINESTRING
dimension:      XY
bbox:           xmin: -179.9 ymin: -4.9 xmax: 8 ymax: 70.7
epsg (SRID):    4269
proj4string:    +proj=longlat +datum=NAD83 +no_defs
First 10 features:
             N.A begin  end Year           N.A_1                       geometry
1  1976143N24271  <NA> <NA> 1976 SUBTROP:UNNAMED LINESTRING (-89 24, -89.6 2...
2  1976155N11265  <NA> <NA> 1976         ANNETTE LINESTRING (-95 11.4, -95.2...
3  1976159N27281  <NA> <NA> 1976         UNNAMED LINESTRING (-79 26.8, -78.5...

st_geometry(hurricane_paths) returns

Geometry set for 1410 features 
geometry type:  LINESTRING
dimension:      XY
bbox:           xmin: -179.9 ymin: -4.9 xmax: 8 ymax: 70.7
epsg (SRID):    4269
proj4string:    +proj=longlat +datum=NAD83 +no_defs
First 5 geometries:
LINESTRING (-89 24, -89.6 24.8, -90 25.4, -90.5...
LINESTRING (-95 11.4, -95.2 11.7, -95.3 12.1, -...
LINESTRING (-79 26.8, -78.5 28, -78.1 29.2, -77...
LINESTRING (-81 26.5, -78.5 28.2, -76.2 30, -73...
LINESTRING (-103 16, -104.1 15.8, -105.1 15.8, ...

我已经在几何列中检查了 NA:

> which(is.na(hurricane_paths$geometry)==T)
integer(0)

plot_sf() 不会 return 任何错误消息,但它 return 在绘图窗格中是一个空白屏幕,因此也不行。

但是 ggplot2 很棒,而且 return 是一个图形,没问题:

> ggplot() + 
+   geom_sf(data = hurricane_paths)

Mapview 也可以:> mapview::mapview(hurricane_paths)

但基本的 plot() 功能仍然很顽固。 可能是什么问题?

没有数据很难提供帮助。 st_length(hurricane_paths) 是否显示任何零长度几何图形?如果您绘制一个子集,例如 plot(hurricane_paths[1:10,]) 绘制前十个怎么办?

我可以通过创建一条只有一个点的线来复制您的错误:

> Line = st_sfc(st_linestring(x = matrix(1, ncol=2, nrow=1), dim = "XYZ"))
> g = st_sf(data.frame(x=1, geom=Line))
> plot(g)
Error in CPL_geos_is_empty(st_geometry(x)) : 
  Evaluation error: IllegalArgumentException: point array must contain 0 or >1 elements.

我怀疑单坐标 "line" 在简单特征规范下是无效的,实际上应该转换为 POINT 几何体,或者可能通过添加第二个相同的坐标来修复,这样它仍然是一个线,但有两个点。

您可以使用 st_is_valid 来检测这样的事情(它甚至用一个点来讨论 LINESTRING),然后将它们转换为 POINT,或者删除它们,或者根据您的分析适当地处理它们。

我遇到了和你一样的问题,问题似乎出在从点到线串的转换上。按照线程 中找到的解决方案,您可能必须使用

summarise(do_union = FALSE) %>%
st_cast("LINESTRING")

在将点数据集转换为线串之前。

希望对您有所帮助。