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

  • 1
    What version of ArcGIS? – user2856 Sep 24 '14 at 23:28
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

  • gis.stackexchange.com/a/4684/115 agrees with your workaround for "creating a new empty group layer in arcpy". – PolyGeo Sep 25 '14 at 4: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. – user37082 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: 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!! – user37082 Sep 30 '14 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy