How to interact with a layout via Portal

61
0
09-27-2022 11:27 PM
MichaelBell
New Contributor III

I've got a layout file that I've copied to my Portal. The layout refers to a webmap (which contains a few layers).

My intention is to print the layout, zoomed to a certain extent (which I'd pass through to it), and only showing certain features (which, again, I can pass to it, or provide in a definition query).

How would I accomplish this? I managed it using a local project, but now I'm trying to do it via Portal and have to use the arcgis.gis module. I've got code that will log in to the Portal and get the required extent, so just need to know how to interact with the layout.

My code, so far, is below:

import arcpy, os, sys
from arcgis.mapping import WebMap
from arcgis.gis import GIS
from arcgis.features import FeatureLayer
#get variables
RequestedId = arcpy.GetParameterAsText(0)
cUN = arcpy.GetParameterAsText(1)
cPW = arcpy.GetParameterAsText(2)
#connect
gis = GIS(r"https://mydomain/portal", cUN, cPW)
#try and load the webmap
wm_item = gis.content.get("WebMapId")
wm = WebMap(wm_item)
#try to access the layer
for lyr in wm.layers:
    if lyr.title == "MyLayer":
        #convert it into a feature layer for local use
        flayer = FeatureLayer(lyr.url)
        ###some handy old code, no longer used
        ###for f in flayer.properties.fields:
        ###    arcpy.AddMessage(f['name'])
        #grab the extent
        MyExtent = flayer.query(where="Id='" + RequestedId + "'", return_extent_only=True)
        #now we're going to need our layout
        lyt_item = gis.content.get("MyLayoutId")
        arcpy.AddMessage(lyt_item)

For reference, this was my original code, which worked fine using a local project. Basically, I'd like to replicate this:

import arcpy
#select our current map, and then the layer we want, and grab our Id from parameters
aprx = arcpy.mp.ArcGISProject("CURRENT")
RequestedId = arcpy.GetParameterAsText(0)
m = aprx.listMaps("MyWebMap")[0]
lyr = m.listLayers("MyLayer")[0]
#set our definition query
lyr.definitionQuery = "Id = '" + RequestedId + "'"
#define our extents
extents = []
#loop through and add our shape extent
with arcpy.da.SearchCursor(lyr,'SHAPE@') as cursor:
    for row in cursor:
        if row[0]:
            extents.append(row[0].extent)
#get our actual extent
eXMin = min([ext.XMin for ext in extents])
eXMax = max([ext.XMax for ext in extents])
eYMin = min([ext.YMin for ext in extents])
eYMax = max([ext.YMax for ext in extents])
#now go into the layout
lyt = aprx.listLayouts("Layout")[0]
mf = lyt.listElements("mapframe_element", "WEBMAP_MAP_FRAME")[0]
mf.map = m
myExtent = arcpy.Extent(eXMin, eYMax, eXMax, eYMin, 0, 0)
mf.camera.setExtent(myExtent)
mf.camera.scale = mf.camera.scale * 1.1
m.defaultCamera = mf.camera
lyt.exportToPDF("C:\\temp\\mylayout.pdf")

I've seen some things saying that you need to know where in the DataStore the layout is saved, but I can't find any way to find this out.

 

0 Kudos
0 Replies