Select to view content in your preferred language

I cannot save queried features as file gdb on AGOL using python

120
1
Sunday
mike_totti
New Contributor

Hi,

I am trying to save the query results as a file gdb using python (arcgis api for python) but keep getting this error:

"Failed to export to File Geodatabase: The Product License has not been initialized."

I googled about it but could not find any solution. 

I do appreciate any help.

Thanks

Here is my code:

from arcgis.gis import GIS
from arcgis.features import FeatureLayer
from datetime import datetime
import os

try:
    # Connect to Portal
    org = '**unspecified**'
    username = os.environ.get('AGOL_SCRIPT_USER')
    password = os.environ.get('AGOL_SCRIPT_PASS')

    # Initialize GIS object with license check
    gis = GIS(org, username, password)

    # Check if the GIS object is valid
    if not gis:
        raise Exception("Failed to initialize ArcGIS Online connection")

    print("Connected to ArcGIS Online")

    # Define feature service and query parameters
    feature_layer_id = "**unspecified**"

    # Get the feature layer
    feature_layer_item = gis.content.get(feature_layer_id)
    feature_layer = FeatureLayer.fromitem(feature_layer_item)

    # Define query parameters
    where_clause = "requestorCode = 'R7'"
    out_fields = "*" 

    # Perform the query
    query_result = feature_layer.query(where=where_clause,
                                       out_fields=out_fields,
                                       return_geometry=True)

    # Check if there are features returned
    if not query_result.features:
        raise Exception("No features matching the query criteria")

    # FGDB name
    file_gdb_name = "test.gdb"

    # Define the save location for the File Geodatabase
    gdb_path = f"/arcgis/home/{file_gdb_name}"

    # Save the query result to a File Geodatabase
    query_result.save(gdb_path, f"{file_gdb_name.split('.')[0]}")

    print(f"Exported queried output to File Geodatabase: {gdb_path}")

except Exception as e:
    print(f"Failed to export to File Geodatabase: {str(e)}")
0 Kudos
1 Reply
MobiusSnake
MVP

I could be wrong but I believe this method requires an ArcPy license.  A few things to check:

  • Do you have access to ArcPy (this would be via ArcGIS Pro)?
  • Is the Python interpreter you're using to run this the same one that's currently activated for ArcGIS Pro?
  • If you're using an AGOL/Enterprise-based license for Pro, are you logged in?
0 Kudos