claiborn

Creating a Feature Layer for each Record in a Feature Class

Discussion created by claiborn on Feb 7, 2013
Latest reply on Feb 8, 2013 by claiborn
I???m converting a geoprocessing Python v2.5 script we used for years with ArcGIS 9.3.1 into a v2.7.2 to work with ArcGIS 10.1.  The script is pretty long, and basically just runs relatively simple location selections on a series of different layers and our parcel layer (and a derived center point layer of those same parcels) and calculates values in the parcel layers based on whether or not they intersect. 

The part I???m stuck on is a short cut that worked fine in the previous version but is failing for some reason in the new version.   The intent with this line is to create a feature class for each record in the voting districts layer.  This layer has three different fields I???ll need to apply to my parcel points.  It???s not an excessive amount of records; only 13 so that shouldn???t be the issue, plus the fact that it worked fine in 9.3.1 and on older PCs for years.

I???ve included the rest of the code below, but I believe the line that???s generating the error is this, where it???s supposed to create a feature layer per each record, via their ObjectID:

arcpy.MakeFeatureLayer_management("BOSElectLayer", "ElectionSelection", "\"OBJECTID\" = " + str(ObjectID))

Now, given that???s out of context, and you???ll have to either believe me or check the code below that there???s no major logical error, or misspelled variable, I think I???ve failed to replace some v2.5 operator or syntax with something new in v2.7.2. 

I???ve tested it line by line in ArcCatalog???s Python Command Line (making the appropriate changes) and it repeatedly hits an error here.   But when it does error, it must be running through all the rows because it???s in a ???try??? group and for the ???except??? I used ???print(???BOS Election Feature Layer Creation Failed???) and it seems to print that line multiple times, possibly because it???s in an endless loop, but I???m not sure why.

Again, this is intended to be a stand alone script.  While it???s just one small part of the entire script, I???ve already tested all preceding parts; this is the first I have not been able to resolve.

Here's the code (not that it's just an excerpt, but I've included all critical lines from previous sections):


# previous line where "AddrPtLayer" is generated in a try group:
 arcpy.MakeFeatureLayer_management("Z:\\Custom_Apps\\WEBGIS\\TESTWEBGIS_10.1.gdb\\ParcelPoints", "AddrPtLayer")

#/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

#GET BOS ELECTION DISTRICT INFORMATION 
try:
    #Get election district layer
    arcpy.MakeFeatureLayer_management("Database Connections\\gis_viewer@york.sde\\york.GISADMIN.BoundaryandGovernment\\york.GISADMIN.VotingDistricts", "BOSElectLayer")

    #Create a search cursor for the election districts 
    ElecRows = arcpy.SearchCursor("BOSElectLayer")  

    #Loop through each election district polygon and calculate parcel polygon attributes accordingly
    while ElecRecord:
        #Select the current election district record
        ObjectID = ElecRecord.getValue("OBJECTID")
        try:
            arcpy.MakeFeatureLayer_management("BOSElectLayer", "ElectionSelection", "\"OBJECTID\" = " + str(ObjectID)) #appears to be the problem line
        except:
            log.write("BOS Election Feature Layer Creation Failed")
            log.write(arcpy.GetMessages(2) + "\n")

        #Get values from selected election polygon
        try:
            Precinct = ElecRecord.getValue("PRECINCT")
            PollName = ElecRecord.getValue("POLLNAME")
            District = ElecRecord.getValue("DISTR")
        except:
            log.write("BOS Field Value Variables Faild")
            log.write(arcpy.GetMessages(2) + "\n")

        #Select Parcel Points that intersect with the selected election polygon          
        try:     
            arcpy.SelectLayerByLocation_management("AddrPtLayer", "INTERSECT", "ElectionSelection", "#", "NEW_SELECTION")
        except:
            log.write("BOS Location Selection Faild")
            log.write(arcpy.GetMessages(2) + "\n") 
        
        #Calculate parcel polygon fields according to respective election polygon
        try:
            arcpy.CalculateField_management("AddrPtLayer", "Precinct", '"' + Precinct + '"')
            arcpy.CalculateField_management("AddrPtLayer", "poll", '"' + PollName + '"')
            arcpy.CalculateField_management("AddrPtLayer", "supdistr", District)
        except:
            log.write("BOS Field Calculations Faild")
            log.write(arcpy.GetMessages(2) + "\n")

        arcpy.SelectLayerByAttribute_management("AddrPtLayer", "CLEAR_SELECTION")
        ElecRecord = ElecRows.next()         
   
except:
    log.write ("BOS Election District attribution failed!" + "\n")
    log.write (arcpy.GetMessages(2) + "\n")

    
#/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

Outcomes