[R-sig-Geo] remove nearby points

Tim Howard tghoward at gw.dec.state.ny.us
Wed Feb 20 16:19:50 CET 2013


I am trying to remove spatial 'duplicates' from a point dataset. The coordinates won't be exactly the same and so I can't use the normal methods for removing the second instance of the points. This generalizes to a question about removing points nearby others, either randomly or based on other criteria (in my case, I want to keep the one with a more recent date attribute). 
 
Although my research and fiddling has got me close, I wonder if there already is a solution I'm missing within the various spatial packages so I'm starting with sig-geo, even though I'm stuck at a spot that would use regular R syntax. 
 
My approach (code at bottom of email):
 
1. Move the full point data set over to SpatialPoints as decimal degrees longlat  [package=sp]
2. Reproject to utm, using spTransform
3. convert to ppp
4. find the distance from each point to its nearest using nndist()  [package = spatstat]
5. identify that nearest using nnwhich()  [package = spatstat]
6. extract those with neighbors closer than 100m
 
** this is where I'm stuck **  I now have a list of neighbors, for which I'd like to keep the first case of each neighbor but remove the second (and sometimes third). Similar to unique(). Here's a dummy example
 
#set up dummy data frame, the dist and neigh columns are from nndist() and nnwhich(), respectively
a <- data.frame(ID=c("one","two","three","four"),dist=c(2.2,5.1,5.1,2.2),neigh=c(4,3,2,1))
 
#here's as far as I've got, I can remove the neighbor to row one with the following line.
#a looping solution seems problematic as the size of the dataframe changes with each loop
 
b <- a[-match(a$neigh[1], rownames(a)),]
 
Questions:
- Is there already a function in a spatial package that offers a way to remove points within a certain distance of others?
- if not, does anyone have any hints for taking the next step from what I've done?
 
### code for what I've got so far.
### dat.wind.tall is the input DF of lat long decimal degree coordinates
 
library(sp)
library(spatstat)
library(maptools)
library(rgdal)
llCRS <-  CRS("+proj=longlat +datum=NAD83")
wind.sp <- SpatialPoints(dat.wind.tall[,c(66,65)], proj4string=llCRS)
prjNew <- CRS("+proj=utm +zone=18 +datum=NAD83")
wind.utm <- spTransform(wind.sp, prjNew)
wind.ppp <- as(as(wind.utm, "SpatialPoints"), "ppp")
turb.dist <- nndist(wind.ppp)
turb.nearest <- nnwhich(wind.ppp)
dat.wind.tall.nbr <- cbind(dat.wind.tall, nneigh=turb.nearest, dist=turb.dist)
closeNeighbors <- dat.wind.tall.nbr[turb.dist<100,]
#code for removing neighbor to first row. 
y <- closeNeighbors[-match(closeNeighbors$nneigh[1], rownames(closeNeighbors)),]
 
Thanks in advance for any help. 
Tim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20130220/8075c6b8/attachment.html>


More information about the R-sig-Geo mailing list