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

Adrian.Baddeley at csiro.au Adrian.Baddeley at csiro.au
Thu Oct 22 03:56:07 CEST 2009


Heather Kharouba <kharouba at zoology.ubc.ca> writes:

> 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. 

Try 'pixellate.ppp' in the library 'spatstat'. 

First convert your point coordinates to a point pattern X (object of class "ppp") using ppp() or as.ppp().

Then, for example if you want square pixels of side length 3 units,
        Z <- pixellate(X, eps=3)
You can specify the exact coordinates of the pixel centres by 
        Z <- pixellate(X, xy=list(x=xcoord, y=ycoord))
where xcoord and ycoord are vectors of the pixel coordinates.

The result Z is an integer valued pixel image (object of class "im"). 
Use 'as.data.frame' to convert it to the format you wanted.
To write to an output file in the desired format,

      sink("results.txt")
     as.data.frame(Z)
     sink()


Adrian Baddeley




   



More information about the R-sig-Geo mailing list