I have been asked to select point features that fall into a polygon in its own shapefile then copy the points selected to a new shapefile. There are around 300 shapefiles with an indiviidual polygon. Although only a very beginnner in python i thought it would be possible to automate the process.

I based my script of one found on this site but cannot get it too work. Im not sure if it is fit for purpose or not.

We are using arcmap 9.3 so no arcpy unfortunately.

# 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 Tools.tbx")

# Script arguments...
Layer_A = sys.argv[1] # polygon
Layer_B = sys.argv[2] # points
outfolder = sys.argv[3]
basename = sys.argv[4]

# create search Cursor that ref the features in layer B
rows = gp.searchcursor(Layer_B)
row = rows.next()

# Create a numbering system for new output files
filenum = 0 

while row:
    # Report a message back to the user about progress
    gp.addmessage("Creating Selection #" + str(filenum) + " from " + Layer_A)

    # Create a feature layer to cycle through selection features
    fidvalue = row.GetValue("FID")
    gp.MakeFeatureLayer(Layer_B,"cursor_lyr","FID =" + str(fidvalue))

    # Process: Select Layer By Location...
    gp.SelectLayerByLocation_management(Layer_A, "INTERSECT", "cursor_lyr", "", "NEW_SELECTION")

    outname = outfolder + "\\" + basename + str(filenum)
    # Process: Copy Features...
    gp.CopyFeatures_management(Layer_A, outname, "", "0", "0", "0")

    #iterate the output file number
    filenum = filenum + 1

    #go to next row
    row = rows.next
  • 1
    Where exactly are you stuck? if you just post a lot of code, and expect people to debug it, you are not going to get answers. – Devdatta Tengshe Feb 10 '16 at 11:09
  • Hi sorry, i really dont know where i am stuck, the script runs yet produces no results just a blank shapefile, I imagine either the MakeFeatureLayer part is incorrect or the outname part, – rsimspon Feb 10 '16 at 11:49
  • I assume you are using a shapefile or DBF since you have an FID column. Your column name in your where clause needs to be in quotes: gp.MakeFeatureLayer(Layer_B,"cursor_lyr","\"FID\" =" + str(fidvalue)) – RHB Feb 10 '16 at 14:15
  • Also, your very last line should be: row = rows.Next() – RHB Feb 10 '16 at 14:25
  • i believe i am a step closer this is my error now Creating Selection #0 from Roecliffe_address Creating Selection #1 from Roecliffe_address <class 'arcgisscripting.ExecuteError'>: Failed to execute. Parameters are not valid. ERROR 000725: Output Layer: Dataset cursor_lyrs already exists. Failed to execute (MakeFeatureLayer). Failed to execute (selctandcopy). – rsimspon Feb 10 '16 at 14:38

FYI - As a new coder, you will have much better luck de-bugging your code if you include error handlers. http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?id=849&pid=839&topicname=Error_handling_with_Python

import sys
import traceback

    # Your indented code goes here

except arcgisscripting.ExecuteError:
    msgs = gp.GetMessage(0)
    msgs += gp.GetMessages(2)
    print msgs
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    pymsg = tbinfo + "\n" + str(sys.exc_type)+ ": " + str(sys.exc_value)
    print pymsg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.