# Generate near table for x number of neighbours using arcpy

There are 3 likely scenerios that I am trying to capture near distances for:

1. An interchange subway station, which has 2 or more neighboring stations. That is, the station in question connects 2 or more major routes and has 2 or more neighboring stations.
2. A terminal subway station, which has only 1 neighboring station. This is the station at the end of the line.
3. An inline subway station, which has exactly 2 neighboring stations, one of either approach.

I am attempting to calculate a value one might call "average distance between neighboring stations"

The `arcpy.GenerateNearTable_analysis()` can handle two options: Distance to closest feature, and Distance between all features.

Does anyone have a clever method for solving for these scenarios? Note that each station is designated as "Interchange", "Terminal" or "Inline" in the attribute table under the field "StationType".

Here is some psuedo code based on @whuber's suggestion in the comments. I don't have time to figure this out just yet, so if anyone wants to take a stab at it you'll be rewarded with a checkmark! ;)

I have taken a look at the NetworkX library and it seems to work as I want it to.

Given the graph:

``````A —― B ―― C ―― D
|
E
``````

as well as the nodes and links:

``````Nodes = ["A", "B", "C", "D", "E"]
Links = [("A", "B"), ("B", "C"), ("C", "D"), ("B", "E")]

def myFunction(node):
identify the links that node belongs to
return someValue
``````
• I guess I can also mention that I am using ArcGIS 10.1 and I love the arcpy.da module (for its speed). I hope that we can use this. – Michael Markieta Jul 13 '12 at 18:51
• FWIW this is purely a graph-theoretic problem with a standard solution: you seek the neighborhood graph of each vertex. It is available almost immediately as soon as you represent the network in a standard format, such as a DCEL (or a generalization if the network in non-planar). This suggests that some out-of-the-box Python solutions might be available. – whuber Jul 13 '12 at 19:21

I beleive your problem, as @whuber, suggested would best be represented in an Adjacency Matrix. That is, if you have the time and inclination to understand the theory behind it, rather than relying on a package to do the job for you.

For a given graph G, with vertices of {v1, v2,...,vn} where n is the number of vertices, you need to create a matrix of size Mi,j where i = n and j = n. Each vertex is then represented in the ith row by the number of paths found to adjacent verticies in the jth column.

Example below: Given this mildly complex form of representing your relatively simple data, you will need to number your vertices in an arbitrary fashion, not representative of any logical order.

NOTE: Assuming no station loops upon itself, a kth row will never have a value other than 0 in the kth column. All definitions below assume this to be true

NOTE: Assuming there are no concurrent lines between the same station, all examples below assume that a cell value will only ever be 1 or 0. The example above also assumes bidirectional travel is permitted.

# Rules to identify station categories:

## 1. Terminal

A terminal would be identified by a kth row having a single column which does not have a value of 0, and which value is 1. See vertices 1, 2, and 3 in example 1 above.

## 2. Junction

A junction would be identified by a kth row having more than two columns containing a value of 1. See vertex 4 in example 1 above, alternatively all vertices in example 3 above.

## 3. Inline

An inline station is signified by having exactly 2 columns in a kth row where the value is 1. See all verticies in example 2 above. (Ignore the fact that {v1, v3} intersects {v2, v4}.)

• Who are you and where did you come from! That was one of the best answers I've received in a long time. Thank you @Geoist. – Michael Markieta Jul 17 '12 at 23:55
• @MichaelMarkieta Funny story, I just learned about this not 2 hours before I saw your post. – nagytech Jul 17 '12 at 23:58

You might try using Shapely. If you convert your arcpy points to shapely points, you can calculate the distance between individual points.

``````import arcpy
import shapely

arc_point1 = arcpy.Point(1,1)
arc_point2 = arcpy.Point(5,5)

shp_point1 = shapely.geometry.Point(arc_point1.X, arc_point1.Y)
shp_point2 = shapely.geometry.Point(arc_point2.X, arc_point2.Y)

distance = shp_point1.distance(shp_point2)
print "distance:", distance
``````
• I should mention that Shapely can only be used for features on a Cartesian plane, so this method won't work if your data are in geographic coordinates. – Cyrus Jul 13 '12 at 21:29
• This answer suggests an ambiguity in how the question might be understood. I have read the question as indicating the distances are known; their computation does not appear to be the issue. What the OP seeks, I believe, is an algorithm for identifying a variable number of immediate neighbors of vertices along a network, to the end of then retrieving their distances and computing a statistical summary of them (such as an average). – whuber Jul 16 '12 at 13:51
• @whuber Oops! Agreed, I answered the question a bit too hastily. – Cyrus Jul 16 '12 at 16:19
• @whuber is on the mark. – hhart Jul 17 '12 at 12:38
• This wont work for me, but thanks! – Michael Markieta Jul 17 '12 at 21:41