-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmaj_vitesses_bd_egm.py
More file actions
206 lines (151 loc) · 8.42 KB
/
maj_vitesses_bd_egm.py
File metadata and controls
206 lines (151 loc) · 8.42 KB
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
"""
Model exported as python.
Name : Modèle
Group :
With QGIS : 34003
"""
from qgis.core import QgsProcessing, QgsDataProvider, QgsVectorDataProvider,QgsField, QgsProject
from qgis.core import QgsProcessingAlgorithm
from qgis.core import Qgis
from qgis.core import QgsProcessingMultiStepFeedback
from qgis.core import QgsProcessingParameterFeatureSource
from qgis.core import QgsProcessingParameterField,QgsProcessingParameterNumber
from qgis.core import QgsProcessingParameterMatrix, QgsVariantUtils
from qgis.PyQt.QtCore import QCoreApplication,QVariant
import processing, gc
class UpdateSpeedEgm(QgsProcessingAlgorithm):
def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterFeatureSource('routes_bdtopo', self.tr('EGM/ERM roads'), types=[QgsProcessing.TypeVectorLine], defaultValue=None))
self.addParameter(QgsProcessingParameterField('nature', self.tr('RTT'), type=QgsProcessingParameterField.Any, parentLayerParameterName='routes_bdtopo', allowMultiple=False, defaultValue=None))
self.addParameter(QgsProcessingParameterField('urbain', self.tr('COR'), type=QgsProcessingParameterField.Any, parentLayerParameterName='routes_bdtopo', allowMultiple=False, defaultValue=None))
self.addParameter(QgsProcessingParameterField('nb_chau', self.tr('MED'), type=QgsProcessingParameterField.Any, parentLayerParameterName='routes_bdtopo', allowMultiple=False, defaultValue=None))
self.addParameter(QgsProcessingParameterNumber('pointe', self.tr('Peak hour factor'),type=Qgis.ProcessingNumberParameterType.Double,defaultValue=0.5))
self.addParameter(QgsProcessingParameterMatrix('table_des_vitesses', self.tr('Car speed table'), numberRows=1, hasFixedNumberRows=False, headers=['RTT','MED','V_RC','V_URB'],
defaultValue=[
'16','1','90','50',
'16','2','120','90',
'14','1','90','50',
'14','2','110','70',
'15','1','80','50',
'15','2','90','50',
'984','1','70','20',
'984','2','80','25']))
def processAlgorithm(self, parameters, context, feedback):
# Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
# overall progress through the model
couche = self.parameterAsVectorLayer(parameters, 'routes_bdtopo', context)
table=self.parameterAsMatrix(parameters,'table_des_vitesses',context)
#pointe=0.5
rtt=self.parameterAsFields(parameters,'nature',context)[0]
cor=self.parameterAsFields(parameters,'urbain',context)[0]
med=self.parameterAsFields(parameters,'nb_chau',context)[0]
pointe=self.parameterAsDouble(parameters,'pointe',context)
vitesses={}
coefs={}
ncols=4
for i in range(0,len(table),ncols):
vitesses[(str(table[i]),str(table[i+1]),'Non')]=str(table[i+2])
vitesses[(str(table[i]),str(table[i+1]),'Oui')]=str(table[i+3])
#feedback.setProgressText(str(vitesses))
# Compute the number of steps to display within the progress bar and
# get features from source
##a=fenetre.split(",")
##fenetre2=QgsRectangle(float(a[0]),float(a[2]),float(a[1]),float(a[3]))
tableau=couche
#champ_existant=champ_existant.strip('"').strip("'")
#print(couche)
champs=tableau.fields()
chaine="QVariant.Double"
noms_champs=[c.name() for c in champs]
#feedback.setProgressText(champ_existant+' '+ ":".join(noms_champs))
taille=15
precision=5
if 'v_hc' not in noms_champs:
#tableau.startEditing()
if tableau.dataProvider().capabilities() & QgsVectorDataProvider.ChangeAttributeValues:
tableau.dataProvider().addAttributes([QgsField('v_hc',eval('QVariant.Double'),len=taille,prec=precision)])
else:
feedback.setProgressText(self.tr("Warning: The layer is not editable"))
#tableau.startEditing()
if 't_hc' not in noms_champs:
if tableau.dataProvider().capabilities() & QgsVectorDataProvider.ChangeAttributeValues:
tableau.dataProvider().addAttributes([QgsField('t_hc',eval('QVariant.Double'),len=taille,prec=precision)])
else:
feedback.setProgressText(self.tr("Warning: The layer is not editable"))
#tableau.commitChanges()
if 'v_hp' not in noms_champs:
#tableau.startEditing()
if tableau.dataProvider().capabilities() & QgsVectorDataProvider.ChangeAttributeValues:
tableau.dataProvider().addAttributes([QgsField('v_hp',eval('QVariant.Double'),len=taille,prec=precision)])
else:
feedback.setProgressText(self.tr("Warning: The layer is not editable"))
if 't_hp' not in noms_champs:
#tableau.startEditing()
if tableau.dataProvider().capabilities() & QgsVectorDataProvider.ChangeAttributeValues:
tableau.dataProvider().addAttributes([QgsField('t_hp',eval('QVariant.Double'),len=taille,prec=precision)])
else:
feedback.setProgressText(self.tr("Warning: The layer is not editable"))
lib_champ1='v_hc'
else:
lib_champ2='v_hc'
tableau.updateFields()
tableau2=couche
id_champ1=tableau2.fields().lookupField('v_hc')
id_champ2=tableau2.fields().lookupField('v_hp')
id_champ3=tableau2.fields().lookupField('t_hc')
id_champ4=tableau2.fields().lookupField('t_hp')
#nbv=id_champ2=max(couche.fields().lookupField('NB_VOIES'),couche.fields().lookupField('nb_voies'))
#urb=id_champ2=max(couche.fields().lookupField('URBAIN'),couche.fields().lookupField('urbain'))
#nat=id_champ2=max(couche.fields().lookupField('NATURE'),couche.fields().lookupField('nature'))
feedback.setProgressText(self.tr("update car speed peak hour and off-peak hour"))
#au1=id_champ2=max(couche.fields().lookupField('TAAV2017'),couche.fields().lookupField('taav2017'))
#au2=id_champ2=max(couche.fields().lookupField('CATEAAV2017'),couche.fields().lookupField('cateav2017'))
#feedback.setProgressText(str(nb))
n=tableau2.featureCount()
feedback.setProgressText(self.tr("updating field..."))
tableau.startEditing()
tableau.beginEditCommand(self.tr("updating field"))
if tableau.dataProvider().capabilities() & QgsVectorDataProvider.ChangeAttributeValues:
# Bloquer tous les rafraîchissements
tableau.blockSignals(True)
for p,f in enumerate(couche.getFeatures()):
num=f.id()
hps='Non'
if str(f[cor])=='2':
hps='Oui'
l=f.geometry().length()
lig=(str(f[rtt]),str(f[med]),hps)
#print(lig)
if lig in vitesses:
valeur=float(vitesses[lig])
else:
valeur=20.0
coef=pointe
valid={id_champ1: valeur,id_champ2: coef*valeur, id_champ3: l*60/(valeur*1000), id_champ4: l*60/(valeur*1000*coef)}
tableau.changeAttributeValues(num,valid)
#tableau.dataProvider().changeAttributeValues({num:valid})
#a2=tableau2.dataProvider().changeAttributeValues({num:valid})
feedback.setProgress(p*100/n)
else:
feedback.setProgressText(self.tr("Warning: The layer is not editable"))
tableau.endEditCommand()
tableau.commitChanges()
tableau.blockSignals(False)
gc.collect()
return {'routes_egm':couche}
def name(self):
return 'UpdateSpeedEgm'
def displayName(self):
return self.tr('Update EGM/ERM car speed')
def tr(self, string):
return QCoreApplication.translate('UpdateSpeedEgm', string)
def tr(self, string, context=''):
if context == '':
context = self.__class__.__name__
return QCoreApplication.translate(context, string)
def group(self):
return self.tr('Network')
def groupId(self):
return 'Network'
def createInstance(self):
return UpdateSpeedEgm()