[R-sig-Geo] extracting polygons
Agustin Lobo
Agustin.Lobo at ija.csic.es
Tue Apr 8 18:55:43 CEST 2008
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
More information about the R-sig-Geo
mailing list