arcpy.mapping.AddLayer (SDE Feature Class)

1818
7
Jump to solution
04-17-2013 11:31 AM
JamesCrandall
MVP Frequent Contributor
When adding an ArcSDE Feature Class to an .mxd, it only shows the first 3 letters of the name in the TOC. 

     arcpy.env.workspace = "C:\\Users\\me\\AppData\\Roaming\ESRI\\Desktop10.1\\ArcCatalog\\mysdeserver.sde"     sdelist = arcpy.ListFeatureClasses()     for sdeFC in sdelist:         ## locate the desired fc and add it to the TOC         if sdeFC == 'GIS.THE_FEATURE_CLASS_NAME':             sdeLyr = arcpy.mapping.Layer(sdeFC)             arcpy.mapping.AddLayer(df, sdeLyr, "BOTTOM")             arcpy.mapping.AddLayer             arcpy.RefreshTOC()        


The above code adds the feature class but it adds it with the name "GIS".  Am I completely using the wrong method (I don't see any other way).  Solution?  Comments?


Thanks!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
I think you want to use MakeFeatureLayer and add that as a layer object to your map.

http://resources.arcgis.com/en/help/main/10.1/index.html#//00170000006p000000

View solution in original post

0 Kudos
7 Replies
MathewCoyle
Frequent Contributor
I think you want to use MakeFeatureLayer and add that as a layer object to your map.

http://resources.arcgis.com/en/help/main/10.1/index.html#//00170000006p000000
0 Kudos
JamesCrandall
MVP Frequent Contributor
I think you want to use MakeFeatureLayer and add that as a layer object to your map.

http://resources.arcgis.com/en/help/main/10.1/index.html#//00170000006p000000


Ah.  Okay that works (sort of).  I get an error if I attempt to set the same fully qualified Feature Class name as it is in SDE.  This fails:

if sdeFC == 'GIS.THE_FEATURE_CLASS_NAME':
   sdeLyr = arcpy.mapping.Layer(sdeFC)
   arcpy.MakeFeatureLayer_management(sdeFC, "GIS.THE_FEATURE_CLASS_NAME")



why would I want to add something to the map display that is NOT named what it is stored as?

Also: Why is it "MakeFeatureLayer"?  This is not very descriptive of "Adding a Feature Class to the TOC"!  Lol...
0 Kudos
MathewCoyle
Frequent Contributor
I'm not sure why you would have an issue with using the qualified name. I don't get any error when using the same layer name as the feature class name. How are you creating your mxd and df objects?
0 Kudos
JamesCrandall
MVP Frequent Contributor
I'm not sure why you would have an issue with using the qualified name. I don't get any error when using the same layer name as the feature class name. How are you creating your mxd and df objects?



def onRectangle(self, rectangle_geometry):
  mxd = arcpy.mapping.MapDocument("CURRENT")
  df = arcpy.mapping.ListDataFrames(mxd)[0]


0 Kudos
MathewCoyle
Frequent Contributor
That all seems fine, what is the error message you get?
0 Kudos
JamesCrandall
MVP Frequent Contributor
That all seems fine, what is the error message you get?



Didn't notice this until now, but arcgisscripting????  What?  That is very odd.

arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000733: Output Layer: Same as input C:\\Users\\me\\AppData\\Roaming\ESRI\\Desktop10.1\\ArcCatalog\\mysdeserver.sde\GIS.THE_FEATURE_CLASS_NAME
Failed to execute (MakeFeatureLayer)

From ArcGIS 10.1 Help:

"000733 : <value>: Same as input <value>.
Description
The input and output have been given the same name.

Solution
To prevent overwriting your input during the operation, change the output name to be unique from the input."
0 Kudos
JamesCrandall
MVP Frequent Contributor
** Changes applied and resolved **

Thanks again for your suggestions.  I wasn't completely sold on the idea of using the MakeFeatureLayer_management function to load the SDE Feature Class and I revisted my initial approach.  So, this actually did work at first ("sort of"):

arcpy.mapping.AddLayer(df, theFC, "BOTTOM") 


The problem is that when it loaded into the TOC, it was named only with the first couple of characters of the full FC datasource. After thinking about it, I figured that maybe I could just simply rename it to the full name after it loads.  And that seems to work for my needs -- just reseting the .name property is fine:


arcpy.env.workspace = "C:\\Users\\me\\AppData\\Roaming\ESRI\\Desktop10.1\\ArcCatalog\\mysdeserver.sde"
    sdelist = arcpy.ListFeatureClasses()
    for sdeFC in sdelist:
        ## locate the desired fc and add it to the TOC
        if sdeFC == 'GIS.THE_FEATURE_CLASS_NAME':
            sdeLyr = arcpy.mapping.Layer(sdeFC)
            arcpy.mapping.AddLayer(df, sdeLyr, "BOTTOM")
            arcpy.mapping.AddLayer
            ## reset the fc's name after it is added to the TOC
            arcpy.mapping.ListLayers(mxd)[0].name = "GIS.THE_FEATURE_CLASS_NAME"
            arcpy.RefreshTOC()

0 Kudos