3

I'm trying to add a new feature in a PolygonZ gpkg layer with Python code.

I have a list of 3D points (QgsPoint), but I don't understand how to create a geometry from this list of QgsPoint. Using QgsGeometry.fromPolygonXY doesn't work because I want to keep the 3D points, not the 2D points.

1 Answer 1

5

You can do either

from qgis.core import (QgsPolygon, QgsLineString, QgsPoint)

# From a Python list
points = [[0.0, 10.0, 3.0], [5.0, 8.0, 4.0], [9.0, 5.0, 1.0], [0, 10, 3.0]]
poly3d = QgsPolygon(QgsLineString([QgsPoint(*point) for point in points]))

or

from qgis.core import (QgsPolygon, QgsLineString, QgsPoint)

# We create manually list of 3D points but you can take/create
# it from your existing 3D point layer
list_of_points = [QgsPoint(0.0, 10.0, 3.0), QgsPoint(5.0, 8.0, 4.0), QgsPoint(9.0, 5.0, 1.0), QgsPoint(0, 10, 3.0)]
poly3d = QgsPolygon(QgsLineString(list_of_points))
2
  • It works fine. Thanks
    – Etienne
    Commented Aug 24, 2021 at 17:15
  • Option rings=[] was not needed. Removed... Also accept the answer, plz. Thanks
    – ThomasG77
    Commented Aug 24, 2021 at 17:22

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.