Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. # Import modules
  2. import arcpy
  3. import sys
  4. import traceback
  5.  
  6. # Set local variables
  7. indata = arcpy.GetParameterAsText(0)
  8. fieldName = "PrintKey_GIS"
  9.  
  10. try:
  11. # check if the PrintKey field exists
  12. if arcpy.ListFields(indata, fieldName): #if field exists, evaluates to true
  13. arcpy.AddMessage("PrintKey_GIS field exists")
  14.  
  15. else:
  16. # Add a new field of that name
  17. arcpy.AddField_management(indata, fieldName, "TEXT", "", "",25, "PrintKey_GIS" , "NULLABLE", "", "",)
  18. arcpy.AddMessage("PrintKey_GIS field does not exist")
  19.  
  20. except Exception as e:
  21. arcpy.AddMessage(arcpy.GetMessages())
  22.  
  23. print "Adding new field first time"
  24. arcpy.AddField_management(fc, "NewField", "TEXT")
  25. print "Adding new field second time"
  26. arcpy.AddField_management(fc, "NewField", "TEXT")
  27. print "Adding new field third time"
  28. arcpy.AddField_management(fc, "NewField", "TEXT")
  29.  
  30. Adding new field first time
  31. Adding new field second time
  32. Adding new field third time
  33.  
  34. Executing (Add Field): AddField C:Users<username>DocumentsArcGISDefault.gdbfc NewField TEXT # # # # NULLABLE NON_REQUIRED #
  35. Start Time: Tue Aug 21 11:01:38 2018
  36. WARNING 000012: NewField already exists
  37. Succeeded at Tue Aug 21 11:01:38 2018 (Elapsed Time: 0.08 seconds)
  38. Executing (Calculate Field): CalculateField C:Users<username>DocumentsArcGISDefault.gdbfc NewField 'blah' PYTHON #
  39. Start Time: Tue Aug 21 11:01:38 2018
  40. Succeeded at Tue Aug 21 11:01:38 2018 (Elapsed Time: 0.12 seconds)
  41.  
  42. if len(arcpy.ListFields(fc, fieldName)):
  43. # Field exists. Do something.
  44. else:
  45. # Field does not exist. Do something else.
  46.  
  47. import arcpy
  48. import os
  49.  
  50. arcpy.env.workspace = r"C:UsersSanjayDocumentsArcGISSimilarPoints by attribute in polygonmygdb.gdb" #Parameter1
  51.  
  52. def FieldExist(featureclass, fieldname): #* Start if function to check if field exist */
  53. fieldList = arcpy.ListFields(featureclass, fieldname)
  54.  
  55. fieldCount = len(fieldList)
  56.  
  57. if (fieldCount == 1):
  58. return True
  59. else:
  60. return False #* End of function to check if field exist */
  61.  
  62. datasets = arcpy.ListDatasets(feature_type='feature')
  63. datasets = [''] + datasets if datasets is not None else []
  64.  
  65. ListOfFieldNeeded = ["ID","Creator","checker","Approver"]#parameter2 #List of field to check if not exist add it.
  66. for Afield in ListOfFieldNeeded: #will check each fc in gdb and add field
  67. for ds in datasets:
  68. for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
  69. if not FieldExist(fc,Afield):
  70. #print "{} Not Exist in featureclass {}".format(Afield,fc)
  71. arcpy.AddField_management(fc,Afield,"TEXT")
  72. print "Field {} added in fc {}".format(Afield,fc) #Addmessage to print model builder environment
  73. #you can add your script validation using else or exception occures
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement