-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmint_network.py
More file actions
127 lines (104 loc) · 5.55 KB
/
mint_network.py
File metadata and controls
127 lines (104 loc) · 5.55 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
"""
Model exported as python.
Name : Mint network
Group : Network
With QGIS : 32602
"""
from qgis.PyQt.QtCore import QCoreApplication,QVariant
from qgis.core import *
from qgis.utils import *
from qgis.core import QgsProcessing
from qgis.core import QgsProcessingAlgorithm
from qgis.core import QgsProcessingMultiStepFeedback
from qgis.core import QgsProcessingParameterVectorLayer
from qgis.core import QgsProcessingParameterFileDestination
from qgis.core import QgsProcessingParameterExpression
import processing
import io
class MintNetwork(QgsProcessingAlgorithm):
def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterVectorLayer('network', self.tr('Network(Qgis)'), types=[QgsProcessing.TypeVectorLine], defaultValue=None))
self.addParameter(QgsProcessingParameterFileDestination('MintNetworkFile', self.tr('Mint network file'), fileFilter='*.txt', createByDefault=True, defaultValue=None))
self.addParameter(QgsProcessingParameterExpression('direction', self.tr('direction'), parentLayerParameterName='network', defaultValue=''))
self.addParameter(QgsProcessingParameterExpression('allow_alighting', self.tr('allow alighting'), parentLayerParameterName='network', defaultValue=''))
self.addParameter(QgsProcessingParameterExpression('allow_boarding', self.tr('allow boarding'), parentLayerParameterName='network', defaultValue=''))
self.addParameter(QgsProcessingParameterExpression('vehicle_capacity', self.tr('vehicle capacity'), parentLayerParameterName='network', defaultValue=''))
self.addParameter(QgsProcessingParameterExpression('headway', self.tr('headway'), parentLayerParameterName='network', defaultValue=''))
self.addParameter(QgsProcessingParameterExpression('travel_time', self.tr('travel time'), parentLayerParameterName='network', defaultValue=''))
self.addParameter(QgsProcessingParameterExpression('i2', self.tr('i'), parentLayerParameterName='network', defaultValue=''))
self.addParameter(QgsProcessingParameterExpression('j2', self.tr('j'), parentLayerParameterName='network', defaultValue=''))
self.addParameter(QgsProcessingParameterExpression('line2', self.tr('line'), parentLayerParameterName='network', defaultValue=''))
def processAlgorithm(self, parameters, context, model_feedback):
# Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
# overall progress through the model
feedback = QgsProcessingMultiStepFeedback(1, model_feedback)
results = {}
outputs = {}
tableau=self.parameterAsVectorLayer(parameters,'network', context)
sortie=parameters['MintNetworkFile']
fich_sortie=io.open(sortie,'w',encoding='utf-8')
formuleContexte=self.createExpressionContext(parameters,context)
formuleContexte.appendScopes(QgsExpressionContextUtils.globalProjectLayerScopes(tableau))
v0=QgsExpression(parameters['direction'])
v0.prepare(formuleContexte)
v1=QgsExpression(parameters['allow_alighting'])
v1.prepare(formuleContexte)
v2=QgsExpression(parameters['allow_boarding'])
v2.prepare(formuleContexte)
v3=QgsExpression(parameters['vehicle_capacity'])
v3.prepare(formuleContexte)
v4=QgsExpression(parameters['headway'])
v4.prepare(formuleContexte)
v5=QgsExpression(parameters['travel_time'])
v5.prepare(formuleContexte)
v6=QgsExpression(parameters['i2'])
v6.prepare(formuleContexte)
v7=QgsExpression(parameters['j2'])
v7.prepare(formuleContexte)
v8=QgsExpression(parameters['line2'])
v8.prepare(formuleContexte)
reseau=tableau.getFeatures()
for f in reseau:
formuleContexte.setFeature(f)
sens=v0.evaluate(formuleContexte)
if str(sens)=='1':
ligne=[v6.evaluate(formuleContexte),
v7.evaluate(formuleContexte),
v8.evaluate(formuleContexte),
v5.evaluate(formuleContexte),
v4.evaluate(formuleContexte),
v3.evaluate(formuleContexte),
v2.evaluate(formuleContexte),
v1.evaluate(formuleContexte)]
if ligne[4]!=NULL:
fich_sortie.write(';'.join([str(s) for s in ligne])+'\n')
fich_sortie.close()
# Mint parameters
results['MintNetworkFile'] = parameters['MintNetworkFile']
return results
def name(self):
return 'MintNetwork'
def displayName(self):
return self.tr('Mint network')
def group(self):
return self.tr('Network')
def groupId(self):
return 'Network'
def tr(self, string):
return QCoreApplication.translate('MintNetwork', string)
def shortHelpString(self):
return self.tr("""
Write a Mint network file (text file with ";" as delimiter) required for a Mint assignment
Required
network: Layer that contains information about i,j line, travel times and headway information
i: initial node Id
j: final node Id
line: line Id
travel time: arc travel time
headway: Interval between two consecutive vehicles
capacity: number of seats of the vehicle
allowboarding: 1 if boarding is allowed, 0 otherwise
alowalighting; 1 if alighting is allowed, 0 otherwise
""")
def createInstance(self):
return MintNetwork()