3

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

4

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

4
  • 1
    this could be done with only update cursor too. No need for search cursor and for loop above.
    – Surya
    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. 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". 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
    Aug 4, 2015 at 2:02
3

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

3
  • This would definitely work but the original poster does not indicate if the source data is a polyline or point dataset.
    – Hornbydd
    Jun 9, 2015 at 10:56
  • @King-Zhao you can test the tool and give the feedback..
    – Learner
    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
    Jun 14, 2015 at 15:23
0

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

4
  • Are your shapefiles projected in Projected Coordinate systems(NOT WGS84) e.g. UTM Zone 34 etc?
    – Learner
    Jun 9, 2015 at 11:26
  • Thank you for code, very helpful.The projection is Japanese JGD_2000
    – King-Zhao
    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
    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
    Jun 9, 2015 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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