[R-sig-Geo] Assistance converting SPDF polygons to holes

Roger Bivand Roger.Bivand at nhh.no
Thu Feb 12 19:09:16 CET 2009


On Thu, 12 Feb 2009, Don MacQueen wrote:

> I feel a bit lazy asking for help, as I know I can do this -- but since I 
> don't work with spatial polygons frequently, I find assembling objects 
> consisting of nested polygons to be a bit challenging.

It can be done, so this approach from the other side may not cater for the 
specifics. Using the NC counties, construct outbox:

library(maptools)
xx <- readShapeSpatial(system.file("shapes/sids.shp", package="maptools")[1],
  IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))
library(splancs)
crds <- sbox(bboxx(bbox(xx)))
crds <- rbind(crds, crds[1,])
outbox <- Polygon(crds, hole=FALSE)

# sample a (much) larger number in outbox

zz <- sample.Polygon(outbox, n=2000, type="random")
plot(SpatialPolygons(list(Polygons(list(outbox), ID="box"))))
points(zz, pch=3, cex=0.5)
plot(xx, add=TRUE, border="red")

# overlay the points and the interior polygons and drop those inside

o <- overlay(as(xx, "SpatialPolygons"), zz)
zzz <- zz[is.na(o)]

zzzz <- zzz[sample(nrow(coordinates(zzz)), 500)]
points(zzzz, pch=3, cex=0.5, col="green")

which does something like your description. It ought to work if there are 
holes in your input polygons too.

It is possible to flatten the input Polygons objects into a single 
Polygons object, add the enclosing Polygon object to the list, and pass to 
checkPolygonsHoles(), but maybe the above is simpler.

Hope this helps,

Roger

>
> I have a Spatial Polygons Data Frame object named "s2poly" (read in from a 
> polygons shapefile). The first portion of the output from str(s2poly) is 
> shown here:
>
> Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
>  ..@ data       :'data.frame':	1173 obs. of  11 variables:
>  .. ..$ OBJECTID  : num [1:1173] 3 12 13 77 78 79 80 81 82 83 ...
>  .. ..$ ENTITY    : Factor w/ 0 levels: NA NA NA NA NA NA NA NA NA NA ...
>  .. ..$ HANDLE    : Factor w/ 1173 levels "10002","10008",..: 348 349 350 
> 351 352 353 354 355 356 357 ...
>  .. ..$ LAYER     : Factor w/ 15 levels "ADMIN_MAP","B-BLDG-LINE",..: 14 8 8 
> 8 8 11 11 11 11 9 ...
>  .. ..$ COLOR     : num [1:1173] 252 4 4 4 4 200 200 200 200 7 ...
>  .. ..$ LINETYPE  : Factor w/ 3 levels "Continuous","DASHED",..: 1 1 1 1 1 1 
> 1 1 1 1 ...
>  .. ..$ ELEVATION : num [1:1173] 0 0 0 0 0 0 0 0 0 0 ...
>  .. ..$ THICKNESS : num [1:1173] 0 0 0 0 0 0 0 0 0 0 ...
>  .. ..$ TEXT      : Factor w/ 0 levels: NA NA NA NA NA NA NA NA NA NA ...
>  .. ..$ SHAPE_AREA: num [1:1173] 10.8 32549.7 16571.2 37.7 59.2 ...
>  .. ..$ SHAPE_LEN : num [1:1173] 19 932.6 608 77.4 120.3 ...
>  .. ..- attr(*, "data_types")= chr [1:11] "N" "C" "C" "C" ...
>  ..@ polygons   :List of 1173
>  .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
>  .. .. .. ..@ Polygons :List of 1
>  .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
>  .. .. .. .. .. .. ..@ labpt  : num [1:2] 1653186 431813
>  .. .. .. .. .. .. ..@ area   : num 10.8
>  .. .. .. .. .. .. ..@ hole   : logi FALSE
>  .. .. .. .. .. .. ..@ ringDir: int 1
>  .. .. .. .. .. .. ..@ coords : num [1:8, 1:2] 1653190 1653190 1653187 
> 1653188 1653183 ...
>  .. .. .. ..@ plotOrder: int 1
>  .. .. .. ..@ labpt    : num [1:2] 1653186 431813
>  .. .. .. ..@ ID       : chr "0"
>  .. .. .. ..@ area     : num 10.8
>
>
> I suspect, but have not confirmed, that some of the instances of Polygons 
> objects have length > 1.
>
> I have also created a simple polygon named "s2bound" that surrounds all of 
> the polygons in s2poly.
>
> I need a single object that I can give to spsample() in which all of the 
> polygons in s2poly are holes within s2bound.
> I don't need to keep any of the attributed data in @data, so I believe I can 
> simplify to a SpatialPolygons object.
>
> My basic request for assistance is this:
>
>  -    can it be done with a modest number of high levels functions? (and if 
> so, I think I need to be shown how)
>
>  -    or will I need to write nested loops of @Polygons within @polygons to 
> change @hole to TRUE in every polygon, in order construct my final polygon?
>
> Thanks
> -Don
>

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