Select to view content in your preferred language

Network Analyst - OD Cost Matrix Help

2316
11
05-13-2011 10:12 AM
JoshuaGelman
Emerging Contributor
Hi there,

I'm somewhat new using the Network Analyst extension and running into a problem I can't figure out how to solve.

I built a network dataset from a street centerlines layer.  What I want to do is, using the junctions layer (nodes at the intersections of the streets), create a table that gives the distance between the origin node and the closest other nodes in each direction.

The O-D Cost Matrix tool seemed like the way to go, but the problem is it gives me distances between the origin node and all subsequent nodes along the path within the specified search tolerance.

What I want is only the first node along each path from the origin node.  I attached a picture to try and explain this better. 

Any suggestions or help would be EXTREMELY appreciated.

--Josh
Tags (2)
0 Kudos
11 Replies
JaySandhu
Esri Regular Contributor
Josh,
What you need for each junction is simply the adjacent junctions and cost to them. You do not need to use any solver (shortest path, OD, etc) to do this. You can simply add the network dataset to ArcMap and then use the VBA below. This VBA lists out the adjacent junctions to each junction in the network and the cost (based on an attribute called "minutes") to get to that junction. You can customize it as needed. For example, change the EID to OID and/or change the cost attribute you need to list out. Currently it is set to show the results in the immediate window of VBA but you can customize it to write to a file.
Regards.
Jay Sandhu



Public Sub List_Adjacent_Junctions()
On Error GoTo eh

  Dim pMxDoc As IMxDocument
  Set pMxDoc = ThisDocument
 
  Dim pNLayer As INetworkLayer
  Set pNLayer = pMxDoc.FocusMap.Layer(0) 'Assume the Network dataset is first layer in TOC
 
  Dim pND As INetworkDataset
  Set pND = pNLayer.NetworkDataset
 
  Dim pNQ As INetworkQuery
  Set pNQ = pND
   
  Dim pEnumNE As IEnumNetworkElement
  Set pEnumNE = pNQ.Elements(esriNETJunction)
   
  Dim pNEdge As INetworkEdge
  Set pNEdge = pNQ.CreateNetworkElement(esriNETEdge)
  Dim pNEFromJunc As INetworkJunction
  Set pNEFromJunc = pNQ.CreateNetworkElement(esriNETJunction)
  Dim pNEToJunc As INetworkJunction
  Set pNEToJunc = pNQ.CreateNetworkElement(esriNETJunction)
 
  Dim pNE As INetworkElement
  Set pNE = pEnumNE.Next

  Dim pNEJunc As INetworkJunction
  Set pNEJunc = pNE
 
  Dim i As Integer
  Do Until pNE Is Nothing
    Debug.Print "Junction: " & pNEJunc.EID & " is adjacent to: " & pNEJunc.EdgeCount & " junctions."
    For i = 0 To pNEJunc.EdgeCount - 1  'For each connected edge...
      pNEJunc.QueryEdge i, True, pNEdge   'Get that connected edge
      pNEdge.QueryJunctions pNEFromJunc, pNEToJunc  'Get To junction of current edge
      Debug.Print " Adjacent Junction: " & pNEToJunc.EID & " Cost = " & pNEdge.AttributeValueByName("minutes")  'List the adjacency and cost to it
    Next
    Set pNE = pEnumNE.Next
  Loop
  Exit Sub
eh:
  MsgBox "Error: " & Err.Description
End Sub
0 Kudos
JoshuaGelman
Emerging Contributor
Thank you!

Now I just need to wait for my VBA Authorization File to try it since I'm running Arc10.  🙂
0 Kudos
JoshuaGelman
Emerging Contributor
Seems to work! 

The only odd thing is I'm getting some records of adjacent nodes that, when viewed on the map, don't appear to be connected at all with the origin nodes.  This may be an issue with the network dataset though because everything looks fine with the code.  Perhaps dissolving the network will help.

In any case, the VBA code seems to work and I modified it to output to a text file. 

Thanks so much!

--Josh
0 Kudos
JaySandhu
Esri Regular Contributor
Glad to know you have it working.

As far as the non-adjacent junction problems, I doubt that dissolve will fix any connectivity issues. You may look at the junctions that are not adjacent and also look at using the Network Identify tool (second tool from right on the NA toolbar) to click on these junctions and see what is adjacent.

Jay Sandhu
0 Kudos
JoshuaGelman
Emerging Contributor
Hmmm...this is probably something silly I'm just overlooking.  Maybe something to do with the Object ID's of the Junctions layer?

Attached is a ZIP of the output file using the VBA code you sent.  Also at (http://www.wxalert.info/work/streets.zip) is a ZIP file of my Junctions layer and the Street Centerlines layer I'm using for the Network Dataset.  The file size was a bit too large to upload here (~ 3.5 MB).

As an example...if you open the CSV file (output) you can see that Node #3 is adjacent to Node #1310, but if you select ObjectIDs #3 and #1310 from the Junctions layer, they're not even close.

Again, I'm probably just missing something silly here, but any guidance would be appreciated.
0 Kudos
JoshuaGelman
Emerging Contributor
I am pretty sure the issue is the VBA code gets EIDs and what I need are the OIDs from the junctions layer.

I just changed .EID to .OID in the code and it seems to have worked.  Is that correct?

Thanks!

Edit: This seems to be working, so unless I'm missing something, changed EID to OID in the code seemed to work.  But if anyone thinks I'm missing something, please let me know.  Thanks again for the help Jay!
0 Kudos
JaySandhu
Esri Regular Contributor
I did say in my original post to "For example, change the EID to OID "

So as long as that is giving you the right results, it should be fine.

Regards,
Jay Sandhu
0 Kudos
JoshuaGelman
Emerging Contributor
Thanks!  I didn't realize that's what you meant at the time.  I just didn't expect it to be that simple. 

Thanks again, much appreciated.
0 Kudos
AlejandroMata
Deactivated User
Hi all !

Is very important for me to do what you mentioned in this thread.

I followed the steps listed to get the distance between junctions but i haven´t achieved good results.

I did that:

1.- Added the Network Dataset to ArcMap.
2.- The Network Dataset is the first layer in TOC.
3.- Added the VBA code to VBA Editor, i changed the EID to OID.
4.- Run the VBA and nothing happens.

I have some doubts:

1.- Where it is supossed the results should be displayed ?
2.- The results are a new table ? a new attibute ? in what layer ?

I hope you can help me.

Best Regards.
0 Kudos