1

I am trying to merge two polygon shapefiles (spatialpolygondataframe, projected).

I have tried the solution in Append/Combine Shape Files however, I cannot seem to make it work.

The rbind only works on SpatialPolygons, so I get rid of my attribute table. Still i get the following error:

rbind(t.poly1, t.poly2, fix.duplicated.IDs=TRUE)

Fehler in function (classes, fdef, mtable) : unable to find an inherited method for function ‘proj4string’ for signature ‘"logical"’

I dont really have power over the format of the proj4string:

CRS arguments: +proj=somerc +lat_0=46.95240555555556 +lon_0=7.439583333333333 +k_0=1 +x_0=600000 +y_0=200000 +ellps=bessel +units=m +no_defs

Do you have any explanation or solution as to what I could do to enable the merge?

Community
  • 1
  • 1
Jacqueline
  • 19
  • 3

2 Answers2

1

If you simply want to plot two (or more) shapefiles together on one plot, you do not need to combine the two shapefiles; they can simply be added as layers on to one plot.

Using shapefiles of the UK and Ireland as examples, we obtain them from DIVA-GIS and unzip them in to shapes/:

download.file("http://biogeo.ucdavis.edu/data/diva/adm/GBR_adm.zip",
          destfile = "shapes/GBR_adm.zip")
download.file("http://biogeo.ucdavis.edu/data/diva/adm/IRL_adm.zip",
          destfile = "shapes/IRL_adm.zip")
unzip("shapes/GBR_adm.zip", exdir = "shapes/")
unzip("shapes/IRL_adm.zip", exdir = "shapes/")

Then, load the required packages:

packages <- c("maptools", "rgeos", "rgdal")
sapply(packages, install.packages, dependencies = T)
sapply(packages, require, character.only = T)
rm(packages)

Then load the shapefiles in to memory:

gb <- readOGR("shapes/", "GBR_adm0")
eire <- readOGR("shapes/", "IRL_adm0")

These can then be plot:

plot(gb)
plot(eire, add = T)

Which gives you the result of both shapefiles:

enter image description here

If you do want to merge/combine both shapefiles in R, I would use the spRbind() function in the rgdal package:

n <- length(slot(gb, "polygons"))
gbEire <- spChFIDs(eire, as.character(n))  # so shapefiles have unique IDs
gbEire <- spRbind(gbEire, gb)
writeOGR(gbEire, dsn = "shapes/", layer = "gbEire", driver = "ESRI Shapefile")

Which should write a new shapefile with both original shapefiles merged in the shapes/ directory.

Phil
  • 4,344
  • 2
  • 23
  • 33
  • Hey Phil, thank you very much for your response. I do infact want to merge the shapefiles and not just plot them together. I now went into the source code of the package "taRifx.geo" and tried the function rbind.SpatialPolygonsDataFrame() and that worked with out any problems, or so it seems... – Jacqueline Mar 18 '15 at 14:50
  • Glad you found a solution that works for you. What about writing it up as an answer to your own question so others know it is an option if they read this post? – Phil Mar 19 '15 at 10:12
  • Hey Phil, yes, like I said, I just used the rbind.SpatialPolygonsDataFrame() function that I found in the package and with that it works fine! So I guess the problem was the underneath mentioned fact that it an rbind() from a different packages was used! – Jacqueline Mar 23 '15 at 11:42
1
rbind(t.poly1, t.poly2, fix.duplicated.IDs=TRUE)

Fehler in function (classes, fdef, mtable) : unable to find an inherited method for function ‘proj4string’ for signature ‘"logical"’

Its actually trying to get a proj4string of the fix.duplicated.IDs argument. Because your objects are SpatialPolygons, the rbind from the sp package is being used rather than the package mentioned in the linked post.

Have you tried:

s1=SpatialPolygons(list(Polygons(list(Polygon(cbind(c(0,1,1,0,0),c(0,0,1,1,0)))),ID=1)))
s2=SpatialPolygons(list(Polygons(list(Polygon(cbind(c(0,1,1,0,0),c(0,0,1,1,0)))),ID=1)))
proj4string(s1)=CRS("+init=epsg:4326")
proj4string(s2)=CRS("+init=epsg:4326")
rbind(s1,s2) # errors...

So just use this handy arg:

rbind(s1,s2,makeUniqueIDs=TRUE)
Spacedman
  • 92,590
  • 12
  • 140
  • 224