Geographic Information Systems Asked on February 6, 2021
I’m trying to implement / reuse Add Feature functionality in my python plugin. there a class/function that handles geometry creation on canvas based on layer geometry definition? I’m checking API but I can’t find a function that gives you functionality to digitize geometry and pass it as a feature to layer – or something similar.
I managed to get layer to editing mode now I can’t find a way to implement this.
So basically —> with PyQGIS API.
Here would be a way to add a new feature to a polygon vector:
# reference your layer, such as the active layer
lyr = iface.activeLayer()
# get the vertexes of your new geometry and add them to a list
coordinatePairs = []
coordinatePairs.append(QgsPoint(-80.23, -3.28))
coordinatePairs.append(QgsPoint(-65.58, -4.21))
coordinatePairs.append(QgsPoint(-65.87, 9.50))
coordinatePairs.append(QgsPoint(-80.10, 10.44))
# create a polygon using the above coordinates
newPolygon = QgsGeometry.fromPolygon([coordinatePairs])
# create a feature, add the polygon to it
feature = QgsFeature()
feature.setGeometry(newPolygon)
# access the layer"s data provider, add the feature to it
dataProvider = lyr.dataProvider()
dataProvider.addFeatures([feature])
# refresh map canvas to see the result
iface.mapCanvas().refresh()
With my comments I think this should be pretty self-explanatory.
In this case you would not even have to start an edit session, and you would write this feature directly to the file, which might not exactly be what you are trying to implement.
Either way, what is now missing is that the points that are appended to this list, are not hard-coded but are actually coming from the user. You would therefore have to combine all of this with a click event that gets the coordinates on click and appends them to the list. Since this is all done via a plugin I would also suggest you add a button that lets you start the editing (i.e. setting new points) and lets you stop it (so a button that allows toggling, like the typical "Add Feature" button, but you could surely add two separate buttons as well). The button functionality would, of course, come from PyQt, and then reference to your PyQGIS functions. You also might want to add a line that combines each single point that is set to be used as a vertex. Although optional, that would greatly enhance the user experience.
The above example, by the way, would create a polygon that more or less hides Colombia:
PS: for inspiration I looked at the PyQGIS Cookbook that was released by Packt Pub a few days ago.
Answered by BritishSteel on February 6, 2021
I spent a while trying to figure this out too.
The button is found in the QgisInterface class.
# Find the layer to edit
layer = qgis.utils.iface.activeLayer()
layer.startEditing()
# Implement the Add Feature button
qgis.utils.iface.actionAddFeature().trigger()
Then add the features you wish to add to the layer.
Answered by Nicola on February 6, 2021
A complete solution with also saving the layer, as requested in a comment:
# Get the active layer
layer = iface.activeLayer()
# Define a function called when a feature is added to the layer
def feature_added():
# Disconnect from the signal
layer.featureAdded.disconnect()
# Save changes and end edit mode
layer.commitChanges()
# Connect the layer to the signal featureAdded, so when a feature is
# added to the layer, the feature_added function is called
layer.featureAdded.connect(feature_added)
# Set the layer in edit mode
layer.startEditing()
# Activate the QGIS add feature tool
iface.actionAddFeature().trigger()
Answered by Marioba on February 6, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP