I need to create a stand-alone script for opening a QGIS template project, modify a layer data source, export the Composition and finally save the modified .qgs project into a new file. So far I have only found some examples for QGIS plugins, but of course they don't work for the stand-alone uses. Can anybody give me some helpful hints? Thanks!
1 Answer
- you can use PyQGIS from outside QGIS (search in gis.stackexchange)
- otherwise, a QGIS project file ( .qgs) is a simple XML file and there are many Python modules to process XML files:
part of a .qgs/XML file:
from xml.etree import ElementTree as ET
with open('test.qgs', 'r') as f:
tree = ET.parse(f)
for elem in tree.findall('.//mapcanvas'):
print("units: "), elem.find('units').text
units : meters
.....
-
Thanks gene! Actually my question was already focused on the PYQGIS solution. Learning from several stand-alone scripts thant I have found online I manage to create brand new compositions, add layers and export the maps. I haven't found the right way to open a qgs project and work on it, yet. Commented Jun 27, 2014 at 9:51
-
to open a qgis project : project = QgsProject.instance() project.read(QFileInfo('pathtoQgisproject.qgs')) Commented Sep 12, 2016 at 8:54
-
@Andreampa do you got the solution ?if yes please post the solution– AdithyaCommented Jan 2, 2021 at 7:22