POST
|
I really don't know why it doesn't work in this script while works in others... maybe the reason is in files themselves, maybe they are broken somehow or in a mix of feature classes and shapefiles. No idea. Anyway, I found a workaround for this problem based on the script sample from ArcGIS Recourse center (http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000004n000000):
from arcpy import env
try:
inFC = current+"\\SK_Poly"
updatedFC = scratch+"\\Test.shp"
desc = arcpy.Describe(inFC)
arcpy.CopyFeatures_management(inFC, updatedFC)
fieldObjList = arcpy.ListFields(updatedFC)
fieldNameList = []
keep = ["CID", "TYPE", "STATUS", "X", "Y"]
for field in fieldObjList:
if not field.required and field.name not in keep:
fieldNameList.append(field.name)
arcpy.DeleteField_management(updatedFC, fieldNameList)
except Exception, e:
# If an error occurred, print line number and error message
import traceback, sys
tb = sys.exc_info()[2]
print "Line %i" % tb.tb_lineno
print e.message
... View more
05-28-2014
11:43 AM
|
0
|
0
|
1237
|
POST
|
Same result... According to the layer properties all field names are correct. Also i noticed that field ALIASES in the output file have same names (without _1 for fields of the joined file at the end) while field NAMES of the joined file have _1 at the end. It looks like the code reads aliases instead of actual names. Is it possible?
... View more
05-28-2014
11:17 AM
|
0
|
0
|
1237
|
POST
|
This thread is quite old, but I still hope for getting some advice. I found posted codes very useful and applied their variation several times very successfully. I'm very grateful to you guys for sharing it. However, recently I faced with very strange behavior of the code. So, what i'm trying to achieve is to join shapefile with feature class and then delete all unnecessary fields except needed. Both input files has same field names so that after join the output file has fields like this: CID, TYPE, STATUS, X, Y, CID_1, TYPE_1, STATUS_1 etc. This is fine. But deleting unnecessary fields in the output doesn't work and I can't figure out why.
from arcpy import env
try:
env.qualifiedFieldNames = False
inFeature = scratch+"\\SK.shp"
inLayer = "SK_lyr"
joinFeature = current+"\\SK_Point"
joinLayer = "SK_Point_lyr"
inLayerField = "CID"
joinLayerField = "CID"
outFeature = current+"\\SK_Poly"
arcpy.MakeFeatureLayer_management(inFeature, inLayer)
arcpy.MakeFeatureLayer_management(joinFeature, joinLayer)
arcpy.AddJoin_management(inLayer, inLayerField, joinLayer, joinLayerField, "KEEP_ALL")
arcpy.CopyFeatures_management(inLayer, outFeature)
except Exception, e:
import traceback, sys
tb = sys.exc_info()[2]
print "Line %i" % tb.tb_lineno
print e.message
infc = current+"\\SK_Poly"
keep = ["CID", "TYPE", "STATUS", "X", "Y"]
try:
discard = []
for field in [f.name for f in arcpy.ListFields(infc) if f.type <> 'OID' and f.type <> 'Geometry']:
if field not in keep:
discard.append(field)
arcpy.DeleteField_management(infc, discard)
except:
arcpy.GetMessages(2)
... View more
05-28-2014
08:13 AM
|
0
|
0
|
1237
|
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:24 AM
|