-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmaj_vitesses_bd_topo.py
More file actions
223 lines (175 loc) · 10.6 KB
/
maj_vitesses_bd_topo.py
File metadata and controls
223 lines (175 loc) · 10.6 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
"""
Model exported as python.
Name : Modèle
Group :
With QGIS : 34003
"""
from qgis.core import QgsProcessing, QgsDataProvider, QgsVectorDataProvider,QgsField
from qgis.core import QgsProcessingAlgorithm
from qgis.core import QgsProcessingMultiStepFeedback
from qgis.core import QgsProcessingParameterFeatureSource
from qgis.core import QgsProcessingParameterField
from qgis.core import QgsProcessingParameterMatrix, QgsVariantUtils
from qgis.PyQt.QtCore import QCoreApplication,QVariant
import processing, gc
class UpdateSpeedBdTopo(QgsProcessingAlgorithm):
def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterFeatureSource('routes_bdtopo', self.tr('BDTOPO road layer'), types=[QgsProcessing.TypeVectorLine], defaultValue=None))
self.addParameter(QgsProcessingParameterField('nature', self.tr('Nature'), type=QgsProcessingParameterField.Any, parentLayerParameterName='routes_bdtopo', allowMultiple=False, defaultValue=None))
self.addParameter(QgsProcessingParameterField('nb_voies', self.tr('Number of lanes'), type=QgsProcessingParameterField.Any, parentLayerParameterName='routes_bdtopo', allowMultiple=False, defaultValue=None))
self.addParameter(QgsProcessingParameterField('urbain', self.tr('Urban'), type=QgsProcessingParameterField.Any, parentLayerParameterName='routes_bdtopo', allowMultiple=False, defaultValue=None))
self.addParameter(QgsProcessingParameterField('taille_au', self.tr('Urban area size'), type=QgsProcessingParameterField.Any, parentLayerParameterName='routes_bdtopo', allowMultiple=False, defaultValue=None))
self.addParameter(QgsProcessingParameterField('cat_commune', self.tr('Municipality category'), type=QgsProcessingParameterField.Any, parentLayerParameterName='routes_bdtopo', allowMultiple=False, defaultValue=None))
self.addParameter(QgsProcessingParameterMatrix('table_des_vitesses', self.tr('Car speed table'), numberRows=1, hasFixedNumberRows=False, headers=['Type','Nb_voies','V_RC','V_URB'],
defaultValue=['Bac ou liaison maritime','0','1','1','Bretelle','0','50','50','Bretelle','1','50','50','Bretelle','2','60','50',
'Chemin','0','1','1',
'Escalier','0','0','1',
'Rond-point','0','20','20','Rond-point','1','20','20','Rond-point','2','25','20',
'Route empierrée','0','30','20',
'Route à 1 chaussée','0','70','20','Route à 1 chaussée','1','80','20','Route à 1 chaussée','2','90','25','Route à 1 chaussée','3','90','25','Route à 1 chaussée','4','90','25',
'Route à 2 chaussées','0','90','20','Route à 2 chaussées','1','90','25','Route à 2 chaussées','2','110','30','Route à 2 chaussées','3','110','30','Route à 2 chaussées','0','110','30',
'Sentier','0','0','1',
'Type autoroutier','0','120','90','Type autoroutier','1','90','60','Type autoroutier','2','120','90','Type autoroutier','3','120','90','Type autoroutier','4','120','90','Type autoroutier','5','120','90','Type autoroutier','6','120','90']))
self.addParameter(QgsProcessingParameterMatrix('coef_pointe', self.tr('Peak hour factor'), numberRows=1, hasFixedNumberRows=False, headers=["Type aire urbaine",'0','1','2','3','4','5'],
defaultValue=['11','1.00','0.75','0.50','0.33','0.25','0.20',
'12','1.00','0.85','0.70','0.60','0.50','0.45',
'13','1.00','0.85','0.70','0.60','0.50','0.45',
'20','1.00','0.95','0.90','0.85','0.80','0.75',
'30','1.00','1.00','1.00','0.99','0.95','0.90']))
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=self.parameterAsMatrix(parameters,'coef_pointe',context)
nat=self.parameterAsFields(parameters,'nature',context)[0]
nbv=self.parameterAsFields(parameters,'nb_voies',context)[0]
urb=self.parameterAsFields(parameters,'urbain',context)[0]
au1=self.parameterAsFields(parameters,'taille_au',context)[0]
au2=self.parameterAsFields(parameters,'cat_commune',context)[0]
vitesses={}
coefs={}
ncols=4
for i in range(0,len(table),ncols):
vitesses[(table[i],table[i+1],'Non')]=table[i+2]
vitesses[(table[i],table[i+1],'Oui')]=table[i+3]
for i in range(0,len(pointe),7):
coefs[(pointe[i],'0')]=float(pointe[i+1])
coefs[(pointe[i],'1')]=float(pointe[i+2])
coefs[(pointe[i],'2')]=float(pointe[i+3])
coefs[(pointe[i],'3')]=float(pointe[i+4])
coefs[(pointe[i],'4')]=float(pointe[i+5])
coefs[(pointe[i],'5')]=float(pointe[i+6])
# 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=2
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.commitChanges()
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"))
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.blockSignals(True)
tableau.startEditing()
tableau.beginEditCommand(self.tr("updating field"))
if tableau.dataProvider().capabilities() & QgsVectorDataProvider.ChangeAttributeValues:
for p,f in enumerate(couche.getFeatures()):
num=f.id()
nb_voies=f[nbv]
if not nb_voies:
nb_voies=0
urbain=f[urb]
if not urbain:
urbain='Non'
nature=f[nat]
if not nature:
nature='Route à 1 chaussée'
lig=(f[nat],str(f[nbv]),f[urb])
if lig in vitesses:
valeur=float(vitesses[lig])
else:
valeur=float(vitesses[(str(f[nat]),'0','Non')])
cat1=f[au1]
if not cat1:
cat1='0'
cat2=f[au2]
if not cat2:
cat2='30'
coef=float(coefs[(cat2,cat1)])
l=f.geometry().length()
try:
valid={id_champ1: valeur,id_champ2: coef*valeur}
except:
feedback.setProgressText(';'.join([str(i) for i in f.attributes()]))
tableau.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_bdtopo':couche}
def tr(self, string, context=''):
if context == '':
context = self.__class__.__name__
return QCoreApplication.translate(context, string)
def name(self):
return 'UpdateSpeedBdTopo'
def displayName(self):
return self.tr('Update IGN BDTOPO car speeds')
def tr(self, string):
return QCoreApplication.translate('UpdateSpeedBdTopo', string)
def group(self):
return 'Network'
def groupId(self):
return 'Network'
def createInstance(self):
return UpdateSpeedBdTopo()