3

I have a QGIS python processing script where, for each output layer, a layer style is applied before exporting to kml and styled geopackage layer. This works for all my output layers EXCEPT for 3 layers: These layers are all derived from the same process- In this case, only one of the layers has its layer style applied- The other 2 refuse to.

I've tried a) using the layer file from the layer that does work (2008) on the layers that don't (1990, 2016): Doesn't work b) changing layer output parameters from 'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT to memory layers i.e.'OUTPUT': "memory:layername_123". Doesn't work c) Doing the layer styling and export for each layer all at once, before moving onto the next. That doesn't work.

Is this a bug in QGIS?

I have had other issues whereby the layer name was inherited from the processname and would therefore overwrite outputs (i.e. 3x derived layers from a clip, all would be named 'clip' so when exporting to geopackage, only 1 layer called 'clip' would be present)

#export styled layers
    alg_params = {
        'INPUT': in_Lucas,
        'OVERLAY': outputs['Titles']['OUTPUT'],
        'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
    }
    outputs['ClipLucas'] = processing.run('native:clip', alg_params, context=context, feedback=feedback, is_child_algorithm=True)

    # Drop field(s)
    alg_params = {
        'COLUMN': ['AREA_HA','SHAPE_Length','SHAPE_Area'],
        'INPUT': outputs['ClipLucas']['OUTPUT'],
        'OUTPUT': "memory:LUCAS_"
    }
    outputs['DropFieldsLucas'] = processing.run('native:deletecolumn', alg_params, context=context, feedback=feedback, is_child_algorithm=True)

    feedback.pushInfo('Lucas 1990')
    alg_params = {
        'FIELD_LENGTH': 10,
        'FIELD_NAME': 'AreaF_ha',
        'FIELD_PRECISION': 3,
        'FIELD_TYPE': 0,  # Float
        'FORMULA': '$area/10000',
        'INPUT': outputs['DropFieldsLucas']['OUTPUT'],
        'OUTPUT': "memory:LUCAS_1990"
        
    }
    outputs['Lucas1990'] = processing.run('native:fieldcalculator', alg_params, context=context, feedback=feedback, is_child_algorithm=True)

    feedback.pushInfo('Lucas 2008')
    
    alg_params = {
        'FIELD_LENGTH': 10,
        'FIELD_NAME': 'AreaF_ha',
        'FIELD_PRECISION': 3,
        'FIELD_TYPE': 0,  # Float
        'FORMULA': '$area/10000',
        'INPUT': outputs['DropFieldsLucas']['OUTPUT'],
        'OUTPUT': "memory:LUCAS_2008"
        
    }
    outputs['Lucas2008'] = processing.run('native:fieldcalculator', alg_params, context=context, feedback=feedback, is_child_algorithm=True)

    feedback.pushInfo('Lucas 2016')
    
    alg_params = {
        'FIELD_LENGTH': 10,
        'FIELD_NAME': 'AreaF_ha',
        'FIELD_PRECISION': 3,
        'FIELD_TYPE': 0,  # Float
        'FORMULA': '$area/10000',
        'INPUT': outputs['DropFieldsLucas']['OUTPUT'],
        'OUTPUT': "memory:LUCAS_2016"
       }
    outputs['Lucas2016'] = processing.run('native:fieldcalculator', alg_params, context=context, feedback=feedback, is_child_algorithm=True)

    
    alg_params = {
        'INPUT': outputs['Lucas2016']['OUTPUT'],
        'STYLE': stylefolder +' \\LUM_LUCAS2016.qml',
        'OUTPUT':QgsProcessing.TEMPORARY_OUTPUT
    }
    outputs['Lucas2016Styled'] = processing.run('qgis:setstyleforvectorlayer', alg_params, context=context, feedback=feedback, is_child_algorithm=True)

    
    alg_params = {
        'AltitudeAddend': 0,
        'AltitudeField': 'altitude',
        'AltitudeInterpretation': 1,  # Use QGIS geometry Z value if present
        'AltitudeMode': 0,  # clampToGround
        'AltitudeModeField': 'alt_mode',
        'DateBeginField': '',
        'DateEndField': '',
        'DateStampField': '',
        'DateTimeBeginField': 'time_begin',
        'DateTimeEndField': 'time_end',
        'DateTimeStampField': 'time_when',
        'DescriptionField': ['LUCNA_1990','LUCNA_2008','LUCNA_2012','LUCNA_2016'],
        'ExportStyle': True,
        'InputLayer': outputs['Lucas2016Styled']['INPUT'],
        'LineWidthFactor': 2,
        'NameField': '',
        'OutputKmz': outputfolder + '/'+ forestname + '_LUCAS2016.kmz',
        'PhotoField': '',
        'TimeBeginField': '',
        'TimeEndField': '',
        'TimeStampField': '',
        'UseGoogleIcon': None
    }
    outputs['ExportKmzlucas2016Styled'] = processing.run('kmltools:exportkmz', alg_params, context=context, feedback=feedback, is_child_algorithm=True)

    
    alg_params = {
        'INPUT': outputs['Lucas2008']['OUTPUT'],
        #'STYLE': stylefolder + '\\LUM_LUCAS2008.qml'
        'STYLE': stylefolder + '\\test.qml'
        #'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
    }
    outputs['Lucas2008Styled'] = processing.run('qgis:setstyleforvectorlayer', alg_params, context=context, feedback=feedback, is_child_algorithm=True)


    alg_params = {
        'AltitudeAddend': 0,
        'AltitudeField': 'altitude',
        'AltitudeInterpretation': 1,  # Use QGIS geometry Z value if present
        'AltitudeMode': 0,  # clampToGround
        'AltitudeModeField': 'alt_mode',
        'DateBeginField': '',
        'DateEndField': '',
        'DateStampField': '',
        'DateTimeBeginField': 'time_begin',
        'DateTimeEndField': 'time_end',
        'DateTimeStampField': 'time_when',
        'DescriptionField': ['LUCNA_1990','LUCNA_2008','LUCNA_2012','LUCNA_2016'],
        'ExportStyle': True,
        'InputLayer': outputs['Lucas2008Styled']['INPUT'],
        'LineWidthFactor': 2,
        'NameField': '',
        'OutputKmz': outputfolder + '/'+ forestname + '_LUCAS2008.kmz',
        'PhotoField': '',
        'TimeBeginField': '',
        'TimeEndField': '',
        'TimeStampField': '',
        'UseGoogleIcon': None
    }
    outputs['ExportKmzlucas2008Styled'] = processing.run('kmltools:exportkmz', alg_params, context=context, feedback=feedback, is_child_algorithm=True)

    alg_params = {
        'AltitudeAddend': 0,
        'AltitudeField': 'altitude',
        'AltitudeInterpretation': 1,  # Use QGIS geometry Z value if present
        'AltitudeMode': 0,  # clampToGround
        'AltitudeModeField': 'alt_mode',
        'DateBeginField': '',
        'DateEndField': '',
        'DateStampField': '',
        'DateTimeBeginField': 'time_begin',
        'DateTimeEndField': 'time_end',
        'DateTimeStampField': 'time_when',
        'DescriptionField': ['LUCNA_1990','LUCNA_2008','LUCNA_2012','LUCNA_2016'],
        'ExportStyle': True,
        'InputLayer': outputs['Lucas2008Styled']['INPUT'],
        'LineWidthFactor': 2,
        'NameField': '',
        'OutputKmz': outputfolder + '/'+ forestname + '_LUCAS2008.kmz',
        'PhotoField': '',
        'TimeBeginField': '',
        'TimeEndField': '',
        'TimeStampField': '',
        'UseGoogleIcon': None
    }
    outputs['ExportKmzlucas2008Styled'] = processing.run('kmltools:exportkmz', alg_params, context=context, feedback=feedback, is_child_algorithm=True)

1 Answer 1

1

There is a space in my code which caused the path to the style file to be invalid.

1
  • There is a good library for that os.path
    – Taras
    Commented Jun 13, 2022 at 5:19

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.