Skip to content

GRASS with R and distances between polygon edges

6 messages · Duncan Golicher, White.Denis@epamail.epa.gov, Colin Robertson +3 more

#
I have a follow up question regarding moving old (R <2.4) sp objects to 
R 2.4. It is indeed easy using GRASS as an intermediary with spgrass6. 
But one of my students has installed GRASS with the QGIS interface 
rather than running GRASS under Cygwin. He wanted to do the same.

I tried with him and quickly got R to communicate with GRASS by simply 
changing the first lines of system to read .....

function (command, intern = FALSE, wait = TRUE, input = NULL,
    show.output.on.console = TRUE, minimized = FALSE, invisible = FALSE)
{
command<-paste(Sys.getenv("COMSPEC"),"/c",command)

etc}

But we still couldn't get spgrass6 to import and export raster layers 
even after fiddling with the code to get rid of cygpath etc. Perhaps 
Roger has a fix? It would be useful.

Also while posting, can anyone suggest a way of measuring the closest 
distance between disjunct polygons in R (forest fragments)? Its easy to 
use centroids in spdep, but we were interested in the distances between 
the edges.

Duncan
#
Duncan Golicher <dgoliche at sclc.ecosur.mx> wrote on 2006-10-30 08:09:49:
to
between
For many polygons each having possibly many points defining the edges,
it's a time consuming search.  I am not aware of any GIS software or R
packages that do this comprehensively.  One alternative is to use
buffering by some set of fixed distances.  Or, write a program/function
to do the search:  for each polygon, find the minimum distance between
all of its vertices and all of the vertices of each other polygon.
However, this algorithm does not work well with two polygons like this:

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

           /\
          /  \
         /____\

A buffering approach would do better.

Denis
#
Hi Duncan,

For calculating the distance between polygons, there are descriptions of
the calculations for average distance and minimum distance in the
reference below.  Although I'm not sure if they're implemented in any R
packages.

Okabe, A. and Miller, H. J. (1996) "Exact computational methods for
calculating distances between objects in a cartographic database,"
Cartography and Geographic Information Systems, 23, 180-195.


Colin


Colin Robertson
MSc Candidate
Dept of Geography
University of Victoria
www.geog.uvic.ca/spar
(250) 885-8065
 

-----Original Message-----
From: r-sig-geo-bounces at stat.math.ethz.ch
[mailto:r-sig-geo-bounces at stat.math.ethz.ch] On Behalf Of Duncan
Golicher
Sent: Monday, October 30, 2006 8:10 AM
To: r-sig-geo at stat.math.ethz.ch
Subject: [R-sig-Geo] GRASS with R and distances between polygon edges

I have a follow up question regarding moving old (R <2.4) sp objects to 
R 2.4. It is indeed easy using GRASS as an intermediary with spgrass6. 
But one of my students has installed GRASS with the QGIS interface 
rather than running GRASS under Cygwin. He wanted to do the same.

I tried with him and quickly got R to communicate with GRASS by simply 
changing the first lines of system to read .....

function (command, intern = FALSE, wait = TRUE, input = NULL,
    show.output.on.console = TRUE, minimized = FALSE, invisible = FALSE)
{
command<-paste(Sys.getenv("COMSPEC"),"/c",command)

etc}

But we still couldn't get spgrass6 to import and export raster layers 
even after fiddling with the code to get rid of cygpath etc. Perhaps 
Roger has a fix? It would be useful.

Also while posting, can anyone suggest a way of measuring the closest 
distance between disjunct polygons in R (forest fragments)? Its easy to 
use centroids in spdep, but we were interested in the distances between 
the edges.

Duncan
#
I think if you compute the minimum distances from each vertex of polygon 
A to each segment of polygon B, then do the same for each vertex of 
polygon B to each segment of polygon A you should get the exact answer. 
At least, for non-overlapping polygons. This is because you cant arrange 
two non-intersecting line segments such that their closest approach 
isn't at one of the four ends. For any point within one of the line 
segments, going one way along the line must get you either closer or 
further to the other line segment. I think thats intuitively obvious now 
I've intuited it enough times...

To find the nearest point-line segment, parameterise the line segment so 
the ends are at p=0 and 1, do a bit of vector math, work out the p for 
the closest point, if 0<p then p=0, and p>1 then p=1, work out the 
distance from your vertex to the point p. Keep the minimum.

  You have to repeat with B's vertices against A's segments since in the 
rectangle-triangle case either could be A or B and the nearest point is 
where the triangle's vertex is close to the rectangles segment.

  It doesnt work for overlapping polygons since the nearest approach of 
two overlapping line segments is partway between the segments. But its 
fairly easy to detect overlapping line segments in one go and then 
distance=0.

  Barry
#
On Mon, 30 Oct 2006, Duncan Golicher wrote:

            
I do not have a stable working version of the winGRASS port (the one 
packaged with QGIS), and since it is described on the GRASS-dev list as at 
best pre-alpha, will not try it soon.

Since it does come with the circular GDAL plugin fix, you should be able 
to read the GRASS rasters using the GRASS driver in readGDAL() in rgdal, 
and maybe vectors too. Of course, the driver is read-only, but it ought to 
work.

As far as spgrass6 is concerned, GRASS on Windows runs under cygwin. The 
number of different GRASS releases in the wild is now substantial, and 
even just keeping the Unix/Linux and cygwin ones working and maintainable 
is not easy. 

If you have usable patches that don't break anything else (cygwin), I 
would be interested, but something will have to let Rwin know how to 
branch to native winGRASS rather than cygwin.

Roger
PS. There is a good deal of vector computational geometry in spatstat, 
often in the internal functions. For example, ?distpl brings up a list 
with many useful possibilities.