ArcPy Project failing as GP Service

3307
17
Jump to solution
01-07-2021 12:50 PM
BrianLeroux
Occasional Contributor III

I have an python tool that runs fine in ArcGIS pro. I was able to publish as a service to my ArcGIS Enterprise however when I try to run the service it gives an error. It looks like the it is hung up when trying to call arcpy.Project_management. Any ideas on why this would run fine in Pro but not as a GP service? Thanks in advance.

 

  • esriJobMessageTypeError: Traceback (most recent call last): File "<string>", line 156, in execute File "<string>", line 441, in getMessages File "d:\program files\arcgis\server\framework\runtime\arcgis\Resources\arcpy\arcpy\management.py", line 11492, in Project raise e File "d:\program files\arcgis\server\framework\runtime\arcgis\Resources\arcpy\arcpy\management.py", line 11489, in Project retval = convertArcObjectToPythonObject(gp.Project_management(*gp_fixargs((in_dataset, out_dataset, out_coor_system, transform_method, in_coor_system, preserve_shape, max_deviation, vertical), True))) File "d:\program files\arcgis\server\framework\runtime\arcgis\Resources\arcpy\arcpy\geoprocessing\_base.py", line 511, in <lambda> return lambda *args: val(*gp_fixargs(args, True)) arcgisscripting.ExecuteError: ERROR 000208: Error creating output feature class Failed to execute (Project).
# what to do with a polygon
        logging.debug(
            "Input feature is a polygon based on config, buffer will not be created")
        poly = arcpy.AsShape(feature["geometry"], True)

        try:
            wkid = feature["geometry"]["spatialReference"]["latestWkid"]
        except Exception:
            wkid = feature["geometry"]["spatialReference"]["wkid"]
            pass

        in_sr = arcpy.SpatialReference(wkid)
        poly_fc = arcpy.CreateFeatureclass_management(
            "in_memory", "tempPoly", "POLYGON", spatial_reference=in_sr)[0]
        logging.debug("Spatial Reference of poly_fc: " +
                      arcpy.Describe(poly_fc).spatialReference.name)

        # Open an insert cursor
        with arcpy.da.InsertCursor(poly_fc, ["SHAPE@"]) as cursor:
            # add the poly to the feature class
            cursor.insertRow([poly])

        # reproject the poly feature class to 4326 if it's not already so the
        # buffer output will be in 4326 and be more accurate
        if wkid == 4326:
            event_poly = poly_fc
        else:
            projected_poly = os.path.join(script_path,"prjPoly.shp")
            arcpy.Project_management(
                poly_fc, projected_poly, arcpy.SpatialReference(4326))
            arcpy.CopyFeatures_management(projected_poly, event_poly)
            arcpy.Delete_management(projected_poly)
            logging.debug("event Poly feature count: " +
                          str(arcpy.GetCount_management(event_poly).getOutput(0)))
            logging.debug("event Poly spatial reference: " +
                          arcpy.Describe(event_poly).spatialReference.name)

 

17 Replies
BrianLeroux
Occasional Contributor III

So I checked the output path of sys.path[0] when the tool runs and it is D:\arcgisserver\directories\arcgissystem\arcgisinput\GPTools\ExposureTool.GPServer\extracted\p20\smsalertgppublish_test. This is a valid directory on the server. and I confirmed there is no existing file there that would need to be overwritten.  I also use the same sys.path[0] to create log files for this tool which are being generated successfully indicated it is not a write permission issue on the folder.

DanPatterson
MVP Esteemed Contributor

@BrianLeroux can we assume that you are raw encoding the path just in case python gets involved because...

pth = "D:\arcgisserver\directories\arcgissystem\arcgisinput\GPTools\ExposureTool.GPServer\extracted\p20\smsalertgppublish_test"

os.path.join(pth, "test.shp")
'D:\x07rcgisserver\\directories\x07rcgissystem\x07rcgisinput\\GPTools\\ExposureTool.GPServer\\extracted\\p20\\smsalertgppublish_test\\test.shp'

would obviously be invalid with the r before "D:\....


... sort of retired...
BrianLeroux
Occasional Contributor III

I ended up fixing by using a scratch GDB for the Project output. I think overall a better approach anyway. I appreciate your input.

0 Kudos
DanPatterson
MVP Esteemed Contributor

yes, but keep it in mind when working with python 3... unicode opens a whole new world of issues for path construction


... sort of retired...
0 Kudos
BrianLeroux
Occasional Contributor III

Noted! Thanks.

0 Kudos
DavidPike
MVP Frequent Contributor

I'm not sure where your outputs are set to, but if for example the script_path folder is not registered to the server datastore, it will fall over.  The error certainly appears to be related to the server being unable to create an output somewhere.

Perhaps share the full code and script tool parameters to get more detailed advice.

Blake is right, as far as I know, Project will only work if output to a physical location, but I'm assuming script_path is a folder which is why it works in desktop.  You can however cheat this by specifying a scratch workspace of scratch GDB as the output location.  On publishing, Server seems to interpret this and modify the path to its own scratch environment.

BrianLeroux
Occasional Contributor III

I can't share all the code as it has proprietary business logic but I will share as much as possible. Yes the current output of Project is to a shapefile in the same directory as the python tool by using sys.path[0] to set the path.

To elaborate a bit more this tool was originally created in ArcMap and published to my current 10.8.1 server. and worked fine. I am converting it to ArcGIS Pro as we plan on not using ArcMap any longer. I might modifications to get it running with python 3 and it does work fine in ArcGIS Pro. Publishing to Server 10.8.1 as a GP service is where I have the issue.

BrianLeroux
Occasional Contributor III

Thanks to the suggestion on the scratch GDB I was able to get this working. I am still baffled on why outputting as a shape file no longer works as it has in the past. Either way using the scratch GDB is probably a better approach anyway.

Here is what I changed in case anyone else runs into this.

I changed this line: projected_point = os.path.join(script_path, "projectedPoint.shp")
To this: projected_point = os.path.join(arcpy.env.scratchGDB, "projectedPoint")

Thanks to all for the help!