Portal data with stand alone script?

1149
10
Jump to solution
09-01-2022 11:25 AM
AustinBachurski
New Contributor III

I'm trying to automate some workflow that happens weekly.  Got everything working except for removing the old and uploading the new data at the end.  When done manually in ArcGIS Pro this is done using the Delete Features tool, then the Append tool.  I put a model together to chain the two, I can run the model in Pro using the Python window and it works fine, but when I transfer the code to my script and run it stand alone it fails with a "ERROR 000732: Target Dataset: Dataset LayerName does not exist or is not supported"  - the layer is hosted on our enterprise portal, so I thought maybe I needed to  use 

 

 

from arcgis.gis import GIS
gis = GIS("http://my.portal.address/", "username", "password")
LayerName = gis.content.get('f6f63262826e40adbc34e8d01ff9b3aa')

 

 

But this also fails with a 

"""

File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 4241, in DeleteFeatures
raise e
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 4238, in DeleteFeatures
retval = convertArcObjectToPythonObject(gp.DeleteFeatures_management(*gp_fixargs((in_features,), True)))
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 512, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
RuntimeError: Object: Error in executing tool

"""

I've tried entering everything into a Jupyter Notebook and viewing the file after assigning the portal path and it is displaying as expected, but I can't go any farther without errors.  If I use a local path and file everything works fine, and I've tried a few different layers from our portal and they all produce the same error.  Hoping for some help, thank you.

0 Kudos
1 Solution

Accepted Solutions
AustinBachurski
New Contributor III

I was finally able to get it figured out late last night with some help from Esri support and a lot of googling, the syntax to login to the portal with a stand alone script is different than the syntax used in a notebook, which is what I was using previously (I think)...   So this section was totally unnecessary.

from arcgis.gis import GIS
gis = GIS("http://my.portal.address/", "username", "password")
LayerName = gis.content.get('f6f63262826e40adbc34e8d01ff9b3aa')

 

After removing that I just had to add this to the code prior to trying to interact with the portal item.

arcpy.SignInToPortal(arcpy.GetActivePortalURL(), 'USERNAME', 'PASSWORD')

 

Then I had to change the target for the file to the web address for the map server.  I did have to add a "/0" to the end of the web address, if I'm honest I'm not sure why that is - guessing it selects the specific layer in the feature?  Not sure, but it wouldn't work without it...

CrimeWebData = "https://portal.address/FeatureServer/0"

 

After making those changes it works as expected.  Thanks for the help!

 

View solution in original post

10 Replies
dgiersz_cuyahoga
Occasional Contributor

"gis.content.get()" returns an Item. Sounds like you need a layer inside that Item. There is a layer.fromitem method that should work or Item has a "layers" property.

myItem = gis.content.get('f6f63262826e40adbc34e8d01ff9b3aa')
myLayer = myItem.layers[0]
myLayer
#CLE #sloth
0 Kudos
AustinBachurski
New Contributor III

Putting that into the notebook to inspect, that produces the url for the map service, but I still get the same error when trying to run the delete operation.

0 Kudos
dgiersz_cuyahoga
Occasional Contributor

How is the layer referenced in the python script exported from Modelbuilder? Is it url to the layer?

#CLE #sloth
0 Kudos
AustinBachurski
New Contributor III

In the exported script it just has the name of the layer as displayed in the Contents pane.

0 Kudos
dgiersz_cuyahoga
Occasional Contributor

When I export a Model to python window, it shows the Model Environment settings and one is the URL to the layer, like layername = url to endpoint

Then when it calls the delete features, it uses that for the in_features parameter:

DeleteFeatures(in_features=layername)

Unfortunately, I don't have any data handy to test this with. So I can't run it myself. 🙂

 

#CLE #sloth
0 Kudos
AustinBachurski
New Contributor III

So this is a direct copy/paste from the python window when I click "Export>Send to Python Window" - which works in Pro, but putting that into a standalone script results in the "ERROR 000732: Input Features: Dataset CrimeWebMap does not exist or is not supported   Failed to execute (DeleteFeatures)." error.  

 

# -*- coding: utf-8 -*-
"""
Generated by ArcGIS ModelBuilder on : 2022-09-01 13:17:09
"""
import arcpy

def UpdateCrimeMap():  # Update Crime Map

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

    Backup = "Backup"
    CrimeWebMap = "CrimeWebMap"

    # Process: Remove Old Data (Delete Features) (management)
    Old_Data_Cleared = arcpy.management.DeleteFeatures(in_features=CrimeWebMap)[0]

    # Process: Append New Data (Append) (management)
    Web_Map_Updated = arcpy.management.Append(inputs=[Backup], target=Old_Data_Cleared, schema_type="TEST", field_mapping="", subtype="", expression="")[0]

if __name__ == '__main__':
    # Global Environment settings
    with arcpy.EnvManager(scratchWorkspace=r"J:\Austin\Crime Map Backup\CurrentWebmapData\ProMapping\ProMapping.gdb", workspace=r"J:\Austin\Crime Map Backup\CurrentWebmapData\ProMapping\ProMapping.gdb"):
        UpdateCrimeMap()

 

 

0 Kudos
dgiersz_cuyahoga
Occasional Contributor

Change the CrimeWebMap value to the URL of the endpoint.

#CLE #sloth
0 Kudos
AustinBachurski
New Contributor III

Produces the same 732 error "does not exist or is not supported".   

0 Kudos
by Anonymous User
Not applicable

Doing it in Pro sets the workspace so you can use just the names and it will find it like it did.  Since workspace is not set in the standalone script, you need to include the full path to the dataset. The second thing is there is a difference between arcpy and ArcGIS for Python and there is some overlap, but generally its:

local data = arcpy

portal/agol = arcgis for python

I suspect it is the inner workings of Pro's C++ code to resolve commands based on arguments past in from Pro (Overloading). In the stand alone arcpy script, it is a direct call to the C++ method that is more specific to the inputs it will accept, such as a local Feature Layer, and not a hosted Feature Layer.

I think deleting/updating the hosted feature layer with ArcGIS for Python would be the appropriate mechanism to interact with that type of data.  Try using edit_features.

0 Kudos