[R-sig-Geo] finding distance from a patch
pgalpern
pgalpern at gmail.com
Wed Feb 1 13:51:59 CET 2012
I have not found a function in an existing package that will do this
exactly, although there may be one. However, here is an approach that
will find the minimum distance from a point to a class based on
functions in the raster package. Note that this uses the grid distance
and not the Euclidean distance for efficiency. (Also, has not been
thoroughly tested.)
DistancePointToClass <- function(classRaster, pointXY, focalClass) {
rasterPointXY <- classRaster
rasterPointXY[] <- NA
rasterPointXY[cellFromXY(classRaster, pointXY)] <- 1
distanceFrom <- gridDistance(rasterPointXY, origin = 1)
minDistance <- min(distanceFrom[classRaster[] == focalClass])
return(minDistance)
}
The classRaster parameter can be your patches object. If you need the
minimum distance to any patch you can set all the patches to the same
focalClass:
patches[!is.na(patches)] <- 1
Or if you need minimum distances to each patch, you can iterate through
all the classes on patches using the focalClass parameter, although this
will be very inefficient on large rasters.
You can find examples of usage of this function in this tutorial:
http://nricaribou.cc.umanitoba.ca/R/RWorkshop3.pdf
--
Paul Galpern, PhD Candidate
Natural Resources Institute
70 Dysart Road
University of Manitoba
Winnipeg, Manitoba, Canada R3T 2M6
http://borealscape.ca
On 31/01/2012 5:38 PM, Lorenzo Cattarino wrote:
> Hi,
>
> I obtained the patches (i.e., clusters of connected cells - queen's case) from a raster object. What I am trying to do is to calculate the euclidean distance between (the border of) a patch and a cell in the raster (outside the patch). What I am looking for is the minimum euclidean distance between the cell outside the patch and the patch.
>
> I would like to find the coordinates of the points of each patch to calculate the distance between each of those points and the cell of interest. Then I will choose the smallest value.
>
> require(raster)
> landscape<- matrix (sample(c(1,0), 100, T), 10)
> w<- raster(landscape)
> #plot(w)
> patches<- clump(w)
> #windows()
> #plot(patches)
>
> Is there a function that might help doing that?
>
> thanks
> Lorenzo
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
More information about the R-sig-Geo
mailing list