[R-sig-Geo] extracting polygons

Andrew Niccolai andrew.niccolai at yale.edu
Tue Apr 8 20:37:34 CEST 2008


I think there may be an issue in your faigcell function for your lines:

    Sr1 <- Polygon(cbind(c(nwx,nex,sex,swx,nwx), c(nwy,ney,sey,swy,nwy)))
    Sr1 <- Polygons(list(Sr1), datafr[i,1])
    lista[i] <- Sr1

try instead:

    Sr1 <- list(Polygons(list(Polygon(cbind(c(nwx,nex,sex,swx,nwx),
c(nwy,ney,sey,swy,nwy)))), datafr[i,1]))
    lista[i] <- Sr1

I can't be sure if that will work as I tend to iteratively claw my way to
successful code but the trick lies somewhere in setting up the
Polygons/Polygon lists.

Andrew Niccolai
Doctoral Candidate
Yale School of Forestry


 

-----Original Message-----
From: r-sig-geo-bounces at stat.math.ethz.ch
[mailto:r-sig-geo-bounces at stat.math.ethz.ch] On Behalf Of Agustin Lobo
Sent: Tuesday, April 08, 2008 12:56 PM
Cc: r-sig-geo at stat.math.ethz.ch
Subject: Re: [R-sig-Geo] extracting polygons

I think that (at least part of) my problem comes
from being confused about Polygons and SpatialPolygons.
(and [ and [[ !!!)

I have an object with all my polygons:

 > class(absUTMpolys)
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"

 > str(absUTMpolys,max.level=2)
Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
   ..@ data       :'data.frame': 69 obs. of  3 variables:
   ..@ polygons   :List of 69
   ..@ plotOrder  : int [1:69] 1 2 3 4 5 6 7 8 9 10 ...
   ..@ bbox       : num [1:2, 1:2]  412000 4584000  466000 4624000
   .. ..- attr(*, "dimnames")=List of 2
   ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots

how is it that an SpatialPolygonsDataFrame has polygons instead
of SpatialPolygons?

Well, I've created absUTMpolys with:
absUTM <- read.table("poblptXsans_absents.txt",header=T)
absUTMpolys <- faigcell(datafr=absUTM)

where faigcell() is a function I've made:

"faigcell" <- function(datafr=absUTM)
{
   require(sp)
   lista <- vector(length=nrow(datafr),mode="list")
   names(lista) <- datafr[,1]
   for(i in 1:nrow(datafr)){
    swx <- datafr[i,5]
    swy <- datafr[i,8]
    sex<- swx + 2000
    sey <- swy
    nex <- sex
    ney <- sey+2000
    nwx <- swx
    nwy <- ney
    Sr1 <- Polygon(cbind(c(nwx,nex,sex,swx,nwx), c(nwy,ney,sey,swy,nwy)))
    Sr1 <- Polygons(list(Sr1), datafr[i,1])
    lista[i] <- Sr1
    }
   datos <- cbind(datafr[,1:2],abnd=rep(0,nrow(datafr)))
   row.names(datos) <- datafr[,1]
   Sr<- SpatialPolygons(lista,pO=1:nrow(datafr),CRS("+proj=tmerc 
+lat_0=0 +lon_0=2.999999982811267 +k=0.999600 +x_0=500000 +y_0=0 
+ellps=intl +units=m +no_defs"))
   SpatialPolygonsDataFrame(Sr, data=datos, match.ID = TRUE)
}

Note that Sr is made using SpatialPolygons(), and then I use 
SpatialPolygonsDataFrame() with Sr. Why don't I have SpatialPolygons
within absUTMpolys ?

The problem is that I need SpatialPolygons to get as.owin to work.

I've found that I can do:
as.owin(SpatialPolygons(absUTMpolys at polygons[1]))

and it works, but is this not very weird? should not the following just 
work:

as.owin(absUTMpolys at polygons[[1]])
?

That is, instead of having polygons:
 > class(absUTMpolys at polygons[[1]])
[1] "Polygons"
attr(,"package")
[1] "sp"

why don't I have SpatialPolygons?

Than

Agus



Agustin Lobo escribió:
> Dear list,
> 
> I want to distribute a set of N circles according to a random distribution
> within a set of polygons (N circles within each polygon).
> 
> I have an object of class SpatialPolygonsDataFrame with the polygons.
> 
> My idea is to use something like:
> 
> for (i in 1:length(absUTMpolys at polygons)){
>  delme <- runifpoint(3, win=as.owin(absUTMpolys at polygons[i]))
>  ...
> 
> but I'm not being successful at extracting each polygon and
> as.owin refuses the conversion:
> 
> I've tried
>  > as(absUTMpolys at polygons[1], "owin")
> Error in as(absUTMpolys at polygons[1], "owin") :
>   no method or default for coercing "list" to "owin"
> 
> and
>  > as(absUTMpolys at polygons[1][[1]], "owin")
> Error in as(absUTMpolys at polygons[1][[1]], "owin") :
>   no method or default for coercing "Polygons" to "owin"
> 
> 
> which is the proper way
> of selecting each polygon from within the SpatialPolygonsDataFrame
> object?
> 
> Thanks!
> 
> Agus
> 
> 

-- 
Dr. Agustin Lobo
Institut de Ciencies de la Terra "Jaume Almera" (CSIC)
LLuis Sole Sabaris s/n
08028 Barcelona
Spain
Tel. 34 934095410
Fax. 34 934110012
email: Agustin.Lobo at ija.csic.es
http://www.ija.csic.es/gt/obster

_______________________________________________
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