-
Notifications
You must be signed in to change notification settings - Fork 1
/
inverted_clip_algorithm.py
90 lines (73 loc) · 3.27 KB
/
inverted_clip_algorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""
Model exported as python.
Name : inverted_clip
Group : mio_wind:routing
With QGIS : 32000
"""
from qgis.core import QgsProcessing
from qgis.core import QgsProcessingAlgorithm
from qgis.core import QgsProcessingMultiStepFeedback
from qgis.core import QgsProcessingParameterExtent
from qgis.core import QgsProcessingParameterVectorLayer
from qgis.core import QgsProcessingParameterFeatureSink
from qgis.core import QgsCoordinateReferenceSystem
import processing
class Inverted_clip(QgsProcessingAlgorithm):
def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterVectorLayer('sourcelayer', 'source layer', types=[QgsProcessing.TypeVectorAnyGeometry], defaultValue=None))
self.addParameter(QgsProcessingParameterExtent('extent', 'extent', defaultValue=None))
self.addParameter(QgsProcessingParameterFeatureSink('Inverted_clip', 'inverted_clip', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True))
def processAlgorithm(self, parameters, context, model_feedback):
# Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
# overall progress through the model
feedback = QgsProcessingMultiStepFeedback(4, model_feedback)
results = {}
outputs = {}
# Create layer from extent
alg_params = {
'INPUT': parameters['extent'],
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['CreateLayerFromExtent'] = processing.run('native:extenttolayer', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(1)
if feedback.isCanceled():
return {}
# Assign projection
alg_params = {
'CRS': QgsCoordinateReferenceSystem('EPSG:4326'),
'INPUT': outputs['CreateLayerFromExtent']['OUTPUT'],
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['AssignProjection'] = processing.run('native:assignprojection', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(2)
if feedback.isCanceled():
return {}
# Clip
alg_params = {
'INPUT': parameters['sourcelayer'],
'OVERLAY': outputs['AssignProjection']['OUTPUT'],
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['Clip'] = processing.run('native:clip', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(3)
if feedback.isCanceled():
return {}
# Difference
alg_params = {
'INPUT': outputs['AssignProjection']['OUTPUT'],
'OVERLAY': outputs['Clip']['OUTPUT'],
'OUTPUT': parameters['Inverted_clip']
}
outputs['Difference'] = processing.run('native:difference', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
results['OUTPUT'] = outputs['Difference']['OUTPUT']
return results
def name(self):
return 'inverted_clip'
def displayName(self):
return 'inverted_clip'
def group(self):
return 'Sail tools'
def groupId(self):
return 'sailtools'
def createInstance(self):
return Inverted_clip()