Reference: Calculate Distance.
Date: 2017-08-22
Cumulative Distance
If you want distance from the first point in a data table to every other point in sequence, then use cumulative distance function below.
Do the following:
- select Python parser
- toogle on Show code block
- paste the following in the Pre-logic Script Code block
- and the last piece in the expression box
Pre-logic Script Code:
<SPAN class="string token">""</SPAN>" input shape field<SPAN class="punctuation token">:</SPAN> returns cumulative distance between points
dist_cumu<SPAN class="punctuation token">(</SPAN>!Shape!<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#enter into the expression box"""</SPAN>
x0 <SPAN class="operator token">=</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">;</SPAN> y0 <SPAN class="operator token">=</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">;</SPAN> distance <SPAN class="operator token">=</SPAN> <SPAN class="number token">0.0</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">dist_cumu</SPAN><SPAN class="punctuation token">(</SPAN>shape<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">global</SPAN> x0<SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">global</SPAN> y0<SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">global</SPAN> distance
x <SPAN class="operator token">=</SPAN> shape<SPAN class="punctuation token">.</SPAN>firstpoint<SPAN class="punctuation token">.</SPAN>X<SPAN class="punctuation token">;</SPAN> y <SPAN class="operator token">=</SPAN> shape<SPAN class="punctuation token">.</SPAN>firstpoint<SPAN class="punctuation token">.</SPAN>Y
<SPAN class="keyword token">if</SPAN> x0 <SPAN class="operator token">==</SPAN> <SPAN class="number token">0.0</SPAN> <SPAN class="operator token">and</SPAN> y0 <SPAN class="operator token">==</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">:</SPAN>
x0 <SPAN class="operator token">=</SPAN> x<SPAN class="punctuation token">;</SPAN> y0 <SPAN class="operator token">=</SPAN> y
distance <SPAN class="operator token">+=</SPAN> math<SPAN class="punctuation token">.</SPAN>sqrt<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>x <SPAN class="operator token">-</SPAN> x0<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">**</SPAN><SPAN class="number token">2</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="punctuation token">(</SPAN>y <SPAN class="operator token">-</SPAN> y0<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">**</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN>
x0 <SPAN class="operator token">=</SPAN> x<SPAN class="punctuation token">;</SPAN> y0 <SPAN class="operator token">=</SPAN> y
<SPAN class="keyword token">return</SPAN> distance
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
calculation field =
dist_cumu<SPAN class="punctuation token">(</SPAN>!Shape!<SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN></SPAN>
NOTES:
If nothing is selected in the table, then the calculation will proceed from the first point and the cumulative distances will be done in sequential order. If you have 3 or more points selected, then it will calculate the values with just the selection. No cannot rearrange the order...write a script, there is only so much you can do in the field calculator.
-----------------------------------------------------------------------------------------------------------------------------------------
Inter-point distance
For distances between sequential point pairs (not cumulative), use the dist_between function.
Do the following:
- select Python parser
- toogle on Show code block
- paste the following in the Pre-logic Script Code block
- and the last piece in the expression box
Pre-logic Script Code:
<SPAN class="string token">""</SPAN>" input shape field<SPAN class="punctuation token">:</SPAN> returns the distance between points <SPAN class="operator token">and</SPAN> its center
dist_between<SPAN class="punctuation token">(</SPAN>!Shape!<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#enter into the expression box"""</SPAN>
x0 <SPAN class="operator token">=</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">;</SPAN> y0 <SPAN class="operator token">=</SPAN> <SPAN class="number token">0.0</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">dist_between</SPAN><SPAN class="punctuation token">(</SPAN>shape<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">global</SPAN> x0<SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">global</SPAN> y0
x <SPAN class="operator token">=</SPAN> shape<SPAN class="punctuation token">.</SPAN>firstpoint<SPAN class="punctuation token">.</SPAN>X<SPAN class="punctuation token">;</SPAN> y <SPAN class="operator token">=</SPAN> shape<SPAN class="punctuation token">.</SPAN>firstpoint<SPAN class="punctuation token">.</SPAN>Y
<SPAN class="keyword token">if</SPAN> x0 <SPAN class="operator token">==</SPAN> <SPAN class="number token">0.0</SPAN> <SPAN class="operator token">and</SPAN> y0 <SPAN class="operator token">==</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">:</SPAN>
x0 <SPAN class="operator token">=</SPAN> x<SPAN class="punctuation token">;</SPAN> y0 <SPAN class="operator token">=</SPAN> y
distance <SPAN class="operator token">=</SPAN> math<SPAN class="punctuation token">.</SPAN>sqrt<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>x <SPAN class="operator token">-</SPAN> x0<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">**</SPAN><SPAN class="number token">2</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="punctuation token">(</SPAN>y <SPAN class="operator token">-</SPAN> y0<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">**</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN>
x0 <SPAN class="operator token">=</SPAN> x<SPAN class="punctuation token">;</SPAN> y0 <SPAN class="operator token">=</SPAN> y
<SPAN class="keyword token">return</SPAN> distance
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
calculation field =
dist_between<SPAN class="punctuation token">(</SPAN>!Shape!<SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN></SPAN>
Notes:
Make sure you have projected data and a double field or nothing will make sense. The same comments apply to calculations using selections as mentioned above.