2

I am looking for a way to loop through all the feature classes (feature layers) within a mxd, to then use some geoprocessing tools on them. Before I run these geoproccesses, the script needs to change these feature classes to a certain workversion.

I have the script working the way I want except for groups or feature datasets. With the looping it thinks that these group names or feature dataset names are a layer of their own and therefore tries to change to a workversion, needless to say the script errors and I am left with no data being changed.

For grouped feature classes this is rather easy, remove the group. However this doesn't work for feature datasets.

My question is: Is there a way to "ungroup" feature datasets or better yet, skip the names of groups/feature datasets all together in the script?

My script as it is now:

import arcpy
import arcpy.mapping
Project_number = arcpy.GetParameterAsText(0)
arcpy.env.overwriteOutput = True
inWorkspace = "G:/[email protected]"
parentVersion = "ZN_KGD_DIENSTVERSIE"
# Execute CreateVersion
arcpy.CreateVersion_management(inWorkspace, parentVersion, "ExPimp_{}".format(Project_number), "PUBLIC")
mxd = arcpy.mapping.MapDocument("G:\\zn\\NM\\GGB\\Applicaties-Admin\\Kerngis\\ExpImp\\{}\\{}.mxd".format(Project_number, Project_number)) # Uses your currently open MXD
for df in arcpy.mapping.ListDataFrames(mxd, ''): # Loop through dataframes
 for lyr in arcpy.mapping.ListLayers(mxd, '', df): # Loop through layers
 arcpy.ChangeVersion_management (lyr.name,
 "TRANSACTIONAL",
 "ZN_KGD.Export_{}".format(Project_number))

I am working with ArcMap 10.3.1 I have tried the solution given below on all the possible ways I can imagen, with no luck. As my knowledge of python/arcpy is very limited at best, would it be possible to explain the thought process behind it?

asked Feb 25, 2020 at 15:38

1 Answer 1

2

You may use the isGroupLayer property to identify group layers to ignore them. To ignore feature dataset layers you could use the dataSource property to evaluate the layer path and ignore those that have an extra level deep being the feature dataset layers inside the database.

mxd = arcpy.mapping.MapDocument("G:\\zn\\NM\\GGB\\Applicaties-Admin\\Kerngis\\ExpImp\\{}\\{}.mxd".format(Project_number, Project_number)) # Uses your currently open MXD
for df in arcpy.mapping.ListDataFrames(mxd, ''): # Loop through dataframes
 for lyr in arcpy.mapping.ListLayers(mxd, '', df): # Loop through layers
 if not lyr.isGroupLayer:
 fcString = lyr.dataSource.split('.gdb\\')[1]
 fcOnly = fcString.find('\\')
 if fcOnly == -1:
 arcpy.ChangeVersion_management (lyr.name,
 "TRANSACTIONAL",
 "ZN_KGD.Export_{}".format(Project_number))
answered Feb 25, 2020 at 16:33
2
  • I am still rather new to python/Arcpy, would this ignore the entire group/feature dataset or would it just ignore the group name but still take the feature classes within the group? The main thing that I am trying to "ignore" is the feature dataset name called "KGD_ZN.Topologie" while still looping throuhg the feature layers within it. As the code is now it comes back with errors. I have tried replacing the ('.gdb\\') with ('KGD_ZN.Topologie\\'), but I am not sure if that is correct. Commented Feb 26, 2020 at 9:41
  • The solution suggested by artwork21 only ignores the actual group layer, but applies the rest of the code to all non-group layers, no matter if they are inside a group layer or not. Commented Sep 4, 2020 at 12:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.