-
Notifications
You must be signed in to change notification settings - Fork 0
/
fichier_aff.py
59 lines (48 loc) · 1.47 KB
/
fichier_aff.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
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.utils import *
import codecs
##CEREMA=group
##arcs=vector
##ij=field arcs
##volume=string volau
##type_arc=string type
##fichier_aff=file
##affectation=output vector
##encodage=string utf_8_sig
links=processing.getObject(arcs)
aff=codecs.open(fichier_aff,"r",encoding=encodage)
champs={}
valeurs=processing.features(links)
fij={}
trafic={}
champs2=QgsFields()
champs2.append(QgsField("ij",QVariant.String))
champs2.append(QgsField("volume",QVariant.Double))
champs2.append(QgsField("type",QVariant.String))
iti=QgsVectorFileWriter(affectation,"UTF-8",champs2,QGis.WKBMultiLineString,links.crs(),"ESRI Shapefile")
for i,j in enumerate(valeurs):
try:
fij[j[ij]]=j
except:
fij[j['i']+'-'+j['j']]=j
for k,i in enumerate(aff):
elements=i.split(";")
if k==0:
for ide, e in enumerate(elements):
champs[e.strip("\"").strip("\n").strip("\r")]=ide
else:
cle=elements[champs["i"]].strip("\"")+"-"+elements[champs["j"]].strip("\"")
volau=elements[champs[volume]].replace(",",".")
type2=elements[champs[type_arc]]
if cle not in trafic:
trafic[cle]=(0,'0')
trafic[cle]=(trafic[cle][0]+float(volau),type2)
for i in trafic:
if i in fij:
f=QgsFeature()
f.setGeometry(fij[i].geometry())
f.setAttributes([i,trafic[i][0],trafic[i][1]])
iti.addFeature(f)
aff.close()