Map Series Automation - Issues with updating map series page after altering geometry of index layer

433
4
05-04-2023 05:30 PM
EBaker
by
New Contributor II

Hello!

So I've got a map series script that looks at two parent datasets with matching WFR_NUM fields and generates a polygon that encompasses both of them. Then, it uses this union polygon (extLayer) as the index layer in the map series itself. The script works fine up until the map series part.  No issues when I do it manually in Pro. When I  run the script, it will export whatever page it was last set to in the Pro doc without showing the index layer for some reason.

The points and black polygon are the parent datasets, the pink polygon is the extLayer (index layer)

EBaker_0-1683246518902.png

 

 

By section 5 I've already updated the geometry of the selected extLayer (index layer) record. Any help here would be appreciated. Please let me know if you need more context. Thank you! 

 

#----------------------------------------------------------------------------------------------------------------------
# 5. Get a list of templates (Layouts) in the project
#----------------------------------------------------------------------------------------------------------------------

# Get an array of "wTemplates" with all the Layouts in the project (templates) used further down to match to Template value in extLayer
waprx = arcpy.mp.ArcGISProject(wfrTemplate)
wTemplates = []
n = 0
for lyt in waprx.listLayouts():  # list the layout templates that are part of the WFR Map series
    arcpy.AddMessage(str(n) + ' - ' + f"  {lyt.name}")
    wTemplates.append(lyt.name)
    n += 1

#----------------------------------------------------------------------------------------------------------------------
# 6. Create Output Locations (if needed)
#----------------------------------------------------------------------------------------------------------------------

# Create the full path to the REGIONS folder
regionFolderPath = os.path.join(netPath, "Output", "REGIONS")

# Create the REGIONS folder (and the Output folder if necessary)
os.makedirs(regionFolderPath, exist_ok=True) # wont error out if it already exists

#----------------------------------------------------------------------------------------------------------------------
# 7. Check Template Values, Create WFR Folder, and Run Map Series
#----------------------------------------------------------------------------------------------------------------------

rowCnt = 0 # use to make sure we only process one row
with arcpy.da.UpdateCursor(extLayer, ['WFR_NUM','WFR_REGION','TEMPLATE'],"\"WFR_NUM\" = '{}'".format(wfr_num)) as wfrs: 
    for obj in wfrs:
        if rowCnt > 1: break # Should only work on one row
        rowCnt += 1
        if obj[2] in wTemplates: # if the template exists, then...
            regionFolderPath = os.path.join(netPath, "Output", "REGIONS", "REGION_" + obj[1]) # this variable stores the output path directory (sans wfr folder and \\)
            wfrFolderPath = os.path.join(regionFolderPath, obj[0]) # this variable stores the whole output path directory (with wfr folder but without \\)

            if not os.path.exists(regionFolderPath):
                os.makedirs(regionFolderPath) # creates the Region_# folder if it doesn't exist
                arcpy.AddMessage("region folder created at: "+regionFolderPath)

            if not os.path.exists(wfrFolderPath):
                os.makedirs(wfrFolderPath) # creates the WFR_# folder if it doesn't exist
                arcpy.AddMessage("wfr folder created at: "+wfrFolderPath)

            mapPath = wfrFolderPath + '\\'#  this variable stores the whole output path directory (sans map name)

####### UNDER CONSTRUCTION ###################################################################################################################

            p = arcpy.mp.ArcGISProject(wfrTemplate)
            l = p.listLayouts(obj[2])[0] # looks at the templates (layouts) again and grabs the first one that matches the template value in the index layer
            print("The value of l (selected layout template) is:", l.name) # this works

            if not l.mapSeries is None:
                ms = l.mapSeries
                if ms.enabled:
                    ms.refresh()
                    indexLyr = ms.indexLayer
                    print("The value of wIndexLyr is:", indexLyr.name) # just making sure its looking at the right layer, it is
                    arcpy.SelectLayerByAttribute_management(indexLyr, "CLEAR_SELECTION") # clear the current selection (just in case?)
                    arcpy.SelectLayerByAttribute_management(indexLyr, "NEW_SELECTION", "WFR_NUM = '033-50-003'") # hard coded for now, will need to be changed later
                    #arcpy.SelectLayerByAttribute_management(indexLyr, "NEW_SELECTION", "\"WFR_NUM\" = '{}'".format(wfr_num))

                    selectedCount = int(arcpy.GetCount_management(indexLyr).getOutput(0)) # shows that one record in the indexLyr is selected
                    print("Number of selected features:", selectedCount)
                    ms.refresh()
                    ms.exportToPDF(mapPath + "\\Ex3_SelectedFeatures.pdf", "SELECTED", resolution=300)
                    arcpy.AddMessage("WFR map created at: "+ mapPath + obj[0] + '_WFR_map.pdf')
                    
        else:
            arcpy.AddMessage('Error! ' + obj[0] + 'might be specifying a template that is not recognized. Please assign template value to WFR layer')

#----------------------------------------------------------------------------------------------------------------------
# arcpy.AddMessage Processing Details
#----------------------------------------------------------------------------------------------------------------------

# Get Time Elapsed and rows processed
ts2 = time.time()
arcpy.AddMessage('time elapsed: ' + "{:.4f}".format(ts2-ts1) + ' seconds')
arcpy.AddMessage('rows processed: ' + "{:,}".format(rowCnt))
exit()

 

0 Kudos
4 Replies
Kara_Shindle
Occasional Contributor III

Normally, my first thought was the ms.refresh(), that is supposed to refresh a map series in the event the source data does change.  

 

It says the map series will retain the original settings until the refresh method is executed, or the project is saved & reopened.  

Perhaps try moving the second refresh to below the exported page?

0 Kudos
EBaker
by
New Contributor II

Hi Kara,

Yeah, it seems like refresh() would be what I need here. Still no dice though, unfortunately. I appreciate you taking a stab at it though! Please let me know if you have any other ideas. 

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

Along those same lines, is it possible for you to manually refresh it by turning the series off and recreating it?

0 Kudos
Kara_Shindle
Occasional Contributor III

I've been thinking about this issue a little - have you had any success with getting it to work?  

 

I update the layer I run a map series on nightly.  It gets updated via one script that extracts the needed features to a geodatabase.  A second script then runs the map series.  After the first script runs, if I were to open the Pro doc, the map series updates automatically.

 

My second script, I just have the refresh built in automatically.  Have you thought about breaking this up and testing if it works if you update the layers and then try the map series?  Or maybe building in a delay?

0 Kudos