Run arcpy-script as geoprocessing tool

arcgis-proarcpymodelbuilderparameterspython-script-tool

I am trying to get my first python/arcpy script going, but I am stuck with implementing it into ArcGIS Pro. If my script is running inside the ArcGIS Pro Python-Window everything is executed well and a new Layer is added to the workspace. But if I try to use it as a tool via geoprocessing with editable input and output the script still executes without any errors but no new layer with the desired data appears inside the GDB/workspace. I need to implement it as a tool to be able to use it as part of an existing ModelBuilder process.

This is the attribute table of the layer, which I want to edit with the script:

FID Shape object drain
1 point 44_6547/2 [55.27517, -142.08384] ; [-71.56405, 5.49818]
2 point 44_6543/6 [45.58417, -50.15136] ; [-40.11681, 5.49818] ; [-28.57185, 128.38152] ; [-48.65803, -78.15412]
3 point 44_6894/5 [9.51578, -20.82284] ; [3.92250, 78.47350]; [-12.93206, 130.76959]
4 point 44_6594/2 [33.01124, -85.81893]

This is my arcpy script:

import arcpy

# I WANT SOME KIND OF THIS TO WORK (LIKE PREPARED STATEMENTS):
# input_layer = arcpy.GetParameterAsText(0)
# output_layer = arcpy.GetParameterAsText(1)

# THIS IS WORKING:
input_layer = 'C:/Users/... local path .../layer/testLayer.shp'

workspace = arcpy.env.workspace
new_layer = arcpy.CreateFeatureclass_management(workspace,'_GetLatLng','POINT')
arcpy.AddField_management(new_layer, 'object', 'STRING', field_length = 255)
arcpy.AddField_management(new_layer, 'lat', 'FLOAT', field_length = 50)
arcpy.AddField_management(new_layer, 'lng', 'FLOAT', field_length = 50)
fields_insert = ['object', 'lat', 'lng']
insert_cursor = arcpy.da.InsertCursor(new_layer, fields_insert)

fields_search = ['Shape', 'object', 'drain']
search_cursor = arcpy.da.SearchCursor(input_layer, fields_search)

for x in search_cursor:
    a = x[2].split(' ; ')
    for i in a:
        print(i)
        e = i.split(', ')
        lat = e[0].lstrip('[')
        lng = e[1].rstrip(']')       
        insert_cursor.insertRow((x[1], float(lat), float(lng)))

del search_cursor, insert_cursor

This is how I tried to setup the tool in ArcGIS Pro:

ArcGIS Pro tool screenshot


Updated code part after comment from @gotchula

inFC = arcpy.GetParameterAsText(0)
outFC = arcpy.GetParameterAsText(1)

workspace = arcpy.env.workspace
spatial_ref = arcpy.Describe(inFC).spatialReference
testedFC = arcpy.ValidateTableName(outFC, workspace)
newFC = arcpy.CreateFeatureclass_management(workspace, testedFC, 'POINT', spatial_ref)

But now I receive an error message about the newFC line.

My desired input type is Feature-Class in File-Geodatabase and my output could either be a feature-class or feature-layer. Does this has to match with the datatypes I declare in the tool properties? Because if I declare Feature-Class instead of Feature-Layer there appear no choices in the list when running the tool…

Best Answer

with regards to adding layers to the active map, the gp framework logic is like this: once a gp tool completes it's processing (aka: script execution) the gp framework iterates over all output parameters, if the dataType of that parameter is one that can be added to a map (say raster, feature class or table) and that parameter has a value (path to an existing dataset) then add that dataset to the map.

Other dataset created during the tool processing that are not associated with an output parameter will not be added to the active map.

So what I think you're missing is an parameter with these properties: direction=output, dataType=FeatureClass. This is something you're want anyways since it gives control to the user of the tool as to where the output feature class will be created and what name it will have. Additionally this allows the output of the tool to be chained into another tool in modelBuilder.

After adding the parameter to the tool you'll want to update your script to catch and use the value

outfc = arcpy.GetParameterAsText(1)
fcWorkspace, fcName = os.path.split(outfc)
arcpy.CreateFeatureclass_management(fcWorkspace, fcName, ...)

Another best practice which I would recommend is when creating the output feature class, set an appropriate spatial reference, else the feature class' storage parameters will likely cause you some problems later on.

edit: reworked the text a bit for clarity, and based on follow on comments below.

Related Question