Selecting with cursors and exporting that data

Discussion created by englandgirl22 on Sep 13, 2011
Latest reply on Sep 16, 2011 by nahmed-esristaff
Hi there,

I am working with cursors to try and achieve the following task.  I have two shapefiles.  One point shapefile with about 50,000 points in and one polygon file with about 5000 shapes.  The two shapefiles are related through the 'Name' field.

I need to iterate through both of them and where the name is the same in both of them do the following:

1. Select the features for that name (in both the point and polygon shapefile)
2. Select the points that fall within the polygon
3. Swiitch the selection
4. Copy the points outside of the polygon to a new shapefile

As the loop cintinues, I would ideally just append each new feature(s)  so that at the end of the analysis I would have a single shapefile that contains all of those points that fall outside of their respective polygons (linked by the 'Name' field)

I may not even be approaching this correctly, but please see my code below and if anyone has any suggestions they would be greatly receieved!  Thanks for looking!!

import arcgisscripting, os, sys, string
gp = arcgisscripting.create()

gp.OverWriteOutput = True

gp.workspace    = "C:\\cursor_testing"
wsp             = gp.workspace
inputFacilities = "\\SampleData.shp"
lyrFacilities   = "temp_sampPoint"
SelectedFacilities = "sel_point"
inputPoly = "\\SampleDataPolygon.shp"
lyrPoly        = "temp_Lines"

# Make Feature Layer for facilities
gp.MakeFeatureLayer_management(inputFacilities, lyrFacilities)
print "Points feature layer complete"

# Make feature layer for polygon
gp.MakeFeatureLayer_management(inputPoly, lyrPoly)
print "Polygon feature layer complete"

# Search Cursor
fcSearch = gp.SearchCursor(lyrFacilities, "", "", "Name") 
fc = fcSearch.Next()
print fc.getvalue("Name")

while fc:
    exp = "Name" + "='" + str(fc.getvalue("Name"))+"'"

    # Process: Select by Attribute...
    gp.SelectLayerbyAttribute_management(lyrFacilities, "NEW_SELECTION", exp)
    gp.SelectLayerbyAttribute_management(lyrPoly, "NEW_SELECTION", exp)
    # Process: Select by Location those that fall within the 10km boundary.
    gp.SelectLayerbyLocation_management(lyrFacilities, "INTERSECT", lyrPoly, "#", "SUBSET_SELECTION")

    # Process: Create new temp layer for selected facilities to enable switch selection to work
    gp.MakeFeatureLayer_management(lyrFacilities, SelectedFacilities)

    # Process:  Switch the selection to get those that are outside the 10km.
    gp.SelectLayerbyAttribute_management(SelectedFacilities, "SWITCH_SELECTION", "#")

    # Need to create shapefile unique to the data
    copy_layer = wsp + "\\" + "Name_" + str(fc.getvalue("Name"))
    # Process: Copy the features to a permanent shapefile
    gp.CopyFeatures_management(SelectedFacilities, copy_layer)

    # Process: Delete the selected features so that the process cna be sped up and to ensure there are no duplicated points
    # Process: Append the selected facilities to the master shapefile  
    gp.Append_management (copy_layer, master, "NO_TEST")
    fc = fcSearch.Next()

del fcSearch, fc

I do not seem to be able to delete and keep creating empty geometry which then immediately locks as the delete has not got rid of the points that have already been calculated!

Hope this is clear!  Cheers!