On Thu, 21 Mar 2019 at 15:59, Raymond Nijssen <r.nijs...@terglobo.nl> wrote: > > > > def toLines(geom): > > return QgsGeometry(geom.get().boundary()) > > > > (Using > > https://qgis.org/pyqgis/master/core/QgsAbstractGeometry.html#qgis.core.QgsAbstractGeometry.boundary > > ) > > > > Nyall > > Thanks Nyall, that works!
Great, thanks for the confirmation. And because I've got to do mapping all day, I'm in a good mood, and you get a free PyQGIS lesson: geom.get() : gives you the underlying fundamental geometry object attached to the feature. QgsFeature.geometry() returns a QgsGeometry object, which is more or less a "container" for geometries. It's got some convenient methods which apply to ALL geometry types, but sometimes you need to dig down to the actual geometry primitive. In that case you use geometry.get(), and you get the fundamental QgsPoint/QgsLineString/QgsPolygon/etc object. It's actually generally preferable to call "geometry.constGet()", IF you are doing some operation which doesn't alter the geometry in place (like you are here). But that's a complex microoptimisation. geom.get().boundary() gives you the topological boundary of the primitive. For polygons this is their exterior + interior rings, for lines it's their start and end point (unless it's a closed ring, in which case you get a null geometry). Points have no boundary. Using boundary() to convert polygons to lines is the most efficient method - it's very heavily optimised, and works perfectly with curved geometry types and maintains any Z or M values which may be present. Lastly, you need to wrap the result back up into a QgsGeometry object - hence QgsGeometry(....boundary() ). This is because most of QGIS API works with QgsGeometry objects (remember, they are like "containers" holding a geometry), and NOT the fundamental geometry objects. Done! A super-efficient, rock solid approach which will work with all input geometry types. Win! (for reference - this is what the polygons to lines algorithm actually does in the background too) Nyall > > > > > > >> > >> I will try tomorrow. > >> > >> One last question, it seems to me like the old code is working again in > >> 3.7. Can you confirm that? > > > > Shouldn't be -- maybe you have a leftover .py file here? > > Indeed. I did a clean install and it is not working in my 3.7 anymore. > > > Regards, > Raymond _______________________________________________ QGIS-Developer mailing list QGIS-Developer@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer