Using a feature service as GP tool input

483
3
Jump to solution
06-27-2023 06:20 PM
LindsayRaabe_FPCWA
Occasional Contributor III

Hi brains trust. I'm writing a python script that uses a feature service (table) as an input for an Export Table tool. I have the code working perfectly on my PC but not on the server where I intend to schedule it. They are running the same versions of Pro and python. The code is below, along with the error I'm getting. Is there a better way to do this (like I mentioned, this works fine on my PC). 

# Variables - update these lines
SPDRoutes = "https://services3.arcgis.com/1GMgEwgl0qHnrBQf/arcgis/rest/services/Distances_Table/FeatureServer/0" # Feature service containing routes to be updated

import arcpy, time, os

# Overwrite Output
arcpy.env.overwriteOutput = True

import keyring
pw = keyring.get_password("ArcGISOnline", "MyUserName")
arcpy.SignInToPortal("https://fpcwa.maps.arcgis.com", "MyUserName", pw)
print("Connected to the GIS")

# Create temporary FGDB
sdproutesgdb = arcpy.CreateFileGDB_management(arcpy.env.scratchFolder, "sdproutes")
print ("Temp FGDB created")

arcpy.env.workspace = arcpy.env.scratchFolder + "\\sdproutes.gdb"
print ("Workspace set to temp FGDB")

# Compare existing SDP Routes to Updated SDP Harvest Plan
ExistingSDPRoutes = arcpy.conversion.ExportTable(
    in_table = SPDRoutes,
    out_table = arcpy.env.workspace + "\\ExistingSDPRoutes",
    where_clause = "",
    use_field_alias_as_name = "NOT_USE_ALIAS",
    field_mapping = 'Name "Name" true true false 1024 Text 0 0,First,#,Plantation SDP Route Distances,Name,0,1024;Total_Length "Total_Length" true true false 0 Double 0 0,First,#,Plantation SDP Route Distances,Total_Length,-1,-1;GlobalID "GlobalID" false false true 38 GlobalID 0 0,First,#,Plantation SDP Route Distances,GlobalID,-1,-1;CreationDate "CreationDate" false true true 8 Date 0 0,First,#,Plantation SDP Route Distances,CreationDate,-1,-1;Creator "Creator" false true true 128 Text 0 0,First,#,Plantation SDP Route Distances,Creator,0,128;EditDate "EditDate" false true true 8 Date 0 0,First,#,Plantation SDP Route Distances,EditDate,-1,-1;Editor "Editor" false true true 128 Text 0 0,First,#,Plantation SDP Route Distances,Editor,0,128'
)
print ("Existing SDP Route distances exported")

Exception: Failed to execute. Parameters are not valid.
ERROR 000732: Input Table: Dataset https://services3.arcgis.com/1GMgEwgl0qHnrBQf/arcgis/rest/services/Distances_Table/FeatureServer/0 does not exist or is not supported
Failed to execute (ExportTable).

Lindsay Raabe
GIS Officer
Forest Products Commission WA
0 Kudos
1 Solution

Accepted Solutions
LindsayRaabe_FPCWA
Occasional Contributor III

It's interesting. It was definitely working for me locally - just not when run server based. I've found an alternate solution now using the FeatureLayer module. Updated code below. 

 

# Variables - update these lines
SDPRoutes = "https://services3.arcgis.com/1GMgEwgl0qHnrBQf/arcgis/rest/services/Distances_Table/FeatureServer/0" # Feature service containing routes to be updated

import arcpy, time, os
from arcgis.gis import GIS
from arcgis.features import FeatureLayer

# Overwrite Output
arcpy.env.overwriteOutput = True

arcpy.CheckOutExtension("Network")
print ("Network Analyst license checked-out")

# Create GIS object
print("Connecting to AGOL")
### Access the stored password with keyring and sign into the GIS      # https://community.esri.com/t5/arcgis-online-blog/connect-to-the-gis-in-python-scripts-without/ba-p/889867
import keyring
pw = keyring.get_password("ArcGISOnline", "MyUserName")
gis = GIS("https://fpcwa.maps.arcgis.com", "MyUserName", pw)
print("Connected to the GIS")

