ArcPy: Near tool fails second iteration

2815
5
04-22-2016 11:30 AM
DavidTreering
New Contributor II

Using ArcPy in ArcGIS Pro 1.2, I have a da.SearchCursor for loop that selects a single city park feature per iteration, and runs the Near tool for points within a specified radius.

In the first iteration, the Near tool completes successfully, but it fails on the second iteration.

map = aprx.listMaps("Layers")[0]
lyrs = map.listLayers()
lyr_Park = [lyrs[0]]
fcs_Crime = [lyrs[1],lyrs[2],lyrs[3],lyrs[4],lyrs[5],lyrs[6]]


for lyr_Crime in fcs_Crime:
  crime_type = lyr_Crime.name

  pCursor = arcpy.da.SearchCursor(lyr_Park[0], ['park_'])

  for park in pCursor:
  park_name = str(park[0])
  expression1 = "park_="+ park_name

  arcpy.SelectLayerByAttribute_management(lyr_Park[0], "NEW_SELECTION", expression1)
  arcpy.SelectLayerByLocation_management (lyr_Crime, "WITHIN_A_DISTANCE", lyr_Park[0], '1320 feet', 'NEW_SELECTION' )

  arcpy.FeatureClassToFeatureClass_conversion (lyr_Crime,"C:\\Users\\dgoldb2\\Documents\\ArcGIS\\Projects\\Crime_Parks_Relationship\\Crime_Parks_Relationship.gdb", "p" + park_name + "_" + crime_type)
  park_crimes = arcpy.ListFeatureClasses("p" + park_name + "_" + crime_type, "Point")

  print (("p" + park_name + "_" + crime_type))


  arcpy.Near_analysis(park_crimes[0], lyr_Park[0], "492 Meters", "LOCATION", "NO_ANGLE")


print ("Finished")

Here is the error:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 44, in <module>
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\analysis.py", line 1162, in Near
    raise e
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\analysis.py", line 1159, in Near
    retval = convertArcObjectToPythonObject(gp.Near_analysis(*gp_fixargs((in_features, near_features, search_radius, location, angle, method), True)))
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py", line 500, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 999999: Error executing function.
Failed to execute (Near).

I have looked carefully at the in_features and near_features to be sure they are both of type "Layer".  Near runs the first iteration, which tells me the types are fine, but fails the second iteration, which tells me something is not progressing in the loop.  Any ideas are welcome.

0 Kudos
5 Replies
DarrenWiens2
MVP Honored Contributor

Have you tried restarting ArcGIS since your problem started? My understanding of cursor syntax is that you should use 'with...' notation because it automatically cleans up your cursor, even if it errors out. Without it, if you make a run that ends in an error, your cursor can get stuck in memory until you delete it with a 'del' statement.

Try restating ArcGIS and change your cursor to something like:

with arcpy.da.SearchCursor(lyr_Park[0], ['park_']) as pCursor:
  for park in pCursor:  

Also, certainly just a copy/paste/formatting error, but everything after 'for park in pCursor' should be indented.

0 Kudos
DavidTreering
New Contributor II

That is definitely good to know.  I've switch the cursor syntax and also run "del pCursor" after every run.

The indentation was just a pasting error, as you assumed.

The issue with the Near tool is unresolved.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Forgoing the SearchCursor discussion for a minute, something seems off with Line 14, i.e., I am surprised your Select Layer by Attribute in Line 16 works because you need to escape strings in WHERE clauses and I don't see that being done in Line 14.

0 Kudos
DavidTreering
New Contributor II

Interesting.  The selection is working just fine. 

I can only guess that the underscore is being treated as a wildcard.  The table has fields integer "park_" and string "park", but no other fields with the word "park...".  Could it be it is selecting from the intended field and not getting confused only because there is no alternative?

0 Kudos
DanPatterson_Retired
MVP Emeritus

did you change your query to exclude _ and then append it to the output name later if you need it.  The underscore in the query is a wildcard and I haven't got a file to check to see if "park" versus "park_" will only return 4 or 5 character cases

Building a query expression—Help | ArcGIS for Desktop

0 Kudos