[R-sig-Geo] raster nearest neighbours

Rafael Wüest rafael.wueest at wsl.ch
Wed Mar 20 09:49:24 CET 2013


Hi Ross

Try

points2<-rasterToPoints(raster2,spatial=TRUE)
distraster<-distanceFromPoints(raster1,points2)
distmasked<-mask(distraster,raster1,inverse=TRUE)

yourraster<-merge(distmasked<3000,raster1)

HTH,
Rafael

On 20.03.2013, at 09:15, Ross Ahmed <rossahmed at googlemail.com> wrote:

> Hi all,
> 
> I have two rasters with the same extent and resolution, similar to these
> two:
> 
> raster2 <- raster1 <- raster(ext=extent(-1.95214, -1.69421, 55.60090,
> 55.72476), crs="+proj=longlat +ellps=WGS84 +datum=WGS84", ncol=50, nrow=50)
> 
> I also have two data frames with point locations (of the species Brent
> Goose) similar to these two:
> 
> df1 <- data.frame(long=rnorm(100, -1.76901, 0.05),
>                  lat=rnorm(100, 55.62953, 0.05))
> coordinates(df1) <- c('long', 'lat')
> proj4string(df1) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84²)
> 
> df2 <- data.frame(long=rnorm(100, -1.90490, 0.05),
>                  lat=rnorm(100, 55.69847, 0.05))
> coordinates(df2) <- c('long', 'lat')
> proj4string(df2) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84")
> 
> I have assigned TRUE to raster cells which contain a point as follows:
> 
> raster1[cellFromXY(raster1, df1)] <- TRUE
> raster2[cellFromXY(raster2, df2)] <- TRUE
> 
> For any cells that are NA in raster1, I need to assign them to TRUE if the
> following condition is met: a TRUE raster2 cell in within 3km of a TRUE
> raster1 cell.
> 
> Is this possible? Let me know if my question is unclear.
> 
> Ross
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 	[[alternative HTML version deleted]]
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



--
Rafael Wüest
Swiss Federal Research Institute WSL
Zürcherstrasse 111
8903 Birmensdorf
Switzerland

+41 44 7392126
rafael.wueest at wsl.ch
http://www.wsl.ch/info/mitarbeitende/wueest/index_EN



More information about the R-sig-Geo mailing list