# Create temporary FGDB
sdproutesgdb = arcpy.CreateFileGDB_management(arcpy.env.scratchFolder, "sdproutes")
print ("Temp FGDB created")

arcpy.env.workspace = arcpy.env.scratchFolder + "\\sdproutes.gdb"
print ("Workspace set to temp FGDB")
   
fl = FeatureLayer(SDPRoutes)
fs = fl.query()
ExistingSDPRoutes = fs.save(arcpy.env.workspace, "ExistingSDPRoutes")
print ("Existing SDP Route distances exported")
    
ExistingSDPRoutes = arcpy.management.CalculateField(
    in_table = ExistingSDPRoutes,
    field = "Name",
    expression = '!Name!.split("_")[0]',
    expression_type = "PYTHON3",
    code_block = "",
    field_type = "TEXT",
    enforce_domains = "NO_ENFORCE_DOMAINS"
)

 

 

Lindsay Raabe
GIS Officer
Forest Products Commission WA

View solution in original post

3 Replies
JoshuaSharp-Heward
Occasional Contributor III

Hi Lindsay,

I didn't think that you could use a feature service as an input to the Export Table geoprocessing tool? In the documentation https://pro.arcgis.com/en/pro-app/latest/tool-reference/conversion/export-table.htm it doesn't indicate that you can do that

JoshuaSharpHeward_0-1687929057269.png

I have managed to do something similar by having the web service in a map in an arcgis pro project before, and accessing the layer via ArcPy.

LindsayRaabe_FPCWA
Occasional Contributor III

It's interesting. It was definitely working for me locally - just not when run server based. I've found an alternate solution now using the FeatureLayer module. Updated code below. 

 

# Variables - update these lines
SDPRoutes = "https://services3.arcgis.com/1GMgEwgl0qHnrBQf/arcgis/rest/services/Distances_Table/FeatureServer/0" # Feature service containing routes to be updated

import arcpy, time, os
from arcgis.gis import GIS
from arcgis.features import FeatureLayer

# Overwrite Output
arcpy.env.overwriteOutput = True

arcpy.CheckOutExtension("Network")
print ("Network Analyst license checked-out")

# Create GIS object
print("Connecting to AGOL")
### Access the stored password with keyring and sign into the GIS      # https://community.esri.com/t5/arcgis-online-blog/connect-to-the-gis-in-python-scripts-without/ba-p/889867
import keyring
pw = keyring.get_password("ArcGISOnline", "MyUserName")
gis = GIS("https://fpcwa.maps.arcgis.com", "MyUserName", pw)
print("Connected to the GIS")

# Create temporary FGDB
sdproutesgdb = arcpy.CreateFileGDB_management(arcpy.env.scratchFolder, "sdproutes")
print ("Temp FGDB created")

arcpy.env.workspace = arcpy.env.scratchFolder + "\\sdproutes.gdb"
print ("Workspace set to temp FGDB")
   
fl = FeatureLayer(SDPRoutes)
fs = fl.query()
ExistingSDPRoutes = fs.save(arcpy.env.workspace, "ExistingSDPRoutes")
print ("Existing SDP Route distances exported")
    
ExistingSDPRoutes = arcpy.management.CalculateField(
    in_table = ExistingSDPRoutes,
    field = "Name",
    expression = '!Name!.split("_")[0]',
    expression_type = "PYTHON3",
    code_block = "",
    field_type = "TEXT",
    enforce_domains = "NO_ENFORCE_DOMAINS"
)

 

 

Lindsay Raabe
GIS Officer
Forest Products Commission WA
JoshuaSharp-Heward
Occasional Contributor III

Well I've learned two things today then! I didn't realise the "Save" method existed for feature sets in the ArcGIS API for Python, that seems very useful! Glad you've managed to find a solution 🙂