【发布时间】:2020-01-01 21:55:37
【问题描述】:
我正在尝试使用 R 包 tmap 绘制 SpatialLinesDataFrame。但是我不断收到此错误消息:
CPL_geos_is_empty(st_geometry(x)) 中的错误:评估错误: IllegalArgumentException:点数组必须包含 0 或 >1 个元素。
我首先尝试使用 tmap 绘制“sp”对象。然后我将 sp 对象转换为“sf”对象,但仍然收到相同的错误消息。我做了一点谷歌搜索,我认为这可能与 SpatialLinesDataFrame 中的线并非完全连接的事实有关。
所以我将一组连接的线作为子集,并使用 tmap 绘制得很好。
我将在这里使用的 shapefile 放在了我的 Github 上。
EX1812_SPB_combined 是包含未连接线的 shapefile,并且是不使用 tmap 绘制并给我上述错误的文件。如果我使用 R 的基本 plot(),它的绘图就很好。
EX1812_SBP_combined_section 是我从前者中提取的包含连接线的 shapefile 子集。这个使用 tmap 绘制得很好。
这就是我想要做的:
尝试 1:尝试绘制 shapefile
library(tmap)
library(sf)
library(rgdal)
library(sp)
# read in shapefile as SpatialLinesDataFrame
sbp <- readOGR(dsn = "file/path", layer = "EX1812_SPB_combined")
# plot using tmap
# this does not work and gives me the aforementioned error
tm_shape(sbp) + tm_lines()
尝试 2:将 SpatialLinesDataFrame 转换为 sf 对象
# convert to sf
sbp_sf <- st_as_sf(sbp) + st_set_crs(4326)
# plot using tmap
# this also does not work and gives me the same error
tm_shape(sbp_sf) + tm_lines()
尝试3:读取连接线的shapefile并绘制
# read in shapefile as SpatialLinesDataFrame
sbp_connect <- readOGR(dsn = "file/path", layer = "EX1812_SBP_combined_section")
# plot using tmap
# this works
tm_shape(sbp_connect) + tm_lines()
即使线路断开,我也只想绘制整个 SpatialLinesDataFrame。有没有办法使用 tmap 解决这个问题?还是我遗漏了一些更大的问题?
【问题讨论】: