3

This code:

seattle <- spChFIDs(seattle.shp,
                    paste("seattle.shp",
                          row.names(seattle.shp), sep="."))

Results in this error:

Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘spChFIDs’ for signature ‘"SpatialPointsDataFrame", "character"’

I have tried raster::union; sp::merge, and rgeos::gUnion. None of which achieve what I want: The analogue of an ArcGIS merge.

library(raster)
?raster::union 
test<-union(phoenix.shp,seattle.shp)
#Error in as.vector(x) : no method for coercing this S4 class to a vector
#s4 class seems to have slots, accessed by the @ symbol.
#This has both polygons and a dataframe.
#all the R methods seems to deal with one or the other; not both. 

library(sp)
?sp::merge #Merge a Spatial object having a data.frame (i.e. merging of non-spatial attributes).
test<-merge(phoenix.shp,seattle.shp) #only nets phoenix. 
test$GEOID10 
slotNames(test) #"data"        "coords.nrs"  "coords"      "bbox"        "proj4string"
test@data #1218+22 so just phoenix
test@coords #just phoenix. 

library(rgeos)
?rgeos::gUnion
test<-gUnion(phoenix.shp,seattle.shp)
#So gUnion gives the topology, but not the dataframe. 
#do I hae an ID I can join the datafrmae back with?
slotNames(test) #"coords"      "bbox"        "proj4string"
test@coords
plot(test)#both sets of points, no data. 

If you need test data, download two counties worth from here

Mox
  • 1,036
  • 6
  • 15
  • https://gis.stackexchange.com/questions/155328/merging-multiple-spatialpolygondataframes-into-1-spdf-in-r – Mox Mar 13 '18 at 16:26

2 Answers2

3

For the equivalent to ArcGIS merge you can use raster::bind

library(raster)

x <- SpatialPoints(cbind(0,0))
y <- SpatialPoints(cbind(1,1))
z <- bind(x, y)

In this case, you can also do sp::rbind(x,y).

The benefit of bind is that it takes care of row names and differences in the data.frames (variable names).

raster::union is only implemented for polygons (the error message you get actually comes from base::union). I have now added support for SpatialLines and SpatialPoints to that (version 2.7-1; forthcoming), by wrapping bind.

Robert Hijmans
  • 10,683
  • 25
  • 35
  • I'm not clear on how this works for two shapefiles. It certainly gives me points. But I don't see how this helps with the dataframe. – Mox Mar 13 '18 at 22:47
  • This may be ArcGIS-induced anxiety. If I bind two points together, and then rbind data associated with those points, how can I be sure the right data will end up associated with the right points? – Mox Mar 13 '18 at 22:54
  • That should be fine, the order of rows should not change, but you can avoid it altogether by doing it in one step (bind two SpatialPolygonsDataFrames) – Robert Hijmans Mar 13 '18 at 23:48
  • I just finished checking your solution against ArcMap, and the results are the same. The order or rows not changing seems like wizardry, but it does work. – Mox Mar 14 '18 at 00:22
0

working code, with checks for the skeptical (like myself). # #need data? #get it here: https://onthemap.ces.census.gov/

# get all files with the .shp extension from working directory
setwd("N:/Dropbox/_BonesFirst/137_Transit_Metros_Points_MetroSplit_by_R")
shps <- dir(getwd(), "*.shp")
# the assign function will take the string representing shp and turn it into a variable
# which holds the spatial points data
for (shp in shps) assign(shp, readOGR(shp))
#checkit...
shps
#ok, that all seems to work. 
slotNames(seattle.shp) #"data"        "coords.nrs"  "coords"      "bbox"        "proj4string"
head(seattle.shp@data)
head(seattle.shp@coords)
#coords.x1 coords.x2
#[1,]  -1967676   3013037
#[2,]  -1967616   3012744
#[3,]  -1967659   3012753
#[4,]  -1967713   3012774
#[5,]  -1967786   3013192
#[6,]  -1967775   3012949

#ok, now we try it. 
#first, merge the polygons for Seattle and Phoenix.
plot(seattle.shp)
plot(phoenix.shp)
head(seattle.shp)
#GEOID10 c000 ca01 ca02 ca03 ce01 ce02 ce03 cns01 cns02 cns03 cns04 cns05 cns06 cns07 cns08 cns09
#1 530330060001001  135   18   79   38   26   45   64     0     0     0    29     0     5    24     2     0
#2 530330060001003    7    0    6    1    1    1    5     0     0     0     0     0     0     0     0     0
#3 530330060001004   86   33   33   20   19   30   37     0     0     0     0     0     2    14     2     0
#4 530330060001005    7    0    5    2    1    0    6     0     0     0     0     0     0     0     0     0
#5 530330060001006   15    4   10    1    8    4    3     0     0     0     0     0     1     0     0     0
#6 530330060001008    3    0    3    0    0    3    0     0     0     0     0     0     0     0     0     0
tail(phoenix.shp)
#GEOID10 c000 ca01 ca02 ca03 ce01 ce02 ce03 cns01 cns02 cns03 cns04 cns05 cns06 cns07 cns08
#1235 040134221045000    2    0    2    0    1    1    0     0     0     0     0     0     0     2     0
#1236 040134221045001    2    0    2    0    0    2    0     0     0     0     0     0     0     0     0
#1237 040134221045005    2    2    0    0    1    1    0     0     0     0     0     0     0     0     0
#1238 040134221045007   30   11   14    5   14   15    1     0     0     0     0     0     0     0     0
#1239 040134221045008   10    3    7    0    3    6    1     0     0     0     7     1     2     0     0
#1240 040134221045009    4    2    2    0    2    0    2     0     0     0     0     0     1     3     0
#
both <- bind(seattle.shp, phoenix.shp)
plot(both) #so we have geometry/topology
slotNames(both) #[1] "data"        "coords.nrs"  "coords"      "bbox"        "proj4string"
both@data #well,that exists. 
head(both) #matches Seattle
#GEOID10 c000 ca01 ca02 ca03 ce01 ce02 ce03 cns01 cns02 cns03 cns04 cns05 cns06 cns07 cns08 cns09
#1 530330060001001  135   18   79   38   26   45   64     0     0     0    29     0     5    24     2     0
#2 530330060001003    7    0    6    1    1    1    5     0     0     0     0     0     0     0     0     0
#3 530330060001004   86   33   33   20   19   30   37     0     0     0     0     0     2    14     2     0
#4 530330060001005    7    0    5    2    1    0    6     0     0     0     0     0     0     0     0     0
#5 530330060001006   15    4   10    1    8    4    3     0     0     0     0     0     1     0     0     0
#6 530330060001008    3    0    3    0    0    3    0     0     0     0     0     0     0     0     0     0
tail(both) #matches Phoenix
#GEOID10 c000 ca01 ca02 ca03 ce01 ce02 ce03 cns01 cns02 cns03 cns04 cns05 cns06 cns07 cns08
#3618 040134221045000    2    0    2    0    1    1    0     0     0     0     0     0     0     2     0
#3619 040134221045001    2    0    2    0    0    2    0     0     0     0     0     0     0     0     0
#3620 040134221045005    2    2    0    0    1    1    0     0     0     0     0     0     0     0     0
#3621 040134221045007   30   11   14    5   14   15    1     0     0     0     0     0     0     0     0
#3622 040134221045008   10    3    7    0    3    6    1     0     0     0     7     1     2     0     0
#3623 040134221045009    4    2    2    0    2    0    2     0     0     0     0     0     1     3     0
#
#well, this is awesome. But how can I know that the right points have been matched to the right data? 
?SpatialPoints
class(both)

#check it in ArcMap, I guess. 
wd<-"N:/Dropbox/_BonesFirst/135_Transit_Metros_Points_Merged_by_R"
writeOGR(obj=both,dsn=wd, layer="both",driver="ESRI Shapefile")
Mox
  • 1,036
  • 6
  • 15