-
Notifications
You must be signed in to change notification settings - Fork 3
/
decaler_lignes.py
265 lines (214 loc) · 9.34 KB
/
decaler_lignes.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Networks
A QGIS plugin
Networks
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2018-02-26
copyright : (C) 2018 by Patrick Palmier
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
__author__ = 'Patrick Palmier'
__date__ = '2018-02-26'
__copyright__ = '(C) 2018 by Patrick Palmier'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from PyQt5.QtCore import QCoreApplication, QVariant
from qgis.core import *
from qgis.utils import *
from qgis.core import (QgsProcessing,
QgsFeatureSink,
QgsProcessingAlgorithm,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterNumber,
QgsProcessingParameterBoolean,
QgsProcessingParameterString,
QgsProcessingParameterExtent,
QgsProcessingParameterField,
QgsProcessingParameterExpression,
QgsProcessingParameterFileDestination)
import io
class ShiftLines(QgsProcessingAlgorithm):
"""
This is an example algorithm that takes a vector layer and
creates a new identical one.
It is meant to be used as an example of how to create your own
algorithms and explain methods and variables used to do it. An
algorithm like this will be available in all elements, and there
is not need for additional work.
All Processing algorithms should extend the QgsProcessingAlgorithm
class.
"""
# Constants used to refer to parameters and outputs. They will be
# used when calling the algorithm from another algorithm, or when
# calling from the QGIS console.
INPUT = 'INPUT'
IJ='IJ'
TYPE='TYPE'
VOLUME='VOLUME'
LIGNE='LIGNE'
SHIFT='SHIFT'
def initAlgorithm(self, config):
"""
Here we define the inputs and output of the algorithm, along
with some other properties.
"""
nom_ij="i+'-'+j"
self.addParameter(
QgsProcessingParameterFeatureSource(
self.INPUT,
self.tr('Network'),
[QgsProcessing.TypeVectorLine]
)
)
self.addParameter(
QgsProcessingParameterExpression(
self.IJ,
self.tr('ij'),
parentLayerParameterName=self.INPUT,
defaultValue=nom_ij
)
)
self.addParameter(
QgsProcessingParameterField(
self.LIGNE,
self.tr('Line'),
parentLayerParameterName=self.INPUT,
type=QgsProcessingParameterField.String,
)
)
self.addParameter(
QgsProcessingParameterExpression(
self.VOLUME,
self.tr('Quantitative variable'),
parentLayerParameterName=self.INPUT
)
)
self.addParameter(
QgsProcessingParameterExpression(
self.SHIFT,
self.tr('Shift variable'),
parentLayerParameterName=self.INPUT
)
)
# We add a feature sink in which to store our processed features (this
# usually takes the form of a newly created vector layer when the
# algorithm is run in QGIS).
def processAlgorithm(self, parameters, context, feedback):
"""
Here is where the processing itself takes place.
"""
# Retrieve the feature source and sink. The 'dest_id' variable is used
# to uniquely identify the feature sink, and must be included in the
# dictionary returned by the processAlgorithm function.
input= self.parameterAsSource(parameters, self.INPUT, context)
lines_data= self.parameterAsVectorLayer(parameters, self.INPUT, context)
ij_exp=QgsExpression(self.parameterAsExpression(parameters,self.IJ,context))
id_ligne=self.parameterAsFields(parameters,self.LIGNE,context)[0]
volume=QgsExpression(self.parameterAsExpression(parameters,self.VOLUME,context))
decalage=self.parameterAsExpression(parameters,self.SHIFT,context).strip("'").strip("\"")
champs=lines_data.fields()
noms_champs=[]
for f in champs:
noms_champs.append(f.name())
lines_data.startEditing()
lines_data.beginEditCommand(self.tr("add field ")+decalage)
if decalage not in noms_champs:
lines_data.dataProvider().addAttributes([QgsField(decalage,QVariant.Double)])
lines_data.updateFields()
ijContexte=self.createExpressionContext(parameters,context, input)
ij_exp.prepare(ijContexte)
vContexte=self.createExpressionContext(parameters,context, input)
volume.prepare(vContexte)
lines=input
pr=lines_data.dataProvider()
decalages={}
for i1 in lines.getFeatures():
ijContexte.setFeature(i1)
ij=ij_exp.evaluate(ijContexte)
vContexte.setFeature(i1)
variable_quanti=volume.evaluate(vContexte)
id=i1[id_ligne]
nij=ij
nb=variable_quanti
if nij not in decalages:
decalages[nij]={}
if id not in decalages[nij]:
decalages[nij][id]=0
decalages[nij][id]+=nb
resultat={}
for j1 in decalages:
tri=sorted(decalages[j1].items(),key=lambda x:x[0],reverse=False)
res={}
tot=0
for j2 in tri:
res[j2[0]]=tot
tot+=j2[1]
resultat[j1]=res
for i1 in lines_data.getFeatures():
id=i1[id_ligne]
ijContexte.setFeature(i1)
ij=ij_exp.evaluate(ijContexte)
nij=ij
lines_data.changeAttributeValue(i1.id(), pr.fieldNameMap()[decalage],resultat[nij][id])
lines_data.commitChanges()
return {self.INPUT: decalage}
def name(self):
"""
Returns the algorithm name, used for identifying the algorithm. This
string should be fixed for the algorithm, and must not be localised.
The name should be unique within each provider. Names should contain
lowercase alphanumeric characters only and no spaces or other
formatting characters.
"""
return 'shift_lines'
def displayName(self):
"""
Returns the translated algorithm name, which should be used for any
user-visible display of the algorithm name.
"""
return self.tr('Shift lines')
def group(self):
"""
Returns the name of the group this algorithm belongs to. This string
should be localised.
"""
return self.tr('Analysis')
def groupId(self):
"""
Returns the unique ID of the group this algorithm belongs to. This
string should be fixed for the algorithm, and must not be localised.
The group id should be unique within each provider. Group id should
contain lowercase alphanumeric characters only and no spaces or other
formatting characters.
"""
return 'Analysis'
def tr(self, string):
return QCoreApplication.translate('ShiftLines', string)
def shortHelpString(self):
return self.tr("""
Update a field from a line layer (with superposed geographic links but with different line ids) to produce map layer with shifted links.
The algorithm will update an numeric attribute with a number that allows to produce flows maps with shifted lines. For example, if there is two superposed links from A to B with line 1 (100 passangers)
and line 2 (200passengers) and you want to produce the flow maps of the total number of passengers,
the algorithm will update an attribute "shift" that will have 0 for line 1 (no shift) and 100 for line 2.
Parameters:
network: lines network
ij: link id (ij attribute or expression)
line: line id
quantitative value: the exprerssion you want to visualize in the flows map (flow variable)
shift variable: field to be update with the shift value
""")
def createInstance(self):
return ShiftLines()