AddField.py
import arcpy
#Vérifier les paramètres d'entrée
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)
#Copier la couche d'entités
arcpy.CopyFeatures_management(inFeatureLayer, outFeatureClass)
#Ajouter le champ
arcpy.AddField_management(outFeatureClass, "field1", "DOUBLE", field_alias = "Champ 1")
arcpy.AddField_management(outFeatureClass, "field2", "DOUBLE", field_alias = "Champ 2")
fields = ["OID@", "SHAPE@X", "SHAPE@Y", "field1", "field2"]
#Entrez une valeur dans le champ
with arcpy.da.UpdateCursor(outFeatureClass, fields) as cursor:
arcpy.AddMessage("Champ avant saisie")
for row in cursor:
arcpy.AddMessage(row)
row[3] = row[1]
row[4] = row[2]
cursor.updateRow(row)
del cursor
#Vérifiez l'état après l'entrée
with arcpy.da.SearchCursor(outFeatureClass, fields) as cursor:
arcpy.AddMessage("Champ après saisie")
for row in cursor:
arcpy.AddMessage(row)
del cursor
Lorsqu'il est exécuté en tant qu'entrée avec une couche d'entités contenant deux polygones.
GetArgumentCount() = 2
type(GetParameter(0)) = <class 'arcpy._mp.Layer'>
GetParameterAsText(0) =Polygone de test
type(GetParameter(1)) = <class 'geoprocessing value object'>
GetParameterAsText(1) = D:\GIS\ArcGIS_Project\GeometryTest\GeometryTest.gdb\Polygone de test_AddField
Champ avant saisie
[1, 134.32332180386206, 30.338120870687362, None, None]
[2, 144.42135929631993, 39.08930527833865, None, None]
Champ après saisie
[1, 134.32332180386206, 30.338120870687362, 134.32332180386206, 30.338120870687362]
[2, 144.42135929631993, 39.08930527833865, 144.42135929631993, 39.08930527833865]
Recommended Posts