Search code examples
rdplyrarcgis

R-ArcGIS: Impossible to perform dplyr join with data frame?


I've just begun to use the R-ArcGIS bridge package arcgisbinding and am running into a problem when I try to join feature class data with the dplyr package. Here is an example where I'm trying to get the ozone columns from two shapefiles into a single data frame, then export back as a shapefile.

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)

From the arcgisbinding package, p and d above are data frame objects with shape attributes. The problem is that once I use left_join, I lose the spatial attribute data in the joined data frame. Is there a way around this?


Solution

  • So apparently this is a known problem (see GitHub here).

    A workaround using the spdplyr package comes by way of Shaun Wallbridge on ESRI GeoNet (link to thread). Basically, convert the arc.data data frame into an sp object, perform analyses, then export as a feature class or 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)