I am building a layout template in ArcGIS and am using dynamic text on the side. On the map I have multiple point feature layers. Each feature layer represents a different visit to a particular site. All layers have the same field headings in their respective attribute tables. I wish display the minimum value from the field 'URN', to 0 decimal places. I am currently able to do this but on a layer by layer basis using the 'minimum' option from the dynamic text menu. What I'm wondering is if it is possible to have a dynamic text box that displays the minimum value from the field 'URN' to 0 decimal places for any layer visible on the map. For example if I turn off 'Layer1' and turn on 'Layer2', the text updates to the minimum value in the 'URN' field of 'Layer2' instead, and so on.
1 Answer
Possible to have a dynamic text box that updates automatically based on the visible layer in ArcGIS. One way to do this is to create a Python script that updates the dynamic text based on the visible layer. You can use the arcpy module to access the attribute tables of the visible layers and find the minimum value of the 'URN' field. You can then use the UpdateTextElement function in the arcpy.mapping module to update the dynamic text element in the layout. You can schedule the script to run at a certain interval so it updates automatically, or you can use an event trigger such as the OnFocus event of the TOC, so the script runs when the user changes the visibility of the layers.
import arcpy
import arcpy.mapping as mapping
# Set the current map document and layout
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
# Initialize a variable to store the minimum value
min_value = None
# Loop through all the point feature layers in the map
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
if lyr.isFeatureLayer and lyr.visible:
with arcpy.da.SearchCursor(lyr, "URN") as cursor:
for row in cursor:
if min_value is None or row[0] < min_value:
min_value = row[0]
# Update the dynamic text element with the minimum value
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
if elm.name == "min_value":
elm.text = "Minimum URN: {}".format(round(min_value,0))
# Refresh the layout
arcpy.RefreshActiveView()
You can use this script as a starting point, and then you can customize it to match your project's requirements. You can schedule the script to run at a certain interval so it updates automatically, or you can use an event trigger such as the OnFocus event of the TOC, so the script runs when the user changes the visibility of the layers. You can also use the script tool to run the script in the ArcGIS Pro or ArcMap to update the Dynamic Text.