[R-sig-Geo] Using R spatial tools to merge polygons

Roger Bivand Roger.Bivand at nhh.no
Tue Sep 2 08:27:39 CEST 2008


On Mon, 1 Sep 2008, Lyndon Ang wrote:

> Good day all,
>
> I've only recently been introduced to the useful spatial tools available in R (and indeed to GIS in general!).  I have found reading some of the posts on this mailing list quite useful, so am hoping that someone can help, or else let me know it can't be done!
>
> I have a map in Shapefile format, which I can read into R. The 
> shapefile is divided into regions. What I want to do is merge the 
> regions on the shapefile into larger areas. The link between each of 
> the individual shapefile regions and the larger regions is on a text 
> file of the following format:
>
> Area_ID    Larger_ID
> 1                    1
> 2                    1
> 3                    1
> 4                    2
> 5                    2
>
> Is it possible for me to do this in R, without the use of other GIS software?

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

>
> Thanks in advance for any help that may be provided.
>
> Kind regards
> Lyndon
>
>
>      Win a MacBook Air or iPod touch with Yahoo!7. http://au.docs.yahoo.com/homepageset
> 	[[alternative HTML version deleted]]
>
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no


More information about the R-sig-Geo mailing list