[R-sig-Geo] closest distance between irregularly-shaped polygons

Rolf Turner r.turner at auckland.ac.nz
Tue Feb 21 09:28:18 CET 2012


On 21/02/12 15:49, Nick Matzke wrote:
> Hi everyone,
>
> I have a number of irregularly-shaped polygons (from shapefiles) in 
> geographic (unprojected lat/long) coordinates.
>
> I would like to measure:
>
> 1. the distance between the polygon centroids (which is easy)
     Are you sure that it's easy?  Many people believe that the centroid
     of a polygon is the same as the centroid (xbar,ybar) of its vertices.
     This is NOT correct!
> and also
>
> 2. the *closest* distances of each polygon to each of the others
>
> Is there an easy function or method for doing the latter? If not, all 
> I can think of is to cycle through all the vertices and do 
> point-to-point distances until I get the closest, although this seems 
> wasteful and might even give the wrong answer in certain situations.

Yes.  It will often give the wrong answer.  In general the shortest 
distance will be the
distance from a vertex of one polygon to a point on an edge (but not an 
endpoint of
that edge) of the other polygon.

Think of the limiting case of two line segments:

         |
         |
         |   -----------
         |
         |

> Any help or suggestions very much appreciated!

There is a bit of a problem with the fact that your coordinates are
unprojected longitude and latitude.

If you are willing to treat your coordinates as Euclidean, or if you can 
project
your coordinates onto a more-or-less flat surface, you can use the tools of
the package *spatstat* to accomplish your goal.

     * convert each polygon to an object of class "owin"; (see below)

     * for each pair of such owin objects, say W1 and W2, calculate

         min(crossdist(as.psp(W1),as.psp(W2),type="separation"))

See Adrian's vignette "shapefiles" for advice on how to express data read
in from shapefiles as objects of the sort that spatstat works with.  There
are explicit instructions as to how to convert an object of class 
"SpatialPolygons"
to an object, or objects, of class "owin".

As of present writing, as far as I know (Adrian keeps adding features at an
incredible clip, and I can't keep up) spatstat does not have a facility 
for handling
distances between points expressed in longitude and latitude.  (Great circle
distances.)  Such a facility will be added eventually --- hard to say 
when, but.

HTH

     cheers,

             Rolf Turner

P. S.  If the polygons ever *overlap*, the solution I've outlined may 
not do what
you want.  It will give the answer 0.  Is that the "right" answer from 
your point
of view?  If not, what is the "right" answer?

             R. T.



More information about the R-sig-Geo mailing list