Question

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

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top