3

I'm trying to print my layout. Sadly, I cannot figure out how to hide the coverage layer.

The setHideCoverage(True) - line doesn't change my output

My code so far:

project = QgsProject.instance()
manager = project.layoutManager()
layout_name = self.listWidget.currentItem().text()
layout = manager.layoutByName(layout_name)
my_atlas = layout.atlas()
my_atlas.setFilterFeatures(False)
# Starts Layout Generation
my_atlas.setHideCoverage(True)
my_atlas.setEnabled(True)
my_atlas.beginRender()
# For 0 to Number of features in Atlas Selection
for i in range(0, my_atlas.count()):
 # Create Next Layout
 if i < my_atlas.count():
 my_atlas.next()
 # Create exporter Layout for each layout generate with Atlas
 exporter = QgsLayoutExporter(my_atlas.layout())
 print('Saving File: ' + str(my_atlas.currentFeatureNumber() + 1) + ' of ' + str(my_atlas.count()))
 # If you want to create a PNG's files
 exporter.exportToImage(self.pfad + "/" + my_atlas.currentFilename() + ".jpg",
 QgsLayoutExporter.ImageExportSettings())
 # Show which file is creating
 print('Create File: ' + my_atlas.currentFilename())
# Close Atlas Creation
my_atlas.endRender()
Matt
19.4k4 gold badges25 silver badges64 bronze badges
asked Dec 22, 2022 at 15:29
3
  • The "setHideCoverage(True)" - line doesnt change my output Commented Dec 22, 2022 at 15:31
  • I suspect this may be a bug. The line my_atlas.setHideCoverage(True) does indeed hide the coverage layer in the atlas, but exporting the atlas using QgsLayoutExporter does not respect this setting. Exporting the atlas via the button in the atlas toolbar does, however. Commented Dec 23, 2022 at 0:08
  • The "HideCoverage"-option is alwayse a little buggy. When i want to plot manualy i have to uncheck the option and then check it again. Else it wont work. Thats why i thought maybe i have to refresh the map but that didnt work too. If i run the script and open the layout after that the options are set correctly AND the covarage layer is hidden. But not in the plot. Commented Dec 23, 2022 at 9:08

2 Answers 2

2

What you can do as a workaround is to set the layer's visibility in the map canvas to False. It still works as a coverage layer, but will not show in your atlas exports.

Replace my_atlas.setHideCoverage(True) with these lines:

# change 'coverage_layer' to the name of your layer
coverage = project.mapLayersByName('coverage_layer')[0] 
coverage_ltl = project.layerTreeRoot().findLayer(coverage)
coverage_ltl.setItemVisibilityChecked(False)

If you want to make it visible after your export, add the inverse at the end of your script:

coverage_ltl.setItemVisibilityChecked(True)

If the coverage layer has already been set in the atlas settings, you can use this to avoid having to hard-code the name:

coverage = my_atlas.coverageLayer()
answered Dec 23, 2022 at 9:57
-1

Yes, it works this way. But I was still curious how to solve the problem. And I finally found a solution. When I use flags to change the imageExportSettings` it works.

Here is my final code:

project = QgsProject.instance()
manager = project.layoutManager()
layout_name = self.listWidget.currentItem().text()
layout = manager.layoutByName(layout_name)
my_atlas = layout.atlas()
my_atlas.setFilterFeatures(False)
# Starts Layout Generation
my_atlas.setHideCoverage(True)
my_atlas.setEnabled(True)
my_atlas.beginRender()
 
# Create exporter Layout for each layout generate with Atlas
exporter = QgsLayoutExporter(my_atlas.layout())
image_settings = exporter.ImageExportSettings()
# hide Coverage
image_settings.flags = QgsLayoutRenderContext.Flag(QgsLayoutRenderContext.FlagHideCoverageLayer | 
 QgsLayoutRenderContext.FlagAntialiasing |
 QgsLayoutRenderContext.FlagUseAdvancedEffects)
# For 0 to Number of features in Atlas Selection
for i in range(0, my_atlas.count()):
 # Create Next Layout
 if i < my_atlas.count():
 my_atlas.next()
 # Create exporter Layout for each layout generate with Atlas
 exporter = QgsLayoutExporter(my_atlas.layout())
 # If you want to create a PNG's files
 exporter.exportToImage(self.pfad + "/" + my_atlas.currentFilename() + ".jpg",
 QgsLayoutExporter.ImageExportSettings())
# Close Atlas Creation
my_atlas.endRender()
Vince
20.5k16 gold badges49 silver badges65 bronze badges
answered Dec 23, 2022 at 13:26
1
  • 1
    How to use multiple flags? A: You can use | operator. Ex: flags = flag1 | flag2 Commented Dec 23, 2022 at 13:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.