Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

now I want to use QgsPolygon (and Qgspolyline,too) class in python

I have a example by using QgsPoint but I don't know how to use in QgsPolygon

following is my Qgspoint example

    feature = QgsFeature()
    qpoint = QgsPoint( float(x), float(y) ) #get qpoint's attitube here
    feature.setGeometry(QgsGeometry.fromPoint(qpoint))#set point's geometry here
    if numpy.isnan(val):
        pass #no assign value means assign NULL value
        attr_val = round(val,4)
    feature_list.append(feature)#append value here

I want to change QgsPolygon(or QgsPolyline) instead of QgsPoint but I couldn't find the variables of QgsPolygon(or QgsPolyline)

where to find the variables of QgsPolygon and QgsPolyline?

2013/06/27 edit:

I have another problem here ,

  for s in dataset:
        feature = QgsFeature()
        qpoint = QgsPoint( float(x) , float(y) )


but it's still get

        TypeError: QgsFeature.setGeometry(): argument 1 has unexpeced type 'NoneType'

how to fix it?

Have I mistake the meaning of your answer?

share|improve this question
up vote 2 down vote accepted

see Geometry Handling

  1. a point: QgsPoint(x,y)
  2. a line: QgsGeometry.fromPolyline([QgsPoint(x1,y1),QgsPoint(x2,y2)]))
  3. a polygon: QgsGeometry.fromPolygon([[QgsPoint(x1,y1),QgsPoint(x2,y2), QgsPoint(x3,y3),QgsPoint(x1,y1)]]) -> the polygon must be closed.


line_start= QgsPoint(50,50)
line_end= QgsPoint(100,100)
line = QgsGeometry.fromPolyline([line_start, line_end])
points = [QgsPoint(60,60),QgsPoint(60,80),QgsPoint(80,80),QgsPoint(80,60),QgsPoint(60,60)] 
polygon= QgsGeometry.fromPolygon([points])


feature.setGeometry(QgsGeometry.fromPolyline([line_start, line_end]))
share|improve this answer
the example Geometry Handling gives that not closed in polygon? gPolygon = QgsGeometry.fromPolygon( [ [ QgsPoint(1,1), QgsPoint(2,2), QgsPoint(2,1) ] ] ) – blueman010112 Jun 24 '13 at 6:18
It depends of the geometry – gene Jun 24 '13 at 7:41
I have another problem above thanks for your help! – blueman010112 Jun 27 '13 at 9:30
a Polygon is not a list but It is built from a list of points – gene Jun 27 '13 at 16:31

Look at the API documentation for QgsGeometry:

share|improve this answer
This is where they are defined… – Nathan W Jun 24 '13 at 5:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.