Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a dataframe with x,y coordinates. I've sampled 10 x,y coordinates from my dataframe:

y <-c(24.637032,21.569813,21.687,19.677183,24.72438,21.433136,24.637032,22.21065,21.443762,25.273882)

x <-c(-156.02597,-112.00287,-112.00287,-105.27803,-159.47324,-157.858,-156.026,-109.68626,-112.12681,-112.15393)

I've converted these points to a SpatialPoints objects:

myPoints <- data.frame(x,y)
coordinates(myPoints) <- c('x', 'y')
proj4string(myPoints) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84")

I've used these points to make a SpatialGridobject:

bb <- bbox(myPoints)
myPointsGrid <- GridTopology(cellcentre.offset =  bb[, 1] + (cs/2), cellsize = c(1, 1), cells.dim = ceiling(diff(t(bb))/cs))
myPointsGrid <- SpatialGrid(myPointsGrid, proj4string = "+proj=longlat +ellps=WGS84 +datum=WGS84")

I need two things 1. find which grid cells contain a point 2. Find the number of points in each grid cell. I tried...

over(myPointsGrid, myPoints)

...but this has thrown an error. How can I complete this task?

share|improve this question
What kind of error do you get? And waht is 'cs' exactly? Cause when I run your code I get an error there. – BlankUsername Feb 21 '13 at 12:51
up vote 2 down vote accepted

Assuming that your code is correct (I have given the value 1 for the cs variable), the error it gives is:

Error in function (classes, fdef, mtable)  :  unable to find an inherited method for function ‘over’ for signature ‘"SpatialGrid", "SpatialPoints"’

According to the help file, this is what the function is waiting as input:

x: geometry (locations) of the queries
y: layer from which the geometries or attributes are queried

It means that you are giving the wrong order of inputs, first is the point location followed by the layer. This is what you want:

> over(myPoints,myPointsGrid)
  [1]  59 268 213 330   1 222  59 215 268  48

with cs=1

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.