I have a custom script-turned-widget that takes a point created using the Draw widget, runs analysis using that point (intersect and nearest), and outputs a custom print. I'm currently trying to tailor the Legend Element in my custom print, and am having a little trouble. I would like to display in the legend only those features that appear in the map extent. So, I'm using the updateItem method, but it doesn't seem to be working. I do have another method I'm using right before this one, which works (removeItem), and I've tried using the layer that's not working in the updateItem method in the removeItem method, and it's successful.
I'm wondering if the problem is coming from a couple of things: 1) this layer is published and served from our servers as a map service; I've tried inserting the map service layer and the (nested) feature service layer into the method, but they both don't work. If I enter the map service layer, the script fails; it says "IndexError: list index out of range." If I enter the feature service layer, the script runs successfully, but the legend still displays all of the symbology (including the ones not in the map extent), SO then 2) I'm wondering if there's a problem with the map extent. It's showing up in my map like I scripted, but could there still be an issue? I've tried df.extent = lyr.getSelectedExtent(), to explicitly set the extent instead of df.zoomToSelectedFeatures(), but there's a Runtime Error and can't set the extent.
Here's my code (I'm still in Python 2, as we are still on ArcServer 10.5.1):
mxd = result.mapDocument
df = arcpy.mapping.ListDataFrames(mxd)
lyr = arcpy.mapping.ListLayers(mxd, "Points")
##Points is the layer created from the Draw widget
bafl = arcpy.mapping.ListLayers(mxd, "Best Available Flood Hazard Layer")
##Best Available Flood Hazard Layer is the feature service layer
## bafl = arcpy.mapping.ListLayers(mxd, "FloodHazard_BestAvail_IDNR_IN") This is the map service layer
# Selects the user-created point, zooms the data frame to the selected point, and then unselects the point
# so it doesn't show as teal in the map
# Removes the user-created point layer from the legend but keeps it in the map
drawn = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT", "Legend")
##updates the legend to only show the symbols or the BAFL layer that exist in the df
drawn.updateItem(bafl, "", "", True, "", "")
dictResult = PerformAnalysis(lyr, ExcelFileName)
Any help would be greatly appreciated. Please let me know if other information is needed!
For those of you following along at home...
Looks like the issue is that the layer is a map service and not a feature service. Unfortunately, I can't add the feature service because it's a very large dataset. Even if I add the feature service layer as completely transparent but make it visible in the legend instead of the map service, the symbology is custom-made and doesn't appear correctly.
If anyone has suggestions, I'm all ears!