无法在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中将其转换为线:
I have hurricane track points which I converted to lines in QGIS:
https://i.imgur.com/PUOTpsi.png
https://i.imgur.com/5cbCdX2.png
我都保存为shapefile,并使用sf
包将它们加载到R中.这些点将使用标准的plot()
函数进行绘制,而线条则不会.
I have both saved as shapefiles and load them into R using the sf
package. The points will plot using the standard plot()
function, but the lines will not.
我遇到错误:
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肯定会加载到几何中,
R definitely loads in the geometry, though:
> 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:
I already checked for NAs in my geometry column:
> which(is.na(hurricane_paths$geometry)==T)
integer(0)
plot_sf()
不会返回任何错误消息,但是会在绘图"窗格中返回空白屏幕,因此也是不可行的.
plot_sf()
does not return any error message, but it returns a blank screen in the plots pane, so that's a no-go either.
但是ggplot2很不错,并且可以返回图形,没问题:
But ggplot2 is great, and returns a graphic, no problem:
> ggplot() +
+ geom_sf(data = hurricane_paths)
Mapview也很好:> mapview::mapview(hurricane_paths)
Mapview also is fine: > mapview::mapview(hurricane_paths)
但是基本的plot()
功能仍然很固执.
可能是什么问题?
But the basic plot()
function remains stubborn.
What could the problem be?
推荐答案
我遇到了和您一样的问题,看来问题出在从点到线串的转换上.遵循在线程在点中创建多线的解决方案之后,按ID和sf包分组,则可能必须使用
I was having a problem just like yours and it seems that the problem is in the conversion from points to linestring. Following a solution found in the thread Create Multilines from Points, grouped by ID with sf package, you may have to use
summarise(do_union = FALSE) %>%
st_cast("LINESTRING")
将点数据集转换为线串之前.
before transforming your point dataset to a linestring.
希望对您有帮助.
这篇关于无法在R中绘制SF线串:CPL_geos_is_empty(st_geometry(x))中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!