[GIS] Merging Multiple Shapefile Polygons in R

mergepolygonrshapefile

I have a regional shapefile of England and Wales but without containing Scotland and Northern Ireland.

Regions

I want to add the outlines of Scotland and Northern Ireland in without regional divisions so the combined would consist of the following polygons:

  • London
  • North-West
  • North-East
  • East of England
  • East Midlands
  • West Midlands
  • South-West
  • South-East
  • Yorkshire & The Humber
  • Scotland
  • Northern Ireland

I have a separate shapefile with just the country border of Scotland and Ireland – my question is it possible to add this to my regions map and if so how can I do so in R?

enter image description here

Best Answer

You can use rbind to combine spatial data objects:

Read in a couple of shapefiles:

> la = readOGR(".","la")
OGR data source with driver: ESRI Shapefile 
Source: ".", layer: "la"
with 12035 features
It has 4 fields
> pr = readOGR(".","pr")
OGR data source with driver: ESRI Shapefile 
Source: ".", layer: "pr"
with 12786 features
It has 3 fields

Try using rbind:

> lapr = rbind(la,pr)
Error in rbind(deparse.level, ...) : 
  numbers of columns of arguments do not match

This has failed because there's a different number of columns in each shapefile - you can't put data with four columns (fields) with data that has three columns:

> names(la)
[1] "POSTCODE" "UPP"      "PC_AREA"  "N"       
> names(pr)
[1] "POSTCODE" "UPP"      "PC_AREA" 

Fix this some way. I'll remove the N column from la:

> la$N=NULL

And now it works:

> lapr = rbind(la,pr)

Giving me an object with the total features:

> dim(lapr)
[1] 24821     3

So you do need to make sure your objects have identically-named columns, which you'll have to do by removing, adding, or renaming columns. Then you can rbind them together.