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
3 Answers 3
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
-
1this could be done with only update cursor too. No need for search cursor and for loop above.Surya– Surya2015年06月09日 12:45:25 +00:00Commented Jun 9, 2015 at 12:45
-
@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 Tereshenkov– Alex Tereshenkov2015年06月10日 08:43:42 +00:00Commented Jun 10, 2015 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 Tereshenkov– Alex Tereshenkov2015年06月10日 08:44:11 +00:00Commented Jun 10, 2015 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-Zhao– King-Zhao2015年08月04日 02:02:57 +00:00Commented Aug 4, 2015 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
- Input Point Feature
- Unique field (in the attribute table)
- 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
-
This would definitely work but the original poster does not indicate if the source data is a polyline or point dataset.Hornbydd– Hornbydd2015年06月09日 10:56:40 +00:00Commented Jun 9, 2015 at 10:56
-
@King-Zhao you can test the tool and give the feedback..Learner– Learner2015年06月13日 14:07:44 +00:00Commented Jun 13, 2015 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-Zhao– King-Zhao2015年06月14日 15:23:49 +00:00Commented Jun 14, 2015 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
-
Are your shapefiles projected in Projected Coordinate systems(NOT WGS84) e.g. UTM Zone 34 etc?Learner– Learner2015年06月09日 11:26:15 +00:00Commented Jun 9, 2015 at 11:26
-
Thank you for code, very helpful.The projection is Japanese JGD_2000King-Zhao– King-Zhao2015年06月09日 11:30:04 +00:00Commented Jun 9, 2015 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.Learner– Learner2015年06月09日 11:36:10 +00:00Commented Jun 9, 2015 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.Hornbydd– Hornbydd2015年06月09日 16:48:17 +00:00Commented Jun 9, 2015 at 16:48