Implement Script Tool with ModelBuilder as GeoProcessing Service

arcmaparcpygeoprocessing-servicemodelbuilderpython-script-tool

I built a model in ArcGIS Desktop 10.8.1 and want to implement a script tool at the end (inside the model).
All elements are using relative paths and %SCRATCHWORKSPACE%\ in the file paths (as Geoprocessing Environment is set to a scratch directory).

I run the model and it works locally, and even when I publish it as a geoprocessing service.

Now, I want to add a Python script tool I made that references the 'final' output from the model and integrate the script into the model.

I added the script tool to the model and ran it locally. It works.
I published the model with the embedded script tool as a geoprocessing service, tied it to a web mapping application widget in Web App Builder, and it fails. (Without the script tool, everything runs as I expect).

I am thinking it's a path issue looking for 'final.shp' but it's not there yet (need relative paths in the script).

Here is the code from the script tool:

 # Import arcpy module
    import arcpy
    arcpy.env.workspace = r"%SCRATCHWORKSPACE"
    arcpy.env.overwriteOutput = True
    
    fc = "final.shp"
    #Add a new filed named mobCode (MUST DO THIS)
    arcpy.management.AddField(fc, "mobCode", "TEXT", "", "", 15) 
    
    
    fields = ["gridcode","soil_code", "mobCode"] 
    
    # calculate GO
    with arcpy.da.UpdateCursor(fc, fields) as cursor:
        for row in cursor:
            if row[0] >= 0 and row[0] <= 15 and row[1] == 'Good':
                row[2] = 'Go'
                cursor.updateRow(row)
    
    # calculate Slow Go
    with arcpy.da.UpdateCursor(fc, fields) as cursor:
        for row in cursor:
            if row[0] > 15 and row[0] <= 45 and row[1] == 'Good':
                row[2] = 'Slow Go'
                cursor.updateRow(row)
    
    with arcpy.da.UpdateCursor(fc, fields) as cursor:
        for row in cursor:
            if row[0] >= 0 and row[0] <= 15 and row[1] == 'Fair':
                row[2] = 'Slow Go'
                cursor.updateRow(row)
    
    # calculate No Go
    with arcpy.da.UpdateCursor(fc, fields) as cursor:
        for row in cursor:
            if row[0] > 15 and row[0] <= 45 and row[1] == 'Fair':
                row[2] = 'No Go'
                cursor.updateRow(row)


with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        if row[0] > 45:
            row[2] = 'No Go'
            cursor.updateRow(row)

Best Answer

There are a few things you need to do with a geoprocessing service. Unfortunately, I need more details before I could offer a more concrete answer.

  1. You've explained what works and doesn't very well; odds are the issue is not Web AppBuilder. However, to be sure, you should try running the service back in ArcMap and see if it errors there.
  2. You should turn Message level to INFO. Hopefully, it will give you a detailed error that can help narrow down the problem.
  3. Your script above is good. However, it does not have any INPUTS or OUTPUT parameters. Parameters are the key to a geoprocessing service. Without these, the tool (service) will operate against the same dataset over and over. Nor will it provide any output for the client to download. Since you have a script inside a model, you first need to make some parameters for the script tool: arcpy.GetParameterAsText(0) (for example), setting the parameter up in the tool properties. Then, inside the model, you need to make sure you've right-click > parameter on that input/output. NOW, that parameter will be available as either input or output in your geoprocessing service. (Unless you've already got this and your code just doesn't show how the shp file is referenced.)

Point #2 is the critical step to figuring out what's going wrong. Without knowing more than "it failed", it's hard to offer more.

Related Question