# Import system modules import arcpy, string # Source directory directory = r'I:\Q_workspace\Sam_GIS\290812_CoveragetoGDB\COVERS' destDir = directory + '\\gdb' # Workspace arcpy.env.workspace = directory # Determine coverages in folders coverageList = arcpy.ListDatasets() for coverage in coverageList: outPath = destDir + '\\' + coverage if arcpy.Exists(outPath): print outPath + " already exists. Stopping..." break else: print "Creating " + outPath newGDB = arcpy.CreateFileGDB_management(destDir, coverage) # Determine feature classes in coverage arcpy.env.workspace = directory + '\\' + coverage fcList = arcpy.ListFeatureClasses() print coverage + " has the following featureclassses: " + ','.join(fcList) arcpy.FeatureClassToGeodatabase_conversion(fcList, newGDB)
>>> arcpy.env.workspace = r"e:\work\basincov" >>> arcpy.ListFeatureClasses() [u'arc', u'label', u'polygon', u'tic']
import os arcpy.env.workspace = os.path.join(directory,coverage) fcList = arcpy.ListFeatureClasses() fcList = fcList.remove("tic") # don't need tics fcList = fclist.remove("annotation") # drop annotation # use a list comprehension to prepend full paths fcListFull = [os.path.join(arcpy.env.workspace,k) for k in fcList] arcpy.FeatureClassToGeodatabase_conversion(fcListFull, newGDB)
>>> zgdb = arcpy.CreateFileGDB_management(r"e:\work","zgdb") >>> x [u'arc', u'label', u'polygon', 'tic'] >>> arcpy.FeatureClassToGeodatabase_conversion(x,zgdb) e:\work\basincov\arc Successfully converted: e:\work\zgdb.gdb\basincov_arc e:\work\basincov\label Successfully converted: e:\work\zgdb.gdb\basincov_label e:\work\basincov\polygon Successfully converted: e:\work\zgdb.gdb\basincov_polygon e:\work\basincov\tic Successfully converted: e:\work\zgdb.gdb\basincov_tic <Result 'e:\\work\\zgdb.gdb'>
import arcpy #setup variables arcpy.env.overwriteOutput = True theCoverage = arcpy.GetParameterAsText(0) if theCoverage == '#': theCoverage = r"c:\_data\_Skwentna07\covers\trans07\arc" #theCoverage = r"c:\_data\_Skwentna07\covers\trans07\arc" theWorkspace = arcpy.GetParameterAsText(1) if arcpy.env.workspace == '#': arcpy.env.workspace = r"c:\_data\_Skwentna07\Reports\RetroFitDev2\newTest.gdb" #C:\_data\_Skwentna07\Reports\RetroFitDev2\newTest.gdb #arcpy.env.workspace = r"c:\_data\_Skwentna07\Reports\RetroFitDev2\newTest.gdb" theWorkspace = arcpy.env.workspace outFC = arcpy.GetParameterAsText(2) if outFC == '#': outFC = "cleanTrans" # Transects print(" Converting the coverage to a FC " + str(outFC)) arcpy.FeatureClassToFeatureClass_conversion(theCoverage, theWorkspace, outFC)
I was also having issues, more with trying to figure out what "datatype" it wanted when attaching the script to a toolbar. I finally got "Dataset" to work (not coverage like I would have guessed). I am using 10.1 btw.
Simplified code with parameter setup is below. I used "Workspace or Feature Dataset" and "Any Value" for the data type of the other two variables.
I was also having issues, more with trying to figure out what "datatype" it wanted when attaching the script to a toolbar. I finally got "Dataset" to work (not coverage like I would have guessed).
"Workspace or Feature Dataset" and "Any Value" for the data type of the other two variables.
theCoverage = arcpy.GetParameterAsText(0)
if theCoverage == '#':
theCoverage = arcpy.GetParameterAsText(0) if not theCoverage: theCoverage = r"c:\_data\_Skwentna07\covers\trans07\arc"
theCoverage = arcpy.GetParameterAsText(0) if theCoverage in ["","#"]: theCoverage = r"c:\_data\_Skwentna07\covers\trans07\arc"
UGH! ((it did work, kind of....wrote to my mxd env which was still set to the default gdb instead of theWorkspace variable.
theWorkspace = arcpy.GetParameterAsText(1) if not theWorkspace: theWorkspace = r"c:\_data\_Skwentna07\Reports\RetroFitDev2\newTest.gdb" arcpy.env.workspace = theWorkspace
Some suggestions below.
GetParameterAsText() will return "" (not "#") if the parameter was not set and you haven't set a default value in the script tool parameters tab.
"" evaluates to false in an if, so I often do this:theCoverage = arcpy.GetParameterAsText(0) if not theCoverage: theCoverage = r"c:\_data\_Skwentna07\covers\trans07\arc"
If you think you may run this from Windows instead of from a toolbox, you can support "#" like this:theCoverage = arcpy.GetParameterAsText(0) if theCoverage in ["","#"]: theCoverage = r"c:\_data\_Skwentna07\covers\trans07\arc"
env.workspace = landpath + os.sep + "ilr" # Get a list of all feature classes in the workspace fcs = arcpy.ListFeatureClasses() # Loop through the list of feature classes for fc in fcs: fc = str(fc) if "." in fc: fc_fix = fc.replace(".", "_") # Replace . with _ for use as a feature class input name #messages("Fixed " + fc_fix) else: fc_fix = fc # Create a Describe object desc = arcpy.Describe(landpath + os.sep + "ilr" + os.sep + fc) # Pull Describe Object properties if hasattr(desc, "featureType"): featureType = desc.featureType if featureType == "CoverageAnnotation": # Process: Import Coverage Annotation arcpy.ImportCoverageAnnotation_conversion(fc, out_dataset_path + os.sep + out_name + os.sep + out_name + "_" + fc_fix, "2400", "CLASSES_FROM_LEVELS", "NO_MATCH", "NO_SYMBOL_REQUIRED", "STANDARD", "", "AUTO_CREATE", "AUTO_UPDATE") else: arcpy.FeatureClassToFeatureClass_conversion(fc, out_dataset_path + os.sep + out_name, out_name + "_" + fc_fix)