Skip to content
Prev 23523 / 29559 Next

How to filter a xyz vector file

Dear Robert,?Thanks a lot. This code really works in a nice and simple way.Ashraf, cheers 


     El Martes 13 de octubre de 2015 19:53, Robert J. Hijmans <r.hijmans at gmail.com> escribi?:
   

 Ashraf,
Here is an approach:

# some random points
x <- runif(10000)
y <- runif(10000)
xy <- cbind(x, y)

# a raster to specficy the extent and resolution
library(raster)
r <- raster(round(extent(xy)), res=0.1)

# find the distance of each point to the center of the cell it falls in
cell <- cellFromXY(r, xy)
cellxy <- xyFromCell(r, cell)
d <- pointDistance(xy, cellxy, lonlat=FALSE)

# combine and get, for each cell, the point nearest to the center
z <- cbind(cell, xy, d)
z <- z[order(z[, 1], z[, 4]), ]
zz <- z[!duplicated(z[,1]), ]

# have a look:
r[] = 1
plot(r)
points(z[,2:3], cex=.1)
points(zz[,2:3], col='red', pch=20)
# plot(as(r, 'SpatialPolygons'), add=TRUE)

Best, Robert

On Sun, Oct 11, 2015 at 11:07 AM, Ashraf Afana via R-sig-Geo
<r-sig-geo at r-project.org> wrote: