## Created By: Cpl Barrett, GASC, 16 Geo Sp Sqn ## Date: 02/05/2013 ## Purpose: Take and input feature class and split it based on the unique values ## in the supplied case field. If an ID field is used the result will be ## feature classes for each row in the input. The name of the input fc is ## prefixed to the output fc. ## note this has thrown an error for null values on a field this will be fixed in the next update. import arcpy, string, os def whereClause(fname, fc, i, unique): """Returns a formatted where clause for use in an SQL statement. The format of the where clause is determined by the input field type. """ # get the field type from the supplied fname using it as a wildcard field = arcpy.ListFields(fc, fname) datatype = field[0].type # construct the whereclause based on if the field is of type Date, Sring or a Number field if datatype == "Date": whereclause = """%(fname)s = date'%(value)s'""" % {"fname":arcpy.AddFieldDelimiters(fc, fname), "value": str(unique)} elif datatype =="String": whereclause = """%(fname)s = '%(value)s'""" % {"fname":arcpy.AddFieldDelimiters(fc, fname), "value": str(unique)} else: whereclause = """%(fname)s = %(value)s""" % {"fname":arcpy.AddFieldDelimiters(fc, fname), "value": str(unique)} return whereclause ## Get the parameters and do the work try: # input feature class fc = arcpy.GetParameterAsText(0) # attribute field to search field = arcpy.GetParameterAsText(1) # output workspace outworkspace = arcpy.GetParameterAsText(2) # list to hold the values found in the attribut table values = [] # search cursor used to return the field values cursor = arcpy.SearchCursor(fc) # determine the pefix for the output feature classes prefix = (os.path.basename(fc)).rstrip(".shp") print prefix # get a list of each value in the attribute table for row in cursor: values.append(row.getValue(field)) # use set() and list() to get a list containing only the unique values unique = list(set(values)) ##for i in range(0, len(unique)): ## print unique # Add a message that states how many unique values found in x number of rows message = str(len(unique)) + " value(s) found in " + str(len(values)) + " rows." arcpy.AddMessage(message) # loop through each unique value and create a new fc for i in range(0,len(unique)): # get the whereclause whereclause = whereClause(field, fc, i, unique) # add a message to show what value is being extracted message = "Extracting: " + str(unique) arcpy.AddMessage(message) # validate the name of the output feature class name = arcpy.ValidateTableName((prefix + "_"+ str(unique))) # make atemp layer to hold the selected values based on the where clause layer = arcpy.MakeFeatureLayer_management(fc, name, whereclause) outfc = outworkspace + os.sep + name # create the output feature class arcpy.CopyFeatures_management(layer, outfc) arcpy.AddMessage(("Created: " + outfc)) except: # Add a generic error message to let the user know something has broken arcpy.AddError("Something has gone wrong. Please check the field values to ensure that there are no illegal characters.") # Return Geoprocessing tool specific errors # for msg in range(0, arcpy.GetMessageCount()): if arcpy.GetSeverity(msg) == 2: arcpy.AddReturnMessage(msg)
Hi there,I'm pretty new to GIS in general, but am starting to look into Python scripts and downloadable toolboxes to see how they work. I've downloaded this python script and toolbox, but can't seem to get it to work properly, so am looking for some advice?http://arcscripts.esri.com/details.asp?dbid=14127I'm not sure if it's something straightforward and obvious I'm missing, or if it's something a bit more specific to my files.I've added the toolbox to ArcToolbox, and then specify my input shapefile (points based) as feature layer, field to query as FID (though I've tried changing this to see if it makes a difference and it doesn't!) and then an output folder (leaving output filename blank). My error message is as follows: Executing (SplitLayerByAttributes_3): SplitLayerByAttributes All_VPs FID # C:\Users\lisa\Desktop\testStart Time: Fri Aug 02 11:54:34 2013Running script SplitLayerByAttributes...Error in script SplitLayerByAttributes.Error in executing: cmd.exe /C W:\5USERA~1\Lisa\ArcGIS\Scripts\SPLITL~1\SPLITL~1.PY "All_VPs" "FID" "#" "C:\Users\lisa\Desktop\test"Failed to execute (SplitLayerByAttributes_3).End Time: Fri Aug 02 11:54:35 2013 (Elapsed Time: 1.00 seconds)Any advice much appreciated! I'm running GIS 9.2.Thanks,Lisa
Přihlášení členové mohou přispívat, sledovat aktualizace a další. Jste tu noví? Zaregistrujte si bezplatný účet.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.