Thursday, 17 March 2016

pyqgis - Finding variables of QgsPolygon and QgsPolyline?


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?



Answer



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

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...