0

I need to update 3 layers in a template mxd then save that mxd. It needs to loop through a fGDB and for each feature, update the three layers then save a copy for each loop.

What I have loops through and updates the layers but I can't seem to figure out how to save each mxd consecutively after each loop. It currently only saves a copy for the last feature in the loop.

# Local variables:
newMXD_wks = ("R:\\Projects\\Active\\Inundation_Mapping\\Inundation_Maps\\")

Current_Grid_wks = "R:\\Projects\\Active\\Inundation_Mapping\\MapBook_Grids.gdb\\Template_Grid"
New_Grid_wks = "R:\\Projects\\Active\\Inundation_Mapping\\MapBook_Grids.gdb"

Current_Structures_wks = "R:\\Projects\\Active\\Inundation_Mapping\\Structures_fGDBs\\Structures_Final.gdb\\Template_Structure"
New_Structures_wks = "R:\\Projects\\Active\\Inundation_Mapping\\Structures_fGDBs\\Structures_Final.gdb"

Current_Inundation_wks = "R:\\Projects\\Active\\Inundation_Mapping\\Inundation_Polygons_Final\\Oconee.gdb\\Template_Inundation"
New_Inundation_wks = "R:\\Projects\\Active\\Inundation_Mapping\\Inundation_Polygons_Final\\Oconee.gdb"

arcpy.env.workspace = New_Grid_wks
grids = arcpy.ListFeatureClasses('Oconee*')

for grid in grids:
    mxd = arcpy.mapping.MapDocument("R:\\Projects\\Active\\Inundation_Mapping\\InundationTemplate.mxd")
    for lyr in arcpy.mapping.ListLayers(mxd):
        if lyr.supports("DATASOURCE"):
            if lyr.dataSource == Current_Grid_wks:
                lyr.replaceDataSource(New_Grid_wks, "FILEGDB_WORKSPACE", grid)

            if lyr.dataSource == Current_Structures_wks:
                arcpy.env.workspace = New_Structures_wks
                structures = arcpy.ListFeatureClasses('Oconee*')
                for structure in structures:
                    lyr.replaceDataSource(New_Structures_wks, "FILEGDB_WORKSPACE", structure)

            if lyr.dataSource == Current_Inundation_wks:
                arcpy.env.workspace = New_Inundation_wks
                inundations = arcpy.ListFeatureClasses('*Final')
                for inundation in inundations:
                    lyr.replaceDataSource(New_Inundation_wks, "FILEGDB_WORKSPACE", inundation)

mxd.saveACopy(newMXD_wks + inundation + ".mxd")
del mxd
2

Put your mxd.saveACopy() into the loop by indenting it in line with your mxd = arcpy.mapping.MapDocument("R:\\Projects\\Active\\InundationTemplate.mxd") line at the start.

arcpy.env.workspace = New_Grid_wks
grids = arcpy.ListFeatureClasses('Oconee*')

for grid in grids:
    mxd = arcpy.mapping.MapDocument("R:\\Projects\\Active\\InundationTemplate.mxd")
    for lyr in arcpy.mapping.ListLayers(mxd):
        if lyr.supports("DATASOURCE"):
            if lyr.dataSource == Current_Grid_wks:
                lyr.replaceDataSource(New_Grid_wks, "FILEGDB_WORKSPACE", grid)

            if lyr.dataSource == Current_Structures_wks:
                arcpy.env.workspace = New_Structures_wks
                structures = arcpy.ListFeatureClasses('Oconee*')
                for structure in structures:
                    lyr.replaceDataSource(New_Structures_wks, "FILEGDB_WORKSPACE", structure)

            if lyr.dataSource == Current_Inundation_wks:
                arcpy.env.workspace = New_Inundation_wks
                inundations = arcpy.ListFeatureClasses('*Final')
                for inundation in inundations:
                    lyr.replaceDataSource(New_Inundation_wks, "FILEGDB_WORKSPACE", inundation)

    mxd.saveACopy(newMXD_wks + inundation + ".mxd")
    del mxd
  • I've already tried the indent and it produces the same result... creating one mxd per the last feature in the loop. – user1714326 Jan 11 '17 at 17:33
  • @user1714326 can you please explain what you mean by "one mxd per the last feature in the loop"? Do you not want it to update all layers and then save the MXD? Or do you want it to save after each layer is updated? In that case indent it into the next loop – Midavalo Jan 11 '17 at 17:38
  • I want it to save a copy after it has looped and updated the 3 layers, then go through the next loop update layers, save a copy ... until all in the "grid" loop is finished – user1714326 Jan 11 '17 at 17:44
  • 2
    Please edit your question to include the values you have for newMXD_wks and inundation. Is it possible that these values aren't changing for each loop and so you're just overwriting the MXD each time it loops? – Midavalo Jan 11 '17 at 17:45
  • @Midalvo I updated the above script to include all variables. You may be right about overwriting the MXD after each loop but I don't see how I could fix that. – user1714326 Jan 11 '17 at 17:56
1

I figured it out.

I was naming the saved mxd for a variable that was falling out of the loop with each progression.

I updated to name the saved mxd for the original variable (grid) and now it works fine.

As it turns out, I also realized that I was looping within a loop and it was overwriting my layers. So I fixed that too. See the final below.

arcpy.env.workspace = New_Grid_wks
grids = arcpy.ListFeatureClasses('D*')

for grid in grids:
    mxd = arcpy.mapping.MapDocument("R:\\Projects\\Template.mxd")
    for lyr in arcpy.mapping.ListLayers(mxd):
        if lyr.supports("DATASOURCE"):
            if lyr.dataSource == Current_Grid_wks:
                lyr.replaceDataSource(New_Grid_wks, "FILEGDB_WORKSPACE", grid)

            if lyr.dataSource == Current_Structures_wks:
                lyr.replaceDataSource(New_Structures_wks, "FILEGDB_WORKSPACE", grid)

            if lyr.dataSource == Current_Inundation_wks:
                lyr.replaceDataSource(New_Inundation_wks, "FILEGDB_WORKSPACE", grid)


    mxd.saveACopy(newMXD_wks + grid + "_Index.mxd")

    del mxd

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.