Performing a definition query (or filter) on a webmap

115
2
Jump to solution
09-26-2022 11:34 PM
MichaelBell
New Contributor III

I'm currently trying to create a script which will do a few things, including loading a webmap, filtering it based on a definition query, zooming to the resulting shape extent, and then creating a PDF using a custom layout. I've managed to do all this using a local project in ArcGIS Pro, but had trouble exporting it (mainly because I was using the "CURRENT" project, and I'm now trying to do this via the Portal).

In any case, I'm having to redo a fair bit of code. I'm getting stuck where I refer to the layer so I can perform a definition query on it. Basically, I'd ideally like the lyr object to refer to a layer, so I can do a "lyr.definitionQuery = etc"

This is what I've got so far. I know the syntax is wrong, I'm just looking for the right one:

 

import arcpy
from arcgis.mapping import WebMap
from arcgis.gis import GIS
#get variables
RequestedId = arcpy.GetParameterAsText(0)
cUN = arcpy.GetParameterAsText(1)
cPW = arcpy.GetParameterAsText(2)
#connect
gis = GIS(r"https://mydomain.com/portal", cUN, cPW)
#try and load the webmap
wm_item = gis.content.get("webmap_id_here")
wm = WebMap(wm_item)
#try to access the layer
arcpy.AddMessage(wm.layers)
lyr = wm.layers("ThisIsMyLayersTitle")[0]

 

For reference, and probably to see more what I'm trying to do, this is my full (old) code that works on a local project:

 

import arcpy
RequestedId = arcpy.GetParameterAsText(0)
#select our current map, and then the layer we want, and grab our Id from parameters
aprx = arcpy.mp.ArcGISProject("CURRENT")
RequestedForestId = 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")

 

0 Kudos
1 Solution

Accepted Solutions
JeffK
by MVP Regular Contributor
MVP Regular Contributor

I think you need to cast the layer to a FeatureLayer.

You can do this by using the properties of the layer and getting the url- This is not tested but taken from esri's guide here

from arcgis.features import FeatureLayer
# I don't think there is a wildcard method for .layers() so use list comprehension to filter
lyr_url = [flyr for flyr in wm.layers() if flyr.name == "LayerTitle"][0]

# Create the layer as FeatureLayer
layer = FeatureLayer(lyr_url.url)

# set the query
query_result1 = layer.query(where=f"Id = '{RequestedId}'")

# get the result geometry
geom = query_result1.features[0].geometry

View solution in original post

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

I think you need to cast the layer to a FeatureLayer.

You can do this by using the properties of the layer and getting the url- This is not tested but taken from esri's guide here

from arcgis.features import FeatureLayer
# I don't think there is a wildcard method for .layers() so use list comprehension to filter
lyr_url = [flyr for flyr in wm.layers() if flyr.name == "LayerTitle"][0]

# Create the layer as FeatureLayer
layer = FeatureLayer(lyr_url.url)

# set the query
query_result1 = layer.query(where=f"Id = '{RequestedId}'")

# get the result geometry
geom = query_result1.features[0].geometry
0 Kudos
MichaelBell
New Contributor III

Great, that works. My code is now:

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 == "MyLayerTitle":
        #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)
0 Kudos