Lorenzo,

If I understand you correctly you want the shortest distance from a set of
points to a (the nearest) patch. In that case, compute the patches (as you
have done), then use 'distance', and then use 'extract' to extract the
distances for your point 'xy'.

d = distance(patches)
v <- extract(d, xy)

If, however, you want the distance from each point to each patch you would
need a loop like Paul suggested. How I would do that loop would depend on
the size of the raster and the area covered by the patches therein. For a
smallish dataset you could use rasterToPoints and pointDistance. For larger
datasets you could use reclass and distance to do the same as above but
then patch class by patch class.

Robert

On Wed, Feb 1, 2012 at 4:51 AM, pgalpern <pgalpern@gmail.com> wrote:

> 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<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@r-project.org
>> https://stat.ethz.ch/mailman/**listinfo/r-sig-geo<https://stat.ethz.ch/mailman/listinfo/r-sig-geo>
>>
>
> ______________________________**_________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/**listinfo/r-sig-geo<https://stat.ethz.ch/mailman/listinfo/r-sig-geo>
>

	[[alternative HTML version deleted]]

