4

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
  1. you can use PyQGIS from outside QGIS (search in gis.stackexchange)
  2. 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. – Andreampa Jun 27 '14 at 9:51
  • to open a qgis project : project = QgsProject.instance() project.read(QFileInfo('pathtoQgisproject.qgs')) – julsbreakdown Sep 12 '16 at 8:54

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.