# Path: safe/gis/processing_tools.py
# def initialize_processing():
#     """
#     Initializes processing, if it's not already been done
#     """
# 
#     need_initialize = False
# 
#     needed_algorithms = [
#         'native:clip',
#         'gdal:cliprasterbyextent',
#         'native:union',
#         'native:intersection'
#     ]
# 
#     if not QgsApplication.processingRegistry().algorithms():
#         need_initialize = True
# 
#     if not need_initialize:
#         for needed_algorithm in needed_algorithms:
#             if not QgsApplication.processingRegistry().algorithmById(
#                     needed_algorithm):
#                 need_initialize = True
#                 break
# 
#     if need_initialize:
#         QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
#         processing.Processing.initialize()
# 
# def create_processing_feedback():
#     """
#     Creates a default processing feedback object
# 
#     :return: Processing feedback
#     :rtype: QgsProcessingFeedback
#     """
#     return QgsProcessingFeedback()
# 
# def create_processing_context(feedback):
#     """
#     Creates a default processing context
# 
#     :param feedback: Linked processing feedback object
#     :type feedback: QgsProcessingFeedback
# 
#     :return: Processing context
#     :rtype: QgsProcessingContext
#     """
#     context = QgsProcessingContext()
#     context.setFeedback(feedback)
#     context.setProject(QgsProject.instance())
# 
#     # skip Processing geometry checks - Inasafe has its own geometry validation
#     # routines which have already been used
#     context.setInvalidGeometryCheck(QgsFeatureRequest.GeometryNoCheck)
#     return context


import processing
from safe.common.custom_logging import LOGGER
from safe.common.exceptions import ProcessingInstallationError
from safe.definitions.processing_steps import clean_geometry_steps
from safe.gis.processing_tools import (
    initialize_processing,
    create_processing_feedback,
    create_processing_context)
from safe.gis.sanity_check import check_layer
from safe.utilities.profiling import profile

"""Try to make a layer valid."""


__copyright__ = "Copyright 2016, The InaSAFE Project"
__license__ = "GPL version 3"
__email__ = "info@inasafe.org"
__revision__ = '$Format:%H$'


@profile
def clean_layer(layer):
    """Clean a vector layer.

    :param layer: The vector layer.
    :type layer: qgis.core.QgsVectorLayer

    :return: The buffered vector layer.
    :rtype: qgis.core.QgsVectorLayer
    """
    output_layer_name = clean_geometry_steps['output_layer_name']
    output_layer_name = output_layer_name % layer.keywords['layer_purpose']

    count = layer.featureCount()

    parameters = {
        'INPUT': layer,
        'OUTPUT': 'memory:'
    }


# Path: safe/gis/processing_tools.py
# def initialize_processing():
#     """
#     Initializes processing, if it's not already been done
#     """
# 
#     need_initialize = False
# 
#     needed_algorithms = [
#         'native:clip',
#         'gdal:cliprasterbyextent',
#         'native:union',
#         'native:intersection'
#     ]
# 
#     if not QgsApplication.processingRegistry().algorithms():
#         need_initialize = True
# 
#     if not need_initialize:
#         for needed_algorithm in needed_algorithms:
#             if not QgsApplication.processingRegistry().algorithmById(
#                     needed_algorithm):
#                 need_initialize = True
#                 break
# 
#     if need_initialize:
#         QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
#         processing.Processing.initialize()
# 
# def create_processing_feedback():
#     """
#     Creates a default processing feedback object
# 
#     :return: Processing feedback
#     :rtype: QgsProcessingFeedback
#     """
#     return QgsProcessingFeedback()
# 
# def create_processing_context(feedback):
#     """
#     Creates a default processing context
# 
#     :param feedback: Linked processing feedback object
#     :type feedback: QgsProcessingFeedback
# 
#     :return: Processing context
#     :rtype: QgsProcessingContext
#     """
#     context = QgsProcessingContext()
#     context.setFeedback(feedback)
#     context.setProject(QgsProject.instance())
# 
#     # skip Processing geometry checks - Inasafe has its own geometry validation
#     # routines which have already been used
#     context.setInvalidGeometryCheck(QgsFeatureRequest.GeometryNoCheck)
#     return context


# Path: safe/gis/vector/clean_geometry.py
import processing
from safe.common.custom_logging import LOGGER
from safe.common.exceptions import ProcessingInstallationError
from safe.definitions.processing_steps import clean_geometry_steps
from safe.gis.processing_tools import (
    initialize_processing,
    create_processing_feedback,
    create_processing_context)
from safe.gis.sanity_check import check_layer
from safe.utilities.profiling import profile


"""Try to make a layer valid."""


__copyright__ = "Copyright 2016, The InaSAFE Project"
__license__ = "GPL version 3"
__email__ = "info@inasafe.org"
__revision__ = '$Format:%H$'


@profile
def clean_layer(layer):
    """Clean a vector layer.

    :param layer: The vector layer.
    :type layer: qgis.core.QgsVectorLayer

    :return: The buffered vector layer.
    :rtype: qgis.core.QgsVectorLayer
    """
    output_layer_name = clean_geometry_steps['output_layer_name']
    output_layer_name = output_layer_name % layer.keywords['layer_purpose']

    count = layer.featureCount()

    parameters = {
        'INPUT': layer,
        'OUTPUT': 'memory:'
    }


    initialize_processing()