Skip to content
Prev 4123 / 29559 Next

Using R spatial tools to merge polygons

On Mon, 1 Sep 2008, Lyndon Ang wrote:

            
Yes, you can use unionSpatialPolygons() in maptools. The specific problem 
is to add Area_ID and Larger_ID to the SpatialPolygonsDataFrame that you 
read in. If Area_ID exists in shapefile, then something like:

library(maptools)
SPDF0 <- readShapeSpatial("my_shapefile.shp", IDvar="Area_ID")
my_ids <- read.table("my_textfile")
row.names(my_ids) <- as.character(my_ids$Area_ID)
SPDF1 <- spCbind(SPDF0, my_ids)
# if there is trouble here, it is because the IDs don't match
IDs <- as.character(SPDF1$Larger_ID)
SP_out <- unionSpatialPolygons(SPDF1, IDs)

giving you a SpatialPolygons object with no attribute data (look at 
aggregate() for ways of merging rows in data.frame objects.

Roger