2

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
    else:
        attr_val = round(val,4)
        feature.addAttribute(0,QVariant(attr_val))
    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?


I have another problem here ,

  for s in dataset:
        x=s[0]
        y=s[1]      
        feature = QgsFeature()
        qpoint = QgsPoint( float(x) , float(y) )
        polygon=[]
        polygon.append(qpoint)

        feature.setGeometry(QgsGeometry.fromPolygon([polygon]))

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?

2 Answers 2

3

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.

thus:

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])

and:

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

Look at the API documentation for QgsGeometry:

1

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.