3

I am trying to combine ListDataframes, ListLayers and ListFields in one ArcPy code.

I can do it one by one (first list layers in one code, and then list fields in second code), but I am trying to do in one code. So, basically I want to list all data frames, layers, all layers with their projection and get field info on those layers (field name, field type). Code by code is not a problem, but combining two codes is.

So far, I have a piece of code, but I do not get results I want. I do get field name and type, but no layer names and dataframes. I know I also have to include Describe tool for spatial reference, but not sure where. This is my code so far. Works, but not as expected.

import arcpy
mxd = arcpy.mapping.MapDocument(r"CURRENT")  
df = arcpy.mapping.ListDataFrames(mxd)[0]  
lyrList = arcpy.mapping.ListLayers(mxd, "", df)  
for lyr in lyrList:  
     if lyr.supports("dataSource"):  
         for field in arcpy.ListFields(lyr.dataSource):  
             print (field.type, field.name, field.aliasName)
for lyr in arcpy.mapping.ListLayers(mxd, df):
    name = lyr.name
    print name
    del mxd 

As I said, this is a result of piece of my code, And I get this:

00_BASE
SUBGROUP
shore_polygon
shore_polyline
sea

On this list, I would like to look like this:

00_BASE (This is layer name)
SUBGROUP (This is subgroup name)
shore_polygon (FID, Shape, ID) (ObjectID, Shape, Long)
shore_polyline (FID, Shape, ID) (ObjectID, Shape, Short)
sea (FID, Shape, Name) (ObjectID, Shape, Text)

EDIT 1: This code works for printing layer name, field name, and field type. Still can not get to print group and subgroup layer names.

import arcpy
mxd = arcpy.mapping.MapDocument(r"CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Main Map")
lyrList = arcpy.mapping.ListLayers(mxd, "", df)
for lyr in lyrList:
    if lyr.supports("dataSource"):  
        for field in arcpy.ListFields(lyr.dataSource):  
            print (lyr.name, field.name, field.type)   
del mxd

And result looks like this:

 (u'shore_polygon', u'FID', u'OID')
 (u'shore_polygon', u'Shape', u'Geometry')
 (u'shore_polygon', u'Id', u'Integer')
 (u'shore_polyline', u'FID', u'OID')

Still want group and subgroup layer names, to look like this:

 GROUP LAYER NAME
 SUBGROUP NAME
 (u'shore_polygon', u'FID', u'OID')
 (u'shore_polygon', u'Shape', u'Geometry')
 (u'shore_polygon', u'Id', u'Integer')
 (u'shore_polyline', u'FID', u'OID')
1
  • In order to get printing right, you should add diagnostic print statements inside your loops. Of course, you only need one lyr loop, and you need to use list comprehensions to generate the field.name and field.alias lists, then you need to learn how to format stings to display those lists.
    – Vince
    Commented Nov 22, 2019 at 13:22

1 Answer 1

4
+50
import arcpy
mxd = arcpy.mapping.MapDocument(r"CURRENT")  
df = arcpy.mapping.ListDataFrames(mxd)[0]  
lyrList = arcpy.mapping.ListLayers(mxd, "", df)  
for lyr in lyrList:  
    if lyr.supports("dataSource"):  
        fields = arcpy.ListFields(lyr.dataSource)
        print("{} {}, {}".format(lyr.name, [f.name for f in fields], [f.type for f in fields]))
    else: print(lyr.name)

enter image description here

Result for the sample layers structure in the image

group
subgroup
layer1 [u'OBJECTID_12', u'Shape', u'Shape_Length', u'Shape_Area'], [u'OID', u'Geometry', u'Double', u'Double']
layer2 [u'OBJECTID_12', u'Shape', u'Shape_Leng', u'F_NAME', u'Shape_Length'], [u'OID', u'Geometry', u'Double', u'String', u'Double']
1
  • Works as it should!
    – Dean7
    Commented Nov 26, 2019 at 14:09

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.