Thank you Nyall!
Sorry for replying so late but I suddenly went on a busy 2 week trip abroad.
Last HF martin and I discussed the current geometry model and some of
the flaws and weird things. Especially the set() and get() are not very
logical function names to me.
To find out about the model I tested it using a script in the qgis
python console. I'm still using it often to figure how to create and
convert geometries. Hope it helps others as well. Would be nice for the
cookbook maybe.
Kind regards,
Raymond
#points
p1 = QgsGeometry().fromWkt('point(0 0)')
print(p1) # <QgsGeometry: Point (0 0)>
print(p1.asPoint()) # <QgsPointXY: POINT(0 0)>
print(p1.centroid()) # <QgsGeometry: Point (0 0)>
print(p1.buffer(1, 1)) # <QgsGeometry: Polygon ((1 0, 0 -1, -1 0, 0 1, 1
0))>
print(p1.get()) # <QgsPoint: Point (0 0)>
p2 = QgsPoint(1,2)
print(p2) # <QgsPoint: Point (1 2)>
p2g = QgsGeometry(p2)
print(p2g) # <QgsGeometry: Point (1 2)>
print(p2) # <QgsPoint: Point (1 2)>
print(p2.centroid()) # <QgsPoint: Point (1 2)>
print(QgsPointXY(p2.x(), p2.y())) # <QgsPointXY: POINT(1 2)>
#print(QgsGeometry(p2)) # <QgsGeometry: Point (1 2)> !!! Converts
p2 in place ? !!!
g = QgsGeometry()
print(g)
g.set(p2)
print(g)
p3 = QgsPointXY(2, 0)
print(p3) # <QgsPointXY: POINT(2 0)>
print(QgsGeometry.fromPointXY(p3)) # <QgsGeometry: Point (2 0)>
print(QgsPoint(p3.x(), p3.y())) # <QgsPoint: Point (2 0)>
# lines
l1 = QgsGeometry().fromWkt('linestring((0 0, 1 1, 1 2))')
print(l1) # <QgsGeometry: LineString (0 0, 1 1, 1 0)>
print(l1.buffer(1, 1).asPolygon())
print(l1.asPolyline()) # [<QgsPointXY: POINT(0 0)>, <QgsPointXY: POINT(1
1)>, <QgsPointXY: POINT(1 0)>]
#print(QgsLineString(l1.asPolyline())) # TypeError: index 0 has type
'QgsPointXY' but 'QgsPoint' is expected
p1 = QgsPointXY(100,100)
print(p1)
p2 = QgsPoint(200,200)
l1 = QgsLineString()
print(l1)
l1.addVertex(QgsPoint(p1.x(), p1.y()))
l1.addVertex(p2)
print(l1)
pg1 = QgsGeometry().fromWkt('polygon((0 0, 0 1, 1 1, 1 0, 0 0))')
print(pg1) # <QgsGeometry: Polygon ((0 0, 0 1, 1 1, 1 0, 0 0))>
pg2 = pg1.get()
print(pg2) # <QgsPolygon: Polygon ((0 0, 0 1, 1 1, 1 0, 0 0))>
g = QgsGeometry()
g.set(pg2)
print(g) # <QgsGeometry: Polygon ((0 0, 0 1, 1 1, 1 0, 0 0))>
On 21-03-19 09:43, Nyall Dawson 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)
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