pythonqgis

How can I create a 3d- polygon in QGIS by python procedure?


there is something like a road in 3d- coordinates. I need to store it into a 3D- Polygon (or anything similar, that QGIS can work with).

example_pointlist = ((0,0,130), (50,0,140), (70,10,150), (70,20,145), (49,10, 135), (0,10,130)) #all points are QgsPoint 

These are 6 points in described as an open polygon. I know that this is not flat. It is a kind of a mesh. How can I create this in QGIS (via python)? Any Ideas?

I tried: QgsGeometry.fromPolygonXY([example_pointlist ]) --> must be QgsPointXY (doesn't work because of missing z- component)

Some other stuff - but nothing worked. Any Ideas? (I work with QGIS 3.28 Firenze)


Solution

  • QgsPointXY doesn't indeed support 3D (Z). You need to build QgsPoints (this way) before making the polygon :

    from qgis.core import QgsLineString, QgsPoint, QgsPolygon
    
    example_pointlist = (
        (0, 0, 130),
        (50, 0, 140),
        (70, 10, 150),
        (70, 20, 145),
        (49, 10, 135),
        (0, 10, 130),
    )
    
    poly3d = QgsPolygon(QgsLineString([QgsPoint(*p) for p in example_pointlist]))
    

    Output (3D Map View) :

    enter image description here