Select to view content in your preferred language

accumulate segment lengths of a polyline

993
4
04-09-2010 12:55 PM
QuayeTrimble
Emerging Contributor
I need a way to accumulate the segment lengths of a polyline.  Each individual segment contains its associated length and contains a unique id which is specific to each feature.  I'm inexperienced with python but learning as I go.  Can anyone provide some ideas or a reference to an existing script that may help to get me started ?
This might help with any questions about what I'm trying to achieve.

ID|LENGTH|CUM_LENGTH
1 |10|10
1 |14|24
1 | 9|33
2 | 6| 6
2 |13|19
2 | 8|27
0 Kudos
4 Replies
RDHarles
Regular Contributor
This should work assuming your ID field is sorted like you have in the example.


# Import modules and create the geoprocessor object  
import arcgisscripting, sys, os
gp = arcgisscripting.create()

gp.workspace = os.getcwd()

# File to process
shp = "Adminbndy1.shp"

# Set total to zero and start a blank list
total=0
keepTrack=[]

# Create update cursor for feature class.
rows = gp.UpdateCursor(shp)
row = rows.Next()
while row:
   
    # Get the values from the fields
    id = row.GetValue("ID")
    len = row.GetValue("LENGTH")
   
    # If id is unchanged   
    if id in keepTrack:
        # Add length to the total
        total = total + len

    # If id changes       
    if not id in keepTrack:
        # Total starts over as length value
        total = len
        # Clear the id list
        keepTrack=[]

    # Keep track of the id
    keepTrack.append(id)       

    print "id: "+str(id), "length: "+str(len), "total: "+str(total)
       
    # Write the total to field "CUM_LENGTH"
    row.CUM_LENGTH = total
      
    # Execute the new value to the table
    rows.UpdateRow(row)
    # Go to the next
    row = rows.Next()
del rows
0 Kudos
RDHarles
Regular Contributor
Here's the code with the proper indenting (I finally figured out how to post code properly on this new site).

# Import modules and create the geoprocessor object   
import arcgisscripting, sys, os
gp = arcgisscripting.create()

gp.workspace = os.getcwd()

# File to process
shp = "File1.shp"

# Set total to zero and start a blank list
total=0
keepTrack=[]

# Create update cursor for feature class.
rows = gp.UpdateCursor(shp)
row = rows.Next()
while row:
    
    # Get the values from the fields
    id = row.GetValue("ID")
    len = row.GetValue("LENGTH")
    
    # If id is unchanged    
    if id in keepTrack:
        # Add length to the total
        total = total + len

    # If id changes        
    if not id in keepTrack:
        # Total starts over as length value
        total = len
        # Clear the id list
        keepTrack=[]

    # Keep track of the id
    keepTrack.append(id)        

    print "id: "+str(id), "length: "+str(len), "total: "+str(total)
        
    # Write the total to field "CUM_LENGTH"
    row.CUM_LENGTH = total
       
    # Execute the new value to the table
    rows.UpdateRow(row)
    # Go to the next
    row = rows.Next()
del rows
0 Kudos
VivekGaikwad
Deactivated User
Dear Sir,

Please help me on how to use this code in ArcMAP Application.


Here's the code with the proper indenting (I finally figured out how to post code properly on this new site).

# Import modules and create the geoprocessor object   
import arcgisscripting, sys, os
gp = arcgisscripting.create()

gp.workspace = os.getcwd()

# File to process
shp = "File1.shp"

# Set total to zero and start a blank list
total=0
keepTrack=[]

# Create update cursor for feature class.
rows = gp.UpdateCursor(shp)
row = rows.Next()
while row:
    
    # Get the values from the fields
    id = row.GetValue("ID")
    len = row.GetValue("LENGTH")
    
    # If id is unchanged    
    if id in keepTrack:
        # Add length to the total
        total = total + len

    # If id changes        
    if not id in keepTrack:
        # Total starts over as length value
        total = len
        # Clear the id list
        keepTrack=[]

    # Keep track of the id
    keepTrack.append(id)        

    print "id: "+str(id), "length: "+str(len), "total: "+str(total)
        
    # Write the total to field "CUM_LENGTH"
    row.CUM_LENGTH = total
       
    # Execute the new value to the table
    rows.UpdateRow(row)
    # Go to the next
    row = rows.Next()
del rows
0 Kudos
RDHarles
Regular Contributor
1.) Make sure your shapefile has the 3 fields that the script lists: ID, LENGTH, CUM_LENGTH
(if not, edit the script to match you fields names)
2.) Save the updated code (posted below) as a python script on your system.
3.) Follow these directions:
http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Adding_a_script_tool
4.) When you get to step 11, put in the following under the two headings:
------------------------------
DisplayName    DataType
-----------------------------
Workspace    Workspace
Shapefile    Shapefile

5.) ...then execute the script


# Import modules and create the geoprocessor object   
import arcgisscripting, sys, os
gp = arcgisscripting.create()

#gp.workspace = os.getcwd()
gp.workspace = sys.argv[1]

# File to process
shp = sys.argv[2]

# Set total to zero and start a blank list
total=0
keepTrack=[]

# Create update cursor for feature class.
rows = gp.UpdateCursor(shp)
row = rows.Next()
while row:
    
    # Get the values from the fields
    id = row.GetValue("ID")
    len = row.GetValue("LENGTH")
    
    # If id is unchanged    
    if id in keepTrack:
        # Add length to the total
        total = total + len

    # If id changes        
    if not id in keepTrack:
        # Total starts over as length value
        total = len
        # Clear the id list
        keepTrack=[]

    # Keep track of the id
    keepTrack.append(id)        

    print "id: "+str(id), "length: "+str(len), "total: "+str(total)
        
    # Write the total to field "CUM_LENGTH"
    row.CUM_LENGTH = total
       
    # Execute the new value to the table
    rows.UpdateRow(row)
    # Go to the next
    row = rows.Next()
del rows
0 Kudos