R-ArcGIS:不可能用数据框执行dplyr连接?
问题描述:
我刚刚开始使用R-ArcGIS桥包arcgisbinding
,并且在尝试将要素类数据与dplyr
包结合时遇到问题。下面是一个例子,我试图从两个shape文件中将臭氧层列转换为单个数据帧,然后将其作为shapefile导出。上述R-ArcGIS:不可能用数据框执行dplyr连接?
library(dplyr)
library(arcgisbinding)
arc.check_product()
fc <- arc.open(system.file("extdata", "ca_ozone_pts.shp",
package="arcgisbinding"))
d <- arc.select(fc, fields=c('FID', 'ozone'))
p<-arc.select(fc,fields=c('FID', 'ozone'))
p$ozone<-p$ozone*2
p<-left_join(p,d,by="FID")
arc.write(tempfile("ca_new", fileext=".shp"), p)
# original dataframe has shape attributes
str(d)
# new dataframe does not
str(p)
从arcgisbinding
包,p
和d
与形状属性的数据帧的对象。问题是,一旦我使用left_join
,我会丢失连接数据帧中的空间属性数据。有没有解决的办法?
答
显然这是一个已知的问题(see GitHub here)。
使用spdplyr
包的解决方法是通过Sharon Wallbridge在ESRI GeoNet(link to thread)上提供的。基本上,将arc.data数据框转换为sp对象,执行分析,然后导出为要素类或shapefile。
library(spdplyr)
library(arcgisbinding)
arc.check_product()
fc <- arc.open(system.file("extdata", "ca_ozone_pts.shp", package="arcgisbinding"))
d <- arc.select(fc,fields=c('FID', 'ozone'))
d.sp <- arc.data2sp(d)
p <-arc.select(fc,fields=c('FID', 'ozone'))
p.sp <- arc.data2sp(p)
p.sp$ozone <- p$ozone*2
joined <- left_join(p.sp, d.sp, by="FID", copy=TRUE)
joined.df <- arc.sp2data(joined)
arc.write(tempfile("ca_ozone_pts_joined", fileext=".shp"), joined.df)