Geoprocessing Rest Service: Configure returned Raster Output?

886
0
11-15-2016 09:36 AM
PeterWilson
Occasional Contributor III

I've created a python script that I've published as a geoprocessing service. The python script takes as input an SGID, a unique id associated with a land parcel, landuse indicating crop fields irrigated and non-irrigated. The map is exported to a jpeg image for each SGID supplied to the python script. I've added arcpy.SetParameter(3, output_jpeg) at line 52 to return the output jpeg to the geoprocessing service.

'''
Created on 12 Sep 2016

Generate a JPEG

for a land parcel

indicating the irrigation

status based on SGID

@author: PeterW
'''
# import site-packages and modules
import math
import arcpy

# set arguments
input_sgid = arcpy.GetParameterAsText(0)
input_mxd = arcpy.GetParameterAsText(1)
output_folder = arcpy.GetParameterAsText(2)

# set environment settings
arcpy.env.overwriteOutput = True


# function to export map document to jpeg based on sgid
def vandv_ddp_jpegs(input_mxd, input_sgid, output_folder):
    """ export map document to jpeg based on sgid """
    mxd = arcpy.mapping.MapDocument(input_mxd)
    df = arcpy.mapping.ListDataFrames(mxd)[0]
    lyrs = arcpy.mapping.ListLayers(mxd, "", df)
    elm_title = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "sgkey")[0]
    try:
        sqlquery = "{0} = {1}".format("SGID", input_sgid)
        arcpy.MakeFeatureLayer_management(lyrs[0], "Cadastre_lyr", sqlquery)
        with arcpy.da.SearchCursor("Cadastre_lyr", ["SGID", "ID", "SHAPE@"]) as scur1:  # @UndefinedVariable
            for row in scur1:
                elm_title.text = row[1]
                SGID = row[0]
                def_query1 = "{0} = {1}".format("SGID", SGID)
                lyrs[0].definitionQuery = def_query1
                lyrs[1].definitionQuery = def_query1
                df.extent = row[2].extent
                df.scale = int(math.ceil(df.scale/5000)*5000)
                arcpy.RefreshActiveView()
                output_jpeg = "{0}\\{1}.jpg".format(output_folder, SGID)
                arcpy.AddMessage("Exporting SGID {0} jpeg".format(SGID))
                arcpy.mapping.ExportToJPEG(mxd, output_jpeg, resolution=150)
                lyrs[0].definitionQuery = ""
                lyrs[1].definitionQuery = ""
                arcpy.SetParameter(3, output_jpeg)
    except arcpy.ExecuteError:
                print(arcpy.GetMessages(2))

vandv_ddp_jpegs(input_mxd, input_sgid, output_folder)
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Within the ArcToolbox parameter settings, I've set the output parameter Output JPEG as a Raster Dataset Data Type and Parameter Type as Derived and Output.

I then ran the python script locally, in order to publish the result as a geoprocessing service.

I copied the REST URL: http://172.16.1.16:6080/arcgis/rest/services/Geoprocessing_VandV/ExportToJPEG/GPServer  into my browser in order to execute it.

The output raster returned is renamed adding "_ags_" prefix, and saved out as TIFF format. and if I rerun the geoprocessing service for the same SGID, it doesn't overwrite the raster but adds a suffix "1".

{
 "results": [{
  "paramName": "Output_JPEG",
  "dataType": "GPRasterDataLayer",
  "value": {
   "url": "http://172.16.1.16:6080/arcgis/rest/directories/arcgisoutput/Geoprocessing_VandV/ExportToJPEG_GPServ...",
   "format": "tif"
  }
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Is there a way to prevent this from happening. I need the raster to be jpeg's not tiff's and the name cant' be changed it needs to match the SGID. The rasters also need to be overwritten if they exist as the data is constantly being updated so the jpegs need to reflect the updates each time they are rerun as they are being imbeded into a SSRS report that is being sent out to the owners of the land parcels. 

I've tried saving the output as a string rather, but unfortunately the path is not returned as a URL but as an absolute path that can't be used by the SSRS report request. Any help will be appreciated.

Python ; Geoprocessing ; ArcGIS for Server ; https://community.esri.com/community/developers/web-developers/arcgis-rest-api 

0 Replies