0

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.

ahmadhanb
41.8k5 gold badges55 silver badges110 bronze badges
asked Apr 28, 2016 at 1:27

1 Answer 1

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.

answered Apr 28, 2016 at 4:27
7
  • 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]"? Commented Apr 28, 2016 at 18:16
  • rename the fdlist = [] I gave you to be fdList = [] (note the uppercase L), and also change fdlist.append(lyr.longName) to fdList.append(lyr.longName) Commented Apr 28, 2016 at 18:18
  • I have fixed the typo in my answer. Python is picky when it comes to case Commented 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. Commented Apr 28, 2016 at 18:30
  • 1
    Yes, 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. Commented Apr 28, 2016 at 18:42

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.