Select to view content in your preferred language

Python Toolbox selected field

627
3
10-02-2021 03:01 AM
KhaledYousefMohammedAhmed
New Contributor

Hi,
I have a script that creates a new field and then chooses the field of diameter and runs an equation to convert it from millimeters to meters and then makes a Buffer
But there is a problem with the script that I don't know about
Note: This is my first try

My script 

Import arcpy

# Script arguments
Input_Feature == arcpy.GetParameterAsText(0)

Field == arcpy.GetParameterAsText(1)

Output_Feature == arcpy.GetParameterAsText(2)

New_Field == "Buf"

Expression == "{ }".format ((!Field!/1000)/2)

# Local variables:
Feature_2 = Input_Feature
Feature_3 = Feature_2

# Process: Add Field
arcpy.AddField_management(Input_Feature, "Buf", "FLOAT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Calculate Field
arcpy.CalculateField_management(Feature_2, "Buf", "Expression ", "PYTHON", "")

# Process: Buffer
arcpy.Buffer_analysis(Feature_3, Output_Feature, "Buf", Side_Type, End_Type, "NONE", "", Method)

0 Kudos
3 Replies
DanPatterson
MVP Esteemed Contributor

Input_Feature == arcpy.GetParameterAsText(0)

This is not an assignment statement, it is an equality check (==), I suspect you want to assign your parameters to variables so you need to use

Input_Feature = arcpy.GetParameterAsText(0)


... sort of retired...
KhaledYousefMohammedAhmed
New Contributor

First, thanks for your interest
Second, I have already tried this method and it did not work. The real problem is that the script is telling the program to choose a field from the input layer and enter this chosen field into the equation

0 Kudos
DanPatterson
MVP Esteemed Contributor

Is your script attached to a tool in Arctoolbox?

If not then you have to provide the values for the parameters either in the code or via the command line.

Input_Feature = r"C:\somefolder\some_gdb\input_featureclass"

Field = "Field_to_use"

Output_Feature = r"C:\somefolder\some_gdb\output_featureclass"

and the change from equality to assignment statements is correct.

If you intend to create a toolbox with a tool, see

A quick tour of creating tools with Python—ArcGIS Pro | Documentation

focus on the "custom" tools


... sort of retired...
0 Kudos