Hello there.
I'm currently trying to set up a python toolbox in order to automate the process of updating .csv-files, importing them, setting up the layers in the .gdb-file and finally colorizing the polygons.
If i paste the raw code (without the layout the toolbox needs) all methods work as desired but as soon as i fill the python toolbox with my methods and call them in the execute method, some of arcpy's methods don't seem to work properly.
The update of the .gdb-file seems to work in this toolbox, the TableToTable-conversion is the first one to fail. The table doesn't get imported into ArcGIS, which leads to problems later on when it comes to the colorization. The reason why i use arcpy.TableToTable_conversion() is because otherwise it wouldn't let me use these methods at all.
I am fairly new to ArcGIS/Arcpy and these toolboxes but i think the reason behind this could be the nonexisting access to some database/libraries..?
It just bothers me that everything works perfectly fine in the python window but doesn't work the same way when using the toolbox.
BTW: The goal is to be able to execute a single tool, pasting the code everytime should be avoided.
Let me know if you need more information and thanks in advance!
from arcgis.gis import GIS
import arcpy
class businessLogic(object):
global gdbPath
gdbPath = r"D:\example\test.gdb"
global featureClass
featureClass = r"Kreisgrenzen_2020\polygons"
def updateGDB(self):
arcpy.Delete_management(gdbPath)
arcpy.CreateFileGDB_management(r"D:\example", "test.gdb")
lyr_Kreisgrenzen = r"D:\example\Kreisgrenzen_2020.lyrx"
arcpy.mp.LayerFile(lyr_Kreisgrenzen)
aprx = arcpy.mp.ArcGISProject("CURRENT")
aprxMap = aprx.listMaps("Karte")[0]
aprxMap.addDataFromPath(lyr_Kreisgrenzen)
arcpy.AddMessage(fr"{gdbPath}")
def setLayers(self):
arcpy.env.workspace = gdbPath
arcpy.TableToTable_conversion(r"D:\example\qryexport01.csv", gdbPath,
"name")
arcpy.FeatureClassToFeatureClass_conversion(featureClass, gdbPath, "Kreisgrenzen_name")
arcpy.CreateRelationshipClass_management("name", fr"{gdbPath}\Kreisgrenzen_name",
"joined_layer_name", "SIMPLE", "polygons", "name",
"NONE", "ONE_TO_MANY", "NONE", "ags_name", "ags", '', '')
arcpy.AddJoin_management("Kreisgrenzen_name", "AGS", "name", "ags_name", "KEEP_ALL")
arcpy.FeatureClassToFeatureClass_conversion(f"Kreisgrenzen_name", gdbPath, "Faelle_name")
def parameter(displayName, name, datatype, defaultValue=None, parameterType='Optional', direction='Input'):
# create parameter with a few defaults
param = arcpy.Parameter(
displayName=displayName,
name=name,
datatype=datatype,
parameterType=parameterType,
direction=direction)
# set new parameter to a default value
param.value = defaultValue
# return the complete parameter object
return param
class Toolbox(object):
def __init__(self):
self.label = 'Toolbox'
self.alias = ''
# List of tool classes associated with this toolbox
self.tools = [Tool1]
class Tool1(object):
def __init__(self):
self.label = 'Tool'
self.canRunInBackground = True
self.parameters = [
parameter('username', 'un', 'GPString', '', 'Required'),
parameter('password', 'pw', 'GPStringHidden', '', 'Required'),
]
def execute(self, parameters, messages):
instance = businessLogic()
instance.updateGDB()
instance.setLayers()
return