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')
asked Nov 22, 2019 at 9:35
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. 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']
answered Nov 25, 2019 at 15:14
1
  • Works as it should! Commented Nov 26, 2019 at 14:09

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.