Pass a null result back to a dynamic map Service from a geoprocessing task

653
18
10-02-2012 10:23 PM
SusanJones
Occasional Contributor II
Howzit All,

I have created a Geoprocessing Service that creates a Heat Map using the Kernel Density Tool. The service works well, and quickly when a feature selection is given to it. However, the tool fails when the feature layer selection returns no records and a null Raster Layer is passed back to the AGS Dynamic Map Service Layer.

Geoprocssing Overview:
- create a Feature Layer from a sql expression
- generate Kernel Density Layer
- set Layer into resulting Dynamic Map Service Layer as a Raster Layer

The task fails when a null Raster Layer does not exist and is passed back to the out Layer using.
arcpy.SetParameterAsText(6,outRaster)

The task succeeds when the heatMap is created from > 1 features in the selection.

Setting arcpy.SetSeverityLevel(0) ensures the task completes.

I think the problem occurs at the map services level because the task completes.

Question:
How can I pass a null Layer back to a Raster Layer in a AGS Dynmaic Map Service Layer from a Geoprocessing Result?

Susan

North South GIS
Auckland, New Zealand
0 Kudos
18 Replies
ShingLin
Esri Contributor
Susan,

You can modify the code for output paramter so there is no result map service returning when the selections result is zero. Here are two options:

(1) If the selections result is zero, set the outputpath of the kernel density path to blank and do not execute the kernel density function. Also, set theoutput parameter as a derived output in the script tool.

Here is the code looks like

import arcpy, os
from arcpy.sa import *

inputval = arcpy.GetParameterAsText(0)

inputpts = r'c:\KernelDensity\data.gdb\Crime'
outputpts = os.path.join(arcpy.env.scratchGDB, "selectcr")
outraster = os.path.join(arcpy.env.scratchGDB, "KernelD_sele1")

query = "ID < " + inputval
arcpy.Select_analysis(inputpts  , outputpts, query)

result = arcpy.GetCount_management(outputpts)
count = int(result.getOutput(0))

if count == 0 :
     arcpy.AddWarning("count = 0")
     outraster = ""
else:
     outKDens = KernelDensity(outputpts, "NONE", 45, 1200, "SQUARE_KILOMETERS")
     outKDens.save(outraster)

arcpy.SetParameter(1, outraster)

(2) Set the output parameter only when the the selection result is zero. Then set the output paramter as optional in the script tool.

import arcpy, os
from arcpy.sa import *

inputval = arcpy.GetParameterAsText(0)
outraster = arcpy.GetParameterAsText(1)

inputpts = r'c:\KernelDensity\data.gdb\Crime'
outputpts = os.path.join(arcpy.env.scratchGDB, "selectcr")

query = "ID < " + inputval
arcpy.Select_analysis(inputpts  , outputpts, query)

result = arcpy.GetCount_management(outputpts)
count = int(result.getOutput(0))
arcpy.AddMessage(" The select count = " + str(count))

if count == 0:
     arcpy.AddWarning("count = 0")
     arcpy.SetParameter(1, "")
else:
     outKDens = KernelDensity(outputpts, "NONE", 45, 1200, "SQUARE_KILOMETERS")
     outKDens.save(outraster)
0 Kudos
SusanJones
Occasional Contributor II
Thanks for getting back about this one. I have tried passing back a null string. The tool completes by first setting the environment by arcpy.SetSeverityLevel(0)

#TODO: set Output
arcpy.AddMessage("***set The Output Layer")
if arcpy.GetCount_management("inFeatures1")>0:
    arcpy.AddMessage("SETTING VALID RASTER")
    arcpy.SetParameter(6,outRaster)
else:
    arcpy.AddMessage("SETTING INVALID RASTER")
    arcpy.SetParameter(6,"")

I am attaching the attached message when we run from the REST Endpoint.
0 Kudos
ShingLin
Esri Contributor
Please note that the output parameter must be optional or derived, then setting the path to empty will work.
0 Kudos
SusanJones
Occasional Contributor II
Hi and Thanks for this,

We have been running with the derived output and then tried switching to the optional output with the same result.

There is no problem when the feature selection is greater than 0. The raster layer returns nice and beautifully into the dynamic map service layer at the conclusion of the script.

We have noticed that the following statement does not fire

#TODO: wont Fire
if arcpy.GetCount_management(lyr)==0:
  arcpy.SetParameter(6,"")
  sys.exit(0)

However, casting the parameter as a string will fire,

#TODO: will Fire
if str(arcpy.GetCount_management(lyr))=="0":
  arcpy.SetParameter(6,"")
  sys.exit(0)

It is probably another issue either with the syntax or the way the function is parsed through python.

I didn't get a chance to test this with ArcGIS Server 10.0, so cannot sayif this is new behaviour.

Susan
0 Kudos
ShingLin
Esri Contributor
What version of server are you using?
My example is on 10.1 server. I have not tried previous version of server.
0 Kudos
SusanJones
Occasional Contributor II
We recently upgraded all the ArcGIS Infrastructure to ArcGIS 10.1 release.

I am not sure if the issue is in the scripting,but rather the way the parameter is parsed into the AGS Dynamic Map Service Layer which is giving problems.
0 Kudos
ShingLin
Esri Contributor
Can you share the data/script you wrote so i can verify where may go wrong? Or just copy the script that I sent to you? I did not get any fails with the script that I replied to you.
0 Kudos
SusanJones
Occasional Contributor II
Attached is the complete script. There is a toolbox which I have not attached becaus eit is probably not necessary.

outRaster is a derived Raster Output Layer.

We pass a load of comma separated strings in the beginning and spend a bit of time building up the sql expression for inFeatures1.

Thanks for checking it out.

Susan
0 Kudos
ShingLin
Esri Contributor
When you run your service with the query set to zero, did you see your "***set The Null Output" wriiten out? If not, can you try the approach in the code that I posted regarding to get count see if you get the message?
0 Kudos