[R-sig-Geo] Changing Shapefile's Coordinates
Barry Rowlingson
b.rowlingson at lancaster.ac.uk
Wed Jul 20 13:20:53 CEST 2011
On Wed, Jul 20, 2011 at 12:31 AM, Wang, Kevin (SYD)
<kevinwang at kpmg.com.au> wrote:
> Hi,
>
>
>
> I'm just wondering if it's possible, in R, to modify a shapefile's
> coordinates?
>
>
>
> I've shapefiles for each state/territory of Australia, and in them there
> are local government areas coordinates. Using readShapePoly(), I am
> able to read them in and plot each state with its local government
> areas.
>
>
>
> However, I also want to be able to "merge" local government areas into
> roughly north-east-south-west - i.e. draw a state then divide it roughly
> into four quadrants (or divide it in other ways by merging various local
> government areas). Is there any easy way that I can manipulate the
> shapefile to do this?
>
Another job for rgeos!
Can do something with gUnionCascaded?:
library(maptools);library(rgeos)
xx <- readShapeSpatial(system.file("shapes/sids.shp",
package="maptools")[1],IDvar="FIPSNO", proj4string=CRS("+proj=longlat
+ellps=clrk66"))
# plot it, label it:
plot(xx)
text(coordinates(xx),as.character(1:nrow(xx)))
# now suppose we want to merge these regions:
z1 = c(10,24,9,71)
# we do:
zx=gUnionCascaded(xx[z1,])
# which gives us a SpatialPolygons object with (in this case) one ring:
plot(zx,add=TRUE,col="#80808080")
Repeat for each of your regions and splat everything together as a
SpatialPolygonsDataFrame if thats what you need.
Look out for digitising errors which result in slivers and other junk
where adjacent polygons dont match up.
Barry
More information about the R-sig-Geo
mailing list