Friday, February 2, 2018

[Code snippet] Extract Feature Layer Fields from Map Document (Mxd) using ArcPy

This code snippet is for getting a list of fields in a feature layer from a map document in ArcPy 10.4.


1:  #Import arcpy mapping library  
2:  import arcpy.mapping;  
3:    
4:  # Map doc path  
5:  mxdPath =r"C:\MyMxd.mxd"  
6:    
7:  #Open map document  
8:  document = arcpy.mapping.MapDocument(mxdPath)  
9:    
10:  #Extract all dataframes inside a map document  
11:  dataFrameList = arcpy.mapping.ListDataFrames(document)  
12:    
13:  #Loop through all DF   
14:  for dataFrame in dataFrameList:  
15:  #Extact all layers in a dataframe   
16:    layerList = arcpy.mapping.ListLayers(document, None, dataFrame)  
17:      
18:    #Loop through all Layers     
19:    for layer in layerList:  
20:      print (dataFrame.name+"--"+layer.name +"--"+layer.dataSource)  
21:        
22:      #Extract all fields in a layer   
23:      fieldList = arcpy.ListFields(layer.dataSource, None, None)  
24:        
25:      #Loop through and print field properties       
26:      for field in fieldList:  
27:         print(field.name,field.aliasName,field.type,field.length,field.required,field.precision)  

ArcPy , Python

0 comments :

Post a Comment

 

© 2011 GIS and Remote Sensing Tools, Tips and more .. ToS | Privacy Policy | Sitemap

About Me