2

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
10
  • Just been trying to understand the flow of logic. First what is finishList it never seems to be populated? Are you not showing the full code? So.. you get id and date from log layer, you select only 1 row in this layer the current id/date. You select from your vms layer rows with the same id/date and activity =1. So the near tool works on these selections only, everything else is set to -1. It says this in the Help.
    – Hornbydd
    Commented Aug 14, 2013 at 21:36
  • Sorry, that was my fault and I accidentally deleted a row when copying it over, I've added it now. finishList is a list of the id and date combinations that I have already iterated over so I don't do anything twice because my sometimes for a given id there will be multiple entries with the same date. And yes, the Help does say everything else is set to -1, however the blog topic that I linked to suggests this shouldn't happen in an iterator.
    – AlmaThom
    Commented Aug 14, 2013 at 21:46
  • This may or may not help but you may want to consider using the Select tool rather than MakeFeatureLayer/SelectByAttributes to get your desired selections isolated while Near runs on them.
    – PolyGeo
    Commented Aug 14, 2013 at 22:09
  • The reason I am hesitant to use the Select tool is that it generates a new feature class so I run into the same problem I have with the Generate Near Table tool in that it takes more processing time and memory than simply iterating the Near tool over a single file.
    – AlmaThom
    Commented Aug 14, 2013 at 22:19
  • I bet your script will run much faster if you copy all your feature data and tables to in-memory.
    – klewis
    Commented Aug 14, 2013 at 22:52

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.