Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge neighboring regions in R (aggregate spatial data)?

Tags:

r

geospatial

I guess I needed to rephrase my awfully worded previous question (deleted it). Here's another try. I want to join to adjacent regions, in a way that their common border disappears and only their outer line can be seen.

Here's a reproducible example:

require(shapefiles)
require(sp)

xx <- readShapeSpatial(system.file("shapes/sids.shp", package="maptools")[1],
                   IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))

# show all the subregions
plot(xx)

enter image description here

Now let's consider only regions regions 3 and 5

plot(xx[c(3,5),])

How can I just aggregate these regions. In practice what I want to do is like having a map of the whole continent showing all countries and producing a map that shows North America and South America.

To me this looks like a pretty common task but I can't find the right function to do it so far. Do I just miss a function or can I simply to it manually?

enter image description here

like image 251
Matt Bannert Avatar asked Jun 17 '12 15:06

Matt Bannert


1 Answers

The rgeos package provides a number of excellent tools for handling Spatial* data, that can be used in this case.

For example:

library(rgeos)
regionOfInterest <- gUnion(xx[3,], xx[5,])

This also has the same result, and may be more useful for multiple polygons:

regionOfInterest <- gUnionCascaded(xx[c(3,5), ])

The result from plot(regionOfInterest):

enter image description here

like image 61
digitalmaps Avatar answered Oct 24 '22 02:10

digitalmaps