Is it possible to replace the pStart in the code with a variable? In other words I have a value that I need to start from however the code is part of a model and the value may change every time the model is run so I cannot simply replace the "1" with whatever the value is(I have tried this and it works) because the model will be run on many datasets with different values. Here is my logic, I need to pass a value into "pStart =," what I have tried is to define a function and pass that as the pStart value. so that pStart = getfieldvalue then auto increment from this value. I need to replace the "1" with the value that my model has found, here is my logic that does not work...
def getfieldvalue():
import arcpy
arcpy.ImportToolbox("Model Functions")
arcpy.GetFieldValue_mb("QueryTable_Statistics", "MAX_FEATURE_NUM", "String", "")
return
rec=0
def autoIncrement():
global rec
pStart = getfieldvalue(MAX_FEATURE_NUM)
pInterval = 1
if (rec == 0):
rec = pStart
else:
rec = rec + pInterval
return rec
Solved! Go to Solution.
Sounds like we are using similar logic, I am having trouble understanding the script, perhaps some of it is being cut off... If you click the "expand toolbar" button(three dots) right of "insert image(camera)" within the Reply dialogue on this thread a "More" button will appear, clicking on more, a drop-down will appear including "Syntax highlighter" by selecting, a new window will appear where you can copy and paste your code, select 'python' from the drop-down and add your complete script into the reply as it would appear in IDLE.
After a week of intensive Model Building we have come up with a solution, we did not need to use python except the autoIncrement() function in pre-logic code of our final Calculate Field tool seen in the model. The simple solution was to create a new integer field and populate every record in that field with the MAX value derived from the Summary Statistics tool then use a simple expression: autoIncrement + [MAXvalueField] we then iterate the entire function for every grid block and viola we have an incrementing field starting at the highest pre-existing number. Now that we have our incrementing value we concatenate a new field with that number and our area code(found by a separate model) and we have a unique identifier for every feature in our dataset. The script looks remarkably simple for such a long model...
# Import arcpy module
import arcpy
# Load required toolboxes
arcpy.ImportToolbox("Model Functions")
# Script arguments
Select_Feature_Class = arcpy.GetParameterAsText(0)
if Select_Feature_Class == '#' or not Select_Feature_Class:
Select_Feature_Class = "" # provide a default value if unspecified
# Local variables:
Feature_Layer = Select_Feature_Class
I_Feature_Layer_FEATURE_ID = Feature_Layer
QueryTable_Statistics = I_Feature_Layer_FEATURE_ID
Feature_Class_w_MAX = QueryTable_Statistics
FeatureClassNewNumber__2_ = Feature_Class_w_MAX
FeatureClassNewNumber = FeatureClassNewNumber__2_
SCleanoutApppended__2_ = FeatureClassNewNumber
NULL_FEATURENUM = I_Feature_Layer_FEATURE_ID
Value = Feature_Layer
scratch_gdb = ""
# Process: Make Feature Layer
arcpy.MakeFeatureLayer_management(Select_Feature_Class, Feature_Layer, "", scratch_gdb, "OBJECTID OBJECTID VISIBLE NONE;Shape Shape VISIBLE NONE;Enabled Enabled VISIBLE NONE;Location Location VISIBLE NONE;InstallContractor InstallContractor VISIBLE NONE;Condition Condition VISIBLE NONE;PrimaryImage PrimaryImage VISIBLE NONE;Size_ Size_ VISIBLE NONE;FACILITYID FACILITYID VISIBLE NONE;FEATURE_ID FEATURE_ID VISIBLE NONE;FEATURE_NUM FEATURE_NUM VISIBLE NONE;OLD_FACILITY_ID OLD_FACILITY_ID VISIBLE NONE;GPS_Date GPS_Date VISIBLE NONE;Datafile Datafile VISIBLE NONE;LifeCycleStatus LifeCycleStatus VISIBLE NONE;FACILITYIDAUTO FACILITYIDAUTO VISIBLE NONE;NewNumber NewNumber VISIBLE NONE;test test VISIBLE NONE")
# Process: Iterate Feature Selection
arcpy.IterateFeatureSelection_mb(Feature_Layer, "FEATURE_ID #", "true")
# Process: Select (2)
arcpy.Select_analysis(I_Feature_Layer_FEATURE_ID, NULL_FEATURENUM, "FEATURE_NUM ='0' AND FACILITYID IS NULL")
# Process: Summary Statistics
arcpy.Statistics_analysis(I_Feature_Layer_FEATURE_ID, QueryTable_Statistics, "FEATURE_NUM MAX", "FEATURE_ID")
# Process: Join Field
arcpy.JoinField_management(NULL_FEATURENUM, "FEATURE_ID", QueryTable_Statistics, "FEATURE_ID", "")
# Process: Calculate Field (2)
arcpy.CalculateField_management(Feature_Class_w_MAX, "test", "!MAX_FEATURE_NUM!", "PYTHON", "")
# Process: Calculate Field
arcpy.CalculateField_management(FeatureClassNewNumber__2_, "NewNumber", "autoIncrement()+ !test!", "PYTHON", "rec=0 \\ndef autoIncrement(): \\n global rec \\n pStart = 1\\n pInterval = 1 \\n if (rec == 0): \\n rec = pStart \\n else: \\n rec = rec + pInterval \\n return rec\\n")
# Process: Append
arcpy.Append_management("") #data paths