I'm working through an exercise for my Python class and my understanding/experience with Python is very limited. In the end, once the script is built, I should come across one error that was purposely built into the script by the instructor, which I must fix, but when I run my script there seems to be more than one error. And in all honesty, I have looked over the step by step guide and cannot see why I am getting these errors. Any help would be greatly appreciated!
Here is the premise of the exercise:
Right click on the SplitFeaturebyAttribute.py python script that you created in part 1 and select Edit with Notepad++.
The next step is to add the actual logic that will execute the split and export of each unique feature class.
To do this the script will have to perform the following tasks:
1. Check to see if the destination geodatabase exits in the output directory.
2. Cursor through the input feature class to find the unique classes.
3. Split and export/create the unique feature classes.
To do this we will write a function that takes in the script tool input parameters (e.g. inputFc, outputDir, and geodatabaseName). When working with data and workspace paths a common python module to use is os (click os to view its library page). For the first task listed above we will have to first check to see if the output directory geodatabase exist or not. If it exists, then the next step will be to cursor through the feature class. If the geodatabase does not exist, then the script will create a new geodatabase. This logic may be represented using an if/else conditional logic statement. We will use a os function called os.path.exists to check to see if the geodatabase exists or not.
Below is the error statement I keep getting:
Executing: SplitFeaturebyAttribute "C:\CSCC\Courses\GIS 2120\Unit 11\Unit11\Part2\10_13_13\test.shp" "C:\CSCC\Courses\GIS 2120\Unit 11\Unit11\Part2\10_13_13" test
Start Time: Sat Nov 18 18:27:55 2017
Running script SplitFeaturebyAttribute...
*Geodatabase exists test in C:\CSCC\Courses\GIS 2120\Unit 11\Unit11\Part2\10_13_13
Traceback (most recent call last):
File "C:\CSCC\Courses\GIS 2120\Unit 11\Unit11\SplitFeaturesbyAttribute.py", line 51, in <module>
splitbyAttribute(inputFc, outputDir, 'CLASS')
File "C:\CSCC\Courses\GIS 2120\Unit 11\Unit11\SplitFeaturesbyAttribute.py", line 48, in splitbyAttribute
arcpy.Select_analysis(fc, newfc, field + '= ' + "'" + value + "'")
File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\analysis.py", line 84, in Select
raise e
ExecuteError: ERROR 000210: Cannot create output C:\CSCC\Courses\GIS 2120\Unit 11\Unit11\Part2\10_13_13//test.gdb//Class1
Failed to execute (Select).
ERROR 000210: Cannot create output C:\CSCC\Courses\GIS 2120\Unit 11\Unit11\Part2\10_13_13//test.gdb//Class1
Failed to execute (Select).
Completed script SplitFeaturebyAttribute...
Failed to execute (SplitFeaturebyAttribute).
Failed at Sat Nov 18 18:27:55 2017 (Elapsed Time: 0.05 seconds)
Below is my script:
"""
---------------------------------------------------------------------------------------------
SplitFeatureAttribute.py
Author: Kevin Surbella
Organization: CSCC
Date: 11/10/2013
Description: This script splits a feature class by
defined field unique classes.
---------------------------------------------------------------------------------------------
"""
import os, arcpy
inputFc = arcpy.GetParameterAsText(0)
outputDir = arcpy.GetParameterAsText(1)
geoDatabaseName = arcpy.GetParameterAsText(2)
try:
if not os.path.exists(outputDir):
arcpy.CreateFileGDB_management(outputDir, geoDatabaseName, 'CURRENT')
else:
arcpy.AddMessage('\n*Geodatabase exists ' + geoDatabaseName + ' in ' + outputDir)
arcpy.env.overwriteOutput = True
def splitbyAttribute(fc, outDir, field):
valueList = []
valueSet = set()
with arcpy.da.SearchCursor(fc, field) as cursor:
for row in cursor:
if row[0]:
if row[0] not in valueSet:
valueList.append(row[0])
valueSet.add(row[0])
for value in valueList:
newfc = outDir + '//' + geoDatabaseName + '.gdb//' + value
arcpy.Select_analysis(fc, newfc, field + '= ' + "'" + value + "'")
arcpy.AddMessage('\nfeature exported = ' + newfc + '\n')
splitbyAttribute(inputFc, outputDir, 'CLASS')
except Exception, e:
import traceback
map(arcpy.AddError, traceback.format_exc().split("\n"))
arcpy.AddError(str(e))