-
Notifications
You must be signed in to change notification settings - Fork 0
/
scriptAutomatisationCarte.py
278 lines (217 loc) · 8.8 KB
/
scriptAutomatisationCarte.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# -*- coding: utf-8 -*-
"""
• L’utilisateur saisi une valeur de buffer
• On crée un buffer autour des boites selon la valeur saisie par l’utilisateur. Le
résultat du buffer est ensuite affiché dans le projet QGIS.
• L’utilisateur saisi deux valeurs capacite et mode de pose
• Sur la couche <cables > faire un filtre sur le champ capacite supérieur à la
valeur saisie et mode_pose = au mode de pose saisi
• A partir du résultat de la sélection faire une carte avec symbologie graduée
en fonction du nombre de prises (colonne nb_prises)
"""
## Import des processing :
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsProcessing,
QgsProcessingAlgorithm,
QgsProcessingException,
QgsProcessingOutputNumber,
QgsProcessingParameterDistance,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterVectorDestination,
QgsProcessingParameterRasterDestination,
QgsVectorLayer,
QgsProcessingParameterNumber,
QgsProcessingParameterString,
####
QgsFeatureSink,
QgsProcessingException,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterField,
QgsProcessingParameterString,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterFile,
QgsField,
QgsExpression,
QgsExpressionContext,
QgsExpressionContextUtils,
QgsSymbol,
QgsRendererRange,
QgsGraduatedSymbolRenderer,
QgsProject)
from qgis.utils import iface
import processing
import os
class ExampleProcessingAlgorithm(QgsProcessingAlgorithm):
def tr(self, string):
return QCoreApplication.translate('Processing', string)
def createInstance(self):
return ExampleProcessingAlgorithm()
def name(self):
return 'myscript'
def displayName(self):
return self.tr('My Script')
def group(self):
return self.tr('Example scripts')
def groupId(self):
return 'examplescripts'
def shortHelpString(self):
return self.tr("Example algorithm short description")
#######################################################################################
#######################################################################################
#######################################################################################
### Déclaration des variables ###
# Couche sur laquelle on fait le buffer :
INPUT = 'INPUT'
# Paramètre de distance du buffer :
BUFFERDIST = 'BUFFERDIST'
# Paramètre de capacité des cables :
CAPACITE = 'CAPACITE'
# Paramètre du mode de pose des cables :
MODE_DE_POSE = 'MODE_DE_POSE'
# Buffer Output :
BUFFER_OUTPUT = 'BUFFER_OUTPUT'
# Cables Output :
CABLES_OUTPUT = 'CABLES_OUTPUT'
# Output final :
OUTPUT = 'OUTPUT'
def initAlgorithm(self, config=None):
# Couche buffer :
self.addParameter(
QgsProcessingParameterFeatureSource(
'INPUT',
self.tr('Couche buffer :'),
types=[QgsProcessing.TypeVectorAnyGeometry]
)
)
# Distance buffer :
self.addParameter(
QgsProcessingParameterDistance(
'BUFFERDIST',
self.tr('Distance buffer :'),
defaultValue = 1.0,
# Unités :
parentParameterName='INPUT'
)
)
# Capacité cable :
self.addParameter(
QgsProcessingParameterNumber(
'CAPACITE',
self.tr('Capacité du cable :'),
optional = False,
defaultValue = 1.0
)
)
# Mode de pose du cable :
self.addParameter(
QgsProcessingParameterString(
'MODE_DE_POSE',
self.tr('Mode de pose du cable :'),
optional = False,
defaultValue = "AERIEN"
)
)
# Buffer output :
self.addParameter(
QgsProcessingParameterVectorDestination(
'BUFFER_OUTPUT',
self.tr('Buffer output'),
)
)
# Cables output :
self.addParameter(
QgsProcessingParameterVectorDestination(
'CABLES_OUTPUT',
self.tr('Cables output'),
)
)
def processAlgorithm(self, parameters, context, feedback):
# Couche buffer :
input_featuresource = self.parameterAsSource(parameters,
'INPUT',
context)
# Buffer distance :
bufferdist = self.parameterAsDouble(parameters,
'BUFFERDIST',
context)
# Check for cancelation
if feedback.isCanceled():
return {}
buffer_result = processing.run(
'native:buffer',
{
# Here we pass on the original parameter values of INPUT
# and BUFFER_OUTPUT to the buffer algorithm.
'INPUT': parameters['INPUT'],
'OUTPUT': parameters['BUFFER_OUTPUT'],
'DISTANCE': bufferdist,
'SEGMENTS': 10,
'DISSOLVE': True,
'END_CAP_STYLE': 0,
'JOIN_STYLE': 0,
'MITER_LIMIT': 10
},
# Because the buffer algorithm is being run as a step in
# another larger algorithm, the is_child_algorithm option
# should be set to True
is_child_algorithm=True,
#
# It's important to pass on the context and feedback objects to
# child algorithms, so that they can properly give feedback to
# users and handle cancelation requests.
context=context,
feedback=feedback)
# Check for cancelation
if feedback.isCanceled():
return {}
# Selection de la couche cable :
cables = QgsVectorLayer(
"/Users/pierre-loupgarrigues/Python JupyterLab/M2/pyqgis/Evaluation_pyqgis/data/cables.shp",
"cables",
"ogr")
cables = iface.activeLayer()
# Séléction des cables en fonction des paramètres choisis :
cables.selectByExpression(" \"capacite\" > '{}' AND \"mode_pose\" = '{}' ".format(parameters[self.CAPACITE], parameters[self.MODE_DE_POSE]))
# Vérification :
selectionCapacite = self.parameterAsDouble(parameters, 'CAPACITE', context)
print(selectionCapacite)
# Vérification :
selectionModeDePose = self.parameterAsString(parameters, 'MODE_DE_POSE', context)
print(selectionModeDePose)
# Sauvegarde de la séléction des cables :
cables_selection = processing.run(
'qgis:saveselectedfeatures',
{'INPUT':cables,
#'OUTPUT':'memory:'
'OUTPUT': parameters['CABLES_OUTPUT']
},
context=context,
feedback=feedback,
is_child_algorithm=True)
mes_cables=cables_selection['OUTPUT']
### SYMBOLOGIE GRADUEE
nb_prises = (
('Faible', 0, 9, 'green'),
('Moyen_1', 10, 25, 'yellow'),
('Moyen_2', 26, 100, 'orange'),
('Eleve', 101, 1000000, 'red'),
)
# creation
ranges = []
for label, lower, upper, color in nb_prises:
symbol = QgsSymbol.defaultSymbol(cables.geometryType())
symbol.setColor(QColor(color))
rng = QgsRendererRange(lower, upper, symbol, label)
ranges.append(rng)
# create the renderer and assign it to a layer
expression = 'nb_prises' # field name
renderer = QgsGraduatedSymbolRenderer(expression, ranges)
cables.setRenderer(renderer)
# Return the results
return {
'BUFFER_OUTPUT': buffer_result['OUTPUT'],
'CABLES_OUTPUT' : cables_selection['OUTPUT']
}