[R-sig-Geo] Counting number of points per pixel in a grid

Paul Hiemstra p.hiemstra at geo.uu.nl
Tue Oct 20 22:20:19 CEST 2009


Hi Heather,

Be sure to take a look a the raster package, in particular the 
pointsToRaster function. Does exactly what you want and is very fast. 
Going from sp-grids to raster-objects is very easy.

cheers,
Paul

Heather Kharouba schreef:
> Hi,
>
> I would like to count the number of points per pixel in a grid(there are
> thousands of points and thousands of pixels). I have a list of occurrence
> points and a grid with a given cell size. Ideally I'd like them saved to
> one file where there is a column listing the pixel number and one listing
> number of points for that pixel. I've tried overlay (sp package) but it
> just spits out a list of all the points in my list with NA value attached
> to each point and it doesn't provide a count:
>
> this is my code so far:
> grid=readGDAL("mask50")
> proj4string(grid)=CRS("+proj=lcc")
> x1<-read.csv(file.choose(),header=TRUE,sep=",", na.strings="");
> proj4string(x1)=CRS("+proj=lcc")
> o<-overlay(grid, x1)
> o
> NA.33354  (49.71, -97.91)    NA
> NA.33355   (50.7, -98.03)    NA
> NA.33356  (56.35, -94.66)    NA
> NA.33357    (50.83, -100)    NA
> NA.33358  (49.36, -96.11)    NA
>
> I'm wondering whether I need to set up a loop for each pixel.
> I've also had no luck with count.points.id (adehabitat package). When I
> use this function, I get a summary of my grid. Any thoughts would be much
> appreciated!
>
> Thanks,
> Heather Kharouba
>
> _______________________________________________
> 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