How to export a new layer by python after select by location?

489
2
08-29-2011 12:35 PM
JiefanYu
New Contributor
My mission is to select some polygons by location ( intersect with existing lines), and then export the result as a new shape file layer. I have been trying different ways and search for helps but it ended to nothing yet. So please help if you know. Following is my current codes:
    # Import system modules
    import sys, string, os, arcgisscripting
    # Create the Geoprocessor object
    gp = arcgisscripting.create()
    # Load required toolboxes
    gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management.tbx")

try:
    # Set the workspace (to avoid having to type in the full path to the data every time)
    gp.Workspace = "C:\temp"
    gp.overwriteOutput = 1
    outFolder = r???C:\Temp???
    outFile = ???Polygon.shp???
    gp.workspace = outFolder
   
 
    gp.CreateFeatureClass (outFolder, outFile, ???Polygon???, ???#???,???#???,???#???, "#")
    # Script arguments...
    Output_Feature_Class = outFile
    Input_Features = sys.argv[1]
   
   
    # Process: Find all stream crossings (points)
    gp.SelectLayerByLocation(Input_Features, "intersect", Output_Feature_Class)

except:
    # If an error occurred while running a tool, print the messages.
    print gp.GetMessages()
0 Kudos
2 Replies
DarrenWiens2
MVP Honored Contributor
Your problem is that you are using SelectLayerByLocation slightly wrong. The syntax is: (in_layer, {overlap_type}, {select_features}, {search_distance}, {selection_type})

The 1st parameter is the feature class from which to select features intersecting features in the 3rd parameter. Your 3rd parameter feature class was created empty with CreateFeatureClass, so there are no intersections with the 1st parameter.

Make the selection (referencing the correct feature class), then output that selection with Copy Features.
0 Kudos
JiefanYu
New Contributor
How about this?
  # Create the Geoprocessor object
import arcgisscripting
gp = arcgisscripting.create()
# Load required toolboxes
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management.tbx")
# Set the workspace (to avoid having to type in the full path to the data every time)
gp.Workspace = "C:\temps"
Input_Features = sys.argv[1]
Select_features= sys.argv[2]
try:
# Select intersect polygons
gp.SelectLayerByLocation("Input_Features", "intersect", "Select_features")

# Write the selected features to a new feature class
gp.CopyFeatures("Input_Features", "C:\temps\result.shp")

except:
    # If an error occurred, print the message to the screen
print gp.GetMessages()

//////////////////////////////////////////////////////////////////////////////////////
But when I try to run it, an error sign still pop up:

<type 'exceptions.IndentationError'>: ERROR 999999: Error executing function
Failed to execute (Intersect).
0 Kudos