Select to view content in your preferred language

.pyt spatial reference help?

1813
5
Jump to solution
10-29-2014 08:50 AM
KevinBell
Deactivated User

I need to make a python toolbox that takes a Lat, Long, buffer distance and returns a list of addresses.  I'm getting hung up on the spatial reference right now.  The buffer is created, but has no SR...   hmmm...  what am I doing wrong here?

import arcpy

import os

WKID = 4326 # WGS-1984

sr = arcpy.SpatialReference()

sr.factoryCode = WKID

sr.create()

arcpy.env.outputCoordinateSystem = sr

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 = "Long",

            parameterType = "Required",

            direction = "Input")

        params.append(param0)

       

        param1 = arcpy.Parameter(

                    displayName = "Longitude",

                    name = "lon",

                    datatype = "Long",

                    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.Buffer_analysis(ptGeometry, 'buffer', distance, \

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

            print 'made the buffer.'

        except Exception, ErrorDesc:

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

            messages.addErrorMessage(sErr)

        return

0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Esri Alum

Get rid of that whole part at the top of your script where you create the sr and set the environment. It'll only get executed once and may get clobbered by anything else. Instead set it on the point:

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

View solution in original post

0 Kudos
5 Replies
JasonScheirer
Esri Alum

Get rid of that whole part at the top of your script where you create the sr and set the environment. It'll only get executed once and may get clobbered by anything else. Instead set it on the point:

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

0 Kudos
KevinBell
Deactivated User

Thanks much Jason!

Now that I have the polygon in the correct location I'll use a search cursor to get intersecting addresses as text, and I need to simply return them as a comma separated string.  Building the string up will be easy enough. 

I want to publish this as a GP service so that we call it passing the lat/lon/buffer as parameters, and then return my address list. 

Are my Param3 datatype and direction correct?  How would I return the string from passing those parameters?

0 Kudos
JasonScheirer
Esri Alum

Yes. You'd do parameters[3].valueAsText = "returnvalue".

0 Kudos
KevinBell
Deactivated User

thanks.  I added parameters[3].valueAsText = 'laksdjflaskdjflksajdf' after the buffer code but I got an error saying:

ERROR:

ParameterObject: Set attribute: valueAsText does not exist

while this is still happening on the desktop rather from the server I'm unsure how I'd get the text back.  Would it print out in the gp window?

0 Kudos
JasonScheirer
Esri Alum

Oh, then use value rather than valueAsText.

0 Kudos