matrice.py

Patrick Palmier, 2017-11-02 01:40 PM

Download (1.34 KB)

 
1
from PyQt4.QtCore import *
2
from PyQt4.QtGui import *
3
from qgis.core import *
4
from qgis.utils import *
5

    
6
##CEREMA=group
7
##noeuds=vector point
8
##numero=field noeuds
9
##pt_depart=point
10
##pt_arrivee=point
11
##nb_passagers=number 1.0
12
##jour=number 1
13
##heure=string 8:00:00
14
##depart=boolean true
15
##fichier_matrice=output file *.txt
16
if depart==True:
17
    sens='d'
18
else:
19
    sens='a'
20

    
21
nodes=processing.getObject(noeuds)
22
index=QgsSpatialIndex()
23
for i in processing.features(nodes):
24
    index.insertFeature(i)
25
pt_depart=pt_depart.split(",")
26
pt_arrivee=pt_arrivee.split(",")
27
dep=QgsPoint(float(pt_depart[0]), float(pt_depart[1]))
28
arr=QgsPoint(float(pt_arrivee[0]), float(pt_arrivee[1]))
29
inode=index.nearestNeighbor(dep,1)
30
jnode=index.nearestNeighbor(arr,1)
31
feat=nodes.getFeatures(request=QgsFeatureRequest(inode[0]))
32
features=[f for f in feat]
33
d=features[0].attribute(numero)
34
feat=nodes.getFeatures(request=QgsFeatureRequest(jnode[0]))
35
features=[f for f in feat]
36
a=features[0].attribute(numero)
37
horaire=heure.strip().split(':')
38
if len(horaire)==3:
39
    h=int(horaire[0])*60.0+int(horaire[1])+int(horaire[2])/60.0
40
elif len(horaire)==2:
41
    h=int(horaire[0])*60.0+int(horaire[1])
42
elif len(horaire)==1:
43
    h=float(horaire)
44

    
45

    
46
sortie=open(fichier_matrice,"w")
47
sortie.write(";".join([unicode(d),unicode(a),unicode(nb_passagers),unicode(jour),unicode(h),sens])+"\n")
48
sortie.close()
49

    
50
    
51
    
52