[R-sig-Geo] Extract values with over()
Paulo Justiniano Ribeiro Jr
paulojus at c3sl.ufpr.br
Thu Nov 24 23:02:43 CET 2011
Edzer
thanks for pointing this
My complete solution goes a bit different without using returnList
and taking advantege of the named vector returned by over()
##
require(sp)
set.seed(12)
xy <- cbind(runif(n=500,min=0, max=20),
runif(n=500,min=0, max=20))
## points
pontos <- SpatialPoints(xy)
## grid of "quadrats"
## i. as SpatialGrid
grid <- SpatialGrid(grid = GridTopology(c(0.5,0.5), c(1,1), c(20,20)))
## ii. as SpatialPixels
gridPix <- grid
fullgrid(gridPix) <- FALSE
## iii. as SpatialPolygons
gridPol <- as.SpatialPolygons.SpatialPixels(gridPix)
PtGr <- table(over(pontos, grid));length(PtGr)
POS <- as.numeric(names(PtGr))
N <- numeric(length(grid))
N[POS] <- PtGr
N
plot(gridPol)
plot(pontos, add=T, pch=19, cex=0.5)
plot(grid, pch=as.character(N), add=T, cex=0.6, col=2)
Paulo Justiniano Ribeiro Jr
LEG (Laboratorio de Estatistica e Geoinformacao)
Universidade Federal do Parana
Caixa Postal 19.081
CEP 81.531-990
Curitiba, PR - Brasil
Tel: (+55) 41 3361 3573
VOIP: (+55) (41) (3361 3600) 1053 1066
Fax: (+55) 41 3361 3141
e-mail: paulojus AT ufpr br
http://www.leg.ufpr.br/~paulojus
On Tue, 22 Nov 2011, Edzer Pebesma wrote:
>
>
> On 11/21/2011 09:24 PM, Paulo Justiniano Ribeiro Jr wrote:
>> Dear Edzer and Alexandre
>>
>> wouldn't the following work as requested?
>>
>> fullgrid(grid) <- FALSE
>> class(grid) ## SpatialPixels
>>
>> ap3 <- as.SpatialPolygons.SpatialPixels(grid)
>> table(over(pontos, ap3))
>
> Paulo, that is a step in the right direction, as
>
>> table(over(pontos, ap3))[1:5]
>
> 1 2 3 4 5
> 2 1 3 1 2
>
> gives frequencies for each cell, but this would also be obtained by the
> grid itself:
>
>> table(over(pontos, grid))[1:5]
>
> 1 2 3 4 5
> 2 1 3 1 2
>
> However, as
>
>> length(table(over(pontos, grid)))
> [1] 298
>> length(grid)
> [1] 400
>
> shows we have empty grid cells. but this gives gaps (implicit 0 values)
> for empty cells in grid. The simpler alternative is to do the over with
> returnList=TRUE, which returns ALL points in each (grid cell, now
> polygon) in ap3:
>
>> over(ap3, pontos, returnList=TRUE)[1:5]
> [[1]]
> [1] 416 459
>
> [[2]]
> [1] 385
>
> [[3]]
> [1] 17 132 351
>
> [[4]]
> [1] 387
>
> [[5]]
> [1] 311 322
>
> taking the length of each vector gives the frequencies (INCLUDING the
> zero frequencies that table does NOT give you):
>
> n = sapply(over(ap3, pontos, returnList=TRUE), length)
> x = SpatialPixelsDataFrame(grid, data.frame(n=n))
> spplot(x, sp.layout=list("sp.points", pontos), at=0:6-.5)
>
> Without doubt, there are other ways of doing this, for instance with
> package raster.
>
> Thanks for helping. It seems to be hard to make complex things simple,
> and I'd be happy to hear suggestions for making this even simpler.
>
>>
>>
>>
>> Paulo Justiniano Ribeiro Jr
>> LEG (Laboratorio de Estatistica e Geoinformacao)
>> Universidade Federal do Parana
>> Caixa Postal 19.081
>> CEP 81.531-990
>> Curitiba, PR - Brasil
>> Tel: (+55) 41 3361 3573
>> VOIP: (+55) (41) (3361 3600) 1053 1066
>> Fax: (+55) 41 3361 3141
>> e-mail: paulojus AT ufpr br
>> http://www.leg.ufpr.br/~paulojus
>>
>> On Mon, 21 Nov 2011, Edzer Pebesma wrote:
>>
>>>
>>>
>>> On 11/21/2011 08:47 PM, ASANTOS wrote:
>>>> Dear colleges,
>>>>
>>>> I try to extract values with over () function , but not working
>>>> with my script, when I make:
>>>>
>>>
>>> it only extracts values when there are values to extract, please read
>>> the documentation.
>>>
>>>>
>>>> was expected that the over() give the frequency of points inside each
>>>> parcel
>>>
>>> I wonder where you got this expectation from. If the documentation gave
>>> you this expectation, please let me know.
>>>
>>> table() can compute frequencies from indices.
>>> --
>>> Edzer Pebesma
>>> Institute for Geoinformatics (ifgi), University of Münster
>>> Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
>>> 8333081, Fax: +49 251 8339763 http://ifgi.uni-muenster.de
>>> http://www.52north.org/geostatistics e.pebesma at wwu.de
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>
> --
> Edzer Pebesma
> Institute for Geoinformatics (ifgi), University of Münster
> Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
> 8333081, Fax: +49 251 8339763 http://ifgi.uni-muenster.de
> http://www.52north.org/geostatistics e.pebesma at wwu.de
>
More information about the R-sig-Geo
mailing list