4

I am using the arcpy.mapping module for the first time, and the lyr.visible command is not working as it should (or I am not using it as I should). I have a list that contains the the layer names of .img files that are loaded into a single data frame - the list is called "layernames". I am using a while loop to go though the layernames list, turn on one of the layers at a time, export a .png file with that layer visible, then turn that layer off and move on to the next layer in the list.

At the beginning of each pass through the loop I specify which raster layer should be turned on by setting layernames.visible = True, and at the end of the pass through the loop I turn off that layer by setting layernames.visible = false.

The first time through the loop the script works and does turn on the correct layer in the list "layernames". But for all subsequent passes through the loop, only the first layer remains visible, and none of the others are turned on or off. In other words, every .png image shows the same raster (the first one that is turned on), instead of a different raster in each .png image. I have pasted my script below.

mxd = arcpy.mapping.MapDocument("Z:\\sharks\\Models\\Summer2013\\daily figures.mxd")
layernames = arcpy.mapping.ListLayers(mxd)
counter = 0
yr = 2005
while counter < 365:
    layernames[counter].visible = True
    x = str(layernames[counter])
    y = x[9:12]
    j = int(y)
    date = datetime.datetime(yr,1, 1)+ timedelta(days=(j-1))
    strDate = str(date)
    if (j + 1)<= 31:
        mxd.title = "January (" + strDate[0:10] + ")"
    elif (j + 1) >= 32 and (j+1) <= 59:
        mxd.title = "February (" + strDate[0:10] + ")"
    elif (j + 1) >= 60 and (j+1) <= 90:
        mxd.title = "March (" + strDate[0:10] + ")"
    elif (j + 1) >= 91 and (j+1) <= 120:
        mxd.title = "April (" + strDate[0:10] + ")"
    elif (j + 1) >= 121 and (j+1) <= 151:
        mxd.title = "May (" + strDate[0:10] + ")"
    elif (j + 1) >= 152 and (j+1) <= 181:
        mxd.title = "June (" + strDate[0:10] + ")"
    elif (j + 1) >= 182 and (j+1) <= 212:
        mxd.title = "July (" + strDate[0:10] + ")"
    elif (j + 1) >= 213 and (j+1) <= 243:
        mxd.title = "August (" + strDate[0:10] + ")"
    elif (j + 1) >= 244 and (j+1) <= 273:
        mxd.title = "September (" + strDate[0:10] + ")"
    elif (j + 1) >= 274 and (j+1) <= 304:
        mxd.title = "October (" + strDate[0:10] + ")"  
    elif (j + 1) >= 305 and (j+1) <= 334:
        mxd.title = "November (" + strDate[0:10] + ")"
    elif (j + 1) >= 335 and (j+1) <= 365:
        mxd.title = "December (" + strDate[0:10] + ")"  
    mxd.save()
    out_png = "Z:/sharks/Models/Summer2013/RF/ALL/SB_RF/AVpresRFP/figures/" + strDate[0:10] + ".png"
    arcpy.mapping.ExportToPNG(mxd, out_png)
    layernames[counter].visible = False
    mxd.save()
    counter +=1

1 Answer 1

5

Some things:

  • You shouldn't need to do all those mxd.save()s. They accomplish nothing.
  • I suspect you're not hiding some other layer in the drawing order and it's showing up on the top. Make them all invisible from the start
  • datetime.strftime is your friend. Don't fear datetime.strftime.

Try this:

# Load MXD
mxd = arcpy.mapping.MapDocument(r"Z:\sharks\Models\Summer2013\daily figures.mxd")

# Get Layers
layers_in_map = arcpy.mapping.ListLayers(mxd)

# 2005 was a good year
year = 2005

# Hide all layers just in case
for layer in layers_in_map:
    layer.visible = False

# Don't need to do an increment manually; let Python handle it
for counter in xrange(365):
    # Make just this one layer visible
    current_layer = layers_in_map[counter]
    current_layer.visible = True

    # Some debug text to see if it's accidentally skipping/including some layers
    # you care about
    print "Current layer: {}".format(current_layer.name)

    # Set up datetime from layer name
    day_delta = int(current_layer.name[9:12]) - 1
    date = datetime.datetime(year, 1, 1)+ timedelta(days=day_delta)

    # Make "Monthname (YYYY-MM-DD)" string
    title = date.strftime("%B (%Y-%m-%d)")
    mxd.title = title

    # Make output file
    out_file = date.strftime("%Y-%m-%d.png")
    out_png = r"Z:\sharks\Models\Summer2013\RF\ALL\SB_RF\AVpresRFP\figures\{}".format(out_file)
    print "Writing to {}".format(out_png)

    # Export map and hide the layer again
    arcpy.mapping.ExportToPNG(mxd, out_png)
    current_layer.visible = False
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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