[R-sig-Geo] joining two polygons into a single polygon region?

Roger Bivand Roger.Bivand at nhh.no
Fri Oct 22 21:01:24 CEST 2010


On Fri, 22 Oct 2010, Malcolm Fairbrother wrote:

> Dear all,
>
> I'm working with a SpatialPolygonsDataFrame, with 176 rows. This is a 
> subset of an RData file downloaded from http://www.gadm.org/country
>
> One row corresponds to the main part of the Indian subdistrict Hubli 
> which you can see here: http://www.dharwad.nic.in/dist_dwd.htm. 
> Currently, a *separate* row in the SpatialPolygonsDataFrame corresponds 
> to the smaller part of the same subdistrict, which as you can see is not 
> contiguous with the larger part of the subdistrict.

Thank you for a full report, this makes things much easier to analyse!

>
> If possible, I'd like to merge the two rows, so that a single row in the 
> SpatialPolygonsDataFrame corresponds to *both* parts of Hubli subregion 
> (and the polygons slot for this row describes both parts of the 
> subdistrict, recognising that they are not contiguous). Is this 
> possible?

The downloaded RData file for India at GADM level 3 contains the 
SpatialPolygonsDataFrame object gadm:

karnat <- gadm[gadm$NAME_1 == "Karnataka",]
dharwad <- karnat[karnat$NAME_2 == "Dharwad",]
plot(dharwad)
text(coordinates(dharwad), labels=as.character(dharwad$NAME_3))

shows that there is a problem. The sub-polygon has NAME_3 given as
"n.a. ( 1897)" not "Hubli". There is one other detached polygon (I think) 
that will need attention: "n.a. ( 2050)". Add an extra variable to karnak:

karnak$NNAMES_3 <- karnak$NAMES_3
karnak$NNAMES_3[karnak$NNAMES_3 == "n.a. ( 1897)"] <- "Hubli"

and make the similar substitution for "n.a. ( 2050)".


>
> My impression is that unionSpatialPolygons might be the function I need, 
> but I haven't been able to figure out the right syntax, and haven't had 
> any luck searching this list's archives. From the help page, I thought 
> that I might need something like:

Then you merge the SpatialPolygons by:

library(maptools)
library(gpclib)
gpclibPermit() # never for non-educational purposes
karnak_1 <- unionSpatialPolygons(as(karnak), "SpatialPolygons"),
   IDs=as.character(karnak$NNAME_3))

which will be identified by the unique values of karnak$NNAME_3.

>
> reg <- 1:dim(karnat)[1] # karnat is my SpatialPolygonsDataFrame
> reg[78] < reg[75]
> karnat2 <- unionSpatialPolygons(karnat, reg) # an attempt to merge or combine rows 75 and 78

This works with reg[78] <- reg[75] and gpclibPermit(), but the region IDs 
are uninformative, and would be hard to match against at the next stage. 
It's better to use informative IDs that are clear and unique. Because the 
output object has no data slot, the IDs are the only way of knowing which 
multipolygon is which.

Hope this helps,

Roger

>
> But this isn't working at all, and I'm a bit stuck. If you can see what 
> I'm trying to do, can anyone suggest the syntax I want?
>
> Any help would be much appreciated.
>
> - Malcolm
>
>
> Dr Malcolm Fairbrother
> Lecturer
> School of Geographical Sciences
> University of Bristol
>
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
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