Select to view content in your preferred language

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

3980
13
Jump to solution
10-07-2015 09:43 AM
Zeke
by
Regular Contributor III

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
1 Solution

Accepted Solutions
RebeccaStrauch__GISP
MVP Emeritus

Greg, for what it's worth, check out my Python addin for data inventory and “broken-link” repair.

I've had to deal with a lot of the same issues you had with groups, etc.  To keep my files unique, I append the name with a datetime.  I also write out to a .csv and a report, with an option for a FGDB (but never found a reason for it for my broken-link list).

I had some other tools on this toolbar, but decided to simplify is for sharing.

View solution in original post

13 Replies
RebeccaStrauch__GISP
MVP Emeritus

Greg, for what it's worth, check out my Python addin for data inventory and “broken-link” repair.

I've had to deal with a lot of the same issues you had with groups, etc.  To keep my files unique, I append the name with a datetime.  I also write out to a .csv and a report, with an option for a FGDB (but never found a reason for it for my broken-link list).

I had some other tools on this toolbar, but decided to simplify is for sharing.

Zeke
by
Regular Contributor III

Thanks Rebecca, will take a look after lunch!

0 Kudos
Zeke
by
Regular Contributor III

Rebecca,

I'm able to use the tools from the toolbox, but the toolbar doesn't appear properly (image below). The tools themselves don't from there either, so it doesn't seem like links to the icon images being broken is the problem. Any ideas? Thanks

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Greg, you didn't attach an image to your last reply.

Are you missing the tool buttons on my addin or on yours?

BTW - I'm working on a new version on my addin that exits cleaner if no mxd's are in the olders/subfolders, has better status checking thru procesing (x of y remaining message), now handles FGDB-standalone-tables, and fixes a flukey isCoverage test for my arctic_circle.shp (false positive with "\arc" as part of the name).  Just an fyi.

0 Kudos
Zeke
by
Regular Contributor III

Thanks Rebecca. The icons are showing up today (??), I'll check them out. When you update the addin, you may want to check the tooltip for 2 - Inventory. There are a couple of typos.

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Thanks Greg. It doesn't surprise me that there are typos In the help/tooltips, etc.  I throw quick notes in and I'm forget to go back and clean things up. I need to go thru to clean things up and add more details.  I'm also kind of a hack (vs a hacker) when it comes to scripts....that is, not always pretty, but I just try to get it to work.  However, I do try to include many comments  (typos and all ).  [note...many Geonet typos by me can be blamed on the iPad trying to second guess me....or just bad typing]

I had another tool the was that allowed a global drive change (e.g. 😧 to F:, or 😧 to a UNC) and then ran the broken link list again, but, although this worked, I decided that was a bit too broad a change and could creat new issues. I decided to only include a more controlled version with the fix links (4) script.

I'm not sure how far along with you script, but I had already been thinking about doing a complete mxd TOC inventor (not just broken links), and don't think it would be hard to change 3a to do this.  I thought that would be a good addition to the toolbar.  Not sure how detailed you were planning to get with your tool, but I'm hoping to add additional functionality to the addin, so if it something you want to share for that, let me know.

(note for those wondering what the 3a, 4, etc that I refer to....that is part on my script name In my addin...make it convenient for me 1) for me to know the order I usually run, and 2) sorts nice in my file-explorer and Wing projects.)

0 Kudos
Zeke
by
Regular Contributor III

Sure, I'll share my script. I have two versions, one as a plain script tool, one as a python toolbox. I'm not sure how to post or send it, though, other than as an attachment to a post here.

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

I followed you, so you can send me a direct message with the attachment if you would like, or post it here.

0 Kudos
Zeke
by
Regular Contributor III

I don't see a way to upload files, either in this box or thr message box. Only options are images or video. I suppose I could copy and paste the code, might be a little long though.

0 Kudos