1

I'm scripting a toolbox in which the user chooses a feature containing multiple points to create a distance table from other selected features in the list.

The following script for every loop overwrites the distance. I want every loop output to be written in a new column for every feature.

Any idea?

this is the toolbox inputs and output

import arcpy

Stations = arcpy.GetParameterAsText(0)
Dest = arcpy.GetParameterAsText(1)
Out = arcpy.GetParameterAsText(2)



in_features = Stations
near_features = Dest
out_table = Out


for near_features in Dest.split(';'):


out=arcpy.GenerateNearTable_analysis(in_features, near_features, out_table)
1

I see where your problem is, you're trying to append to Out but the tool keeps overwriting the output table so you only get the last result.

You would probably want to keep a track of which feature class the particular 'near' relates to, so I suggest trying it this way:

import arcpy

Stations = arcpy.GetParameterAsText(0)
Dest = arcpy.GetParameterAsText(1)
Out = arcpy.GetParameterAsText(2)

in_features = Stations
near_features = Dest
out_table = Out
temp_names = [] # new blank list to keep track of tables generated
for near_features in Dest.split(';'):
    # I'm assuming this is working so far
    # store this near table as an in memory table
    TempTab = 'in_memory\\{}_Table'.format(near_features)
    arcpy.GenerateNearTable_analysis(in_features, near_features, TempTab)
    temp_names.append(TempTab) # add this name to the list of tables to be merged
    arcpy.AddField_management(TempTab,"NearFt","TEXT",field_length=250) # Add a new field to store the feature class name

    # Populate the feature class name for this near 
    arcpy.CalculateField_management(TempTab,"NearFt","\"{}\"".format(near_features),"PYTHON") 

# merge all the temp tables to the output table
arcpy.Merge_management(temp_names,Out)

# clean up resources
for ThisTable in temp_names:
    arcpy.Delete_management(ThisTable)

Each individual Near output is stored as a separate table in the in_memory workspace, I add a field to the temporary near table, populating it with the feature class name using Calculate Field then at the end merge all the temporary tables to the Out table, this will give you a single table with all the near outputs and provide the feature class name that each particular measurement belongs to.

  • Michael, Thank you for your response. I've run this code on IDLE 2.7.12, I've got this error message: cannot create output in_memory\1_Table – Mostafa Rezaali Feb 19 '18 at 14:59
  • 1
    Michael, I referred to your post here gis.stackexchange.com/questions/123736/… I changed {}_Table to R{}_Table to avoid adding a number to the first part of the word. The problem was solved till I got new error which is : line 891, in GenerateNearTable raise e ExecuteError: ERROR 999999: Error executing function. Failed to execute (GenerateNearTable). – Mostafa Rezaali Feb 19 '18 at 15:47
  • After re-running the script the error changes to ERROR 000725: Output Table: Dataset in_memory\R1_Table already exists. Failed to execute (GenerateNearTable). – Mostafa Rezaali Feb 19 '18 at 15:56
  • Ah, that's because arcpy.env.overwriteOutput isn't set to overwrite.. set arcpy.env.overwriteOutput = True and that might fix it. It may have fallen over previously and not cleaned up in_memory. Would you by any chance have two feature classes called 'R1'? – Michael Stimson Feb 19 '18 at 21:30
  • I really appreciate your help. without your help, it was impossible for me to find a solution to this problem. If "R1" or any feature like that I had, I would change the "R1" to something els. – Mostafa Rezaali Feb 19 '18 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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