Copyfeatures_management with joined table Q

879
8
Jump to solution
01-29-2013 12:18 PM
TonyAlmeida
Occasional Contributor II
I need some help on a script that i am putting together.
Script is as follows.

1. Select feature
2. Export the selected feature
3. Load the export feature
4. Label the feature

I have a shapefile with a joined table in Arcmap. The script runs fine until it gets to the labeling part. I get back Acres filed does not exist to lable. I check the "NewFeatures" layer and i noticed that the attributes fields names were named after the feature class name like vector_D_3, Vector_D4, etc.. and not the actual tables names.

I have attached a picture of what the tables looks after the copyfeatures_management.

Here is my code.
import arcpy,os, sys import arcpy.mapping # Set workspace arcpy.env.workspace = "C:/Temp" arcpy.env.overwriteOutput = 'True'  directory="C:/Temp/Split.pdf" if os.path.exists(directory):     try:         os.remove(directory)     except:         print "Exception: ",str(sys.exc_info()) else:     print 'File not found at ',directory  #arcpy.RefreshActiveView()  mxd = arcpy.mapping.MapDocument("current") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] lyr = arcpy.mapping.ListLayers(mxd, "TaxParcels")[0] df.extent = lyr.getSelectedExtent() df.scale = 5000  if int(arcpy.GetCount_management("TaxParcels").getOutput(0)) > 0:          #arcpy.CopyFeatures_management("TaxParcels", "C:/temp/NewFeatures.shp")     arcpy.Select_analysis("TaxParcels", "C:/temp/NewFeatures.shp")  #Adding Labels layer = arcpy.mapping.ListLayers(mxd, "NewFeatures")[0]  if layer.supports("LABELCLASSES"):     for lblclass in layer.labelClasses:         lblclass.className = "vector_D_1" #This should be Account         lblclass.expression = "[vector_D_1] & VBNewLine & Round([vector_D_5],2)" #these should be "ACCOUNT" & "ACRES"     lblclass.showClassLabels = True layer.showLabels = True  arcpy.RefreshActiveView()  arcpy.mapping.ExportToPDF(mxd,"C:/Temp/Split.pdf", "")
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
Yeah, I am not exactly sure of the string formatting they use when automatically truncating fields.  The way the qualified fields work is "tableName_Field".  Therefore using shapefiles is risky business here since it is likely just the fc/table name will take up all or most of the 10 character field name limit. 

So do all these fields start out DBO (or does the shapefile name start out vector_DBO)?  If that is the case it will have to do the vector_D_1.... and so on because the field names would all be the same if they were just truncated to just the first 10 characters.  By changing the qualified fields to false  or exporting to a gdb feature class should fix the problem.  I think the gdb is the best option though because the field names can be up to 64 characters, usually allowing you to maintain fully qualified field names.

View solution in original post

0 Kudos
8 Replies
by Anonymous User
Not applicable
I need some help on a script that i am putting together.
Script is as follows.

1. Select feature
2. Export the selected feature
3. Load the export feature
4. Label the feature

I have a shapefile with a joined table in Arcmap. The script runs fine until it gets to the labeling part. I get back Acres filed does not exist to lable. I check the "NewFeatures" layer and i noticed that the attributes fields names were named after the feature class name like vector_D_3, Vector_D4, etc.. and not the actual tables names.

I have attached a picture of what the tables looks after the copyfeatures_management.

Here is my code.
import arcpy,os, sys
import arcpy.mapping
# Set workspace
arcpy.env.workspace = "C:/Temp"
arcpy.env.overwriteOutput = 'True'

directory="C:/Temp/Split.pdf"
if os.path.exists(directory):
    try:
        os.remove(directory)
    except:
        print "Exception: ",str(sys.exc_info())
else:
    print 'File not found at ',directory

#arcpy.RefreshActiveView()

mxd = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "TaxParcels")[0]
df.extent = lyr.getSelectedExtent()
df.scale = 5000

if int(arcpy.GetCount_management("TaxParcels").getOutput(0)) > 0:
   

    #arcpy.CopyFeatures_management("TaxParcels", "C:/temp/NewFeatures.shp")
    arcpy.Select_analysis("TaxParcels", "C:/temp/NewFeatures.shp")

#Adding Labels
layer = arcpy.mapping.ListLayers(mxd, "NewFeatures")[0] 
if layer.supports("LABELCLASSES"):
    for lblclass in layer.labelClasses:
        lblclass.className = "vector_D_1" #This should be Account
        lblclass.expression = "[vector_D_1] & VBNewLine & Round([vector_D_5],2)" #these should be "ACCOUNT" & "ACRES"
    lblclass.showClassLabels = True
layer.showLabels = True

arcpy.RefreshActiveView()

arcpy.mapping.ExportToPDF(mxd,"C:/Temp/Split.pdf", "")


This is because you are exporting to shapefiles.  Shapefiles have a limit of 10 characters for field names (just like regular dbase files).  By default, joined tables use qualified field names when exported into a new feature class or table.  You can change this in the environment settings:

arcpy.env.qualifiedfieldNames = False


