Select to view content in your preferred language

Python script works in Catalog but fails as a GPService in Flex

686
3
10-19-2013 02:35 PM
AndrewBlakey1
Emerging Contributor
Hello:

I've got a script that works consistently in Catalog/Desktop  but it fails unexpectedly once published as Geoprocessing Service.  It's driving me nuts and I hope I'm missing something obvious.

Also: What are good ways to troubleshoot it? Is there a way I can force Server to keep the resulting data so I can look at it? Can I get Flex to output messages at my will from the script?  Ie. the way I can arcpy.addMessage("var X broke!")

The code below breaks at the last line I have provided with the error:

"C:\arcgisserver\directories\arcgissystem\arcgisinput\rq10.GPServer\extracted\v101\published_data\rq.py", line 120, in <module>
    boxWidth = boxXMax-boxXMin
NameError: name 'boxXMax' is not defined

Failed to execute (rq).


I assume the error is because boxXMax is never being made, which I assume because row3 is null because there are no entries for the SearchCursor.  But why is that data coming back empty?   How could I test or investigate this?

# Esri start of added imports
import sys, os, arcpy
# Esri end of added imports

# Esri start of added variables
g_ESRI_variable_1 = u'%scratchGDB%\\inputPoint'
g_ESRI_variable_2 = u'%scratchGDB%\\waterbodyPoly'
g_ESRI_variable_3 = u'%scratchGDB%\\waterbodyBuffer'
g_ESRI_variable_4 = u'%scratchGDB%\\waterbodyBound'
g_ESRI_variable_5 = u'angle'
g_ESRI_variable_6 = u'%scratchGDB%\\waterbodyBuffLine'
g_ESRI_variable_7 = os.path.join(arcpy.env.packageWorkspace,u'scratch.gdb\\pathLines')
g_ESRI_variable_8 = u'%scratchGDB%\\intersectPoints'
g_ESRI_variable_9 = u'%scratchGDB%\\intersectSinglePts'
g_ESRI_variable_10 = u'%scratchGDB%\\waypoints'
# Esri end of added variables

#Imports
import arcpy
from arcpy import env
from math import radians, tan, cos

env.overwriteOutput = True

env.workspace = arcpy.env.packageWorkspace
env.scratchWorkspace = "c:\published_data\scratch.gdb"


### INPUTS / OUTPUTS ###
wBody = arcpy.GetParameterAsText(0)
inPoint = arcpy.GetParameterAsText(1)
transSpacing = float(arcpy.GetParameterAsText(2))
shorelineBuffer = float(arcpy.GetParameterAsText(3))  
outTransect = arcpy.GetParameterAsText(4)
rotation_method = arcpy.GetParameterAsText(5)

#Project Data
sr = arcpy.SpatialReference()
sr.factoryCode = 32617
sr.create()

#wbpoly waterbodypoly
#inpoint inputpoint
inputPoint = arcpy.Project_management(inPoint,g_ESRI_variable_1,sr)
waterbodyPoly = arcpy.Project_management(wBody,g_ESRI_variable_2,sr)


### VARIABLES ###
kfPerimeter = shorelineBuffer - (transSpacing / 2)
sRef = arcpy.Describe(waterbodyPoly).spatialReference

# Get start point from geometry
startPoint = arcpy.Point()
descInPoint = arcpy.Describe(inputPoint)
sfName = descInPoint.ShapeFieldName

rows = arcpy.SearchCursor(inputPoint)
for row in rows:
    feat = row.getValue(sfName)
    pnt = feat.getPart()
    startPoint.X = pnt.X
    startPoint.Y = pnt.Y
    startPoint.ID = 0

# New empty polyline FC.
arcpy.CreateFeatureclass_management(env.scratchGDB,"pathLines", "POLYLINE", spatial_reference = sRef)

# Buffer Waterbody
arcpy.Buffer_analysis(waterbodyPoly,g_ESRI_variable_3,0.0 - shorelineBuffer)

# Get Bounding Box
arcpy.MinimumBoundingGeometry_management(g_ESRI_variable_3,g_ESRI_variable_4,"RECTANGLE_BY_WIDTH")

# Add "angle" Field
arcpy.AddField_management(g_ESRI_variable_4,"angle","DOUBLE")

# Calculate Angle

arcpy.CalculatePolygonMainAngle_cartography(g_ESRI_variable_4, g_ESRI_variable_5, rotation_method)

# Get Angle as var
sCur = arcpy.SearchCursor(g_ESRI_variable_4)
for row2 in sCur:
    angle = row2.angle

# Waterbody to polyline
arcpy.PolygonToLine_management(g_ESRI_variable_3, g_ESRI_variable_6)

# Build Transect Lines based on primary angles

# Get bounding box extents
bBoxCur = arcpy.SearchCursor(g_ESRI_variable_4)
for row3 in bBoxCur:
    geom = row3.shape
    ext = geom.extent  
    boxXMin = ext.XMin
    boxXMax = ext.XMax
    boxYMin = ext.YMin
    boxYMax = ext.YMax

boxWidth = boxXMax-boxXMin
Tags (2)
0 Kudos
3 Replies
MatthewLewis
Deactivated User
Hello, Make sure your using FeatureSets in your Geoprocessing tool.

Hopefully this help will guide you

http://resources.arcgis.com/en/help/main/10.2/index.html#//002z0000001n000000
0 Kudos
KevinHibma
Esri Regular Contributor
Using Flex (or any other WebAPI) probably isn't the best method for testing/debugging a GP Service. You can do it sure, but I usually find it quicker just to consume the service in ArcMap. This way you don't need to worry about writing to console or popping up boxes or what not to read GP Service messages. Just turn them to INFO on the service, consume in ArcMap and see the Result messages.

For your error at hand, based on it - yeah if you're seeing undefined, it's like it hasn't gone into that cursor and set the variable.
However, your cursor, bBoxCur = arcpy.SearchCursor(g_ESRI_variable_4)" is using that variable_4.
variable_4 is used in GP tools all through your script before the cursor, so my best guess would say that variable is not empty, otherwise it would have failed before that.

I'd put some AddMessage's in to see where its getting to and whats happening.

bBoxCur = arcpy.SearchCursor(g_ESRI_variable_4)
for row3 in bBoxCur:
    geom = row3.shape
    ext = geom.extent  
    arcpy.AddMessage(ext)
    boxXMin = ext.XMin
    boxXMax = ext.XMax
    boxYMin = ext.YMin
    boxYMax = ext.YMax
0 Kudos
AndrewBlakey1
Emerging Contributor
Kevin:

Of course! Using it in ArcMap!  I knew you could do that but for some reason it just didn't click and I thought my options were to use it in a web API or test it tediously in the REST service in a browser.

Reinvigorated with a much easier way to troubleshoot, I'm off to debug.  I'll return with results when available.
0 Kudos