Skip to content

Commit e5d10b6

Browse files
committedApr 6, 2017
Fix Refactor Fields error if layer has no features
1 parent a30cf29 commit e5d10b6

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed
 

‎python/plugins/processing/algs/qgis/FieldsMapper.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -153,30 +153,33 @@ def processAlgorithm(self, feedback):
153153
inFeat = QgsFeature()
154154
outFeat = QgsFeature()
155155
features = vector.features(layer)
156-
total = 100.0 / len(features)
157-
for current, inFeat in enumerate(features):
158-
rownum = current + 1
159-
160-
geometry = inFeat.geometry()
161-
outFeat.setGeometry(geometry)
162-
163-
attrs = []
164-
for i in range(0, len(mapping)):
165-
field_def = mapping[i]
166-
expression = expressions[i]
167-
exp_context.setFeature(inFeat)
168-
exp_context.lastScope().setVariable("row_number", rownum)
169-
value = expression.evaluate(exp_context)
170-
if expression.hasEvalError():
171-
error_exp = expression
172-
break
173-
174-
attrs.append(value)
175-
outFeat.setAttributes(attrs)
176-
177-
writer.addFeature(outFeat)
178-
179-
feedback.setProgress(int(current * total))
156+
if len(features):
157+
total = 100.0 / len(features)
158+
for current, inFeat in enumerate(features):
159+
rownum = current + 1
160+
161+
geometry = inFeat.geometry()
162+
outFeat.setGeometry(geometry)
163+
164+
attrs = []
165+
for i in range(0, len(mapping)):
166+
field_def = mapping[i]
167+
expression = expressions[i]
168+
exp_context.setFeature(inFeat)
169+
exp_context.lastScope().setVariable("row_number", rownum)
170+
value = expression.evaluate(exp_context)
171+
if expression.hasEvalError():
172+
error_exp = expression
173+
break
174+
175+
attrs.append(value)
176+
outFeat.setAttributes(attrs)
177+
178+
writer.addFeature(outFeat)
179+
180+
feedback.setProgress(int(current * total))
181+
else:
182+
feedback.setProgress(100)
180183

181184
del writer
182185

6 commit comments

Comments
 (6)

m-kuhn commented on Apr 6, 2017

@m-kuhn
Member

@borysiasty looks like this causes crashes / segfaults (at least) in unit tests. Can we revert this and work out in a pull request what's causing trouble here?

nyalldawson commented on Apr 6, 2017

@nyalldawson
Collaborator

@m-kuhn it's not this commit - it's from one of my processing ones. I need to make sure the providers are all correctly deregistered when the test suite finishes. Will do asap

m-kuhn commented on Apr 6, 2017

@m-kuhn
Member

Thanks, @nyalldawson

borysiasty commented on Apr 6, 2017

@borysiasty
MemberAuthor

Thanks @nyalldawson. @m-kuhn I didn't make a PR, as it's just one if-else statement added (all the rest is just indentation change)

arnaud-morvan commented on Apr 6, 2017

@arnaud-morvan
Contributor

@borysiasty : Note that it was not necessary to indent the for loop if the layer is empty, to avoid conflicts.

borysiasty commented on Jul 11, 2017

@borysiasty
MemberAuthor

Right, sorry for the unnecessary buzz.

Please sign in to comment.