21

I've been visiting and revisiting the page on geometry handling in the PyQGIS Cookbook: http://documentation.qgis.org/2.0/en/docs/pyqgis_developer_cookbook/geometry.html but can't seem to figure out how to get the polygon to draw from the Python console. Can anyone help?

Hugo Roussaffa
2,35617 silver badges44 bronze badges
asked Feb 18, 2014 at 1:56

2 Answers 2

34

it is not realy complicated, look at Memory provider in vector: :

  • a point is created with QgsPoint(x,y) and QgsGeometry.fromPoint(QgsPoint(x,y))
  • a line is created with two points: QgsGeometry.fromPolyline([QgsPoint(x1,y1),QgsPoint(x2,y2)]))
  • a polygon is created with a list of points: QgsGeometry.fromPolygon([[QgsPoint(x1,y1),QgsPoint(x2,y2), QgsPoint(x3,y3)]])

1) two points:

# create a memory layer with two points
layer = QgsVectorLayer('Point', 'points' , "memory")
pr = layer.dataProvider() 
# add the first point
pt = QgsFeature()
point1 = QgsPoint(50,50)
pt.setGeometry(QgsGeometry.fromPoint(point1))
pr.addFeatures([pt])
# update extent of the layer
layer.updateExtents()
# add the second point
pt = QgsFeature()
point2 = QgsPoint(100,150)
pt.setGeometry(QgsGeometry.fromPoint(point2))
pr.addFeatures([pt])
# update extent
layer.updateExtents()
# add the layer to the canvas
QgsMapLayerRegistry.instance().addMapLayers([layer])

enter image description here

2) the line connecting the two points

layer = QgsVectorLayer('LineString', 'line' , "memory")
pr = layer.dataProvider() 
line = QgsFeature()
line.setGeometry(QgsGeometry.fromPolyline([point1,point2]))
pr.addFeatures([line])
layer.updateExtents()
QgsMapLayerRegistry.instance().addMapLayers([layer])

enter image description here

3) a polygon covering the points

layer = QgsVectorLayer('Polygon', 'poly' , "memory")
pr = layer.dataProvider() 
poly = QgsFeature()
points = [point1,QgsPoint(50,150),point2,QgsPoint(100,50)]
# or points = [QgsPoint(50,50),QgsPoint(50,150),QgsPoint(100,150),QgsPoint(100,50)] 
poly.setGeometry(QgsGeometry.fromPolygon([points]))
pr.addFeatures([poly])
layer.updateExtents()
QgsMapLayerRegistry.instance().addMapLayers([layer])

enter image description here

-

Changes in QGIS 3.0 and onward:

For QGIS 3.0 and onward the above workflow is still correct, but certain functions have changed. See https://qgis.org/api/api_break.html

To update the above code, change following functions:

QgsPoint -> QgsPointXY
QgsfromPoint -> QgsfromPointXY
QgsfromPolyline -> QgsfromPolylineXY
QgsfromPolygon -> QgsfromPolylineXY
QgsfromPolyline -> QgsfromPolylineXY
QgsMapLayerRegistry -> QgsProject
answered Feb 18, 2014 at 17:26
3
  • Thank you so much for the code. I was wondering how can I get rid of CRS selection dialog after I run the code? Commented Jun 13, 2014 at 4:31
  • how can I add style? Commented May 18, 2017 at 5:34
  • That update was extremely useful for me -couldn't seem to find it in the documentation- I've used this answer several times now for my tools. Thanks so much! Commented Feb 20, 2020 at 15:43
4

Just select the CRS in the layer definition : QgsVectorLayer('Polygon?crs=epsg:2154', 'poly' , "memory") for instance (here EPSG 2154 is for Lambert 93 projection, standard in Metropolitan France, but you can put whatever you want)

answered Jul 6, 2016 at 14:50
1
  • how can i add style? Commented May 18, 2017 at 5:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.