Near analysis does not run in stand alone script

444
3
09-21-2012 09:48 AM
JuanOrozco
Occasional Contributor
I'm using a bunch of tools in a stand alone script, and the near analysis tool does not work. Behavior is erratic, it runs from within ArcGIS only if the candidates are supplied as a string, not as a feature layer.

import arcpy

pMXD = arcpy.mapping.MapDocument("C:\\Proj\\BBerry.mxd")
pDF = arcpy.mapping.ListDataFrames(pMXD, "Layers")[0]

## Candidates are point feature class
for fc in pDF:
    if fc.name == "Candidates":
        nearFeatures = fc
    if fc.name == "tmp":
        inFeatures = fc

## arcpy.Near_analysis(inFeatures, nearFeatures, "", "NO_LOCATION", "NO_ANGLE") ## Does not work!
arcpy.Near_analysis(inFeatures, "Candidates", "", "NO_LOCATION", "NO_ANGLE") ## Works 


The goal is to add this function to a stand alone script, but it does not work as such no matter what. Stand alone script error message is:


[INDENT]arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Near Features: Dataset Candidates does not exist or is not supported
Failed to execute (Near).[/INDENT]


Any suggestions are welcome.
Tags (2)
0 Kudos
3 Replies
ChristopherThompson
Occasional Contributor III
I think its because you have two if statements that write to the same object, so with the second if you are overwriting the result of the first if.   That second statement should be an else or an elif (else if), maybe like this:
for fc in pDF:
    if fc.name == "Candidates":
        nearFeatures = fc
    elif fc.name == "tmp":
        inFeatures = fc
0 Kudos
JuanOrozco
Occasional Contributor
I think its because you have two if statements that write to the same object, so with the second if you are overwriting the result of the first if.   That second statement should be an else or an elif (else if), maybe like this:
for fc in pDF:
    if fc.name == "Candidates":
        nearFeatures = fc
    elif fc.name == "tmp":
        inFeatures = fc


Thanks for the response. It did not make a difference since the loop checks all feature layers names and sets variables depending on the layer's name anyway.

The error message when passing feature layers instead of string like I did in my first posting is:

[INDENT]  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\analysis.py", line 794, in Near
raise e
arcgisscripting.ExecuteError: ERROR 999999: Error executing function.[/INDENT]


Someone mentioned the possibility of near analysis and at least other tool being buggy. I think I'll write my own near function and stop wasting time.

Thanks a lot.
0 Kudos
JuanOrozco
Occasional Contributor
Solved, the near features must be passed in a list (Tool's documentation and tool examples suck). Now that the near features are inside brackets it runs as stand-alone script.

import arcpy

pMXD = arcpy.mapping.MapDocument("C:\\Proj\\BBerry.mxd")
pDF = arcpy.mapping.ListDataFrames(pMXD, "Layers")[0]

## Candidates are point feature class
for fc in pDF:
    if fc.name == "Candidates":
        nearFeatures = fc
    if fc.name == "tmp":
        inFeatures = fc

arcpy.Near_analysis(inFeatures, [nearFeatures], "", "NO_LOCATION", "ANGLE")



Rule of thumb, if a tool takes multiple inputs for a given parameter pass them in a list even if you have just one item.
0 Kudos