1

I have a geodatabase with several feature dataset and feature classes associated with it respectively. I would like to create a .mxd where the Table of contents consist of group layer and each group layer has several shapefile. The group layer name should be same as Feature dataset name.The feature classes in each feature dataset comes shapefiles with in that particular group layer as shown in figure.

enter image description here

PolyGeo
  • 65,136
  • 29
  • 109
  • 338

1 Answers1

2

The following code snippet works in ArcGIS 10.1+.

Note that creating a new mxd can't be done in arcpy, you need to reference an existing empty mxd. Also, I couldn't find a way of creating a new empty group layer in arcpy, so I created one in ArcMap and saved it to a .lyr file and used that in the code.

import os,arcpy

gdb=r'C:\TEMP\Default_1.gdb'
template_mxd = r'C:\Temp\empty.mxd'
template_group_layer = r'C:\Temp\empty_group.lyr'
output_mxd = r'C:\Temp\not_empty.mxd'

mxd=arcpy.mapping.MapDocument(template_mxd)
df = arcpy.mapping.ListDataFrames(mxd)[0]
groups={}
for path, dirs, files in arcpy.da.Walk(gdb):
    #Any "folder" in a GDB is a Feature Dataset 
    #and there can only be a single level
    for d in dirs: 
        #Add an empty group layer to ArcMap
        lyr=arcpy.mapping.Layer(template_group_layer)
        lyr.name=d
        arcpy.mapping.AddLayer(df,lyr)
        groups[d]=arcpy.mapping.ListLayers(mxd,d,df)[0]

    for f in files:
        fp=os.path.join(path,f)
        dsc=arcpy.Describe(fp)
        lyr=None
        view=None
        if dsc.dataType == 'FeatureClass':
            lyr=arcpy.management.MakeFeatureLayer(fp,os.path.basename(fp))[0]
        elif dsc.dataType == 'RasterDataset':
            lyr=arcpy.management.MakeRasterLayer(fp,os.path.basename(fp))[0]
        elif dsc.dataType == 'Table':
            view=arcpy.management.MakeTableView(fp,os.path.basename(fp))[0]
        else:continue


        if path==gdb and lyr:
            lyr.visible=False
            arcpy.mapping.AddLayer(df,lyr)
        elif view:
            arcpy.mapping.AddTableView(df, view)
        else:
            d=os.path.basename(path)
            arcpy.mapping.AddLayerToGroup(df, groups[d], lyr, "BOTTOM")

mxd.saveACopy(output_mxd)

Results from above mentioned code

user2856
  • 65,736
  • 6
  • 115
  • 196
  • http://gis.stackexchange.com/a/4684/115 agrees with your workaround for "creating a new empty group layer in arcpy". – PolyGeo Sep 25 '14 at 04:34
  • Luke, This script works great in importing all the feature classes from several different feature datasets of a geodatabase. But if you see the picture in the question, I have clearly mention that for each feature dataset in a geodatabase, i need to create a group layer and the feature classes of each feature dataset comes under each group layer as shown in the figure. As pet the image , I have created a empty group layer but still the feature does not came under the group layer. Can you please try at your end. –  Sep 30 '14 at 20:40
  • 2
    @PrashantKuranjekar Please note that you are undermining the usefulness of this page by asking the author to contact you in private. – underdark Sep 30 '14 at 20:54
  • Welcome to the site @Prashant Kuranjekar. Please note that I edited your comment to remove your private request for information. This site is an open forum and, therefore, all responses should be open and available to the public. Please read the information on posting questions at our help center: http://gis.stackexchange.com/help – Aaron Sep 30 '14 at 20:55
  • Fine with me....I just wanted to send him screen shots...Waiting for your response Luke!! –  Sep 30 '14 at 20:59
  • @prashant You can edit your question to post your screenshots. Though I can't see what the problem is, as the script worked fine when I tested it and added feature classes that are in feature datasets in my test gdb to the correct group layers in the ArcMap TOC. – user2856 Oct 02 '14 at 09:38
  • It does work Luke. But i do have a question - 1) How to get rid of "_layer1" from each layer? 2) How to get complete layer name in TOC. (See the boundary layer name got broken). What changes we need to make in the above mentioned code? I have attached the screen shot of the results from your code. –  Nov 24 '14 at 18:12
  • @prashant - ArcGIS 10.1 doesn't add the "_layer1" to the layer name so I didn't see that when I tested it, but I've since upgraded to 10.2 which does add "_layer1" to the layer name. I have edited my question to workaround (change arcpy.management.Make*(fp) to arcpy.management.Make*(fp,os.path.basename(fp))). This is why you should have answered the question about what version of ArcGIS you are running in my original comment below your question..... – user2856 Dec 03 '14 at 22:51
  • Thanks Luke!! It does work....Leant something new today!! Thx Again –  Dec 04 '14 at 21:16