I am writing a Python script to perform a near analysis on features from two different shapefiles with common attributes. This is happening in a Search Cursor.
However, whenever I tried to use the Near tool after I selected the relevant features from each shapefile OR created new layers of only the relevant features, the Near tool performs correctly on the selected rows but fills the rest of the table NEAR_FID and NEAR_DIST rows with -1. I have double and triple checked this process out of the cursor loop and my selection code works correctly. Obviously, this means that I cannot do this iteratively as each use of the tool will overwrite the previous use with -1 values. I am currently solving this issue by using the Generate Near Table tool and then appending all tables together to join back to the original shapefile, but this takes a much longer time than the Near tool alone. According to the ArcGIS blog post Near By Group this tool should work in iterations. Is there something unique about the ModelBuilder iterator tools that allows this that doesn't exist in python Cursors?
Here is my cursor with the Near tool:
finishList = []
arcpy.MakeFeatureLayer_management("myGDB.gdb\\lb07", "log")
arcpy.MakeFeatureLayer_management("myGDB.gdb\\vms07", "vms")
rows = arcpy.SearchCursor("myGDB.gdb\\lb07")
for row in rows:
id = row.fmc_logves_id
date = str(row.string_date)
finished = str(id) + " " + str(date)
if finished in finishList:
continue
else:
arcpy.SelectLayerByAttribute_management("log", "NEW_SELECTION", """"fmc_logves_id" = """ + str(id) + """ AND "string_date" = '""" + str(date) + """'""")
arcpy.SelectLayerByAttribute_management("vms", "NEW_SELECTION", """"fmc_ves_id" = '""" + str(id) + """' AND "string_date" = '""" + str(date) + """' AND "activity" = 1""")
arcpy.Near_analysis("vms", "log")
finishList.append(finished)
del row, rows
Answer
This is the only solution I could find immediately to this problem, still using the Generate Near Table tool.
finishList = []
count = 0
rows = arcpy.SearchCursor(lines)
for row in rows:
vess = row.fmc_logves_id
date = str(row.string_date)
finished = str(vess) + " " + str(date)
if count == 0:
count = 1
firsttable = 'in_memory\\finalTable'
arcpy.SelectLayerByAttribute_management("log", "NEW_SELECTION", """"fmc_logves_id" = """ + str(vess) + """ AND "string_date" = '""" + str(date) + """'""")
arcpy.SelectLayerByAttribute_management("vms", "NEW_SELECTION", """"fmc_ves_id" = '""" + str(vess) + """' AND "string_date" = '""" + str(date) + """'""")
arcpy.GenerateNearTable_analysis("vms", "log", firsttable, "#", "LOCATION")
if finished in finishList:
continue
else:
table = 'in_memory\\near'
arcpy.SelectLayerByAttribute_management("log", "NEW_SELECTION", """"fmc_logves_id" = """ + str(vess) + """ AND "string_date" = '""" + str(date) + """'""")
arcpy.SelectLayerByAttribute_management("vms", "NEW_SELECTION", """"fmc_ves_id" = '""" + str(vess) + """' AND "string_date" = '""" + str(date) + """'""")
arcpy.GenerateNearTable_analysis("vms", "log", table, "#", "LOCATION")
arcpy.Append_management(table, firsttable)
arcpy.Delete_management(table)
finishList.append(finished)
del row, rows
It's not exactly elegant but it gets the job done.
No comments:
Post a Comment