[R-sig-Geo] Distance from a point to the closest polygon

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Wed May 25 19:53:09 CEST 2011


On Wed, May 25, 2011 at 6:34 PM, Francois Rousseu
<francoisrousseu at hotmail.com> wrote:
>
> Hi useRs
>
> I want to measure the distance between several points on a map and the closest water body, represented by polygons. The method I found uses the dist2Line function from package geosphere. In my example, I create a circular polygon made from a 100 000 points and measure the distance separating it from a given point:
>
>  buff<-destPoint( c(-74,46), b=seq(1,365,length.out=100000), d=1000)
> system.time(dist2Line(c(-75,45), buff))
>
> This takes almost 10 seconds on my computer which is way too long for me. In my case, I need to determine that distance between thousands of points in a complex lake system.  Is there a faster way to do this in R?

 Do your lakes really have 100,000 coordinates defining them? (And do
circles really have 365 degrees in them?).

 Trying:

 buff<-destPoint( c(-74,46), b=seq(1,360,length.out=N), d=100)

shows that the time to do dist2Line is about linear with the
complexity of the polygon, here N. If your lakes are that finely
digitized, then can you thin their boundaries? Can you pre-test
against the bounding boxes of the lakes to reduce the number of
point-line segment tests? Note that I don't think the nearest lake
will always be the one with the nearest bbox, but you do know the max
possible distance to that lake is the distance to the furthest corner
of the bbox. Any lakes whose nearest bbox corner is _further_ than
that distance can be eliminated.

 Beyond that you are possibly looking into spatial indexing and maybe
loading it into a PostGIS database with this kind of optimised spatial
indexing searches.

 And rgeos of course.

 Random thoughts.

Barry



More information about the R-sig-Geo mailing list