Select to view content in your preferred language

Add Field then Set the Field Value?

2510
12
Jump to solution
07-30-2012 11:20 AM
RobertStewart
Deactivated User
I have a python script in which the end result is a single polygon in a file geodatabase that is added to the current map view. That part works fine.

What I want to do is create 2 new fields and set the values in those fields to be two parameters set in the original run of the tool (getparametersastext).

For example, would like to create two new fields: ZFile and Planner
Then set those fields to be equal to the parameters set manually at the start of the tool: Zfile and Planner

I've successfully created the fields, but I've had no success at setting the values for either.

As I mentioned, there is only one row in the dataset. I can set it manually, but the idea is to automate all of this.

Any points in the right direction are gratefully accepted.

Cheers!
Tags (2)
0 Kudos
12 Replies
ChrisSnyder
Honored Contributor
Okay one last stab... Note the #comments...

import arcpy
arcpy.SetProduct("ArcInfo") #or whatever lic level you are using
myFC = arcpy.GetParameterAsText(0) #IMPORTANT: In the toolbox GUI, this parameter should be a type of 'FeatureLayer'
var1 = arcpy.GetParameterAsText(1) #Toolbox type of 'String'
var2 = arcpy.GetParameterAsText(2) #Toolbox type of 'String'
arcpy.AddField_management(myFC, "FIELD1", "TEXT", "", "", "50")
arcpy.AddField_management(myFC, "FIELD2", "TEXT", "", "", "50")
updateRows = arcpy.UpdateCursor(myFC)
for updateRow in updateRows:
    updateRow.FIELD1 = var1
    updateRow.FIELD2 = var2
    updateRows.updateRow(updateRow)
del updateRow, updateRows
0 Kudos
RobertStewart
Deactivated User
I double checked what the Toolbox variables were set at, and they seem to be correct. However, it did make me think that perhaps I'm doing something wrong somewhere else that's causing this to fail.

The code works 'except' that last copy of the data into the fields. And that, after all this, is minor. Feel free to point out the mistakes etc if you feel like it, critical advice is always welcome. It's my first real attempt at python.

Thanks for all your help. I wouldn't have gotten as far as I did without your help.

Cheers!


import arcpy,
from arcpy import env
arcpy.SetProduct("ArcInfo")
#Setting the workspace
arcpy.env.workspace = "I:\GIS\Mapping_GIS.gdb"
try:
    # Overwrite pre-existing files
    arcpy.env.overwriteOutput = True

    # Get the input values from the toolbox
    strtnum = arcpy.GetParameterAsText(0)#street number
    strtnam = arcpy.GetParameterAsText(1)#street name
    ZFile = arcpy.GetParameterAsText(2)#This is a file number like Z-00-0000
    Planner = arcpy.GetParameterAsText(3) #Usually initials
        
    FileName = strtnam + "_" + strtnum
    SaveFile = "I:\\GIS\\Mapping_GIS.gdb\\" + FileName

    ParcelTempLayer = "I:\\GIS\\Mapping_GIS.gdb\\ParcelTempLayer"
    AMANDA_PROPERTY = "Database Connections\\Amanda Connection to pama.sde\\AMANDA.PROPERTY"
    SDE_Parcels = "Database Connections\\Planning Connection to ctbgis.sde\\SDE.Planning\\SDE.Parcels"

    print "variables set"

    #Sets the map document to be the current one open
    mxd = arcpy.mapping.MapDocument("CURRENT")
   
    print "mxd set. Creating ParcelTempLayer - a layer file of SDE_Parcels"

    #Makes a layer from the SDE_Parcels data saves it under the parceltemplayer variable
    arcpy.MakeFeatureLayer_management(SDE_Parcels, ParcelTempLayer)

    print "ParcelTempLayer created. Creating AMANDA TableView"

    #Makes a table view from the AMANDA property records
    PROPNAM = "PROPNAM"
    arcpy.MakeTableView_management(AMANDA_PROPERTY, PROPNAM, "")

    print "AMANDA TableView created. Joining Table to Layer"

    #Joins the SDE_Parcels and PROPNAM
    arcpy.AddJoin_management(ParcelTempLayer, "PCL_PIN", "PROPNAM", "PROPGISID1", "KEEP_COMMON")
    print "Joined. Now extracting selection"

    expression = "PROPHOUSE = '" + strtnum + "' AND PROPSTREET = '" + strtnam + "'"

    #selects data

    print "Using this: " + expression

    arcpy.Select_analysis(ParcelTempLayer, SaveFile, expression)

    df = arcpy.mapping.ListDataFrames(mxd, "PLOCMain")[0]
    addLayer = arcpy.mapping.Layer(SaveFile)
    arcpy.mapping.AddLayer(df, addLayer, "TOP")

    arcpy.AddField_management(addLayer,"ZFile", "TEXT", "", "", "15")
    arcpy.AddField_management(addLayer, "Planner", "TEXT", "", "", "5")

    arcpy.CalculateField_management(addLayer, "ZFile", "'" + ZFile + "'", "PYTHON")
    arcpy.CalculateField_management(addLayer, "Planner", "'" + Planner + "'", "PYTHON")
    
    updateRows = arcpy.UpdateCursor(addLayer)
    for updateRow in updateRows:
        updateRow.ZFile = ZFile
        updateRow.Planner = Planner
        updateRows.updateRow(updateRow)
    del updateRow, updateRows
    del mxd, addLayer

    print "Complete"

   
except Exception, e:

   import traceback, sys
   tb = sys.exc_info()[2]
   print "Line %i" % tb.tb_lineno
   print e.message

del strtnam
del strtnum
del ZFile
del Planner
del PROPNAM
#del PROPNUM

del ParcelTempLayer
del SaveFile
del FileName
del AMANDA_PROPERTY
del SDE_Parcels
print "variables deleted"


0 Kudos
RobertStewart
Deactivated User
That was confirmed.

Removing the try/except statements and dedenting it made the script work perfectly (using the updatecursor commands, NOT the calculatefield cammand).

Cheers!
0 Kudos