Hi Adam, Here is code for something very similar that I was doing, which should help with what you want to do. In this case, I made grids of varying resolution to cover the extent of the area defined by my points, and then collected the grid ids for each resolution and bound them to the SpatialPointsDataFrame. I also got the center coordinates from each grid cell so that I could represent the cells as points.
# Create sampling grid # Grid template, bumped out 20 km on either side from extent of p (SpatialPointsDataFrame) rex <- extent(p) gr <- raster(xmn = rex@xmin - 20000, xmx = rex@xmax + 20000, ymn = rex@ymin- 20000, ymx = rex@ymax+ 20000) # Adjust grid size and collecting grid ids under points for each resolution polID.l <- c("km1", "km5", "km10", "km20") res.l <- c(1000, 5000, 10000, 20000) coord.l <- c("xy1", "xy5", "xy10", "xy20") grid.l <- vector("list", length(res.l)) # list to hold dummy grids # Extract for(i in 1:4) { gr.2 <- gr res(gr.2) <- res.l[i] print(res.l[i]) projection(gr.2) <- p@proj4string gr.2[] <- 1:ncell(gr.2) grid.l[[i]] <- gr.2 xy <- cbind(1:ncell(gr.2), xyFromCell(gr.2, 1:ncell(gr.2))) # Center coordinates colnames(xy)[1] <- polID.l[i] print(coord.l[i]) assign(coord.l[i], xy) p$km <- extract(gr.2, p) colnames(p@data)[length(colnames(p@data))] <- polID.l[i] } Hope this helps. Cheers, Lyndon On Thu, Feb 2, 2012 at 12:13 AM, ADAM PETER CARDILINI <ap...@deakin.edu.au>wrote: > G'day All, > > > > I have been struggling to figure out an answer to this question and wasn't > sure where else to ask, so here goes. I have a large amount of > presence/absence point data (~125,000) with lon/lat coordinates. Like this: > > Lat lon abs/pres > date > > 113.6481 -24.90278 0 > 1/1/1998 > > 113.6481 -24.90278 1 > 1/2/1998 > > 113.6481 -24.90278 0 > 1/3/1998 > > 137.5175 -35.69556 1 > 1/1/1998 > > 137.5175 -35.69556 1 > 1/2/1998 > > 137.5175 -35.69556 1 > 1/3/1998 > > > > I have been trying to work out a way to identify the cells that each point > would fall into if it were mapped onto a raster grid (I am using rasters > with the same cell resolution as BIOCLIM rasters). That is, for all points > falling within the same cell, I would like to identify them as being a part > of the same cell. I am trying to model site occupancy for this species from > repeated survey data, using the package 'unmarked', which requires records > to be associated with sites. I don't have site IDs with this data so I need > to come up with a proxy for this. I thought I could use grid cell id as a > proxy for site id, so that all records that fall within the same grid cell > can be considered the same site. > > How do I identify the grid cell that each point falls into on a raster > grid and associate it with my presence/absence data, to produce a dataset > that looks like this? > > Lat lon abs/pres > date cellID > > 113.6481 -24.90278 0 > 1/1/1998 234 > > 113.6481 -24.90278 1 > 1/2/1998 234 > > 113.6481 -24.90278 0 > 1/3/1998 234 > > 137.5175 -35.69556 1 > 1/1/1998 250 > > 137.5175 -35.69556 1 > 1/2/1998 250 > > 137.5175 -35.69556 1 > 1/3/1998 250 > > > > Thanks for your help in advance. > > > > Kind regards, > > Adam Cardilini > > > [[alternative HTML version deleted]] > > _______________________________________________ > R-sig-Geo mailing list > R-sig-Geo@r-project.org > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo