[R-sig-Geo] Shortest distance between points within a polygon

Roman Luštrik roman.lustrik at gmail.com
Mon Oct 13 20:13:56 CEST 2014


I was thinking along the lines of this example from `gCrosses`. If
line crosses the shape, it returns TRUE. If not, you've got your
distance. I can't speak for the performance part, you better do some
field testing.

require(rgeos)

l1 = readWKT("LINESTRING(0 3,1 1,2 2,3 0)")
l2 = readWKT("LINESTRING(0 0.5,1 1,2 2,3 2.5)")
l3 = readWKT("LINESTRING(1 3,1.5 1,2.5 2)")

pt1 = readWKT("MULTIPOINT(1 1,3 0)")
pt2 = readWKT("MULTIPOINT(1 1,3 0,1 2)")

p1 = readWKT("POLYGON((0 0,0 2,1 3.5,3 3,4 1,3 0,0 0))")
p2 = readWKT("POLYGON((2 2,3 4,4 1,4 0,2 2))")


plot(p1,border='blue',col='blue');plot(l1,add=TRUE)
title(paste("Crosses:",gCrosses(p1,l1),
            "\nOverlaps:",gOverlaps(p1,l1)))

Cheers,
Roman


On Mon, Oct 13, 2014 at 7:12 PM, Barry Rowlingson
<b.rowlingson at lancaster.ac.uk> wrote:
> On Mon, Oct 13, 2014 at 12:12 PM, Roman Luštrik <roman.lustrik at gmail.com> wrote:
>> One way would be to calculate the distance and see if the line connecting
>> the dots intersects with the polygon edge. `rgeos` package would be the
>> tool of trade for this.
>>
>
>  I can't see how that works...
>
>  A solution would be to rasterise your polygon and use `gdistance` to
> compute a cost-weighted minimum distance on the raster - you'd set the
> cost on the transition surface outside your polygon to +Inf and inside
> the polygon to +1, or maybe its zero cost inside the polygon and
> anything finite outside.
>
> The gdistance vignette has an example of travelling between places in
> Europe where travel is restricted to the land mass only.
>
> The precision of your raster will determine the precision of your
> result, but finer rasters will take longer.
>
> Barry



-- 
In God we trust, all others bring data.



More information about the R-sig-Geo mailing list