def intersection(source, mask):
    """Intersect two layers.

    Issue https://github.com/inasafe/inasafe/issues/3186

    :param source: The vector layer to clip.
    :type source: QgsVectorLayer

    :param mask: The vector layer to use for clipping.
    :type mask: QgsVectorLayer

    :return: The clip vector layer.
    :rtype: QgsVectorLayer

    .. versionadded:: 4.0
    """
    output_layer_name = intersection_steps['output_layer_name']
    output_layer_name = output_layer_name % (
        source.keywords['layer_purpose'])

    parameters = {'INPUT': source,
                  'OVERLAY': mask,
                  'OUTPUT': 'memory:'}

    # TODO implement callback through QgsProcessingFeedback object

    initialize_processing()

    feedback = create_processing_feedback()
    context = create_processing_context(feedback=feedback)
    result = processing.run('native:intersection', parameters, context=context)
    if result is None:
        raise ProcessingInstallationError

    intersect = result['OUTPUT']
    intersect.setName(output_layer_name)
    intersect.keywords = dict(source.keywords)
    intersect.keywords['title'] = output_layer_name
    intersect.keywords['layer_purpose'] = \
        layer_purpose_exposure_summary['key']
    intersect.keywords['inasafe_fields'] = \
        dict(source.keywords['inasafe_fields'])
    intersect.keywords['inasafe_fields'].update(
        mask.keywords['inasafe_fields'])
    intersect.keywords['hazard_keywords'] = \
        dict(mask.keywords['hazard_keywords'])
    intersect.keywords['exposure_keywords'] = dict(source.keywords)
    intersect.keywords['aggregation_keywords'] = dict(
        mask.keywords['aggregation_keywords'])

    check_layer(intersect)
    return intersect
def intersection ( source , mask ) : output_layer_name = intersection_steps [ 'output_layer_name' ] output_layer_name = output_layer_name % ( source . keywords [ 'layer_purpose' ] ) parameters = { 'INPUT' : source , 'OVERLAY' : mask , 'OUTPUT' : 'memory:' } initialize_processing ( ) feedback = create_processing_feedback ( ) context = create_processing_context ( feedback = feedback ) result = processing . run ( 'native:intersection' , parameters , context = context ) if result is None : raise ProcessingInstallationError intersect = result [ 'OUTPUT' ] intersect . setName ( output_layer_name ) intersect . keywords = dict ( source . keywords ) intersect . keywords [ 'title' ] = output_layer_name intersect . keywords [ 'layer_purpose' ] = layer_purpose_exposure_summary [ 'key' ] intersect . keywords [ 'inasafe_fields' ] = dict ( source . keywords [ 'inasafe_fields' ] ) intersect . keywords [ 'inasafe_fields' ] . update ( mask . keywords [ 'inasafe_fields' ] ) intersect . keywords [ 'hazard_keywords' ] = dict ( mask . keywords [ 'hazard_keywords' ] ) intersect . keywords [ 'exposure_keywords' ] = dict ( source . keywords ) intersect . keywords [ 'aggregation_keywords' ] = dict ( mask . keywords [ 'aggregation_keywords' ] ) check_layer ( intersect ) return intersect
Intersect two layers.

    Issue https://github.com/inasafe/inasafe/issues/3186

    :param source: The vector layer to clip.
    :type source: QgsVectorLayer

    :param mask: The vector layer to use for clipping.
    :type mask: QgsVectorLayer

    :return: The clip vector layer.
    :rtype: QgsVectorLayer

    .. versionadded:: 4.0