3

According to documentation: https://qgis.org/pyqgis/master/core/QgsGeometry.html?highlight=frompolygonxy#qgis.core.QgsGeometry.fromPolygonXY

fromPolygonXY(polygon: object)→ QgsGeometry¶ Creates a new geometry from a QgsPolygon

Parameters polygon (object) –

Return type QgsGeometry

But on executing the following code, when i try to give out a QgsPolygon object as an argument to QgsGeometry.fromPolygonXY it throws out an error

def create_circle(x, y, r):
    layer = QgsVectorLayer('Polygon?crs=epsg:4326', 'poly' , "memory")
    pr = layer.dataProvider() 

    pt = QgsFeature()
    point = QgsPoint(x,y)
    # pt.setGeometry(QgsGeometry.fromPoint(point))
    pr.addFeatures([pt])

    layer.updateExtents()

    circle = qgis.core.QgsCircle.fromCenterDiameter(center = point, diameter = 2*r)
    feat = QgsFeature()
    # print(dir(QgsGeometry))
    print(dir(circle))
    print(circle.toPolygon())
    feat.setGeometry(QgsGeometry.fromPolygonXY(circle.toPolygon()))
    
    # circle_geom = QgsGeometry.makeCircle(point, r)
    pr.addFeatures([feat])
    return layer

circle.toPolygon() returns a QgsPolygon object as QgsCircle class has QgsEllipse as the base class

https://qgis.org/pyqgis/master/core/QgsEllipse.html?highlight=topolygon#qgis.core.QgsEllipse.toPolygon

toPolygon(self, segments: int = 36)→ QgsPolygon¶ Returns a segmented polygon.

Parameters segments (int = 36) – Number of segments used to segment geometry.

Return type QgsPolygon

qgis details:

QGIS version
3.21.0-Master
QGIS code revision
1906e19899
Qt version
5.15.2
Python version
3.8.7
GDAL/OGR version
3.2.3
PROJ version
6.3.2
EPSG Registry database version
v9.8.6 (2020-01-22)
GEOS version
3.9.1-CAPI-1.14.2
SQLite version
3.31.1
PDAL version
2.2.0
PostgreSQL client version
12.3
SpatiaLite version
4.3.0a
QWT version
6.1.4
QScintilla2 version
2.11.4
OS version
macOS 11.1
1
  • @MrXsquared I get NameError: name 'QgsPolygonXY' is not defined Commented Sep 16, 2021 at 19:19

1 Answer 1

5

I finally figured it out, from other parts of the documentation the correct arguments that should be provided to fromPolygonXY is a [[QgsPointsXY_obj1, QgsPointsXY_obj2, ...]]

link: https://docs.qgis.org/3.16/en/docs/pyqgis_developer_cookbook/geometry.html?highlight=polygonxy#geometry-construction

and the documentation at https://qgis.org/pyqgis/master/core/QgsGeometry.html?highlight=frompolygonxy#qgis.core.QgsGeometry.fromPolygonXY seems to be incorrect.

I also raised a Bug Report: https://github.com/qgis/QGIS/issues/45099

I fixed it in the code with a little bit of rearrangements as:

def create_circle(x, y, r):
    layer = QgsVectorLayer('Polygon?crs=epsg:4326', 'poly' , "memory")
    pr = layer.dataProvider() 

    pt = QgsFeature()
    point = QgsPoint(x,y)
    pr.addFeatures([pt])

    layer.updateExtents()

    circle = qgis.core.QgsCircle.fromCenterDiameter(center = point, diameter = 2*r)
    feat = QgsFeature()
    circle_points_qgspoint_xy = []
    for i in circle.points():
        circle_points_qgspoint_xy.append(QgsPointXY(i))
    feat.setGeometry(QgsGeometry.fromPolygonXY([circle_points_qgspoint_xy]))
    
    pr.addFeatures([feat])

    # add layer to the registry
    QgsProject.instance().addMapLayer(layer)

    canvas = QgsMapCanvas()
    # set extent to the extent of our layer
    canvas.setExtent(layer.extent())

    # set the map canvas layer set
    canvas.setLayers([layer])

create_circle(0.662, 0.1822, 0.25)
1
  • Python list comprehension: circle_points_qgspoint_xy = [QgsPointXY(i) for i in circle.points()] Commented Oct 1, 2021 at 8:51

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.