Problems creating, editing Python Script that clips feature classes from a database

2867
2
08-29-2011 02:49 PM
by Anonymous User
Not applicable
Original User: jeffja

I'm new to python and have only a little experience in programming. I need clip approximately  20 feature classes from a SDE database that has approximately 100 feature classes in it . I will need these 20 clipped features for numerous projects. they need to have different output locations. I was told not to put them in a dataset that they need to stay the way they are in the SDE database. I also do not have permission to edit in the SDE database.

My solution was to copy (and make two edits) a clipping script from ArcGIS Resource Center that seems close to what i need.  The script was imported to notepad, then python 2.6 and a script tool was create with four user input parameters (input, clip feature, output  and cluster tolerance)
Attached are two files. one is my code from the script. the second is the errors from the python command line

The edits I made were
1.  Replace fcs = arcpy.ListFeatureClasses() with a list   fclist =  ("S_R04_FIF.ActivityPolygon","S_R04_FIF.GeosciSri", etc etc)
2.   Add �??_Clip�?� to fc to make  (fc +�??_Clip�?�)

It does not work. It allows me to add the four parameters. And begins processing  and fails after 11 seconds with a syntax error in the code. Don�??t know what the error are. If I add the code to the python command line I get all sorts of indent errors which does not seem correct .  Hopefully somebody has an idea or solutions.

thanks
0 Kudos
2 Replies
StacyRendall1
Occasional Contributor III
Firstly, one of the lines of your script is split over two lines, this could be the syntax error... Also the line if fc <> os.path.basename(clipFeatures): might be better as  if fc != os.path.basename(clipFeatures): - that is ! (not) = (equal). Here is an edited version of your script to include these changes:
# Script Name: Clip Multiple Feature Classes
# Description: Clips one or more shapefiles
#              from a folder and places the clipped
#              feature classes into a geodatabase.
# Created By:  Me.
# Date:        082711.

# Import ArcPy site-package and os modules
#
import arcpy 
import os

# Set the input workspace
#
arcpy.env.workspace = arcpy.GetParameterAsText(0)

# Set the clip featureclass
#
clipFeatures = arcpy.GetParameterAsText(1)

# Set the output workspace
#
outWorkspace = arcpy.GetParameterAsText(2)

# Set the XY tolerance
#
clusterTolerance = arcpy.GetParameterAsText(3)

try:
    # Get a list of the featureclasses in the input folder
    #
    fclist = ("S_R04_FIF.ActivityPolygon","S_R04_FIF.GeosciSri")

    for fc in fclist:   
        # Validate the new feature class name for the output workspace.
        #
        featureClassName = arcpy.ValidateTableName(fc + "_Clip", outWorkspace)
        outFeatureClass = os.path.join(outWorkspace, featureClassName)
        
        # Clip each feature class in the list with the clip feature class.
        # Do not clip the clipFeatures, it may be in the same workspace.
        #
        if fc != os.path.basename(clipFeatures):
            arcpy.Clip_analysis(fc, clipFeatures, outFeatureClass, clusterTolerance)

except:
    arcpy.AddMessage(arcpy.GetMessages(2))
    print arcpy.GetMessages(2)




Now, the reason it doesn't work from the command line is that you need to enter everything from both the Try and the Except statements before getting the interpreter to run it, as well as the indented blocks within those...

You shouldn't really run much from the interpreter (command line), especially not something this complex - you need to make a Python script, then set up Python to run it.

First off, go to Run, enter cmd and hit enter (this brings up the command prompt), then type python and hit enter. This may (but probably won't - see the instructions below) start the Python interpreter, like so:
C:\>python
Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)
Type "help", "copyright", "credits" or "license" for more information.
>>>


If that works, you can skip this paragraph, if not here are the instructions of how to set up your Python:
[INDENT]NOTE: Doing this requires administrator rights on the computer you are using.
Add the ArcGIS Python installation to your systems Environment Variable Path. To do this you need to know where ArcGIS has installed its Python - search your system for python.exe and write down the path and folder it lives in. For example, on my system, it is C:\Python26\ArcGIS10.0\
WARNING: Donâ??t make any mistakes here (like deleting things), or your system might not workâ?¦
Now, on Windows XP:
[INDENT]Right click on My Computer, select Properties, go to the Advanced tab, then click Environment Variables[/INDENT]
On Windows 7 (and, I presume, Vista):
[INDENT]Right click on Computer, click on Advanced system settings on the left, then click Environment Variables[/INDENT]
In the â??System variablesâ?? pane, scroll the box down and select the â??Pathâ?? variable, then go to Edit. Place the cursor in the box, and hit End to get the cursor at the end, add a semi-colon ( ; ) and then enter the path to the Python installation that you wrote down earlier. So, on my computer, I added this:
[INDENT];C:\Python26\ArcGIS10.0\[/INDENT]
Click OK, then close the other dialog boxes... To test it works, try opening python from the command prompt again.[/INDENT]


Now, save your file somewhere as 'myClip.py' or something like that, then navigate the command prompt to the folder containing the file (using cd), and enter:
python myClip.py


This will possibly show some other errors if your paths aren't set up correctly or if there are other underlying problems in your script. Anyway, at least you can properly test the script now...
0 Kudos
by Anonymous User
Not applicable
Original User: jeffja

wow thanks for all your help the code works.
0 Kudos