Tengo que hacer unos 1000 mapas que representen la misma zona (se trata de una imagen de satélite reclasificada, es decir, rasterizada). El estilo es el mismo, sólo cambia la fecha. He averiguado cómo dar estilo a todas las capas en poco tiempo y cómo imprimir todas las imágenes en el Atlas. Me gustaría automatizar también el proceso de creación de temas (se llamaba preset en QGIS 2.x) en el panel de capas, por lo que no tendría que hacer manualmente todos los temas que utilizaría más tarde en Atlas. Me gustaría nombrarlos con el nombre de la capa o de la imagen rasterizada (representa la fecha de la imagen satelital).
Respuesta
¿Demasiados anuncios?Puedes hacerlo ejecutando en la consola de Python lo siguiente, utilizando PyQGIS
root = QgsProject.instance().layerTreeRoot()
mapThemesCollection = QgsProject.instance().mapThemeCollection()
mapThemes = mapThemesCollection.mapThemes()
# Where you need to set your images names
# Could be retrieve if only raster names wanted with
# [layer.name() for layer in QgsProject.instance().mapLayers().values() if isinstance(layer, QgsRasterLayer)]
# If you want all layers names and filter them manually
# [layer.name() for layer in QgsProject.instance().mapLayers().values()]
layersToChanges = ['CartoDB Light', 'OpenStreetMap', 'OpenTransports'] # Replace with your list of raster layers instead
for layer in layersToChanges:
for child in root.children():
if isinstance(child, QgsLayerTreeGroup):
print("- group: " + child.name())
elif isinstance(child, QgsLayerTreeLayer):
print("- layer: " + child.name() + " ID: " + child.layerId())
# Layer you want to tick
if (child.name() == layer):
child.setItemVisibilityChecked(True)
print("Check only once")
elif child.name() in layersToChanges:
child.setItemVisibilityChecked(False)
print("Check the others you want to hide")
mapThemeRecord = QgsMapThemeCollection.createThemeFromCurrentState(
QgsProject.instance().layerTreeRoot(),
iface.layerTreeView().model()
# For QGIS 3.18+, instead of above line, use iface.layerTreeView().layerTreeModel()
)
mapThemesCollection.insert(layer, mapThemeRecord)