The Wayback Machine - https://web.archive.org/web/20160325092731/http://gis.stackexchange.com/questions/150240/how-to-calculate-path-length-of-each-point-from-the-start-using-arcpy
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

As shown in the picture below I would like to calculate the path length of each point from the start. Can anyone give me some help in writing this in python for ArcGIS?

Example

share|improve this question

So, you have a point feature class. Every point has a unique attribute that represents the sequence (e.g., from 1 to 100).

You want to get a point with PointID = 1, find a distance to the point with PointID = 2, and write this distance value into the Distance field in the feature class (Distance field for feature PointID2 will contain the distance from PointID1 to PointID2).

This solution would work for Basic license.

import arcpy fc = r"C:\ArcGIS\Default.gdb\_PointDistanceFc" features = [f for f in arcpy.da.SearchCursor(fc,("PointID","SHAPE@"), sql_clause=(None,"ORDER BY PointID"))] index = 1 length = 0 update_values = [(0,0)] for f in features: if index < len(features): length += f[1].distanceTo(features[index][1]) index += 1 update_values.append((f[0],length)) else: pass with arcpy.da.UpdateCursor(fc,("Distance","PointID"),sql_clause=(None,"ORDER BY PointID")) as upd_cur: index = 0 for row in upd_cur: row[0] = update_values[index][1] upd_cur.updateRow(row) index += 1 

The UpdateCursor will write the length value to the field. It is important to order the point features in the sequence you want to calculate the length with (you cannot rely on the OID since those might not match the order of features creation). Here is the sample result:

enter image description here

share|improve this answer
1  
this could be done with only update cursor too. No need for search cursor and for loop above. – SuryaJun 9 '15 at 12:45
    
Thank you @Alex, it works!! – King-ZhaoJun 10 '15 at 5:28
    
@Surya, I've used the search cursor to get a list of tuples which could be returned and used in another application or serve other purposes. Using UpdateCursor is optional as it was not asked originally to write the data to the fc. – Alex TereshenkovJun 10 '15 at 8:43
    
@King-Zhao, no problem, glad it works for you. You should probably accept the answer, so the thread will be "closed". – Alex TereshenkovJun 10 '15 at 8:44
    
@Alex Tereshenkov Hi, u mentioned that is important to order the point features in the sequence. Do u know how to sort the points into a proper orders? Could u give some idea? Thanks. – King-ZhaoAug 4 '15 at 2:02

I have made a tool that does this measurement. This tool needs only three parameters: Just download the Measure_Length.tbx and locate in the ArcCatalog and run.script

  1. Input Point Feature
  2. Unique field (in the attribute table)
  3. A field that will be populated by this script, data type for this field must be double

Please find the tool in the github at here.

OH YES CREDITS GO TO ALEX

share|improve this answer
    
This would definitely work but the original poster does not indicate if the source data is a polyline or point dataset. – HornbyddJun 9 '15 at 10:56
    
Thank you very much! – King-ZhaoJun 9 '15 at 11:14
    
@King-Zhao you can test the tool and give the feedback.. – SIslamJun 13 '15 at 14:07
    
@SIslam Ok, thanks, i'll try tomorrow,then will back to you!!!And this tool box is really great!!!Big appreciation!!! – King-ZhaoJun 14 '15 at 15:23

Thank you ->SIslam, Ur code is very helpful. Then i tried to revised the code to fit my process, the revision as below: import arcpy from arcpy import env

env.workspace="C:/Temp/test0608/" Pnt_move="C:/Temp/test0608/Pnt_move.shp" curS = arcpy.da.SearchCursor(Pnt_move,["OID@","SHAPE@"]) counter = 0 for i in curS: print "OID is: {0}".format(i[0]) counter+= i[1].length print counter 

But it not work properly just like pic show below, u can see the all length is 0.0.

enter image description here

The original point data has more 4000 points like the pic below. enter image description here

share|improve this answer
    
Are your shapefiles projected in Projected Coordinate systems(NOT WGS84) e.g. UTM Zone 34 etc? – SIslamJun 9 '15 at 11:26
    
Thank you for code, very helpful.The projection is Japanese JGD_2000 – King-ZhaoJun 9 '15 at 11:30
    
Yap that is the problem. Convert to JGD_2000_UTM_Zone_51N etc or JGD_2000_Japan_Zone_4 etc consult who knows. – SIslamJun 9 '15 at 11:36
    
Your code is flawed as @SIslam approach works only for polylines your data is clearly a point dataset. I do not believe this is an issue with the coordinate system but the geometry type of your data. Alex's approach is best. – HornbyddJun 9 '15 at 16:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.

close