Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add feature in shape file with pyqgis?

Tags:

python

qgis

I am now have a shape file and I want to add value to fill it with color

but now I have a problem

http://www.qgis.org/pyqgis-cookbook/vector.html#add-features

 feat = QgsFeature()
  feat.addAttribute(0,"hello")
  feat.setGeometry(QgsGeometry.fromPoint(QgsPoint(123,456)))
  (res, outFeats) = layer.dataProvider().addFeatures( [ feat ] )

Here is the example for point,and now I have polygons and polylines

I want to know if I have polygon how to add feature to the shape file I select with python??

like image 552
blueman010112 Avatar asked Jun 30 '13 10:06

blueman010112


1 Answers

A few things. If you want to add a new layer, the call for it is like the one below (the keyword is Polygon).

 va = QgsVectorLayer("Polygon?crs=epsg:4326", "Segment buffers", "memory")

To set geometry to a Polyline, the code is like the following:

line_geometry=QgsGeometry.fromPolyline(QgsPoint(x,y),QgsPoint(w,z),...,QgsPoint(t,r))

The first and last points will be the extremities, while the middle ones will provide the actual shape (curvature) of the line feature

To set geometry to a Polygon, the code is like the following:

polygon_geometry=QgsGeometry.fromPolygon( [[ QgsPoint(x,y), QgsPoint(w,z),..., QgsPoint(p,l) ] ] )

Attributes work just the same for all geometry types. Hope it helps.

For more references, you can look at http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/geometry.html

like image 84
PCamargo Avatar answered Sep 19 '22 00:09

PCamargo