I wrote my code in Jupyter notebook and it works through the Jupyter environment and as well as on Python terminal inside of the ArcGIS Pro. I am trying to convert it into a toolbox to use it for different purposes, I followed the guide to create a toolbox but I cannot make it work and I get error when using Calculate Field and try to access a column that was created by Join. Here is the code, ignore the variables I do not use.
# -*- coding: utf-8 -*- import arcpy class Toolbox(object): def __init__(self): """Define the toolbox (the name of the toolbox is the name of the .pyt file).""" self.label = "Toolbox" self.alias = "nearToolBox" # List of tool classes associated with this toolbox self.tools = [Tool] class Tool(object): def __init__(self): """Define the tool (tool name is the name of the class).""" self.label = "Tool" self.description = "" self.canRunInBackground = False def getParameterInfo(self): """Define parameter definitions""" params = None return params def isLicensed(self): """Set whether tool is licensed to execute.""" return True def updateParameters(self, parameters): """Modify the values and properties of parameters before internal validation is performed. This method is called whenever a parameter has been changed.""" return def updateMessages(self, parameters): """Modify the messages created by internal validation for each tool parameter. This method is called after internal validation.""" return def execute(self, parameters, messages): """The source code of the tool.""" import arcpy import os arcpy.env.workspace = r"C:\Users\USER\Desktop\NearProjesi\Near.gdb" arcpy.env.overwriteOutput = True in_features = "Bina" near_features = "Yol" out_table = "bina_yol_yakinlik" search_radius = '1500 Meters' location = 'NO_LOCATION' angle = 'NO_ANGLE' closest = 'ALL' closest_count = 3 arcpy.CopyFeatures_management("Bina", "bina_kopya") # arcpy.CopyFeatures_management("Yol", "yol_kopya") arcpy.GenerateNearTable_analysis("bina_kopya", "Yol", out_table, search_radius, location, angle, closest, closest_count) arcpy.AddJoin_management(out_table, 'NEAR_FID', 'Yol', "OBJECTID", "KEEP_COMMON") arcpy.management.CalculateField(out_table, "YOL_ADI", "!Yol.yoladi!", "PYTHON3") arcpy.management.RemoveJoin(out_table) in_fc = r"C:\Users\USER\Desktop\NearProjesi\Default.gdb\bina_yol_yakinlik" nearest_dict = dict() with arcpy.da.SearchCursor(in_fc, ["YOL_ADI", "IN_FID"]) as rows: for row in rows: nearest_id = row # get OID value for that row input_id = row # get NEAR_FID value if input_id in nearest_dict: # if a dict key already exists, append near id to value list for that key nearest_dict[input_id].append(nearest_id) else: # if the key does not exist then create a new list with near id # and add it to the dictionary nearest_dict[input_id] = [nearest_id] return
the error is
Traceback (most recent call last): File "<string>", line 61, in execute File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 5209, in CalculateField raise e File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 5206, in CalculateField retval = convertArcObjectToPythonObject(gp.CalculateField_management(*gp_fixargs((in_table, field, expression, expression_type, code_block, field_type), True))) File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 511, in <lambda> return lambda *args: val(*gp_fixargs(args, True)) arcgisscripting.ExecuteError: ERROR 000539: Invalid field yol.yoladi Failed to execute (CalculateField). Failed to execute (Tool).
If you could look at this, the first output is the one generated by the very first join:
arcpy.AddJoin_management(out_table, 'NEAR_FID', 'Yol', "OBJECTID", "KEEP_COMMON")
the second one is:
arcpy.management.CalculateField(out_table, "YOL_ADI", "!yol.yoladi!","PYTHON3", "TEXT")
and the third one is after i do:
I am able to do this on jupyter notebook and as well as python terminal. But when i put it in the toolbox and run in i get the error on the second code i shared which is this one again :
arcpy.management.CalculateField(out_table, "YOL_ADI", "!yol.yoladi!","PYTHON3")
In this part i am trying to make a new column named as "YOL_ADI" and copy the data from the Yol->yoladi which i have on the output table. I tried to set field type parameter but it did not help. @Hornbydd