Finding minimum distance between two raster layer pixels in R -


i have 2 thematic raster layers r1 , r2 same area each following same classification scheme , has 16 classes. need find minimum distance between cell of r1 , cell of r2 same value. e.g. nth cell in r1 has value 10 , coordinates x1,y1. , in r2, there 2 cells value 10 , coordinates x1+2,y1+2 , x1-0.5,y1-0.5. value need cell 0.5,0.5.

i tried distance raster package gives distance, cells na, nearest cell not na. confused how can include second raster layer this.

so, use rastertopoints extract spatialpoints object unique thematic class. use sp::spdists function find distance between points.

library(raster)   r1 <- raster( nrow=10,ncol=10) r2 <- raster( nrow=10,ncol=10)  set.seed(1) r1[] <- ceiling(runif(100,0,10)) r2[] <- ceiling(runif(100,0,10))  dist.class <- null for(i in unique(values(r1))){ p1 <- rastertopoints(r1, fun=function(xx) xx==i, spatial=t) p2 <- rastertopoints(r2, fun=function(xx) xx==i, spatial=t) dist.class[i] <- min(spdists(p1,p2)) } cbind(class = unique(values(r1)),dist.class) 

the loop may not efficient you. if it's problem, wrap function , lapply it. also, carefull class, if aren't 1:10, loop won't work. if projection in degree, need geosphere package accurate results. best in case think use projection in meters.


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -