Unique file or feature class name:
def UniqueFileName(Location = "in_memory", Name = "file", Extension = ""): """ returns an unused file name 'Location' will be the file folder 'Name' will be the file name plus numeric extension if needed 'Extension' is the file extension similar to arcpy.CreateUniqueName """ import arcpy import os if Extension: outName = os.path.join (Location, Name + "." + Extension) else: outName = os.path.join (Location, Name) i = 0 while arcpy.Exists (outName): i += 1 if Extension: outName = os.path.join (Location, Name + "_" + str(i) + "." + Extension) else: outName = os.path.join (Location, Name + "_" + str(i)) return outNameUnique feature layer name:
def UniqueLayerName(inFC = None, sql = None): """ returns an unused feature layer name """ import arcpy lyrName = "lyr0" i = 0 while arcpy.Exists (lyrName): i += 1 lyrName = "lyr" + str(i) if inFC: arcpy.MakeFeatureLayer_management (inFC, lyrName, sql) return lyrNameUnique field name:
def UniqueFieldName(inFC, inField = "NEWFIELD"): """ returns an unused field name """ import arcpy Type = arcpy.Describe (inFC).dataType if Type == "ShapeFile": inField = inField[:10] fields = [f.name for f in arcpy.ListFields (inFC)] i = 0 outField = inField while outField in fields: i += 1 outField = inField + "_" + str(i) q = 0 while Type == "ShapeFile" and len (outField) > 10: q -= 1 outField = inField[:q] + "_" + str(i) return outField
No comments:
Post a Comment