My Python tool works in ArcGIS Pro but not as a Geoprocessing service!

847
5
Jump to solution
01-25-2022 03:11 PM
tristan_goers_fnsb
New Contributor II

I am trying to develop a Geoprocessing tool that creates a report from my parcel service in a Web AppBuilder, using the Geoprocessing widget.

I have included my code below, it is not complete yet as I am planning to create a PDF. I am currently outputting a text file with all the data I want to  include. 

I am trying to be able to select a parcel polygon in the Web AppBuilder, then output that data to a report. When I test the script in ArcGIS Pro it works! When I select the same parcel in Web AppBuilder a URL is returned linking to the text file, however no parcel data is in the text file (As seen below).

Code:

import arcpy
import os
import sys

def ParcelData(parcels):
  arcpy.AddMessage("ParcelData...")
  fields = []
  try:
    desc = arcpy.Describe(parcels)
    FIDSet = desc.FIDSet
    record = [int(FID) for FID in FIDSet.split(";")] if FIDSet else []
    record = record[0]
    fields = arcpy.da.SearchCursor(parcels, "*", where_clause="OBJECTID=" + str(record))
  except:
    try:
      pan = 403539
      fields = arcpy.da.SearchCursor(parcels, "*", where_clause="PAN=" + str(pan))
    except Exception as e:
      arcpy.AddMessage(e)

  return fields

def PDFGenerator(in_data, pdf_name):
  fields = ParcelData(in_data)
  arcpy.AddMessage(fields)
  arcpy.AddMessage("PDFGenerator...")
  try:
    record = ()
    file = os.path.join(arcpy.env.scratchFolder, pdf_name + ".txt")
    text = open(file, 'w')
    with fields as cursor:
      for row in cursor:
        record = row
        arcpy.AddMessage(record)
    text.write(str(record) + " TEST!!!")
    text.close()
  except Exception as e:
    arcpy.AddMessage(e)
    file = os.path.join(arcpy.env.scratchFolder, pdf_name + ".txt")
    text = open(file, 'w')
    text.write(str(e))
    text.close()

  arcpy.SetParameter(2, file)
  return file

if __name__ == '__main__':
  arcpy.AddMessage("Starting...")
  arcpy.AddMessage("Parcels...")
  try:
    in_data = arcpy.management.MakeFeatureLayer(arcpy.GetParameter(0), 'parcels')
    arcpy.AddMessage("- Feature Layer")
  except:
    in_data = arcpy.GetParameter(0)
    arcpy.AddMessage("- Layer")

  pdf_name = arcpy.GetParameterAsText(1)
  arcpy.AddMessage("Output PDF...")

  PDFGenerator(in_data, pdf_name)

 ArcGIS Pro Results:

(468169, 'NORTH ADDITION', None, '01', 'Lot', None, 'tbd parcels doc 01S01W10_obj43861', None, None, None, 'Northeast City', 'Exempt ', 'Shoreway Drive Park', 'FAIRBANKS AK 99701 4615', '', 0.0, 2155.0, '800 CUSHMAN ST', 19.56, 'FAIRBANKS CITY OF', '', '', 'FAIRBANKS, CITY OF', 2021, 2155.0, 24261, 42826.871352714435, 863.0507574903871, '{A5D60ADF-F7B1-4B38-8FDC-B2DB9A8DD764}', (1372593.2270107588, 3966946.471093751)) TEST!!!

Web AppBuilder Results:

() TEST!!!
0 Kudos
1 Solution

Accepted Solutions
tristan_goers_fnsb
New Contributor II

I took your advice and included some logging in my script. I found that although the FIDSet did not appear when using arcpy.da.Describe(), the JSON data of the selected polygons did. I changed my strategy for accessing the data.

View solution in original post

0 Kudos
5 Replies
by Anonymous User
Not applicable

Is the user you have for the service able to access the data?  Might try adding some logging to the script to output what is going on.

tristan_goers_fnsb
New Contributor II

The Describe function returns the FIDSet on ArcGIS Pro but not as a GP Service...

 

GP Service:

{'catalogPath': 'in_memory\\feature_set', 'FIDSet': None, 'aliasName': 'feature_set'

ArcGIS Pro:

{'catalogPath': 'https://gisportal.fnsb.gov/referenced/rest/services/_/Parcels_with_TaxInfo/FeatureServer\\1', 'isArchived': False, 'FIDSet': [7430], 'aliasName': 'Parcels_with_TaxInfo', 

 

Both are pointing to the same service.

0 Kudos
by Anonymous User
Not applicable

I'd add some logging to the script so you can look at how it performs while being ran as a service. The in_data isn't getting parsed correctly somewhere and there are a lot of places to look for issues. Maybe look at the input types- input Types

You may want to handle the record = record[0] explicitly so it handles the index of range that will get raised trying to access index 0 if record is [].

record = [int(FID) for FID in FIDSet.split(";")][0] if FIDSet else []

if record:
   fields = arcpy.da.SearchCursor(parcels, "*", where_clause="OBJECTID=" + str(record))
else:
    try:
      pan = 403539
      fields = arcpy.da.SearchCursor(parcels, "*", where_clause="PAN=" + str(pan))
    except Exception as e:
      arcpy.AddMessage(e)

 

0 Kudos
tristan_goers_fnsb
New Contributor II

I took your advice and included some logging in my script. I found that although the FIDSet did not appear when using arcpy.da.Describe(), the JSON data of the selected polygons did. I changed my strategy for accessing the data.

0 Kudos
AzinSharaf
Occasional Contributor II

- what is the output of arcpy.env.scratchFolder when you run it with arcgis server service account?

- haven't you missed a single quote in the WHERE clause?

0 Kudos