3

I'm trying to implement QgsGeometry.smooth() in my plugin as according to the API specifications.

API Link here

Here is my code:

def lineSmooth(self, line):
    ''' Takes a line geometry and applies smooth
    '''
    iterations = int(self.dlg.smoothIterations.value())
    offset = float(self.dlg.smoothOffset.value())
    minDist = float(self.dlg.smoothMinDist.value())
    maxAngle = float(self.dlg.smoothMaxAngle.value())

    return line.smooth(iterations, offset, minDist, maxAngle)

I am getting this error:

TypeError: QgsGeometry.smooth(int, float): too many arguments

When I print the parameter values I'm sending to the method:

(1, 0.25, -1.0, 180.0)

Now, I'm pretty new to Python and programming in general, and this is my first QGIS plugin, so I might be doing something silly.

1
  • You need a try...except clause.
    – xunilk
    Nov 26, 2016 at 9:55

1 Answer 1

3

First, I tried out this code:

layer = iface.activeLayer()

feat = layer.getFeatures().next()

geom = feat.geometry()

length = geom.length()

print length

new_geom = geom.smooth(1, 0.25)

print new_geom.exportToWkt()

and it worked; as it can be watched at next image (new_geom was exported in WKT format and it was visualized with QuickWKT plugin):

enter image description here

When it is necessary four parameters, you should use a try...except clause. In my particular case it worked nicely.

layer = iface.activeLayer()

feat = layer.getFeatures().next()

geom = feat.geometry()

length = geom.length()

print length

try:
    new_geom = geom.smooth(1, 0.25, -1, 180)

except TypeError:
    pass

print new_geom.exportToWkt() 

The new geometry (four parameters) is visualized in green color.

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.