Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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
share|improve this question
    
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 Aug 14 '13 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 Aug 14 '13 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 Aug 14 '13 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 Aug 14 '13 at 22:19
    
I bet your script will run much faster if you copy all your feature data and tables to in-memory. –  klewis Aug 14 '13 at 22:52
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.