Select to view content in your preferred language

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

5309
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
1 Solution

Accepted Solutions
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.

View solution in original post

0 Kudos
12 Replies
by Anonymous User
Not applicable
Original User: ionara_wilson

I figured that you can't really convert a model to a script and think everything is going to work. When I converted the model to script, the calculate field process was referencing Selected_features but selected features was a variable that was equal to Selecting Features which meant nothing. I added the Selected Features variable after the select layer by location and made it equal to the Input_points_layer, which was a real layer created before and already selected. Now I just need to figure out how to get the value for the second parameter, which is just showing %value% and not the value I entered for that parameter.

# 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

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")
Selected_Features = Input_Points_Layer
# Process: Calculate Field
arcpy.CalculateField_management(Selected_Features, "EditField", "'%value%'", "PYTHON_9.3", "")

0 Kudos
TimDonoyou
Regular Contributor
hi there,

you should just be able to use value as the variable in the calculatefield tool without any % or '

you might need to use str() or int() depending on the field type

hope this helps

cheers

Tim
0 Kudos
by Anonymous User
Not applicable
Original User: ionara_wilson

I tried that but I got this error:

Executing: CalcFeatures "Feature Set" sdfgsdfgsdfg
Start Time: Mon Sep 30 16:20:07 2013
Running script CalcFeatures...

Traceback (most recent call last):
  File "D:\ArcGISData\ESRI_GEO_EX\calcfeatures_script.py", line 35, in <module>
    arcpy.CalculateField_management(Selected_Features, "EditField", value2, "PYTHON_9.3", "")
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\management.py", line 3129, in CalculateField
    raise e
ExecuteError: ERROR 000539: Error running expression: sdfgsdfgsdfg
Traceback (most recent call last):
  File "<expression>", line 1, in <module>
NameError: name 'sdfgsdfgsdfg' is not defined

Failed to execute (CalculateField).


Failed to execute (CalcFeatures).
Failed at Mon Sep 30 16:20:08 2013 (Elapsed Time: 1.00 seconds)

# -*- 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)
value2 = str(value)

# 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, "", "", "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")
Selected_Features = Input_Points_Layer
# Process: Calculate Field
arcpy.CalculateField_management(Selected_Features, "EditField", value2, "PYTHON_9.3", "")
0 Kudos
ChrisPedrezuela
Frequent Contributor
I think it should just be
arcpy.CalculateField_management(Selected_Features, "EditField", value, "VB", "")


I figured that you can't really convert a model to a script and think everything is going to work. When I converted the model to script, the calculate field process was referencing Selected_features but selected features was a variable that was equal to Selecting Features which meant nothing. I added the Selected Features variable after the select layer by location and made it equal to the Input_points_layer, which was a real layer created before and already selected. Now I just need to figure out how to get the value for the second parameter, which is just showing %value% and not the value I entered for that parameter.

# 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

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")
Selected_Features = Input_Points_Layer
# Process: Calculate Field
arcpy.CalculateField_management(Selected_Features, "EditField", "'%value%'", "PYTHON_9.3", "")

0 Kudos
by Anonymous User
Not applicable
Original User: ionara_wilson

No, that did not work
0 Kudos
TimDonoyou
Regular Contributor
just out of interest - why do you makefeaturelayer? and redefine selected_features?

i think you should be able to simplify your script to just:

import arcpy

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

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

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

# Process: Calculate Field
arcpy.CalculateField_management(Input_Points, "EditField", str(value), "PYTHON_9.3")

thanks

Tim
0 Kudos
by Anonymous User
Not applicable
Original User: Wayne_Whitley

Good work, Tim...I referred her look here again at her other thread.
0 Kudos
ionarawilson1
Deactivated User
Thank you Tim. When I run that script you provided I get an error though. Don't I have to have a layer instead of a feature class to do a selection?

Traceback (most recent call last):
  File "D:\ArcGISData\ESRI_GEO_EX\calcfeatures_script.py", line 22, in <module>
    arcpy.SelectLayerByLocation_management(Input_Points, "INTERSECT", Selecting_Features, "", "NEW_SELECTION")
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\management.py", line 6559, in SelectLayerByLocation
    raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000368: Invalid input data.
Failed to execute (SelectLayerByLocation).
0 Kudos
by Anonymous User
Not applicable
Original User: Wayne_Whitley

Ah, good catch, directly from the 10.1 help the 1st parameter which is the layer you are selecting from must be a feature layer:

"The input can be a layer in the ArcMap table of contents, or a layer created in ArcCatalog or in scripts using the Make Feature Layer tool. The input cannot be the path to a feature class on disk."

I think the 'select layer' or the layer/fc you are using to select with can be an fc...both can be a layer if I'm not mistaken - what's interesting is you can fetch layer references from your map, i.e. feed in layer objects.

So, try this:
import arcpy

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

# Local variables:
Selecting_Features = r' enter path to your selecting features '

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

# Process: Calculate Field
arcpy.CalculateField_management(Input_Points, "EditField", str(value), "PYTHON_9.3")


And if you want to define a 3rd input parameter to select a layer from the map for Selecting_Features, that should be an option available to you --- that's what you have to test to confirm.
0 Kudos