australia traveling travelling travel
|

PyQGIS: Create a Point Feature

The QGIS Python API (PyQGIS) makes it possible to automate creation of point features. This tutorial will show you how to add features to an existing shapefile. For this example we will assume the layer containing the point features is already opened in the QGIS table of contents.

First, we save all layers with a specified name to a variable. Then open the first layer in that list as a QgsVectorLayer.

layers = QgsProject.instance().mapLayersByName('layer name')
layer = QgsVectorLayer(layers[0].dataProvider().dataSourceUri(), '', 'ogr')

Next, we need to save the layer capabilities. This will tell us what actions we can and can’t take with the this layer. We will need to check the capabilities to be sure we can add features to the layer.

caps = layer.dataProvider().capabilities()

Now we’ll write an if statement to make sure we have the capability to add features to the layer. The remainder of the code will be placed inside the if statement so it won’t run if the layer doesn’t have the proper capabilities.

if caps & QgsVectorDataProvider.AddFeatures:

If our layer has the proper capabilities we need to create the new feature to add. This feature needs to have the same fields as the layer it will be added to. This layer has two fields: an integer field and a text field. We specify the field values by passing a list to setAttributes().

if caps & QgsVectorDataProvider.AddFeatures:
    feat = QgsFeature(layer.fields())
    feat.setAttributes([0, 'added programatically'])

Now we’ll specify the location of the point by setting the feature geometry. This is a point feature so we’ll pass a QgsPointXY geometry to setGeometry(). This layer is in a UTM projection. Make sure the coordinates you pass are in the correct format for the projection/coordinate reference system or your layer.

if caps & QgsVectorDataProvider.AddFeatures:
    feat = QgsFeature(layer.fields())
    feat.setAttributes([0, 'added programatically'])
    feat.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(453257, 4631706)))

Finally, we add a list of new features to the layer. This returns a result telling us if the addition was successful, and a list of features.

if caps & QgsVectorDataProvider.AddFeatures:
    feat = QgsFeature(layer.fields())
    feat.setAttributes([0, 'added programatically'])
    feat.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(453257, 4631706)))
    res, outFeats = layer.dataProvider().addFeatures([feat])

Here’s the full code block. See the video below for a step by step demonstration. Good luck!

layers = QgsProject.instance().mapLayersByName('layer name')
layer = QgsVectorLayer(layers[0].dataProvider().dataSourceUri(), '', 'ogr')
caps = layer.dataProvider().capabilities()

if caps & QgsVectorDataProvider.AddFeatures:
    feat = QgsFeature(layer.fields())
    feat.setAttributes([0, 'added programatically'])
    feat.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(453257, 4631706)))
    res, outFeats = layer.dataProvider().addFeatures([feat])

Similar Posts