Select to view content in your preferred language

Python toolbox - check for nested group layers, and 2 more ?'s

4842
13
Jump to solution
10-07-2015 09:43 AM
Zeke
by
Honored Contributor

I have a python toolbox which crawls directories, or optionally a single mxd, and writes out various properties of the layers in them to a csv file, mostly interested in the data source. This works great, except...,.

  1. I check for group layers to skip those. The tool does skip top level group layers, but not group layers nested in other group layers. How can I skip those?
  2. I originally set a default location and file name for the output csv file, which the user can change. This works if the file doesn't already exist, but if it does, that parameter gets a big red X when the tool is run. The file is set to 'w', so I assumed it would just overwrite the existing file. Is it possible to do what I thought would happen?
  3. In the self.description in the __init__ function, I'd like to break the text on different lines and paragraphs. But no matter how many \n's I add, the tool ignores them when run, and collapses the text into one block. Is there a way to accomplish this?

Thanks, relevant code below.

for mxd_path in mxd_list:
    try:
        mxd = arcpy.mapping.MapDocument(mxd_path)
        dflist = arcpy.mapping.ListDataFrames(mxd)

        for df in dflist:
            for lyr in arcpy.mapping.ListLayers(mxd, '', df):
                if not lyr.isGroupLayer:
                    nm = lyr.name
                    ds = '-- X --'
                    if lyr.supports('dataSource'):
                        ds = lyr.dataSource
                    lyr_attributes = [mxd_path, nm, ds]
                    writer.writerow(lyr_attributes)
                else:
                    if not lyr.isGroupLayer:
                        arcpy.AddMessage('\nLayer {0} does not support the dataSource property'.format(nm))
                        unsupported.append(nm)
                        lngnm = lyr.longName
    except Exception as e:
        arcpy.AddError('EXCEPTION: {0}\t{1}\t{2}'.format(mxd_path, lngnm, e))
        continue
    writer.writerow([''])
    del mxd
0 Kudos
13 Replies
RebeccaStrauch__GISP
MVP Emeritus

​you need to be outside of the Geonet inbox...just open the message in a new tab.  When you reply, in the upper right, there should be a "used advanced editor". Select that, and then there should be an attach icon in lower right corner .

to do a direct message, if we follow each other, then there will be a message option for you if you view my profile. There is a pull down version usually if you hover over my avatar too, but I do t see that right now on my device.

0 Kudos
Zeke
by
Honored Contributor

Here you go.Didn't see an Advanced Editor on the direct message box.

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Thanks.  I'll take a look at it later this week.

Sorry for the confusion in the last message.  There isn't an attach on the DM.  That was if we needed to take it offline with direct emails.  (not enough coffee yet for a Monday morning)

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Hi Greg,

Unfortunately, I didn't have much luck with your tools. After I fixed the source tot he python script, I ran it thru the Utilities toolbox and it said it ran successfully, but the .csv was never created.

The PyUtilities tool created the .csv file, but never seemed to finish, so I ended up cancelling it.

So unfortunately, I can't use either tool directly, but I do like some of your streamlined coding.  Mine tends to get long, so I'm going to look to see if I can swipe any of your code to merge into mine.  Thanks for sharing.

0 Kudos