0

I'm trying to programmatically build .qgs files and add styles with .qml files I saved from my template project. For some reason, when I open the new .qgs file, the styles haven't been applied. I'd like to do this for both my points and rasters.

The only thing I care about the styles is the size and color of the points for the .shp and the color ramps for the .img.

My code now:

import os

home = os.path.expanduser('~')
PATH = os.path.join(home, 'miniconda2', 'envs', 'qgis', 'share', 'qgis', 'python')
import sys

sys.path.append(PATH)

from qgis.core import QgsApplication, QgsProject, QgsMapLayerRegistry
from qgis.core import QgsRasterLayer, QgsVectorLayer
from PyQt4.QtCore import QFileInfo


def make_qgs(qml, input):
    shapes = {'cold': os.path.join(input, 'PIXELS', 'cold.shp'),
              'hot': os.path.join(input, 'PIXELS', 'hot.shp')}

    rasters = {'ndvi': os.path.join(input, 'INDICES', 'ndvi_toa.img'),
               'ts': os.path.join(input, 'ts.img')}

    keys = ['cold', 'hot', 'ndvi', 'ts']

    QgsApplication.setPrefixPath(PATH, True)
    qgs = QgsApplication([], False)
    qgs.initQgis()

    project = QgsProject.instance()
    project.read(QFileInfo(os.path.join(qml, 'qgis_template.qgs')))
    project_path = QFileInfo(os.path.join(input, 'calbration_map.qgs'))
    for key, path in rasters.items():
        layer = QgsRasterLayer(path)
        if not layer.isValid():
            print("file {} is not a valid raster file".format(path))
        QgsMapLayerRegistry.instance().addMapLayer(layer)

    for key, path in shapes.items():
        fileInfo = QFileInfo(path)
        path = fileInfo.filePath()
        baseName = fileInfo.baseName()
        layer = QgsVectorLayer(path, baseName, 'ogr')
        if not layer.isValid():
            print("file {} is not a valid vector file".format(path))

        QgsMapLayerRegistry.instance().addMapLayer(layer)

    for layer, name in zip(QgsMapLayerRegistry.instance().mapLayers().values(), keys):
        layer.loadNamedStyle(os.path.join(qml, '{}{}'.format(qml, '.qml')))

    project.write(project_path)
    qgs.exitQgis()


if __name__ == '__main__':
    home = os.path.expanduser('~')
    path = os.path.join(home, 'IrrigationGIS', 'tests', 'qgis')
    folder = os.path.join(path, 'LC08_041027_20150807')
    make_qgs(path, folder)

Is this a sensible approach? If I have a .qgs template file, should I try modifying the .xml?

Other possibly pertinent information:

When I open the new .qgs, I get a warning that 'Project file is older:'

About: QGIS version 2.18.20 QGIS code branch Release 2.18 Compiled against Qt 4.8.7 Running against Qt 4.8.7 Compiled against GDAL/OGR 2.2.2 Running against GDAL/OGR 2.2.2 Compiled against GEOS 3.5.1-CAPI-1.9.1 Running against GEOS 3.5.1-CAPI-1.9.1 r4246 PostgreSQL Client Version 9.5.13 SpatiaLite Version 4.3.0a QWT Version 6.1.2 PROJ.4 Version 492 QScintilla2 Version 2.9.1

  • 1
    I think the issue may be in this statement, for layer, name in zip(QgsMapLayerRegistry.instance().mapLayers().values(), keys):, getting the layer object and the path to the .qml file. Maybe just load the style directly after the layer is added to the map. Also, you may want to add some print statement to verify the .qml paths. – artwork21 Jul 3 '18 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.