davidorme73

gp.select_analysis returns empty shapefiles from within loop

Discussion created by davidorme73 on Nov 4, 2011
Hi,

I'm writing a Python script (ArcGIS 9.3.1 SP2) that runs an analysis on a series of sets of point features. Each of the sets is of the same size and typically small (10 -20) and the sets are stored in a single shapefile, with each set differentiated by the value of a field ('SRC_FIDT'). The script therefore contains a loop to identify the unique values of SRC_FIDT and then a second loop which uses gp.select_analysis() to output a shapefile of the particular subset for each value of SRC_FIDT.

The problem is that it works fine up to a point and then the select command starts outputting empty shapefiles, where the file does contain a set of matching rows. The same select expression and same file gives the correct result when run manually from ArcMap. Once this has happened, that instance of the geoprocessor object will not then select correctly, even using expressions that worked before the problem occurs. Deleting the gp instance and restarting at that point then works correctly but only for little while. It seems like something is corrupting the select process.

Does anyone have any ideas about what might be causing this? I've posted the code to generate the list of SRC_FIDT values and the main part of the file loop below.

Many thanks,
David

Loop to get the target select values:
ObsCur = gp.SearchCursor(sampleFile)
ObsRow = ObsCur.Next()
currFID = -1 # impossible value to start looped comparison on
obsPoints = dict([('INDIV', []), ('SRC_FIDT',[]), ('SRC_X',[]), ('SRC_Y',[])])

while ObsRow:

    # load points into individual path dictionary if we have a new SRC_FID
    nextFID = ObsRow.getvalue('SRC_FIDT')
    if currFID <> nextFID:
        obsPoints['SRC_FIDT'].append(nextFID)
        obsPoints['SRC_X'].append(ObsRow.getvalue('SRC_X'))
        obsPoints['SRC_Y'].append(ObsRow.getvalue('SRC_Y'))
        obsPoints['INDIV'].append(ObsRow.getvalue('INDIV'))
        currFID = nextFID

    # get the next row in the cursor
    ObsRow = ObsCur.Next()


Loop to output subsets of points for analysis, including print statements used for bug tracking, leaving out most of the actual analysis. The error still occurs with only this minimal set of instructions
for ind, fid, srcx, srcy in zip(obsPoints['INDIV'], obsPoints['SRC_FID'], obsPoints['SRC_X'], obsPoints['SRC_Y']):
    
    print ind, fid, srcx, srcy

    # create a multiple point sampling file by selecting all SRC_FIDT on the sample layer
    selectString = "\"SRC_FIDT\" = \'" + str(fid) + "\'"
    print selectString 
    gp.Select_analysis(sampleFile,  tmpDest, selectString)
    
    # analysis goes in here

    # tidy up for the next file
    gp.Delete(tmpDest)

Outcomes