-
Notifications
You must be signed in to change notification settings - Fork 4
/
AddAllShpCurrentPrj.py
38 lines (32 loc) · 1.24 KB
/
AddAllShpCurrentPrj.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#add all shp in subdirectory into current project in arcmap
#Kyaw Naing Win, OneMap GIS Program Manager
# #---instruction----
# (1)type mydir = in python windows
# (2)from ArcCatalogue windows, drag main folder in to python windows after mydir =
# copy and paste below codes into python windows
# press enter twice
import arcpy,os
#mydir = #drag and drop a folder from ArcCatalogue
# listing all files in subdirectoires
# ---below code borrowed from https://goo.gl/g6Mihd
def list_files(dir):
r = []
subdirs = [x[0] for x in os.walk(dir)]
for subdir in subdirs:
files = os.walk(subdir).next()[2]
if (len(files) > 0):
for file in files:
r.append(subdir+ '\\' + file)
return r
# #--access current project and data frame
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
# #--loop through all files ending in .shp, add to current frame
for file in list_files(mydir):
if file.endswith(".shp"):
lyr = arcpy.mapping.Layer(file)
arcpy.mapping.AddLayer(df, lyr, "AUTO_ARRANGE")
#clean up
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
del mxd, df, lyr, mydir, file, list_files