Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

2013/06/27 edit:

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?

share|improve this question
add comment

2 Answers 2

up vote 1 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.

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

Look at the API documentation for QgsGeometry:

share|improve this answer
    
This is where they are defined qgis.org/api/1.8/… –  Nathan W Jun 24 '13 at 5:55
add comment

Your Answer

 
discard

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.