I am attempting to create a simple polygon on QGIS 3.14 using PyQGIS by adapting the code from How to draw polygons from the python console?.
I am running into an error with the
setGeometry line where the function
fromPolylineXY appears to need a
QgsPointXY object but the
pts object is a list.
Code so far is listed below.
tmp = QgsVectorLayer('Polygon?crs=epsg:29194', '200905_Bdy',"org") #Layer for polygon prv = tmp.dataProvider() #Data object for polygon layer ply01 = QgsFeature() #Object for polygon # Object with polygon verticies pts = [QgsPointXY(396100,8969000),QgsPointXY(396100,8973900),QgsPointXY(397900,8973900),QgsPointXY(397900,8969000)] ply01.setGeometry(QgsGeometry.fromPolylineXY([pts]))
You should use
fromPolygonXY instead of
fromPolylineXY to create a polygon.
If you want to create a polyline before creating a polygon, you should know this:
pts object is a list. And
fromPolylineXY expects "a list of
QgsPointXY". When you use extra brackets like
[pts], it will be a list of list of
[[QgsPointXY, QgsPointXY, ...]]).
To create o polyline you should remove the bracket in
fromPolylineXY([pts]) to get a list of
QgsPointXY. Just use
To create a polygon from point list:
To create a polyline from point list:
Answered by Kadir Şahbaz on December 13, 2020
0 Asked on September 1, 2020 by albine-pro
1 Asked on September 1, 2020 by arnpry
0 Asked on September 1, 2020 by sargam-panwar
1 Asked on August 31, 2020 by ali-mokhtari
1 Asked on August 31, 2020
1 Asked on August 30, 2020 by tomas
0 Asked on August 30, 2020 by jjd
1 Asked on August 30, 2020 by jordy-w
1 Asked on August 30, 2020 by pileoni
1 Asked on August 30, 2020 by pac_co
2 Asked on August 30, 2020 by npt_rich
2 Asked on August 29, 2020 by aviddabbler
1 Asked on August 29, 2020 by guy-forssman
1 Asked on August 29, 2020 by salrad
1 Asked on August 28, 2020 by david1212k
2 Asked on August 28, 2020 by sam-murphy
1 Asked on August 27, 2020 by wout
1 Asked on August 26, 2020 by icypy
Get help from others!