Created
January 17, 2019 17:19
-
-
Save 3nids/8a0d3e25e6650f457e4b8e9e5d4eea9d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os.path | |
from qgis.PyQt.QtCore import QCoreApplication | |
from qgis.core import QgsProject, QgsApplication | |
from qgis.utils import iface | |
# This is a python script intended to be used within QGIS to split a project according to its map theme collection. | |
# It will produce as many project file as existing themes. | |
# Configure hereafter and run using: | |
# _QGIS_APP_PATH_ --nologo --noplugins --noversioncheck --code _PATH_TO_theme_slicer.py_ | |
# Configure | |
project_file = '/Users/denis/Documents/temp/qfield_search/qfield.qgs' | |
exit_qgis = True | |
### !!! DO NOT EDIT BELOW !!! ### | |
# Open python console for debugging | |
iface.actionShowPythonDialog().trigger() | |
p = QgsProject.instance() | |
# open original project | |
p.clear() | |
p.setFileName(project_file) | |
p.read() | |
QCoreApplication.processEvents() | |
themes = [] | |
for theme in p.mapThemeCollection().mapThemes(): | |
# make copy of the project | |
file_parts = os.path.splitext(project_file) | |
new_project = "{path}_{theme}{ext}".format(path=file_parts[0], theme=theme, ext=file_parts[1]) | |
print("Writing {}".format(new_project)) | |
p.setFileName(new_project) | |
p.write() | |
QCoreApplication.processEvents() | |
# apply theme | |
p.mapThemeCollection().applyTheme(theme, p.layerTreeRoot(), iface.layerTreeView().layerTreeModel()) | |
QCoreApplication.processEvents() | |
# save project | |
iface.actionSaveProject().trigger() | |
QCoreApplication.processEvents() | |
# exit | |
if exit_qgis: | |
QCoreApplication.processEvents() | |
QgsApplication.exitQgis() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment