POST
|
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.
... View more
09-06-2016
05:38 AM
|
0
|
0
|
336
|
POST
|
So you were able to pass the output of your summary stats into the autoIncrement function?!? I would love to see the code; if I could make this work in my model it would greatly simplify my logic... Also, how did you track down the actual name of the summary stats table?
... View more
09-02-2016
01:03 PM
|
0
|
3
|
1902
|
POST
|
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
... View more
08-26-2016
06:14 AM
|
0
|
0
|
2238
|
POST
|
Pardon my ignorance, I am quite green in python. This is the way I am running the code I have tried to place the import both before and inside the recursive as well as placing your entire snippet outside and inside of the autoIncrement function, several variations have been unsuccessful, perhaps there is something obvious I am doing wrong: import arcpy
rec=0
def autoIncrement():
global rec
pStart = [i[0] for i in arcpy.da.SearchCursor("QueryTable_Statistics", "MAX_FEATURE_NUM")][0]
pInterval = 1
if (rec == 0):
rec = pStart
else:
rec = rec + pInterval
return rec
I am running this as Pre-logic script code in the Calculate field tool of my model. I was thinking the code would assume the environment of the model and dataset in which it is working but maybe I need to define an environment in the code?
... View more
08-24-2016
09:35 AM
|
0
|
5
|
1902
|
POST
|
EDIT: Since "Get Field Value" is a "model only tool", I think this is at least part of the reason my code is not running, python is not able to find the tool because to it (python) the tool does not exist... Maybe someone can confirm...
... View more
08-23-2016
01:55 PM
|
0
|
7
|
1902
|
POST
|
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
... View more
08-23-2016
01:36 PM
|
0
|
11
|
3524
|
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:25 AM
|