Select to view content in your preferred language

Python toolbox (pyt): how to get output

3705
6
11-25-2014 10:40 AM
KevinBell
Deactivated User

It would be tremendously helpful if someone could post a sample python toolbox (pyt) that accepts a dummy input parameter or two, and returns "hello world of pyt gp services!"  I figure with that template, I could do my own gp routines during the execution, and replace the "hello world" with the results of my gp work.  I don't need any geometry returned, just text.

The use case here is to return nearby addresses from an input lat/long.

This code fails:

import arcpy

import os

class Toolbox(object):

    def __init__(self):

        self.label = "Get Nearby Addresses"

        self.alias = ""

        # List of tool classes associated with this toolbox

        self.tools = [GetNearbyAddresses]

class GetNearbyAddresses(object):

    def __init__(self):

        self.label = "Get Nearby Addresses"

        self.description = "Get Nearby Addresses within a buffer."

        self.canRunInBackground = False

    def getParameterInfo(self):

       

        params = []

       

        param0 = arcpy.Parameter(

            displayName = "Latitude",

            name = "lat",

            datatype = "Double",

            parameterType = "Required",

            direction = "Input")

        params.append(param0)

       

        param1 = arcpy.Parameter(

                    displayName = "Longitude",

                    name = "lon",

                    datatype = "Double",

                    parameterType = "Required",

                    direction = "Input")

        params.append(param1)

       

        param2 = arcpy.Parameter(

            displayName = "Buffer Distance",

            name = "distance",

            datatype = "Long",

            parameterType = "Required",

            direction = "Input")

        params.append(param2)

       

        param3 = arcpy.Parameter(

                    displayName = "Address List",

                    name = "addresses",

                    datatype = "GPString",

                    parameterType = "Derived",

                    direction = "Output")

        params.append(param3)       

        return params

    def execute(self, parameters, messages):

        try:

            arcpy.env.workspace = r'E:\gis\default.gdb'

            arcpy.env.overwriteOutput = True

           

            lat = parameters[0].valueAsText

            lon = parameters[1].valueAsText

            distance = parameters[2].valueAsText

           

            #inpoint = arcpy.Point(X=lon, Y=lat)

            #ptGeometry = arcpy.PointGeometry(inpoint, arcpy.SpatialReference(4326))

            #arcpy.Buffer_analysis(ptGeometry, 'buffer', distance + ' FEET', \

            #                      "FULL", "ROUND", "NONE", "")

            #get intersecting addresses here

           

            parameters[3].value = 'a nice list of addresses!'  # I need this from a gp service!

           

            return

       

        except Exception, ErrorDesc:

            sErr = "ERROR:\n" + str(ErrorDesc)

            messages.addErrorMessage(sErr)

        return

0 Kudos
6 Replies
DallasShearer
Deactivated User

if you just want to add the results to the toolbox messages. just add:

arcpy.AddMessage("your list results")

if you want to add the results to a text file. do it like this:

with open(r"c:\yourdirectory\yourfile.txt", "w") as f:

     f.write("your list results")

I don't think you necessarily need to define an output parameter (param3), unless your intention is to add it to arcmap on completion.

0 Kudos
KevinBell
Deactivated User

I have web developers that would like to consume the results as a web service, so I'm not too clear on the best way to set the output, but I don't think adding to the tool message is it.  I don't really need to persist the data to a text file either.  It's a quick call to get some temp data, then it's tossed.

0 Kudos
MarkJones7
Regular Contributor

How would you get that text file to the user? I am working on a geoprocessing task and I need to send a file back to the user (allowing them to select where on their own computer they would like to save the file).

0 Kudos
AhmedEl-Sisi
Deactivated User

Try to set your output parameter like the following:

arcpy.SetParameter(3,'a nice list of addresses!')

KevinBell
Deactivated User

hmmm....   submitting the job via the rest directory fails still.  The tool succeeds in ArcMap though.

0 Kudos
DallasShearer
Deactivated User

ok, i didn't realize you were wanting to publish to the rest service. I copied your code from above and it displayed the output as it should...

Capture.PNG

0 Kudos