Skip to content
Permalink
Browse files
Fix Refactor Fields error if layer has no features
  • Loading branch information
borysiasty committed Apr 6, 2017
1 parent a30cf29 commit e5d10b69463a59e8d80b63762c6da26b9ebfd538
Showing with 27 additions and 24 deletions.
  1. +27 −24 python/plugins/processing/algs/qgis/FieldsMapper.py
@@ -153,30 +153,33 @@ def processAlgorithm(self, feedback):
inFeat = QgsFeature()
outFeat = QgsFeature()
features = vector.features(layer)
total = 100.0 / len(features)
for current, inFeat in enumerate(features):
rownum = current + 1

geometry = inFeat.geometry()
outFeat.setGeometry(geometry)

attrs = []
for i in range(0, len(mapping)):
field_def = mapping[i]
expression = expressions[i]
exp_context.setFeature(inFeat)
exp_context.lastScope().setVariable("row_number", rownum)
value = expression.evaluate(exp_context)
if expression.hasEvalError():
error_exp = expression
break

attrs.append(value)
outFeat.setAttributes(attrs)

writer.addFeature(outFeat)

feedback.setProgress(int(current * total))
if len(features):
total = 100.0 / len(features)
for current, inFeat in enumerate(features):
rownum = current + 1

geometry = inFeat.geometry()
outFeat.setGeometry(geometry)

attrs = []
for i in range(0, len(mapping)):
field_def = mapping[i]
expression = expressions[i]
exp_context.setFeature(inFeat)
exp_context.lastScope().setVariable("row_number", rownum)
value = expression.evaluate(exp_context)
if expression.hasEvalError():
error_exp = expression
break

attrs.append(value)
outFeat.setAttributes(attrs)

writer.addFeature(outFeat)

feedback.setProgress(int(current * total))
else:
feedback.setProgress(100)

del writer

6 comments on commit e5d10b6

@m-kuhn

This comment has been minimized.

Copy link
Member

@m-kuhn m-kuhn replied Apr 6, 2017

@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

This comment has been minimized.

Copy link
Collaborator

@nyalldawson nyalldawson replied Apr 6, 2017

@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

This comment has been minimized.

Copy link
Member

@m-kuhn m-kuhn replied Apr 6, 2017

Thanks, @nyalldawson

@borysiasty

This comment has been minimized.

Copy link
Member Author

@borysiasty borysiasty replied Apr 6, 2017

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

This comment has been minimized.

Copy link
Contributor

@arnaud-morvan arnaud-morvan replied Apr 6, 2017

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

@borysiasty

This comment has been minimized.

Copy link
Member Author

@borysiasty borysiasty replied Jul 11, 2017

Right, sorry for the unnecessary buzz.

Please sign in to comment.