dgoldb2@luc.edu

ArcPy: Near tool fails second iteration

Discussion created by dgoldb2@luc.edu on Apr 22, 2016
Latest reply on Apr 25, 2016 by Dan_Patterson

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.

Outcomes