Select to view content in your preferred language

Model converted to Script -  Why Can't it find the field?

5311
12
Jump to solution
09-30-2013 12:50 PM
by Anonymous User
Not applicable
Original User: ionara_wilson

I have a model that an ESRI associate sent me as an example. When I run the model everything is great.
But I need to have it as a script so I converted it to a script and set the Selecting Features parameter to be a "Feature Set"  and have a schema of a polygon layer. However when I convert this model to a script and try to run as a script, I get an error saying it cannot find the EditField. However the EditField should have been copied to the layer in the Make Feature Layer process. It is not clear why it cannot find the EditField ! Any help?
The first picture shows the model,  the second shows the model when I run it, the third shows the error I get, and the fourth shows the parameters I set before I run the tool. Thank you!!!


[ATTACH=CONFIG]27890[/ATTACH][ATTACH=CONFIG]27891[/ATTACH]

[ATTACH=CONFIG]27892[/ATTACH][ATTACH=CONFIG]27893[/ATTACH]


# -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # calcfeatures_script.py # Created on: 2013-09-30 15:22:46.00000 #   (generated by ArcGIS/ModelBuilder) # Usage: calcfeatures_script <Selecting_Features> <value>  # Description:  # test # ---------------------------------------------------------------------------  # Import arcpy module import arcpy  # Script arguments Selecting_Features = arcpy.GetParameterAsText(0) if Selecting_Features == '#' or not Selecting_Features:     Selecting_Features = "in_memory\\{1CFDCCDD-5D8D-42F8-AEB2-E66ED4A51A44}" # provide a default value if unspecified  value = arcpy.GetParameterAsText(1)  # Local variables: Input_Points = "D:\\ArcGISData\\ESRI_GEO_EX\\GDB.gdb\\SamplePoints" Final_Output = value Selected_Features = Selecting_Features Input_Points_Layer = "SamplePoints_Layer"  # Process: Make Feature Layer arcpy.MakeFeatureLayer_management(Input_Points, Input_Points_Layer, "", "", "OBJECTID OBJECTID VISIBLE NONE;SymbologyField SymbologyField VISIBLE NONE;DomainField DomainField VISIBLE NONE;Shape Shape VISIBLE NONE;Subtype Subtype VISIBLE NONE;EditField EditField VISIBLE NONE")  # Process: Select Layer By Location arcpy.SelectLayerByLocation_management(Input_Points_Layer, "INTERSECT", Selecting_Features, "", "NEW_SELECTION")  # Process: Calculate Field arcpy.CalculateField_management(Selected_Features, "EditField", "'%value%'", "PYTHON_9.3", "") 
0 Kudos
12 Replies
ionarawilson1
Deactivated User
Thank you for your help Wayne. I still get an error when I try your last solution. I need the selecting features to be something selected by the user, because it will be digitized when the script runs. I am not sure why I am getting an error but I guess you have to refer to layers in your mxd and that would work great in that selection process, but I prefer not to rely on the mxd since this geoproceessing service will be used in a web app. Anyway, this (below) worked for me. It works when I run in ArcMap and I don't get any errors when I run the geoprocessing service, however the feature does not get updated when I run the geoprocessing service, just when I run the script inside arcmap and that's what I need to find out, why would that field not get updated when running the geoprocessing service?


Thank you so much for your help

# Script arguments
Selecting_Features = arcpy.GetParameterAsText(0)
value = arcpy.GetParameterAsText(1)


# Local variables:
Input_Points = "D:\\ArcGISData\\ESRI_GEO_EX\\GDB.gdb\\SamplePoints"
#Final_Output = value

Input_Points_Layer = "SamplePoints_Layer"

# Process: Make Feature Layer
arcpy.MakeFeatureLayer_management(Input_Points, Input_Points_Layer)
# Process: Select Layer By Location
arcpy.SelectLayerByLocation_management(Input_Points_Layer, "INTERSECT", Selecting_Features, "", "NEW_SELECTION")
Selected_Features = Input_Points_Layer
# Process: Calculate Field



with arcpy.da.UpdateCursor(Selected_Features, ("EditField")) as rows:
        # row comes back as a tuple in the order specified here, so Office is row[0], Forester is row[1]
        for row in rows:
            row[0] = value
           
            rows.updateRow(row)
0 Kudos
by Anonymous User
Not applicable
Original User: Wayne_Whitley

Yes, I see the error now...it's with the expression -- apparently (I always forget about this) it needs to be enclosed in quotes.
I included that at your other thread.  (Probably duplicate threads should be avoided in the future, okay?)

...so try this, substituting for the expression parameter [from my post above it is 'str(value)']:

'"' + value + '"'

All that does is add the quotes...let me know if that works.
0 Kudos
ionarawilson1
Deactivated User
You got it!!! Thank you so much, here is my code with your expression:

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# calcfeatures_script.py
# Created on: 2013-09-30 15:22:46.00000
#   (generated by ArcGIS/ModelBuilder)
# Usage: calcfeatures_script <Selecting_Features> <value> 
# Description: 
# test
# ---------------------------------------------------------------------------

# Import arcpy module
import os, sys, arcpy, traceback, arcgisscripting

# Script arguments
Selecting_Features = arcpy.GetParameterAsText(0)
value = arcpy.GetParameterAsText(1)


# Local variables:
Input_Points = "D:\\ArcGISData\\ESRI_GEO_EX\\GDB.gdb\\SamplePoints"
#Final_Output = value

Input_Points_Layer = "SamplePoints_Layer"

# Process: Make Feature Layer
arcpy.MakeFeatureLayer_management(Input_Points, Input_Points_Layer)
# Process: Select Layer By Location
arcpy.SelectLayerByLocation_management(Input_Points_Layer, "INTERSECT", Selecting_Features, "", "NEW_SELECTION")

# Process: Calculate Field

# Process: Calculate Field
arcpy.CalculateField_management(Input_Points_Layer, "EditField", '"' + value + '"', "PYTHON_9.3")
0 Kudos