Hi > On 21 Mar 2019, at 06:43, Nyall Dawson <[email protected]> wrote: > > On Thu, 21 Mar 2019 at 15:59, Raymond Nijssen <[email protected]> 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)
Thanks for the lesson for those of us watching from the sidelines too! Regards Tim > > 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 > [email protected] > List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer > Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer — Tim Sutton Co-founder: Kartoza Ex Project chair: QGIS.org Visit http://kartoza.com <http://kartoza.com/> to find out about open source: Desktop GIS programming services Geospatial web development GIS Training Consulting Services Skype: timlinux IRC: timlinux on #qgis at freenode.net
_______________________________________________ QGIS-Developer mailing list [email protected] List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
