Distance Calculations Revisited...

2068
0
04-20-2016 07:05 AM
Labels (1)
DanPatterson_Retired
MVP Emeritus
2 0 2,068

There are a large number of questions that deal with some aspect of distance.  If you are interested in quick distance calculations, then download and read the attachment.

The tools are builtin to arcpy to get the data into this form to facilitate calculations.

You can determine distances using different metrics... euclidean distance is shown here.

If you need to find the nearest, a list of closest objects or to calculate the perimeter of a polygon or length of a line represented by a sequence of points, there are other options available to you.

Associated references:

Python Near Analysis (No Advanced Licence)

Single origin to multiple destinationsMultiple origin to multiple destinations

The example here uses a single origin in an origin list.  You will note that the coordinates are given as a list, of lists, of coordinates.

Example 1...

Single origin...
[[ 0.  0.]]
Multiple destinations
[[ 4.  0.]
 [ 0.  2.]
 [ 2.  2.]
 [-2. -3.]
 [ 4.  4.]]
Distances: [ 4.    2.    2.83  3.61  5.66]

An example for 3D length/distance

X, Y and Z values array, their differences and the resultant distances and the length if they formed a circuit

e_leng(d3d,verbose=True)
Input array....(shape=(1, 8, 3))
[[[ 0.  0.  0.]
  [ 1.  1.  1.]
  [ 0.  1.  0.]
  [ 1.  0.  1.]
  [ 0.  1.  1.]
  [ 1.  1.  0.]
  [ 1.  0.  0.]
  [ 0.  0.  1.]]]
differences...


[[[-1. -1. -1.]
  [ 1.  0.  1.]
  [-1.  1. -1.]
  [ 1. -1.  0.]
  [-1.  0.  1.]
  [ 0.  1.  0.]
  [ 1.  0. -1.]]]
distances...
[[ 1.73  1.41  1.73  1.41  1.41  1.    1.41]]
length...[ 10.12]

This example  uses the same destinations.

Example 2...
Multiple Origins...
[[[ 0.  0.]]
 [[ 1.  0.]]

 [[ 0., 1.]]
 [[ 1.  1.]]]

Destinations...
[[ 4.  0.]
 [ 0.  2.]
 [ 2.  2.]
 [-2. -3.]
 [ 4.  4.]]

Distances...
[[ 4.    2.    2.83  3.61  5.66]
 [ 3.    2.24  2.24  4.24  5.  ]
 [ 4.12  1.    2.24  4.47  5.  ]
 [ 3.16  1.41  1.41  5.    4.24]]

Origin-Destination, distance matrix
dests->:     0     1     2     3     4
origins
      0: [ 4.    2.    2.83  3.61  5.66]
      1: [ 3.    2.24  2.24  4.24  5.  ]
      2: [ 4.12  1.    2.24  4.47  5.  ]
      3: [ 3.16  1.41  1.41  5.    4.24]

That's all for now...

About the Author
Retired Geomatics Instructor at Carleton University. I am a forum MVP and Moderator. Current interests focus on python-based integration in GIS. See... Py... blog, my GeoNet blog...
Labels