2

I'm trying to migrate a python plugin I wrote from QGIS 2.18 to QGIS 3.4. In the plugin, I'm creating new vector features and eventually adding them to an existing vector layer. I'm trying to have the added features selected automatically in QGIS after running the code described below. For QGIS 2.18, the QgsVectorLayer class had an addFeatures method that contained a very convenient makeSelected flag. When the flag was set to true, the added features were selected.

In QGIS 3.4 the addFeatures method still exists, but the flag is gone. I also tried to select the features using some of the class's selection methods but with no success. It seems that after adding the features to the layer, their id's change and I don't have access to these specific features and can not select them using their old ids. The only way that somehow works is assigning some sentinel value to one of the fields in all the new features and then selecting them using a query. I'd like to avoid this way because I'm afraid that at some point it can mess up a user's data.

here's an example of how I created features and added them to the current selected layer in Qgis. it can be run in the Qgis Python terminal:

layer = iface.activeLayer()
layer.startEditing()
feat = QgsFeature(layer.fields())
point_list = [QgsPointXY(0, 0), QgsPointXY(0,1), QgsPointXY(1,1), QgsPointXY(1,0), QgsPointXY(0,0)]
feat.setGeometry(QgsGeometry.fromPolygonXY(point_list))
layer.addFeature(feat)

# I'd like to add feat to the layers selection at this point

self.layer.endEditCommand()


I'm using QGIS 3.4.1 (migrating from QGIS 2.18.26), and the pyqgis API of the above versions.

I'm aware that Qgis3 uses python 3 instead of python 2.

2
  • This script does not works in the QGIS Python terminal,
    – xunilk
    Commented Jan 22, 2019 at 7:52
  • Hi xunik. Why did you change your comment? Your comment from before showed that the script did work for you in the Python terminal.
    – meir412
    Commented Jan 22, 2019 at 9:00

2 Answers 2

2

I've managed to reach the functionality I desired, and I'd like to share my solution.

I haven't found an elegant way to use the PyQGIS API to select the features you are adding to your layer, but I used the feature's geometry to track them down and add them to the selection.

I'm generalizing my solution to a list of features that you'd like to add to your layer and have them selected. I'm assuming a list called geom_list which contains any number of QgsGeometry objects which represent the geometries of the features you're adding to your layer.

# prepare layer for editing
layer = iface.activeLayer()
layer.startEditing()
feature_list = []

# create all features with geometries from geom_list and fields from our layer and store in list
for index in range(len(geom_list)):
  feat = QgsFeature(layer.fields())
  feat.setGeometry(geom_list[index]]))
  feature_list.append(feat)

# add new features to layer
layer.addFeatures(feature_list)

# Select all of the new added features
for general_feat in layer.getFeatures():
  for desired_feat in feature_list:
    if str(desired_feat.geometry()) == str(general_feat.geometry()):
      layer.select(general_feat.id())

#finish editing
self.layer.endEditCommand()


0

There is a "QgsVectorDataProvider" class that writes features to a layer, its "addFeatures" method returns the result of adding and a list of added features - Tuple[bool, List[QgsFeature]]. Since the features are already written to the layer, they have an ID by which you can select them.

    added_feats = layer.dataProvider().addFeatures(your_features)[1]
    layer.selectByIds([f.id() for f in added_feats])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.