Is it possible to create feature dataset, based on the name from group layers. I have two separate codes, one for finding out the names of group layers, and other to create feature datasets.
>>> import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.isGroupLayer == True:
if lyr.longName.find('\\') == -1:
print lyr.longName
This is the code for searching the name of datasets, and this is the code to create feature datasets.
>>> arcpy.env.overwriteOutput = True
fdList = ["Dataset_A", "Dataset_B", "Dataset_C"]
folList = ["D:\\GIS_Temp\Folder A", "D:\\GIS_Temp\\Folder B", "D:\\GIS_Temp\\Folder C"]
workRange = range(len(fdList))
for thisIndex in workRange:
fd = fdList[thisIndex]
arcpy.env.workspace = folList[thisIndex]
arcpy.CreateFeatureDataset_management("D:\\GIS_Temp\\TEST.gdb", fd, "D:\\GIS_Temp\\Projection.prj")
for impFC in arcpy.ListFeatureClasses():
fcName,fcExt = os.path.splitext(impFC)
fcName.replace(" ","_")
arcpy.FeatureClassToFeatureClass_conversion(os.path.join(folList[thisIndex],impFC),os.path.join("D:\\GIS_Temp\\TEST.gdb",fd),fcName)
Is it possible to merge the two codes, into one, so the name fdList = ["Dataset_A", "Dataset_B", "Dataset_C"]
, would actually be names returned from the group layers based on the first code? I am trying to merge these two codes, so the searched names would actually be names of datasets.
1 Answer 1
If lyr.longName
is what you want to use to name your Feature Dataset, then you can just create an empty list fdList = []
and append it with the lyr.longName
to pass as FD name later.
import arcpy, os
mxd = arcpy.mapping.MapDocument("CURRENT")
fdList = []
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.isGroupLayer == True:
if lyr.longName.find('\\') == -1:
print lyr.longName
fdList.append(lyr.longName)
arcpy.env.overwriteOutput = True
folList = ["D:\\GIS_Temp\Folder A", "D:\\GIS_Temp\\Folder B", "D:\\GIS_Temp\\Folder C"]
workRange = range(len(fdList))
for thisIndex in workRange:
fd = fdList[thisIndex]
arcpy.env.workspace = folList[thisIndex]
arcpy.CreateFeatureDataset_management("D:\\GIS_Temp\\TEST.gdb", fd, "D:\\GIS_Temp\\Projection.prj")
for impFC in arcpy.ListFeatureClasses():
fcName,fcExt = os.path.splitext(impFC)
fcName.replace(" ","_")
arcpy.FeatureClassToFeatureClass_conversion( os.path.join(folList[thisIndex], impFC), os.path.join("D:\\GIS_Temp\\TEST.gdb", fd), fcName)
Please note I haven't debugged the rest of your script, so if it works for you then making the fdList=[]
change in the script should work.
-
Sadly, this is not working. It just keeps giving "File "<string>", line 11, in <module>. NameError: name 'fdList' is not defined". And I did leave it empty first, and I also put fdlist = [lyr.longName] also, but nothing. Is it maybe the problem in this line "fd = fdList[thisIndex]"?Dean7– Dean72016年04月28日 18:16:56 +00:00Commented Apr 28, 2016 at 18:16
-
rename the
fdlist = []
I gave you to befdList = []
(note the uppercaseL
), and also changefdlist.append(lyr.longName)
tofdList.append(lyr.longName)
2016年04月28日 18:18:11 +00:00Commented Apr 28, 2016 at 18:18 -
I have fixed the typo in my answer. Python is picky when it comes to case2016年04月28日 18:20:05 +00:00Commented Apr 28, 2016 at 18:20
-
That part of code seems to be working now. I really also missed the uppercase problem. This part of code seems to be working, but now it gives "File "<string>", line 24, in <module> NameError: name 'os' is not defined" error, when I put code you compiled.Dean7– Dean72016年04月28日 18:30:26 +00:00Commented Apr 28, 2016 at 18:30
-
1Yes, this is it now. In this code, you really answered more than one questions I had, because I had this problem in other codes. I am still a rookie.Dean7– Dean72016年04月28日 18:42:11 +00:00Commented Apr 28, 2016 at 18:42