Select to view content in your preferred language

calculateing fields with python script

640
1
04-14-2010 06:29 AM
JeremyTibbetts
Emerging Contributor
Hello, I have a script that reads a line, divides the line based on a values from a text file, and creates point features at the divisions. Now what I want to do is add fields and calculate the fields are the points are being created. But i'm not sure how it would be done.
I was thinking of creating a function, with the multiple calculate field statements in it, and then call the function as the points are being created.... below is my code..

#Import native arcgisscripting module
#
import arcgisscripting, os

# Create the geoprocessor object
#
gp = arcgisscripting.create(9.3)
pnt = gp.CreateObject("Point")
gp.workspace = "H:\Nova Scotia Geomatics Centre Project\Index_Photos.gdb"

Rollnumber = gp.GetParameter(0)
Flightline = gp.GetParameter(1)
Denom_Table = gp.GetParameterAsText(2)
SourceData = gp.GetParameterAsText(3)
OutSource = gp.GetParameterAsText(4)
Photocentre_Start = gp.GetParameter(5)

int(Photocentre_Start)
print "This is the photocentre startpoint" + Photocentre_Start
# Load required toolboxes
#
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")

# Local variables
#
Photocentres_Flightline_ = "H:\\Nova Scotia Geomatics Centre Project\\Index_Photos.gdb\\Photocentres_Flightline_"
Index_Photos_gdb = "H:\\Nova Scotia Geomatics Centre Project\\Index_Photos.gdb"


# Process: Create Feature Class
#
gp.CreateFeatureclass_management(Index_Photos_gdb, ("Photocentres_Flightline_" + Rollnumber + "_" + Flightline), "POINT", "", "DISABLED", "DISABLED", "", "", "0", "0", "0")