If you want to ensure that you keep the full field names you should export the new FC's into a geodatabase.  Here's some more info on shapefiles:

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Geoprocessing_considerations_for_shape...
0 Kudos
TonyAlmeida
Occasional Contributor II
I would have thought the fields name would have been truncated like when you right click a layer that has a joined table and export data.
0 Kudos
by Anonymous User
Not applicable
Yeah, I am not exactly sure of the string formatting they use when automatically truncating fields.  The way the qualified fields work is "tableName_Field".  Therefore using shapefiles is risky business here since it is likely just the fc/table name will take up all or most of the 10 character field name limit. 

So do all these fields start out DBO (or does the shapefile name start out vector_DBO)?  If that is the case it will have to do the vector_D_1.... and so on because the field names would all be the same if they were just truncated to just the first 10 characters.  By changing the qualified fields to false  or exporting to a gdb feature class should fix the problem.  I think the gdb is the best option though because the field names can be up to 64 characters, usually allowing you to maintain fully qualified field names.
0 Kudos
TonyAlmeida
Occasional Contributor II
Thank you for the response and help.

i have added the arcpy.qualifiedfieldNames = False and i am still getting the same results for the NewFeatures table, it's not truncated the fields.

import arcpy,os, sys
import arcpy.mapping
# Set workspace
arcpy.env.workspace = "C:/Temp"
arcpy.env.overwriteOutput = 'True'
arcpy.env.qualifiedfieldNames = False

directory="C:/Temp/Split.pdf"
if os.path.exists(directory):
    try:
        os.remove(directory)
    except:
        print "Exception: ",str(sys.exc_info())
else:
    print 'File not found at ',directory

#arcpy.RefreshActiveView()

mxd = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "TaxParcels")[0]
df.extent = lyr.getSelectedExtent()
df.scale = 5000

if int(arcpy.GetCount_management("TaxParcels").getOutput(0)) > 0:
   

    #arcpy.CopyFeatures_management("TaxParcels", "C:/temp/NewFeatures.shp")
    arcpy.Select_analysis("TaxParcels", "C:/temp/NewFeatures.shp")

#Adding Labels
layer = arcpy.mapping.ListLayers(mxd, "NewFeatures")[0] 
if layer.supports("LABELCLASSES"):
    for lblclass in layer.labelClasses:
        lblclass.className = "vector_D_1" #This should be Account
        lblclass.expression = "[vector_D_1] & VBNewLine & Round([vector_D_5],2)" #these should be "ACCOUNT" & "ACRES"
    lblclass.showClassLabels = True
layer.showLabels = True

arcpy.RefreshActiveView()

arcpy.mapping.ExportToPDF(mxd,"C:/Temp/Split.pdf", "")
0 Kudos
T__WayneWhitley
Frequent Contributor
Probably just case-sensitivity issue, qualifiedFieldNames, surprised no error was thrown?
0 Kudos
MathewCoyle
Frequent Contributor
Thank you for the response and help.

i have added the arcpy.qualifiedfieldNames = False and i am still getting the same results for the NewFeatures table, it's not truncated the fields.

import arcpy,os, sys
import arcpy.mapping
# Set workspace
arcpy.env.workspace = "C:/Temp"
arcpy.env.overwriteOutput = 'True'
arcpy.env.qualifiedfieldNames = False

directory="C:/Temp/Split.pdf"
if os.path.exists(directory):
    try:
        os.remove(directory)
    except:
        print "Exception: ",str(sys.exc_info())
else:
    print 'File not found at ',directory

#arcpy.RefreshActiveView()

mxd = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "TaxParcels")[0]
df.extent = lyr.getSelectedExtent()
df.scale = 5000

if int(arcpy.GetCount_management("TaxParcels").getOutput(0)) > 0:
   

    #arcpy.CopyFeatures_management("TaxParcels", "C:/temp/NewFeatures.shp")
    arcpy.Select_analysis("TaxParcels", "C:/temp/NewFeatures.shp")

#Adding Labels
layer = arcpy.mapping.ListLayers(mxd, "NewFeatures")[0] 
if layer.supports("LABELCLASSES"):
    for lblclass in layer.labelClasses:
        lblclass.className = "vector_D_1" #This should be Account
        lblclass.expression = "[vector_D_1] & VBNewLine & Round([vector_D_5],2)" #these should be "ACCOUNT" & "ACRES"
    lblclass.showClassLabels = True
layer.showLabels = True

arcpy.RefreshActiveView()

arcpy.mapping.ExportToPDF(mxd,"C:/Temp/Split.pdf", "")


It is arcpy.env.qualifiedFieldNames not arcpy.env.qualifiedfieldNames. Case is important.
0 Kudos
T__WayneWhitley
Frequent Contributor
That was a simple mistake....would you please mark Caleb's post as answered - because it was actually his answer that solved your orig problem.  We just pitched in to help.

...working on your other additional question now...
0 Kudos
by Anonymous User
Not applicable
Oops! Sorry Tony!  I did not realize I did not have the correct capitalization for the qualifiedFieldNames environment setting.  Good eye Wayne and Matt!  It would be nice if it did throw an exception when there is a typo to let you know it is not a valid env setting.
0 Kudos