Select to view content in your preferred language

Newbie question!!

884
4
08-02-2013 02:56 AM
LisaMcRavey1
Emerging Contributor
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=14127

I'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\test
Start Time: Fri Aug 02 11:54:34 2013
Running 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
Tags (2)
0 Kudos
4 Replies
RichardFairhurst
MVP Alum
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=14127

I'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\test
Start Time: Fri Aug 02 11:54:34 2013
Running 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


Based on the error it is most likely the path you supplied that used the wrong slash format in a string.  The following techniques will all work in a Python script path:

1.) forward slashes = /
2.) double backslashes = \\
3.) raw string with backslashes = r"H:\InfoRequest"

Single backshashes do not work, since the backshash is an escape character that is not read as a string character when it is by itself in a string that is not interpreted raw.  So "C:\Users\lisa\Desktop\test" is invalid in Python.  These would be valid:

"C:/Users/lisa/Desktop/test"
"C:\\Users\\lisa\\Desktop\\test"
r"C:\Users\lisa\Desktop\test"
0 Kudos
markdenil
Frequent Contributor
Not having seen the script or toolbox, I would suggest
running the script itself in IDLE or Pythonwin
(supplying any arguments passed from the toolbox
such as input fc, split fiield, etcetera).
That will allow you to see what is going on at the script level
instead of at the toolbox level.

rfairhur24's advice is appropriate too.
0 Kudos
DanPatterson_Retired
MVP Emeritus
I I communicated with you offline, don't use the desktop as the source and/or destination for the inputs and result.   9.2 is no longer supported
0 Kudos
DaveBarrett
Deactivated User
Hi,

I have used this script before in its original form and had a few dramas, but I have since updated it for our toolbox at work.

Here is my code:

## 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)


This isn't perfect yet as it has an issue when fields contain null values. Haven't had a chance to alter it yet.

Hope it helps

Dave
0 Kudos