[GIS] Generate near table for x number of neighbours using arcpy

arcpyNetworkpython

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".

Added:

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
    count the number of links
    calculate the total link lengths
    divide the total link lengths by the number of links
    return someValue

Best Answer

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:

enter image description here

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}.)

Related Question