Follow-up: Utility Network, ArcPy.mp, and JSAPI Enable Map Product Delivery via Portal

1239
0
04-09-2021 02:06 PM
MarkCederholm
Occasional Contributor III
0 0 1,239

Because the deadline for submitting recorded user presentations for DevSummit 2021 was so tight (February 15!), there were some issues I ran into that I could not resolve in time, mainly revolving around the use of arcpy.mp in ArcGIS Server 10.8.1. As a workaround for the presentation, I created a standalone enterprise 10.8 install on a test box in Flagstaff, which was quite slow.

The recorded presentation is available here:

https://www.youtube.com/watch?v=19eQ7Gp1LoI

The slides and sample code are also available here:

http://www.pierssen.com/arcgis/python.htm

 

Arcpy.mp Errors

 

It turns out that most of the issues I encountered at 10.8.1 were fixed by a single patch, the "Print Service and Text Element Patch," available here:

https://support.esri.com/en/Products/Enterprise/arcgis-server/ArcGIS-Server/10-8-1#downloads?id=7868

Here’s a list of the problems I encountered which were fixed by the patch:

  • Layout.listElements("TEXT_ELEMENT") fails: "java.lang.Exception: Could not service request."
  • Setting TextElement.text fails: "java.lang.Exception: Could not service request."
  • UniqueValueRenderer.listMissingValues crashes
  • Layout.exportToPDF fails: "OSERROR"

Since installing the patch, I was able to implement the web tool and application on our 10.8.1 Q Portal in Tucson, which is considerably faster. The COTTONWOOD 1 map, which took 51 seconds to run in the presentation, took only 14 seconds to run in the Q Portal.

 

Authentication: arcpy vs. ArcGIS API for Python

 

Another issue that I ran into was authentication. While the ArcGIS API for Python correctly encapsulates the identity of the user of the web tool, the arcpy identity is the owner of the ArcSOC process. Why is this a problem? If the layers in a Pro project are not available to the ArcSOC owner, they will not render in the exported PDF. Currently, there is no way for arcpy to sign into portal using the credentials of the web tool user.

[If this is a concern to you, please vote up my idea here: https://community.esri.com/t5/python-ideas/arcpy-sign-into-portal-using-python-api/idi-p/1039109 ]

For the presentation, I worked around the problem by sharing the layers with everyone. However, this is not acceptable for sensitive map data in a Portal which is exposed to the public. The workaround I settled upon is to use the “cryptography” module which is included with ArcGIS Pro:

https://pypi.org/project/cryptography/

I created a built-in user in Portal which could access the layers in the Pro project, and created a text file in the project folder containing the encryption key and the encrypted user name and password. I then used that file to sign arcpy into Portal:

 

 

    def _SignIn(self):
        # Retrieve credentials and sign in to Portal
        from cryptography.fernet import Fernet
        sKeyFile = os.path.join(self.__sProjectDir, "Encrypted.txt")
        with open(sKeyFile) as f:
            sContent = f.read()
        tok = sContent.split("\n")
        sKey = bytes(tok[0], encoding="utf-8")
        sUserTok = bytes(tok[1], encoding="utf-8")
        sPassTok = bytes(tok[2], encoding="utf-8")
        f = Fernet(sKey)
        sUser = f.decrypt(sUserTok)
        sPass = f.decrypt(sPassTok)
        sPortal = arcpy.GetActivePortalURL()
        arcpy.SignInToPortal(sPortal, sUser, sPass)
        return

 

 

See the updated version of the Python toolbox (attached) for more info.