简体   繁体   中英

how can I extract the distances between the points after a Delaunay Triangulation with deldir in R?

I would like to calculate distances between cities using Delaunay Triangulations. I have the longitudes and latitudes of the twenty cities I would like to calculate distances between, but I have some trouble figuring out how to extract the distance information out of the triangulation. I've used deldir() (from library deldir) so far. See the code below.

x   <- c(2.3,3.0,7.0,1.0,3.0,8.0)
y   <- c(2.3,3.0,2.0,5.0,8.0,9.0)
try <- deldir(x,y,list(ndx=2,ndy=2),c(0,10,0,10))

str(try)
List of 8
$ delsgs  :'data.frame':    23 obs. of  6 variables:
..$ x1  : num [1:23] 3 7 7 1 1 3 3 3 8 8 ...
..$ y1  : num [1:23] 3 2 2 5 5 8 8 8 9 9 ...
..$ x2  : num [1:23] 2.3 2.3 3 2.3 3 3 7 1 7 3 ...
..$ y2  : num [1:23] 2.3 2.3 3 2.3 3 3 2 5 2 8 ...
..$ ind1: num [1:23] 2 3 3 4 4 5 5 5 6 6 ...
..$ ind2: num [1:23] 1 1 2 1 2 2 3 4 3 5 ...
$ dirsgs  :'data.frame':    15 obs. of  8 variables:
..$ x1  : num [1:15] 1.65 4.56 5.75 0 1.65 ...
..$ y1  : num [1:15] 3.65 0.74 5.5 2.86 3.65 ...
..$ x2  : num [1:15] 4.56 4.51 4.56 1.65 3.5 ...
..$ y2  : num [1:15] 0.74 0 0.74 3.65 5.5 ...
..$ ind1: num [1:15] 2 3 3 4 4 5 5 5 6 6 ...
..$ ind2: num [1:15] 1 1 2 1 2 2 3 4 3 5 ...
..$ bp1 : logi [1:15] FALSE FALSE FALSE TRUE FALSE FALSE ...
..$ bp2 : logi [1:15] FALSE TRUE FALSE FALSE FALSE FALSE ...
$ summary :'data.frame':    10 obs. of  9 variables:
..$ x       : num [1:10] 2.3 3 7 1 3 8 0 10 0 10
..$ y       : num [1:10] 2.3 3 2 5 8 9 0 0 10 10
..$ n.tri   : num [1:10] 4 4 6 5 5 5 4 3 4 2
..$ del.area: num [1:10] 4.5 6.05 18.67 7.5 15 ...
..$ del.wts : num [1:10] 0.045 0.0605 0.1867 0.075 0.15 ...
..$ n.tside : num [1:10] 4 4 5 4 5 3 1 1 2 1
..$ nbpt    : num [1:10] 4 0 4 2 2 4 2 2 2 2
..$ dir.area: num [1:10] 9.09 10.74 23.32 9.39 18.06 ...
..$ dir.wts : num [1:10] 0.0909 0.1074 0.2332 0.0939 0.1806 ...
$ n.data  : int 6
$ n.dum   : int 4
$ del.area: num 100
$ dir.area: num 100
$ rw      : num [1:4] 0 10 0 10
- attr(*, "class")= chr "deldir"

I am quite sure that somewhere in 'try' the distances between the points calculated by deldir are stored, but I just don't know where. I have tried figuring it out by calculating the distances and looking for the values among the $ elements, but I could not find them. For me, the best way to use this information would be if I could plot the length of each lines on the plot onto each individual line, then I can calculate the distances between all of the cities by hand.

Thanks for your help!

Solution:

Calculate all distances possible and store them into matrix called all.distances (distances are in meters). ( dat is a source data.frame containing latitudes and longitudes)

library(BoSSA)
all.distances<-distGPS(dat$lat, dat$lon)

Now you have much more distances than you need, so you must generate code which will extract only values you need. Create list in which you can see which trees are neighbours (by Delaunay Triangulation)

library(tripack)
tree.nb<-neighbours(tri.mesh(dat$lat,dat$lon))

Create two vectors, so you have combination of neighbours

my.FC<-rep(c(1:length(tree.nb)), sapply(tree.nb, length))
my.SC<-unlist(tree.nb)

It will look like this:

my.FC
[1]
1  1  1  2  2  2  2  3  4  4

my.SC
[1]
6  7  8  4  7  5  8  5  2  9

Read as columns, so location "1" is neighbour with locations "6","7","8". Location "2" is neighbour with location "4","7","5","8", and so on.....

Now comes the clever part... You need to use combination of neighbours as coordinates of desired values in distance matrix. And to extract these values into a vector

Creating empty vector for distance values:

nb.dist<-numeric(sum(sapply(tree.nb, length)))

Code for extracting values:

for (i in 1:sum(sapply(tree.nb, length)))
    nb.dist[i] <- all.distances[my.FC[i],my.SC[i]]

However, in created vector "nb.dist" will be all values twice (because "1" is neighbour of "8" and "8" is neighbour of "1" also.

So just use unique()

unique(nb.dist)

Enjoy :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM