[GRASS5] counting number of vector points in same position?

Roger Bivand Roger.Bivand at nhh.no
Wed Aug 10 13:21:12 EDT 2005


On Wed, 10 Aug 2005, Markus Neteler wrote:

> Hi,
> 
> I have to transform a vector points map of disease reports.
> Often more than one report falls into the same location as
> reports were done/geocoded based on cities. This results into
> a vector map with often several vector falling into the
> same coodinate pair.
> 
> I have to transform this map into a new vector map with
> single point per coordinate pair and number of cases as
> attribute.
> 
> How to do that?
> 
> I was thinking of v.distance -a to calculate zero-distances.
> This requires to exclude that the points "see" themselves.
> But then, I am not sure how to transform this into a map.
> Maybe with some SQL fun (distinct() operator or so)?
> 
> Suggestions welcome. This functionality appears to me to
> be needed for GRASS. I darkly remember that it was asked
> time ago, but cannot find the mail(s).
> 

If you can move the points into R:

> xcoord <- runif(300)
> ycoord <- runif(300)
> reps <- 1+rbinom(300, 3, 0.3)
> table(reps)
reps
  1   2   3   4 
 96 132  66   6 
> crds <- cbind(rep(xcoord, reps), rep(ycoord, reps))
> crds_s <- crds[sample(nrow(crds)),]

replicates your problem, here 582 points for 300 unique locations, in 
random order.

> u_crds_s <- unique(crds_s)
> cnts <- integer(nrow(u_crds_s))
> for (i in 1:nrow(u_crds_s)) cnts[i] <- length(which(apply(crds_s, 1, 
+   function(x) identical(x, u_crds_s[i,]))))
> table(cnts)
cnts
  1   2   3   4 
 96 132  66   6 
> res <- cbind(u_crds_s, cnts)

then export res back to GRASS maybe as a shapefile. If the coordinates 
have ID tags, matching would be easier, but this works for non-fuzzy 
coordinates. If the coordinates need snapping, all.equal() with a 
tolerance could be used instead of identical, but it is much slower.

Hope this helps,

Roger


> Thanks
> 
>  Markus
> 
> _______________________________________________
> grass5 mailing list
> grass5 at grass.itc.it
> http://grass.itc.it/mailman/listinfo/grass5
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no




More information about the grass-dev mailing list