Select to view content in your preferred language

List Map Elements - Pro arcpy

460
7
10-02-2024 05:09 PM
DrewCover
Regular Contributor

Is there or can there be a 'List Map Elements' function, so I can search for and remove/replace Text Elements from all maps in the project.  I have code for doing this in Layout but not Map

Tags (4)
0 Kudos
7 Replies
jcarlson
MVP Esteemed Contributor

What would a text element in a map be? Are you talking about annotations? Or graphics layers?

- Josh Carlson
Kendall County GIS
BrandonMcAlister
Frequent Contributor

Assuming he means annotations or graphics layers in a map,

I would create a map object maps = arcpy.mp.ArcGISProject("CURRENT").listMaps()[] https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/arcgisproject-class.htm 

Then iterate through the map objects with a while loop.

Within each map object I would iterate through the layers maps.listLayers()[]

https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/layer-class.htm 

Then identify the annotation layers or graphics layers:

Then probably have to use a search cursor to identify Text Elements:

Then delete the records I don't want.

Best of luck

Thanks,
Brandon
0 Kudos
DrewCover
Regular Contributor

Referring to Text graphics layers within a Map Object.  

0 Kudos
BrandonMcAlister
Frequent Contributor

@DrewCover 

I was partially correct in my above statement. You could use the method I listed above for Annotation Feature classes because they have an attribute table associated with them, but Graphics Layers do no since they are stored in the map as Elements and not features.

You would have to use the listElements method under the graphics element class to access the graphics layers. Then you should be able to loop through the element looking for specifics to edit or delete using a variety of other methods.

 

Review this web page, note I have it set to ArcPro 3.2 not 3.3

https://pro.arcgis.com/en/pro-app/3.2/arcpy/mapping/graphicelement-class.htm  

Thanks,
Brandon
0 Kudos
TonyAlmeida
Frequent Contributor

Here is snippet of something I use, I use to modify a text element in the layout of the current ArcGIS project (aprx). It iterating through all the text elements within each layout, and looks for Template text. It updates the text with the Name = arcpy.GetParameterAsText(1). Something like this,

 

 

 

 

aprx = arcpy.mp.ArcGISProject("CURRENT")
Name = arcpy.GetParameterAsText(1)
for lyt in aprx.listLayouts():
    for elm in lyt.listElements("TEXT_ELEMENT"):
        if elm.text == "Template":
            elm.text = (Name)

 

 

 

 

 

 

0 Kudos
DrewCover
Regular Contributor

Thanks for the try effort Tony.  I want to do exactly this, but on the list of project map objects.  Ideally I'd be able to do something like this:

##omitting setup of previous lines
for m in aprx.listMaps():
    for lyr in m.listLayers("Graphics Layer"):
        if elm.text == "Bay St":
            elm.text.delete()

 

0 Kudos
TonyAlmeida
Frequent Contributor

I don't think there is a built-in List Map Elements function, only text elements in layouts.

0 Kudos