4

I am attempting to calculate point stationing (footage) along a pipeline at a set interval via python script, thing is I'm just now learning to code in Python, I come from a VBA programming background. In regards to this exercise I will be creating an empty file geodatabase, within this database I will be creating an empty feature class table. This feature class table will contain an empty field/column named, "Calculated Footage". My desired result is to populate the content of my feature clas table via Python script such as the ones below.

I have been successful in obtaining the desired results within a python list as shown below but when attempting to do so in a file geodatabase I seem to be at a lost. Simply put how do I go about applying the logic below to the "Calculated Footage" field in my feature class table? In other words how do I populate the "Calculated Footage" field with the result of my while loop? I'm simply trying to append, load or insert data into the field in question

B_Measure = 0; E_Measure = 5280
Curr_Measure = 0; Interval = 1000
My_List=[]

C_Measure = 0

# Body of the Main program
while abs(E_Measure-C_Measure)>Interval:
    if My_List==[]:
        My_List.append(C_Measure)
        print C_Measure
    else:
        C_Measure=C_Measure + Interval
        My_List.append(C_Measure)
        print C_Measure[/CODE]

I've done this exercise in Excel as well, using the following VBA Code.

Sub Calculate_Footage()
'Declaring variables
Dim X, Y, Beginning_Point, Ending_Point, Interval, Current_Point As Integer

'Assigning initial value to variables
 X = 2
 Y = 1
 Beginning_Point = 0
 Ending_Point = 5280
 Interval = 1000
 Current_Point = 0

'Assign Field/Column Name
Cells(1, 1).Value = "Calculated Footage"

'This While Loop will populate my "Calculated Footage" field
Do While (Ending_Point - Current_Point) > Interval
Current_Point = Current_Point + Interval
Cells(X, Y).Value = Current_Point

'Move to the next row but same column
X = X + 1
Loop

End Sub

Current Python Code # Import system modules import arcpy from arcpy import env

# Set workspace in which to create geodatabase
env.workspace = "C:/data"

# Set local variables
out_folder_path = "C:/data" 
out_name = "fGDB.gdb"

# Execute CreateFileGDB
arcpy.CreateFileGDB_management(out_folder_path, out_name)

# Set environment settings for Table in geodatabase
in_database = out_folder_path+"/"+out_name
table_Name = "Custom_Stationing"
arcpy.CreateFeatureclass_management(in_database, table_Name)

# Set workspace for Add Field
in_Table = in_database+"/"+table_Name

Field_Names=["Route_Id","Route_Code","Series","Measure","Calculated_Field"]
for n in Field_Names:
    arcpy.AddField_management(in_Table, n)

I would like to apply the following code sample to insert rows located hereINSert Cursor

2
  • I have to say, this problem is not well stated and has gotten far too convoluted. You start off by saying you want to "calculate point stationing (footage) along a pipeline at a set interval via python script". That is the real question, is it not? What does the data you are working with look like? Please frame your question within an answerable scope. All the ancillary stuff such as creating a geodatabase, adding fields, etc. really has nothing to do with the problem. Remove unrelated information so the focus is on the part you really need help with, or start separate questions for those.
    – blah238
    Commented Oct 31, 2012 at 4:10
  • @blah238 I agree with you the question is convoluted. I guess I stated everything I wrote so there would not be any confusion as to what I was trying to accomplish. I know have geodatabase , containing a feature class table with a couple of fields but no rows or records. For some of the fields in my Table I need to assign a constant values for others I need to calculate a value but in order to do any one of these I first need to add records or rows, right? If you can run my code incorporating the InsertCursor function sameple per the link above you might be able to duplicate my problem.
    – lemuel
    Commented Oct 31, 2012 at 12:29

1 Answer 1

1

You're better off using the Calculate Field tool in ArcGIS to achieve the results you are looking for since you can better specify linkages between each row in your table and the resulting value of "Calculated Footage".

The method (list/looping) you are using above is not the best programming practice for this job. Some information on field calculation fundamentals are available here. Likewise, here are some examples. Your best bet, if possible, will be to break down the calculation into a format which can be carried out across each row, for example: column4 + column 6 = "Calculated Footage".

Where possible, lists/looping should be avoided when populating tables as there is a lot of room for errors to creep in (e.g. missing/deleted FIDs). The Calculate Field tool, which can be scripted using Python is more reliable.

4
  • 3
    Calculate Field cannot insert rows, which sounds like what is being asked here. For that you could use Append followed by Calculate Field, or use an InsertCursor.
    – blah238
    Commented Oct 29, 2012 at 21:08
  • + 1 True - if rows are being inserted @blah238's solution is the way to go.
    – Radar
    Commented Oct 29, 2012 at 21:18
  • Thanks guys, I'll try your recommendation and let you know which one works best.
    – lemuel
    Commented Oct 30, 2012 at 0:17
  • Okay guys, I've been successful in creating my geodatabase, table and was also able to assign field names using a for loop. In regards to inserting rows into the table I've created I've not been sucessful at implimenting the InsetCursor function. Based on what I'm trying to accomplish I'm sure this is the function I need to impliment. Thank you in advance @blah238 and Radar. Also I've update my original post with my current Python Code exluding the Cursor script.
    – lemuel
    Commented Oct 31, 2012 at 3:27

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