I would do:

#r <- raster(nrows=733, ncols=732, xmn=-158, xmx=-121, ymn=-28.3, ymx=8.35,
crs=NA)
#  there is reason to do do   r[] <- 0
#p <- read.table("09_16_B.txt", sep="\t", dec=".", header=F)
#p <- p[,-3]

# replacing above with simulated data, to make a self contained example:
r <- raster(ncols=5, nrows=5)
x <- runif(1000) * 360 - 180
y <- runif(1000) * 180 - 90
p <- cbind(x, y)

# simplest

r <- rasterize(p, r, fun=sum)
xyn <- rasterToPoints(r)


# can be more efficient:

n <- table(cellFromXY(r, p))
# either (cell number 'ID')
celln <-  cbind(as.integer(names(n), n)
# or coordinates
xyn <- cbind(xyFromCell(r, as.integer(names(n))), n)


Robert


On Tue, Dec 6, 2011 at 9:20 AM, Edzer Pebesma <edzer.pebesma@uni-muenster.de
> wrote:

>
>
> On 12/06/2011 05:17 PM, sandra werb wrote:
> >
> >
> > 2011/12/6 Edzer Pebesma <edzer.pebesma@uni-muenster.de
> > <mailto:edzer.pebesma@uni-muenster.de>>
> >
> >
> >
> >     On 12/06/2011 12:06 PM, sandra werb wrote:
> >     > Dear all,
> >     >
> >     > I am working with a Lidar dataset, which contains thousands of
> >     > undistributed points. What I want to do is to count the number of
> >     points
> >     > within a raster cell and extract this information. Not just as
> single
> >     > numbers, but together with the ID or the coordinates of the
> >     raster. I came
> >     > to this so far:
> >     >
> >     > - Create an empty raster, all cells are 0
> >     >
> >     > empty.r <- raster(nrows=733, ncols=732, xmn=-158, xmx=-121,
> ymn=-28.3,
> >     > ymx=8.35, crs=NA)
> >     > empty.r[] <- 0
> >     >
> >     > - read in the dataset of points containing only xy information and
> >     define
> >     > these as spatialpoints
> >     >
> >     > r1_points <- read.table("09_16_B.txt", sep="\t", dec=".", header=F)
> >     > p <- r1_points[,-3]
> >     >  p<-SpatialPoints(data.frame(p))
> >     >
> >     > - create an overlay of the raster and the spatial points and use
> >     function
> >     > table to
> >     >
> >     > o <- overlay(as(empty.r, "SpatialGrid"),p)
> >     > tab <- table(o)
> >
> >     untried, from ?over:
> >
> >     g = as(empty.r, "SpatialGrid")
> >     o = sapply(over(as(g, "SpatialPolygons"), p, returnList = TRUE),
> length)
> >     gdf = SpatialGridDataFrame(g, data.frame(o = o))
> >     spplot(gdf)
> >
> >
> > Thanks, this works. When I sum up the counts within the overlay I get a
> > number that is higher than the points I have in the dataset. Might it be
> > the case that points, which lie on/very close to boundaries of the cells
> > are counted twice?
>
> Yes, that must be the case. (Maybe shifting all your points with 1 mm or
> so helps?)
>
> >
> >
> >
> >     >
> >     > tab gives me the numbers of points within one cell, but I don't
> >     know how to
> >     > get this information together with the ID or XY coordinates as one
> >     table or
> >     > else. Can anyone help?
> >     >
> >     > Thank you very much in advance!
> >     >
> >     > Leni
> >     >
> >     >       [[alternative HTML version deleted]]
> >     >
> >     > _______________________________________________
> >     > R-sig-Geo mailing list
> >     > R-sig-Geo@r-project.org <mailto:R-sig-Geo@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 <tel:%2B49%20251%208339763>
> >      http://ifgi.uni-muenster.de
> >     http://www.52north.org/geostatistics      e.pebesma@wwu.de
> >     <mailto:e.pebesma@wwu.de>
> >
> >     _______________________________________________
> >     R-sig-Geo mailing list
> >     R-sig-Geo@r-project.org <mailto:R-sig-Geo@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@wwu.de
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]

