jeudi 14 juin 2018

Find a shortcut for "Add Layer code" in ArcPY (Python 2.7.8)

In this code i try to add a layer files to a MXD file- only from sub folders that have only one layer file in them. If there are 2 layers files or more, i don't add it to the map.

This code works just fine, but i seek a shortcut for this code- it a little bit clumsy:

import os,sys,fnmatch,shutil,arcpy

rootPath = r"G:\desktop\Project\lyr\taba\7650"
mxd = arcpy.mapping.MapDocument(r"G:\desktop\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
list1 = []
list2 = []
list3 = []
for root, dirs, filenames in os.walk(rootPath):
   for d in dirs:
       DIR = os.path.join(root,d)
       lyr_files = fnmatch.filter(os.listdir(DIR), '*.lyr')
       if len(lyr_files)==0:
           list1.append(d) 
       if len(lyr_files)>1: 
           list2.append(d)
       if len(lyr_files)==1:
           list3.append(d)
           for i in lyr_files:
               if i[0].isupper():
                   addLayer = arcpy.mapping.Layer(os.path.join(DIR, i))
                   arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")  
mxd.save()
print
print 'folders with 0 lyr files:'
for i in list1:
     print i
print
print 'folders with 2 lyr files and more:'
for j in list2:
    print j
print
print 'folders with 1 lyr files:'
for k in list3:
     print k

Aucun commentaire:

Enregistrer un commentaire