[R-sig-Geo] Using the Z value for 3-D polygons?

Torleif Markussen Lunde torleif.lunde at cih.uib.no
Wed Mar 4 00:20:56 CET 2009


Still not as good as it should be, but at least a try. Some of the code could 
be a lot more efficient, but I guess you could use this as a template:


require(maptools)
nc <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1], 
proj4string=CRS("+proj=longlat +datum=NAD27"))

for (i in 1:length(nc at data$NWBIR79)) {
  zv <- rep(nc at data$NWBIR79[i], length(nc at polygons[[i]]@Polygons[[1]]@coords))
  xy <- nc at polygons[[i]]@Polygons[[1]]@coords
  colnames(xy)  <- c("x", "y")
  xyz.tmp <- cbind(xy, zv)
  if (i == 1) xyz <- xyz.tmp else xyz <- rbind(xyz, xyz.tmp)
}


#install.packages(c("R.basic"), contriburl="http://www.braju.com/R/repos/")
require(R.basic)
xyz <- data.frame(xyz)
with(xyz, plot3d(x,y,zv, type="n", theta=35, phi=30))


un <- unique(xyz$zv)
my.colors <- colorRampPalette(c("green", "blue", "red"))
mypalette <- rev(my.colors(100))

for (ii in 1:length(un)) {
  a <- un[ii]
  b <- data.frame(x=xyz$x[xyz$zv==a], y=xyz$y[xyz$zv==a], 
zv=xyz$zv[xyz$zv==a])
  for (iii in 1:length(b$x)) {
    if (iii != length(b$x)) {
      ab <- b[iii:(iii+1),] 
      } else ab <- rbind(b[iii,], b[1,])
    ab <- rbind(ab, ab[2,], ab[1,])
    ab[3:4,3] <- 0
    ab$uni <- iii
    ab$group <- ii
    if (iii == 1) out <- ab else out <- rbind(out, ab)
   }
  if (ii == 1) p.out <- out else p.out <- rbind(p.out, out)
}

p.out$uni <- as.integer(rep(1:(length(p.out$uni)/4), each = 4))

length(p.out$x)
iv1 <- 1
while (iv1 <=  length(p.out$x)) {
  for (i in iv.1:(iv1+2460)) {
  with(p.out, polygon3d(x[uni==i], y[uni==i], zv[uni==i], border = NULL, 
lty=1, col="green"))# or mypalette[group[(i+]]))
  }
iv1 <- iv1+2460
}

for (i in 1:length(un)) {
with(xyz, polygon3d(x[zv==un[i]],y[zv==un[i]],zv[zv==un[i]], col = "green")) # 
or col=mypalette[i]))
}




On Tuesday 03 March 2009 03:29:46 am Jim Burke wrote:
> Hi Everyone,
>
> QUESTION: How can I plot a spatial polygon (SpatialPolygonsDataFrame) as
> 3-d? Perhaps using "spplot.polygons" with its z value?
>
> GOAL: To have a series of polygons look like approximately like the last
> example in the PDF below.
> http://ocw.mit.edu/NR/rdonlyres/Urban-Studies-and-Planning/11-521Spatial-Da
>tabase-Management-and-Advanced-Geographic-Information-SystemsSpring2003/ACA8
>0C9F-4089-403E-9669-50763381D08A/0/lect13c.pdf
>
> EXAMPLE SUGGESTION: Use "spplot.pologons" with the venerable SourceForge
> NC fig21.R example and using its color coding values as the polygon $z
> value? Assuming lat and long are in the sp.
> http://r-spatial.sourceforge.net/gallery/#fig21.R
>
> Then if its not as straightforward as above, what would be the steps to
> convert a spatial polygon to points or something to graph that way?
>
> Thanks,
> Jim Burke
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list