7

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 1

3
  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
 .....
3
  • 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
    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
    – Adithya
    Commented Jan 2, 2021 at 7:22

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.