Distance Matrix
Previous  Top  Next

(continued from click here)

Distance matrices are a very important part of street network calculations, as it is usual input to many other functions. In this example, it is assumed that we have two themes of point-objects and we want to calculate the distance from all elements in the first theme to all elements in the second theme. The two themes may be the same.

For simplicity, we assume access to the coordinates of the point objects in 2 arrays (x1,y1 for theme1 and x2,y2 for theme2):

This first loop calculates the nearest node for all points in theme2, so we don't have to do that for every time in the inner loop:
for j = 1 to theme2.count
  RWnetBase1.Coordinate2node(x2[j],y2[j],node2[j],dist)
  RWcalc1.nodelistset(j,node2[j])
next j

Main loop
for i = 1 to theme1.count
  RWnetBase1.Coordinate2node(x1[i],y1[i],node1,dist)
  RWcalc1.isocost(node1,0)
or RWcalc1.isocostnodelist(node1,theme2.count)
  for j = 1 to theme2.count
    routetime = RWcalc1.GetNodeCost(node2[j])
    if routetime<1e36 then
      routedist = RWcalc1.GetNodeExtra(node2[j])
      print "From "+str$(i)+" (theme1) to "+str$(j)+" (theme2): "+str$(routetime)+" minutes and "+str$(routedist)+" miles"
    else
      print "From "+str$(i)+" (theme1) to "+str$(j)+" (theme2) : The 2 nodes are not connected"
    end if
  next j
next i

If the points in theme2 is located in a smaller part of the street network, the calculations can be made faster, if we use the nodelist for storing theme2. This is marked with red in the sample code above. That approach requires you to have less nodes than can be in the nodelist. If your nodes are spread all over the network, it may even be a little slower (but not much).

Please also note, we don't call the RouteFind function, since we are only interested in the distances and the actual list of links making up the route isn't relevant.

·Dynamic segmentation  

If you want to calculate a distance table based on dynamic segmentation instead, it is possible to use the same fast isochrone approach as above:

for j = 1 to theme2.count
  RWnetBase1.Coordinate2location(x2[j],y2[j],link2[j],percent2[j],side,dist,x,y)
  RWcalc1.locationlistset(j,link2[j],percent2[j])
next j

for i = 1 to theme1.count
  RWnetBase1.Coordinate2location(x1[j],y1[j],link1,percent1,side,dist,x,y)
  RWcalc1.isocostdynlocationlist(link1,percent1,theme2.count)
  for j = 1 to theme2.count
    routetime = RWcalc1.getlinkcostdyn(link2[j],percent2[j])
    if routetime>=0 then
      routedist = RWcalc1.getlinkextradyn(link2[j],percent2[j])
      print "From "+str$(i)+" (theme1) to "+str$(j)+" (theme2): "+str$(routetime)+" minutes and "+str$(routedist)+" miles"
    else
      print "From "+str$(i)+" (theme1) to "+str$(j)+" (theme2) : The 2 locations are not connected"
    end if
  next j
next i