2

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!

www
  • 38,575
  • 12
  • 48
  • 84
Annemarie
  • 689
  • 6
  • 14
  • 28
  • 4
    Please follow the guidelines here: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example . Frankly, we're a pretty welcoming bunch but I will not be helping with this one, as you have not shown a willingness to become part of the community by following even its most basic conventions, while receiving plenty of help on questions you could likely have answered on your own. Please consider learning more about StackOverflow and how to use the site and be a good community member. Try starting here http://meta.stackexchange.com/questions/tagged/faq – Ari B. Friedman Jul 02 '12 at 11:58
  • @Annemarie, to keep on the good side of the SO community, do these 3 things. (a) Show that you have attempted to solve the problem yourself by including some code (b) Ensure the code is minimal and reproducible so that it can be copied into other people's R setups for experimentation (c) Accept the best answer to each of your questions by clicking the big white tick mark at the upper left of the best answer. You have only accepted answers for 2 out of 7 past questions, which means that you have not recognised the efforts of those who answered. People will get irate and stop helping you. – SlowLearner Jul 02 '12 at 12:27

1 Answers1

1

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 :)

Ladislav Naďo
  • 822
  • 12
  • 27
  • 1
    This is a wonderful solution. But it looks if the function distGPS is no longer available neither in the package BoSSA nor in the package chroGPS. Instead use the function mdist from the geosphere package: all.distances <- unlist(mdist(as.matrix(data.frame(dat$lat, dat$lon)))) – WJH Nov 08 '22 at 21:17