AnsweredAssumed Answered

Batch Near Script Produces Inconsistent Results

Question asked by dbirkigt on Feb 22, 2012
Latest reply on Feb 24, 2012 by dbirkigt
Hi,

I am working on a batch python near tool that does the following. Given a folder, near analysis is performed between all the point feature classes (source) in the folder and all the polygon/line features (target) in the folder. Ie it loops through two enumeration lists and performs near on everything. The output field of each near is written to a non-spatial dbf file (one file for each set of input points).

The issue I have is that the tool produces inconsistent results. By this I mean when I input a file and a copy of that same file it will perform the near analysis on the first file and return -1 for all rows in the second file, then it will work correctly on the third file enumerated in the list.

Given a different set of input data I get different inconsistencies. This inconsistency is that near will perform correctly for the first enumerated file but return -1 for the rest (Given file A,B,C A is correct B and C return -1) when I change the input order, placing one of the files which did not calculate properly first it will then calculate correctly, and a file which returned values the previous time will return -1 (Given the same files in order B,C,A B will calculate but -1 will be returned for C and A).

I realize near returns a value of -1 when no features are found within the set search distance, but this is not the issue for me as I do not use a search distance and the order of input seems to dictate the efficacy of the result.
Also, near runs quickly on the files that produce the correct result and terribly slowly when -1 is returned.

Does any one have any ideas?
I have included my code, does anyone spot anything suspect (Beyond the normal oblivion)?

I have also attached a folder with some test data and my script, if you try the script you will need to hardcode the location of the input and output folders.

Thanks
David

import sys, random, arcgisscripting, arcpy, os from arcpy import env from datetime import datetime   #Set the workspace, location of files env.workspace = "C:\Users\Desktop\DemoData" outputLocation = "C:\Users\Desktop\DemoData\Output"   print "Start time " + str(datetime.now())  # Set geoprocessor object property to overwrite existing output arcpy.gp.overwriteOutput = True  #preparing some storage lists fcTargetList=[] fcPointList=[] fcPolyList=[]  #This is the list of point features, the destination for the near fcPointList = arcpy.ListFeatureClasses('*', 'POINT')  #This is the listed features for near targets fcTargetList = arcpy.ListFeatureClasses('*', 'LINE') fcPolyList = arcpy.ListFeatureClasses('*', 'POLYGON')  #Combine the two target lists fcTargetList.extend(fcPolyList)  #For each point feature in the folder perform near analysis with the target features for point in fcPointList:     print "Finding features near to " + str(point)      #Create a name for the ouput table     tableName = str(point[0:(len(point)-4)]) + ".dbf"      #Create the table that will store output     arcpy.CreateTable_management(outputLocation, tableName)      #Add a field to the table, the name is the FID list from the point input file     newTable = outputLocation + os.sep + tableName     arcpy.AddField_management(newTable, "PointID", "LONG")      #Open a insert cursor on the new table     insrows= arcpy.InsertCursor(newTable)      #Open a search cursor     #Loop through the rows in the input and write the FID to the output table     for row in arcpy.SearchCursor(point):          #Prepare a insert row objewct for the new table         insrow=insrows.newRow()          #Assign the FID to the object that will become that row, the row is called point ID         insrow.PointID = row.getValue("FID")          #Insert the new row object         insrows.insertRow(insrow)      #Delete the cursor to release the file lock     del insrows, row      #For each feature in the feature class list, perform near anlysis and assign the value to a row in an output table     for feature in fcTargetList:          print "Processing feature " + feature         #Prepare the table to hold the new output          #Prepare a name for the output field         newField = str(feature[0:(len(feature)-4)])          #Check to see is the name is shorter then 10 the max number of elements in a field name         if len(newField) >10:             print "Caution field name being reduced from " + newField +" to " + newField[0:9]             newField= newField[0:9]          #Add a field to the output table to hold the near results         arcpy.AddField_management(newTable, newField, "LONG")          #Perform near anlysis         arcpy.Near_analysis(point, feature)          #Open an update cursor on the new table         upTable = arcpy.UpdateCursor(newTable)         up = upTable.next()          #Open a search cursor on the point file to get the near data and loop through it copying it to the out table         for val in arcpy.SearchCursor(point):             #Print for debugging             print val.getValue("NEAR_DIST")             #Enter the near value in the output table             up.setValue(newField, val.getValue("NEAR_DIST"))             #Update the table             upTable.updateRow(up)             #load the next row             up = upTable.next()         #Delete the cursor to remove the lock         del upTable          print "Completed " + feature print "Completed at " + str(datetime.now())

Attachments

Outcomes