# Add Fields
gp.AddField_management((OutSource + Rollnumber + "_" + Flightline), "DLCODE", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
gp.AddField_management((OutSource + Rollnumber + "_" + Flightline), "PhotoID", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")[/SIZE]
... There are many more fields but left them out for simplicity...

# Indentify the geometry field
#
desc = gp.Describe(SourceData)
shapefieldname = desc.ShapeFieldName

# Create search cursor and insert cursor
#
inrows = gp.SearchCursor(SourceData)
inrow = inrows.next()
outrows = gp.InsertCursor(OutSource + Rollnumber + "_" + Flightline)

# Assign the denominator table to the varible infile1
#
infile1 = Denom_Table

#check if the file exists
#if it does not exist, a print statement that tells the user
#
if os.path.exists(infile1) is True:
    infile = open(infile1, "r")
    #print "Demoninators for flightline  + outfc")

else:
    print "The file was not found"

# While loop for each feature
#
while inrow:
    # Create the geometry object 'feat'

    features = inrow.GetValue(shapefieldname)

    # Create a variable called feature class, based on the feature class which contains the line segements
    #
    featureclass = (SourceData)

    #print "Feature " + str(inrow.getvalue(desc.OIDFieldName)) + ":"

    #Collect the coordinate for a feature
    #

    #Assign the coordinate to a variable
    #
    x1 = inrow.Shape.FirstPoint.X
    y1 = inrow.Shape.FirstPoint.Y
    x2 = inrow.Shape.LastPoint.X
    y2 = inrow.Shape.LastPoint.Y

    #print "first coordinate is:", x1 ,",", y1
    #print "second coordinate is:", x2 ,",", y2

    # The user inputs what the line should be divided by
    # This number is assigned to the variable den (denominator)
    # Den, then has to be converted to an integer.
    #
    current_record = infile.readline()    
    den = int(current_record)

    # Based on input, the equation used to find new coordinates will differ
    #
    if den == 2:
       
        # If user inputs 2, this formula is used
        #
        x3 = (x2 + x1) / 2
        y3 = (y2 + y1) / 2
        #print "new photocentre's coordinates are:", x3, ",",  y3

        # Use coordinates to create a point feature
        #
        pnt.X = x1
        pnt.Y = y1

        # Insert the point feature into the feature class
        #
        inrow = outrows.NewRow()
        inrow.shape = pnt
        outrows.InsertRow(inrow)
        outrows.NewRow
        I would like to have a function be called here, after the point has been created to populate the previously added fields with values.

        # Use coordinates to create a point feature in the feature class
        #
        pnt.X = x3
        pnt.Y = y3
       
        # Insert the point feature into the feature class
        #
        inrow = outrows.NewRow()
        inrow.shape = pnt
        outrows.InsertRow(inrow)
        outrows.NewRow
         Same here.. and so on
    
    elif den == 3:
        # If user inputs 3, this formula is used
        #
        x3 = x1 + ((x2 - x1) / 3)
        y3 = y1 + ((y2 - y1) / 3)
        x4 = x1 + ((x2 - x1) / 3)*2
        y4 = y1 + ((y2 - y1) / 3)*2
        #print "new photocentre's coordinates are:", x3, ",", y3
        #print "new photocentre's coordinates are:", x4, ",", y4
       
        # Use coordinates to create a point feature
        #
        pnt.X = x1
        pnt.Y = y1

        # Insert the point feature into the feature class
        #
        inrow = outrows.NewRow()
        inrow.shape = pnt
        outrows.InsertRow(inrow)
        outrows.NewRow

       
        # Use coordinates to create a point feature
        #
        pnt.X = x3
        pnt.Y = y3
       
        # Insert the point feature into the feature class
        #
        inrow = outrows.NewRow()
        inrow.shape = pnt
        outrows.InsertRow(inrow)
        outrows.NewRow

        # Use coordinates to create a point feature
        #
        pnt.X = x4
        pnt.Y = y4
       
        # Insert the point feature into the feature class
        #
        inrow = outrows.NewRow()
        inrow.shape = pnt
        outrows.InsertRow(inrow)
        outrows.NewRow

    else:
        # If the user inputs anything other than 2 or 3 (ei. 1), this means no division is needed
        #

        # Use coordinates to create a point feature
        #
        pnt.X = x1
        pnt.Y = y1

        inrow = outrows.NewRow()
        inrow.shape = pnt
        outrows.InsertRow(inrow)
        outrows.NewRow


        #print "No Division Necessary"   

   
    inrow = inrows.next()

# Use coordinates to create a point feature
#
pnt.X = x2
pnt.Y = y2

# Insert the point feature into the feature class
#
inrow = outrows.NewRow()
inrow.shape = pnt
outrows.InsertRow(inrow)
#outrows.NewRow

infile.close()

del inrows


I'm relatively new to python, and i'm not sure exactly how to write the function i'd like to create or even if its the best way to calculate the fields as the point is being created..

Please help me out thanks!!
0 Kudos
1 Reply
BradPosthumus
Frequent Contributor
Since you're already using an insert cursor and creating new rows, you just need to set the attribute values of each new row before inserting it into the table. So you can change this:

inrow = outrows.NewRow()
inrow.shape = pnt
outrows.InsertRow(inrow)
outrows.NewRow

...to this:

inrow = outrows.NewRow()
inrow.shape = pnt
inrow.SetValue("DLCODE", <DLCODE value>)
inrow.SetValue("PhotoID", <PhotoID value>)

outrows.InsertRow(inrow)
#outrows.NewRow - no need for this line

Since this code repeats itself in your script, you could instead define it within a function (as you mentioned).

def addNewRow(outrows, pnt, dlcodeValue, photoIdValue):
[INDENT][INDENT]inrow = outrows.NewRow()
inrow.shape = pnt
inrow.SetValue("DLCODE", dlcodeValue)
inrow.SetValue("PhotoID", photoIdValue)
outrows.InsertRow(inrow)[/INDENT][/INDENT]

Drop this at the top of your code, and then call it like this:

..
..
..
# Use coordinates to create a point feature
#
pnt.X = x1
pnt.Y = y1

# Insert the point feature into the feature class
#
addNewRow(outrows, pnt, <DLCODE value>, <PhotoID value>)

# Use coordinates to create a point feature in the feature class
#
pnt.X = x3
pnt.Y = y3
..
..
..


Also, don't forget to delete your outrows variable at the end of the script (del outrows). Just in case.


Brad Posthumus
0 Kudos