2

I have a script shown below with the help from @Midavalo that exports irrig_0, citylyr, boundlyr as a GIF. Once this is done the script then exports irrig_1, citylyr, boundlyr as another GIF and so on. I am now trying to include a text element that reflects the 'Irrig' layer name (i.e. irrig_2) and that changes for each corresponding exported GIF.

enter image description here

import arcpy

projFolder = r"E:\IrrigatedLands\FC_test"

mxd = arcpy.mapping.MapDocument(r"{}\irrig2.mxd".format(projFolder))
df = arcpy.mapping.ListDataFrames(mxd, '')[0]

GIFPath = r"E:\IrrigatedLands\FC_test"

layers = ['irrig_0','irrig_1','irrig_2'] # List of your output layers

lyrlist = arcpy.mapping.ListLayers(mxd, '', df)

for layer in layers:
    layerOn = "" # When required layer is turned on this will be the layer name to output
    for lyr in lyrlist:
        lyr.visible = False
        #if lyr.name in layers and lyr.name <> layer:
        #   lyr.visible = False
        if lyr.name == "citylyr":
            lyr.visible = True
        if lyr.name == "boundlyr":
            lyr.visible = True
        if lyr.name == layer:
            lyr.visible = True
            layerOn = lyr.name

        for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
            if elm.name == 'TestName':
                elm.text = lyr.name

            else:
                elm.text = ""
    if layerOn:
        arcpy.mapping.ExportToGIF(mxd,r"{}\{}.gif".format(GIFPath,layerOn))

del mxd
  • When you say text element, I take it you mean a dynamic text element located on your layout somewhere? If so that might be worth putting in to the question's text so it's a little clearer. If that's the case, have you considered using an arcpy/data driven pages combo? – nwduncan Mar 8 '16 at 4:51
  • It sounds like you are trying to implement what I call Pseudo-dynamic Text. It is a coding pattern that creates a text element object from a dummy text element pre-authored into a map, and then switches its text to be the contents of a variable (in your case the layer name). – PolyGeo Mar 8 '16 at 4:54
  • @ Fezter could you use a similar process in a PyQGIS standalone script? If not, could you point me to an example or other documentation? Thank you – brgionta May 9 '16 at 22:27
3

There are a few ways you can do this with text elements. You cannot create text elements in arcpy. You can however have a few text elements off the page and move them onto the page when needed. You can also have blank text elements and only populate the text when you need it. I suggest you have a look at text elements.

The trick is to loop through all the text elements and set the text property to the layer name. Be sure to reset back to empty text when you loop to the next layer.

The page on Listing Layout Elements should get your started.

EDIT: Based on your comment, you'll have to name your layout elements (see the Size and Position tab). If you name them the same as your layers, then you can do something like this:

for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
    if elm.name == lyr.name:
        elm.text = lyr.name
    else:
        elm.text = ""

Obviously, your code will probably look different, but hopefully, this will get you going.

  • Thank you for the suggestions. Would you have an example script showing how to loop through the layer names, especially how to "reset back to the empty text"? – brgionta Mar 8 '16 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.