[R] Distance between points and a polygon?

Hans Gardfjell hans at eg.umu.se
Thu Nov 21 22:03:33 CET 2002

Thanks Rolf,

The necessary function do indeed exists in the spatstat package, but 
rather well hidden I would say.
But after looking thru the bdist.points code I came up with this example:

> library(spatstat)
  #Define a matrix with all the points
> points<-matrix(c(1,3,3,3,6,3),3,2,byrow=T) 
  #Define a matrix with the points the polyline consists of
> pl<-matrix(c(1,1,4,1,4,4),3,2,byrow=T) 
  #Returns the minimun distance for each point to the polyline
> apply(distppll(points,cbind(pl[-nrow(pl),],pl[-1,])),1,min)
> [1] 2 1 2


Hans Gardfjell
Umeå University, Sweden

Rolf Turner wrote:
> Look at the spatstat package, available from CRAN.
> The function bdist.points() does ***essentially*** what you want.  It
> assumes that the points are the points of a spatial point pattern,
> and that the polygon is the window though which the pattern is
> observed.  But it would not be at all hard to hack this  function
> into a form that would work in a more general context.
> Hope this helps.
> 				cheers,
> 					Rolf Turner
> 					rolf at math.unb.ca

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list