Nearest Node
Previous  Top  Next

(continued from click here)

When you want to know the nearest object in a store theme for every object in a customer theme, there is generally no faster way to do it, but calling the function NearestNode like this:

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

for j = 1 to storetheme.count
  RWnetBase1.Coordinate2node(x2[j],y2[j],node2,dist)
  RWcalc1.nodelistset(j,node2)
next j

for i = 1 to customertheme.count
  RWnetBase1.Coordinate2node(x1[i],y1[i],node1,dist)
  ID = RWcalc1.nearestnode(node1,storetheme.count)
  if ID > 0 then
    node2 = RWcalc1.nodelistget(ID)
    routetime = RWcalc1.GetNodeCost(node2)
    routedist = RWcalc1.GetNodeExtra(node2)
    print "Nearest for customer "+str$(i)+": Store "+str$(ID)+" was nearest and "+str$(routetime)+" minutes and "+str$(routedist)+" miles away"
  else
    print "Nothing found for customer "+str$(i)
  end if
next i

If store theme has more nodes than can be in the node list, you will have to calculate an isochrone so big that at least one node is within the isochrone and then traverse all nodes in store theme to find the nearest - this will be slower and if your isochrone doesn't contain any nodes at all, you will have to increase the size of the isochrone.

·Faster method  

In the example above, we have calculated the distance from the customer theme to storetheme. In many situations we don't really care if we use the other direction (from storetheme to customer theme), since we are only interested in the total distance anyway. To make it as correct as possible (even if we have one-way restrictions), we can swap the direction of all restrictions first by using the SwapOneWay function. Note: There is no function for "swapping" the direction of turn-restrictions !

Now, we can use this method instead:

for j = 1 to storetheme.count
  RWnetBase1.Coordinate2node(x2[j],y2[j],node2,dist)
  RWcalc1.nodelistset(j,node2)
next j

The key to get fast results is to choose the 125 minutes as close to the maximum time from any customer to a store as possible
RWnetBase1.SwapOneWay()
RWcalc1.isocostmulti(storetheme.count,125)

RWnetBase1.SwapOneWay()

for i = 1 to customertheme.count
  RWnetBase1.Coordinate2node(x1[j],y1[j],node1,dist)
  routetime = RWcalc1.GetNodeCost(node1)
  if routetime<1e36 then
    routedist = RWcalc1.GetNodeExtra(node1)
    print "Nearest for customer "+str$(i)+": Store "+str$(ResultGetNode(node1))+" was nearest and "+str$(routetime)+" minutes and "+str$(routedist)+" miles away"
  else
    print "Nothing found for customer "+str$(i)
  end if
next i