Pairwise Distances (fwd)

Eric G . Miller egm2 at jps.net
Tue Jul 11 21:20:34 EDT 2000


On Tue, Jul 11, 2000 at 03:53:20PM -0600, kegallag at NMSU.Edu wrote:
> Hi all,
> 
>      I am attempting to use the program GRASS to obtain spatial
>      distances for ALL possible pairs of plant individuals within a
>      population.  To date, I have obtained distances (not coordinates)
>      of individuals relative to only TWO OTHER individuals (in order
>      to use triangulation to create polygons).  Now, I need to
>      calculate pairwise distances for EACH plant individual relative
>      to EVERY OTHER plant individual within populations.  Could you
>      please tell me GRASS has the capability to compute the distances
>      between all pairs of points in a spatial network comprised of
>      only a limited number of pairwise distances?
>  
>      Please do not hesitate to contact me if you require
>      clarification.  I would really appreciate your input! Thanks for
>      your consideration.

I don't know if GRASS has a function to do this, but you can do it in
PostgreSQL.  The coordinates have to be in an x,y type projection like
UTM (lat/lon won't work).

Here's how:

1. dump your plants (they are sites?) into an ascii file (s.out.ascii).

2. Open postgresql and do the following (you might have to edit the
ascii plant file to get it into a format for postgresql).

a) Create the table...

CREATE TABLE plants (
	plant_id    INTEGER PRIMARY KEY,
	coordinate  POINT   NOT NULL
);

b) Copy the data into postgresql

COPY plants FROM '/path/to/plants.asc' USING DELIMITERS '|';

#file should look like below with x & y being the floating point coords.
1|(x,y)
2|(x,y)
...

c) Do a relational projection calculating the distance between each pair
of points and weeding out the zeros (this may take a long time if you
have alot of data, and may bomb if you have tons of data).

SELECT a.plant_id, (a.coordinate::point <-> b.coordinate::point) as distance,
	b.plant_id
FROM plants a, plants b
WHERE (a.coordinate::point <-> b.coordinate::point) > 0.0
ORDER BY a.plant_id;

3. Send the output to a file (possible without fancy formatting). It'll
look like:

 plant_id |     distance     | plant_id 
----------+------------------+----------
        1 | 2.54950975679639 |        2
        1 | 8.74642784226795 |        3
        1 | 20.2869416127715 |        4
        1 |  30.500655730656 |        5
        2 | 2.54950975679639 |        1
        2 | 6.21288982680363 |        3
        2 | 17.8488094841085 |        4
        2 | 28.0251672608746 |        5
        3 | 8.74642784226795 |        1
        3 | 6.21288982680363 |        2
        3 | 12.5483066586691 |        4
        3 | 22.3895064706661 |        5
        .     .                       .
        .     .                       .
        .     .                       .

If you need help with the postgresql stuff, let me know.

-- 
According to MegaHAL:
    The emu is a mass of incandescent gas, a gigantic nuclear furnace.




More information about the grass-user mailing list