How to add feature in shape file with pyqgis?
Asked Answered
F

1

7

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??

Freeland answered 30/6, 2013 at 10:54 Comment(0)
S
7

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

Skim answered 5/8, 2014 at 22:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.