how do you return a file GDB from a python-tool based geoprocessing service

3830
0
11-04-2014 02:26 PM
TomRippetoe
Occasional Contributor

Hello,

 

I have created a python tool which

1) creates a new file geodatabase

2) extracts data from a Map Service via a REST call

3) creates a feature class in the fGDB created in step 1 via the JSONtoFeatures tool.

 

The tool works as expected when running ArcCatalog or ArcMap. I can publish the tool as a geoprocessing service with no problems, and then make calls against the GP service with no errors.  If I publish the service as a synchronous process and then use the Services Directory browser to execute a task, I get a bunch of messages saying that everything was processed without error. However, the fGDB is not returned.  If i publish the service as an asynch process and then submit the job, the "completed job" response gives me a URL, but when i browse to the URL, the response back is 'Invalid URL'.

 

In both cases (synch and asych), i can get on the server hosting the Service and see that the fGDB was created at the location i set for the arcpy,env.workspace (set as part of the execute method).  If i don't set arcpy.env.workspace, the script wont run as tool.

 

What step(s) or settings(s) am I missing to have the GP service return the file geodatabase?

 

Thank you.

 

parameter definition

    def getParameterInfo(self):         """Define parameter definitions"""          # Output Type         param0 = arcpy.Parameter(         displayName="Output type",         name="output_type",         datatype="GPString",         parameterType="Required",         direction="Input")          param0.filter.type = "ValueList"         param0.filter.list = ['fGDB', 'pGDB']          param1 = arcpy.Parameter(         displayName="Test Service Layer URL",         name="test_service_layer_url",         datatype="GPString",         parameterType="Required",         direction="Input")         param1.value = "http://xx.xx.xx.xx/arcgis/rest/services/testservice/MapServer/0"          param2 = arcpy.Parameter(         displayName="Code",         name="code",         datatype="GPString",         parameterType="Required",         direction="Input")         param2.value = "07040008"          param3 = arcpy.Parameter(         displayName="Source Field",         name="source_field",         datatype="GPString",         parameterType="Required",         direction="Input")         param3.value = "data_field_1"          param4 = arcpy.Parameter(         displayName="Output File",         name="outFile",         datatype="DEFile",         parameterType="Derived",         direction = "Output")          params = [param0, param1, param2, param3, param4]         return params

 

 

 

and the execute definition:

   def execute(self, parameters, messages):         try:                out_type = parameters[0].valueAsText.lower()              service_url = parameters[1].valueAsText             o = urlparse(service_url)             if not re.search(r"/\d+$", o.path):                 raise InputParameterError("Missing expected layer index at the end of the input parameter 'Test Service Layer URL'.")             query_url = service_url + "/query"              Code = parameters[2].valueAsText             where_clause = "data_field_2 = '" + huc8Code + "'"              data_field = parameters[3].valueAsText             outfields = []             outfields.append(data_field )              arcpy.env.workspace = r"C:\arcgisserver\directories\arcgisjobs\exportdata_gpserver"             out_folder_path = arcpy.env.workspace             messages.addMessage("output folder: " + out_folder_path)             out_name = ""             json_out_folder_path = arcpy.env.workspace             json_out_name = prependTimeStamp("output.json")                         out_fcName = data_field             messages.addMessage("output FC name: " + out_fcName)             messages.addMessage("output JSON name: " + json_out_name)                         if out_type == 'pgdb':                 out_name = prependTimeStamp("testPGDB.mdb")                 arcpy.CreatePersonalGDB_management(out_folder_path, out_name)             elif out_type == 'fgdb':                 out_name = prependTimeStamp("testFGDB.gdb")                 arcpy.CreateFileGDB_management(out_folder_path, out_name)              payload = {'where': where_clause, 'outfields': ', '.join(outfields), 'f':'pjson'}              with open(os.path.join(json_out_folder_path,json_out_name), 'wb') as handle:                 response = requests.get(query_url, params=payload, stream=True)                 print (response.status_code)                 for block in response.iter_content(1024):                     if not block:                         break                     handle.write(block)             arcpy.JSONToFeatures_conversion(json_out_name, os.path.join(out_name,out_fcName))              arcpy.SetParameter(4, os.path.join(out_folder_path, out_name))          except Exception as e:             arcpy.AddError(e)         return
0 Replies