Select to view content in your preferred language

Portal hosted geoprocessing service won't put out

182
0
05-13-2024 03:43 PM
AlcoGISUser
New Contributor III

I created a script tool in ArcGIS Pro that prompts user to input a .csv file as an address table and then designate an output csv path.

The tool uses a geocoding service hosted on our enterprise portal and uses an ArcGIS Server based token to access the geocoding service.

I ran the tool and it works fine in ArcGIS Pro. I then ran it in jupyter notebooks outside of ArcGIS Pro pointing to a cloned python environment. This also worked fine.

I then published the tool to our enterprise portal hoping to use it as a geoprocessing tool in Web App Builder. I've checked the box to allow exports as part of the process but when I run the tool in WAB I get a token error. There is no option in WAB geoprocessing widget to designate output location.

 

Anyway I can do this so the geocoding outputs are written to a user specified path? I'm hoping that nothing higher than a viewer license is needed for this as the output is not being stored on portal, the geocoding service is hosted on our portal using our own internal reference data, and token is being used to access the portal histed geocoding service and geocoding geoprocessing service.

 

Here is my code:

 

"""This a tool in python that can be published from ArcGIS Pro to enterprise ArcGIS Portal. The tool should be able to geocode
a .csv file listing addresses."""

import arcpy
from arcgis.gis import GIS
from arcgis.geocoding import geocode, get_geocoders
import pandas as pd

# Get the input parameters
csv_path = arcpy.GetParameterAsText(0)
single_line_address = arcpy.GetParameterAsText(1) # This should be a boolean parameter
output_csv = arcpy.GetParameterAsText(2)

# Format output title with .csv extension and file directory
output_csv = output_csv + ".csv"

# Connect to the GIS
gis = GIS("https://myportal.org/arcgis", api_key='Mytokenplaceholder')

# Get the first geocoder
geocoders = get_geocoders(gis)

# Distill the composite locator that uses SPAD, parcels and street files for reference data
for geocoder in geocoders:
if "SPAD" in str(geocoder):
SPAD_Comp_locator = geocoder
arcpy.AddMessage(f"SPAD locator found: {SPAD_Comp_locator}")
break

# Read the CSV file
df = pd.read_csv(csv_path)

# Create an empty DataFrame to store the results
results_df = pd.DataFrame(columns=['address', 'location', 'score'])

# Loop through the rows of the DataFrame
for index, row in df.iterrows():
# If the address is in a single field, use that field as the address
if single_line_address:
address = row['Address']
# Otherwise, concatenate the city, state, and zip fields
else:
address = f"{row['Address']}, {row['City']}, {row['State']}, {row['Zip']}"

# Geocode the address
geocode_result = geocode(address, geocoder=SPAD_Comp_locator)

# Print the result
print(geocode_result[0]['address'], geocode_result[0]['location'], geocode_result[0]['score'])

# If the geocode result is not empty, add it to the results DataFrame
if geocode_result:
result = geocode_result[0]
results_df.loc[index] = [result['address'], result['location'], result['score']]

# Write the results DataFrame to a new CSV file in the output directory
results_df.to_csv(output_csv, index=False)

0 Kudos
0 Replies