Populate fields using python

5453
8
10-01-2015 06:41 AM
TerryGustafson
Occasional Contributor II

I'm new to python and trying to figure out if this is possible.  I have a measured layer with segments in it.  I want to add a begin segment and end segment field.  The layer has the following fields.  Corridor, Ref Marker, shape length, beg_seg and end_seg.  I would like to populate the begin seg and end seg from the shape_length field and accumulate them.  So the mile post zero beg_seg would start at zero and the end_seg field would be the length of the zero segment.  Then mile post 1 beg_seg would be the value of the end_seg of mile post one.  The end_seg for mile post one would be the value of mile post 0 length plus the length of mile post one.  this would continue till the last mile post.  I have attached a image of the table that I manually entered the values for.  I was hoping to create a python script to perform this as I have a ton of corridors with a ton of records.  One other piece of information is some of the corridors may be missing mile posts.

Tags (1)
0 Kudos
8 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Terry,

Try the following:

import arcpy
from arcpy import env
env.workspace = r"C:\temp\python\test.gdb"

fc = "Sample"

rmList = []

with arcpy.da.SearchCursor(fc, ["RM"]) as cursor:
    for row in cursor:
        rmList.append(row[0])

del cursor

rmList.sort()

for RM in rmList:
    if RM == 0:
        with arcpy.da.UpdateCursor(fc, ["RM", "SHAPE_LEN", "beg_seg", "end_seg"], "RM = " + str(RM)) as cursor:
            for row in cursor:
                row[2] = 0
                length = row[1]
                row[3] = length                
                cursor.updateRow(row)
    else:
        with arcpy.da.UpdateCursor(fc, ["RM", "SHAPE_LEN", "beg_seg", "end_seg"], "RM = " + str(RM)) as cursor:
            for row in cursor:
                row[2] = length
                newLength = row[1]
                row[3] = length + newLength                
                cursor.updateRow(row)
                length = row[3]
del cursor
TerryGustafson
Occasional Contributor II

Jake,

So this is how basic my knowledge is.. When I copy and paste this script into the python window how do I execute it? I have only done a little python scripting and used it in the field calculator..

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Since you are running this through the python window in ArcMap, you will only need to copy lines 5 - 33.  Here are the steps:

1.  Add the feature class to ArcMap

2.  Change the fc variable in line 5 from 'Sample' to the feature class name

3.  Open the python window

4.  Copy lines 5 -33 and paste into the window

5.  Click 'Enter' a couple of times to execute the code

TerryGustafson
Occasional Contributor II

Steelers going to win tonight?

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Sadly, I'm an Eagles fan.

TerryGustafson
Occasional Contributor II

Oh boy.. I’m a cowboys fan so your welcome for Murray..

Thanks for the help on the script..

0 Kudos
TerryGustafson
Occasional Contributor II

I spoke to soon..  I have attached another sample..  I have a table that has multiple routes as shown so would want to build an array of the routes?  then populate the fields?

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Though I swore to never help a Cowboys fan, try the following:

fc = "Sample"

rmList = []

with arcpy.da.SearchCursor(fc, ["ROUTE_ID"]) as cursor:
    for row in cursor:
        route = row[0].split("_")[0]
        rmList.append(route)

del cursor

rmList.sort()
rmList = set(rmList)

for RM in rmList:
    newList = []
    with arcpy.da.UpdateCursor(fc, ["ROUTE_ID"], "ROUTE_ID LIKE '" + str(RM) + "%'")  as cursor:
        for row in cursor:            
            newList.append(row[0].split("_")[1])
    del cursor

    newList.sort()
    
    firstTime = True
    for val in newList:
        with arcpy.da.UpdateCursor(fc, ["ROUTE_ID", "SHAPE_LEN", "beg_seg", "end_seg"], "ROUTE_ID = '" + str(RM) + "_" + str(val) + "'")  as cursor:
            for row in cursor:
                if firstTime:
                    row[2] = 0
                    length = row[1]
                    row[3] = length                
                    cursor.updateRow(row)
                    firstTime = False
                else:
                    row[2] = length
                    newLength = row[1]
                    row[3] = length + newLength                
                    cursor.updateRow(row)
                    length = row[3]
del cursor