AddField.py
import arcpy
#Check input parameters
arcpy.AddMessage("GetArgumentCount() = {0}".format(arcpy.GetArgumentCount()))
for i in range(arcpy.GetArgumentCount()):
arcpy.AddMessage("type(GetParameter({0})) = {1}".format(i, type(arcpy.GetParameter(i))))
arcpy.AddMessage("GetParameterAsText({0}) = {1}".format(i, arcpy.GetParameterAsText(i)))
inFeatureLayer = arcpy.GetParameter(0)
outFeatureClass = arcpy.GetParameter(1)
#Copy feature layer
arcpy.CopyFeatures_management(inFeatureLayer, outFeatureClass)
#Add field
arcpy.AddField_management(outFeatureClass, "field1", "DOUBLE", field_alias = "Field 1")
arcpy.AddField_management(outFeatureClass, "field2", "DOUBLE", field_alias = "Field 2")
fields = ["OID@", "SHAPE@X", "SHAPE@Y", "field1", "field2"]
#Enter a value in the field
with arcpy.da.UpdateCursor(outFeatureClass, fields) as cursor:
arcpy.AddMessage("Field before input")
for row in cursor:
arcpy.AddMessage(row)
row[3] = row[1]
row[4] = row[2]
cursor.updateRow(row)
del cursor
#Check the status after input
with arcpy.da.SearchCursor(outFeatureClass, fields) as cursor:
arcpy.AddMessage("Field after input")
for row in cursor:
arcpy.AddMessage(row)
del cursor
When executed as input with a feature layer containing two polygons.
GetArgumentCount() = 2
type(GetParameter(0)) = <class 'arcpy._mp.Layer'>
GetParameterAsText(0) =Test polygon
type(GetParameter(1)) = <class 'geoprocessing value object'>
GetParameterAsText(1) = D:\GIS\ArcGIS_Project\GeometryTest\GeometryTest.gdb\Test polygon_AddField
Field before input
[1, 134.32332180386206, 30.338120870687362, None, None]
[2, 144.42135929631993, 39.08930527833865, None, None]
Field after input
[1, 134.32332180386206, 30.338120870687362, 134.32332180386206, 30.338120870687362]
[2, 144.42135929631993, 39.08930527833865, 144.42135929631993, 39.08930527833865]
--Copy Features (https://pro.arcgis.com/en/pro-app/tool-reference/data-management/copy-features.htm) --Add Field (https://pro.arcgis.com/en/pro-app/tool-reference/data-management/add-field.htm)
Recommended Posts