[R-sig-Geo] Recombining polygon shapefiles using maptools

Don MacQueen macq at llnl.gov
Fri Mar 28 01:16:22 CET 2008


I have six polygon shapefiles. Two of them represent my area of 
interest (call them A and B), and the other four (call them C, D, E, 
F) represent holes in the first two.

I would like to create a single object that can be passed to 
spsample() for spatial sampling, such that spsample will place 
samples inside A and B, but not in C, D, E, or F.

I know how to do this by what might be called "brute force" (see below).

The real question is, are there ways to do this more effectively 
using higher level functions?

If there were, it might make for easier to understand scripts, for 
example, or be easier to repeat using different sets of shapefiles 
(the script below doesn't easily generalize, especially if any of the 
shapefiles consist of multiple polygons).

Thanks
-Don



Here is my solution; I've run it and it works. I apologize for not 
being able to supply the shapefiles and thus a reproducible example.

Each shapefile consists of a single polygon, and I don't need any of 
the attribute information from the shapefiles.
This simplifies things, quite a lot, I think.

Extract the single polygon from each, into six separate two column matrices.

# A
vz1 <- readShapePoly('shapefiles-zones/Zone-TK')
tmp <- as(vz1 , 'SpatialPolygons')
tmp <- tmp at polygons[[1]]   ## since I know it has only one polygon
poly1 <- tmp at Polygons[[1]]@coords  ## a matrix of coordinates

## repeat for additional shapefiles 2 through 6


Combine the polygons following the example in ?overlay

## this example uses only the first three of my polygons
tmp <- Polygons(
                 list(Polygon(poly1,hole=FALSE),  # A
                       Polygon(poly2,hole=FALSE),  # B
                       Polygon(poly3,hole=TRUE)),  # C
                 ID=1)
sr <- SpatialPolygons(list(tmp))


plot(sr)
tmp <- spsample(sr, type='random', n=500)
points(tmp)  ## looks good!


-- 
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA
925-423-1062




More information about the R-sig-Geo mailing list