【问题标题】:Cannot plot sf linestring in R: Error in CPL_geos_is_empty(st_geometry(x))无法在 R 中绘制 sf 线串:CPL_geos_is_empty 中的错误(st_geometry(x))
【发布时间】:2020-02-12 11:25:18
【问题描述】:

我有飓风轨迹点,我在 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) 返回

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() 不返回任何错误消息,但它会在绘图窗格中返回一个空白屏幕,所以这也是不行的。

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

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

Mapview 也可以:&gt; mapview::mapview(hurricane_paths)

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

【问题讨论】:

    标签: r geometry gis qgis sf


    【解决方案1】:

    我遇到了和你一样的问题,问题似乎在于从点到线串的转换。按照线程Create Multilines from Points, grouped by ID with sf package 中找到的解决方案,您可能必须使用

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

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

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      没有数据很难提供帮助。 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.
      

      我怀疑单坐标“线”在简单特征规范下是无效的,应该真正转换为 POINT 几何图形,或者可能通过添加第二个相同的坐标来修复,这样它仍然是一条线但有两个指向它。

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

      【讨论】:

      • 您好,感谢您的回复。这是一个很好的建议,但st_is_valid(hurricane_paths) 仅返回我的数据集的TRUE 值。数据可以在这里找到:ncdc.noaa.gov/ibtracs/index.php?name=wmo-data
      • 您能否更具体地了解哪个数据集?你在 QGIS 中做了什么?您能否通过共享站点将您已读入 R 的 QGIS 输出提供?
      • 您是否尝试通过子集来缩小范围?
      猜你喜欢
      • 2020-01-28
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2020-08-14
      • 2016-01-11
      • 2019-12-03
      • 1970-01-01
      • 2019-11-27
      相关资源
      最近更新 更多