Using a Python Script Exported from ModelBuilder to Modify Hosted Feature Data

192
5
Jump to solution
07-07-2022 11:32 AM
BrantCarman
Occasional Contributor

Hi, I have a model that works with a couple of AGOL hosted feature layers to summarize the number of points within polygons, and then perform an add join on the result in order to field calculate those numbers into a field within the polygon layer.  The model works as expected, but exporting to a Python script and running the script doesn't seem to work.  I am actually running the script using Windows Task Scheduler, and have had success with this method with other python scripts exported from ModelBuilder.

I'm wondering if somebody here could take a look at the script I'm trying to use and let me know if there is anything obvious that could be done to make it work?  Thanks in advance for any assistance!

# -*- coding: utf-8 -*-
"""
Generated by ArcGIS ModelBuilder on : 2022-07-07 11:07:30
"""
import arcpy

def TrapperArea_SiteCounts():  # TrapperArea_SiteCounts

    # To allow overwriting outputs change overwriteOutput option to True.
    arcpy.env.overwriteOutput = True

    arcpy.ImportToolbox(r"c:\program files\arcgis\pro\Resources\ArcToolbox\toolboxes\Analysis Tools.tbx.tbx")
    L0AM_Trapper_Areas_Current = "https://services1.arcgis.com/cgAtrI0lk5jKC8Wy/arcgis/rest/services/AM_TrapperAreas_Ongoing/FeatureServer/0"
    L0AM_Trap_Sites_Current = "https://services1.arcgis.com/cgAtrI0lk5jKC8Wy/arcgis/rest/services/AM_Survey_ActiveSites_VL/FeatureServer/0"
    L0AM_Trapper_Areas_Current_2_ = "https://services1.arcgis.com/cgAtrI0lk5jKC8Wy/arcgis/rest/services/AM_TrapperAreas_Ongoing/FeatureServer/0"

    # Process: Summarize Within (Summarize Within) (analysis)
    c0_SummarizeWithin = "C:\\Users\\bcarman.SSV\\Washington State Executive Branch Agencies\\WSDA-PP - LID - General\\Automations\\Automation Scripts\\Apple Maggot\\AM Automation Models\\AM Automation Models\\Default.gdb\\c0_SummarizeWithin"
    Output_Grouped_Table = ""
    arcpy.analysis.SummarizeWithin(in_polygons=L0AM_Trapper_Areas_Current, in_sum_features=L0AM_Trap_Sites_Current, out_feature_class=c0_SummarizeWithin, keep_all_polygons="KEEP_ALL", sum_fields=[], sum_shape="ADD_SHAPE_SUM", shape_unit="", group_field="", add_min_maj="NO_MIN_MAJ", add_group_percent="NO_PERCENT", out_group_table=Output_Grouped_Table)

    # Process: Add Join (Add Join) (management)
    Updated_Input_Layer_or_Table_View = arcpy.management.AddJoin(in_layer_or_view=L0AM_Trapper_Areas_Current_2_, in_field="area_name", join_table=c0_SummarizeWithin, join_field="area_name", join_type="KEEP_ALL", index_join_fields="NO_INDEX_JOIN_FIELDS")[0]

    # Process: Calculate Field (Calculate Field) (management)
    L0AM_Trapper_Areas_Current_ = arcpy.management.CalculateField(in_table=Updated_Input_Layer_or_Table_View, field="L0AM_Trapper_Areas__Current.active_trap_count", expression="$feature['c0_SummarizeWithin.Point_Count']", expression_type="ARCADE", code_block="", field_type="TEXT", enforce_domains="NO_ENFORCE_DOMAINS")[0]

if __name__ == '__main__':
    # Global Environment settings
    with arcpy.EnvManager(scratchWorkspace=r"C:\Users\bcarman.SSV\Washington State Executive Branch Agencies\WSDA-PP - LID - General\Automations\Automation Scripts\Apple Maggot\AM Automation Models\AM Automation Models\Default.gdb", workspace=r"C:\Users\bcarman.SSV\Washington State Executive Branch Agencies\WSDA-PP - LID - General\Automations\Automation Scripts\Apple Maggot\AM Automation Models\AM Automation Models\Default.gdb"):
        TrapperArea_SiteCounts()
0 Kudos
1 Solution

Accepted Solutions
DannyMcVey
Esri Contributor

I see a problem on line 12...  "*.tbx.tbx" doesn't look right

 

View solution in original post

0 Kudos
5 Replies
JeffK
by MVP Regular Contributor
MVP Regular Contributor

It looks like your path in line 18, c0_SummarizeWithin is missing its raw decorator (r' ... ') and it could be tripping on the spaces in the path.

DannyMcVey
Esri Contributor

I see a problem on line 12...  "*.tbx.tbx" doesn't look right

 

0 Kudos
MatthewBeversdorf
New Contributor

You may also have to authenticate with AGOL to access the services.  Perhaps use the arcgis.gis module | ArcGIS API for Python.  

# Usage Example 2: Built-in Login to ArcGIS Online

gis = GIS(username="someuser", password="secret1234")

 

RhettZufelt
MVP Frequent Contributor

I suspect it might be a permissions issue.  Running as a model you are logged into Pro with AGOL user credentials that have permissions to perform the update.

Task Scheduler runs as a windows user (you can change which user), but don't know if you can authenticate with AGOL unless you are using single sign on.

BrantCarman
Occasional Contributor

Wow, thanks for all of the ideas!  @DannyMcVey  had the solution with the error on line 12: "*.tbx.tbx" should just have been "*.tbx"

After changing that, the script seems to be running perfectly!  I did mess with adding the arcgis.gis module and AGOL user credentials, but that didn't seem to make a difference.  Not sure why... maybe something to do with ArcPro being on the same machine and having my credentials saved already?

Thanks again for the quick suggestions!