1

I changed the code to create a convexhull for selected features from this question (Convexhull for selected features and performing buffer for the resultant convexhull using python) to adapt it to QGIS3, but something does not work. Where is the problem?

layer = iface.activeLayer()
feat = layer.selectedFeatures()[0]
geom = feat.geometry()
convexhull = geom.convexHull()
CRS = layer.crs().postgisSrid()
URI = "Polygon?crs=epsg:"+str(CRS)+"&field=id:integer""&index=yes"
mem_layer = QgsVectorLayer(URI,"convexhull","memory")
mem_layer.startEditing()
feat2 = QgsFeature()
feat2.setGeometry(convexhull)
feat2.setAttributes([1])
mem_layer.addFeature(feat2, True)
mem_layer.commitChanges()
QgsProject.instance().addMapLayers([mem_layer])
iface.mapCanvas().refresh()

EDIT: To be more clear, using this code this is what I get. It's a convexhull for just one feature.

enter image description here

And this is what I want. It's for all selected features. I reached this using Minimum bounding geometry from processing toolbox.

enter image description here

2

I've tested your code and made modifications to merge geometry of selected features using Merge selected feature pyqgis post. You will find below a working correction. After this first solution, you will find an alternate solution using processing from pyqgis

layer = iface.activeLayer()

selectFeatures = layer.selectedFeatures()

geom = None
for feat in selectFeatures:
    if geom == None:
       geom = feat.geometry()
    else:
       geom = geom.combine(feat.geometry())

convexhull = geom.convexHull()
CRS = layer.crs().postgisSrid()
URI = "Polygon?crs=epsg:"+str(CRS)+"&field=id:integer""&index=yes"
mem_layer = QgsVectorLayer(URI,"convexhull","memory")

mem_layer.startEditing()
feat2 = QgsFeature()
feat2.setGeometry(convexhull)
feat2.setAttributes([1])
mem_layer.addFeature(feat2)
mem_layer.commitChanges()
QgsProject.instance().addMapLayer(mem_layer)
iface.mapCanvas().refresh()

Processing alternative from PyQGIS

from qgis import processing

layer = iface.activeLayer()
newconvexhulllayer = processing.run("qgis:minimumboundinggeometry", {
              'INPUT': QgsProcessingFeatureSourceDefinition(layer.id(), True),
              'TYPE': 3,
              'OUTPUT': 'memory:'})

QgsProject.instance().addMapLayer(newconvexhulllayer['OUTPUT'])
12
  • Hi thanks for your answer. But also this solution does not work. Maybe the reason is that I'm using a polygon layer? – Lorenzo Feb 22 '20 at 15:39
  • I also use a polygon layer... Did you really select a feature in the original layer? – ThomasG77 Feb 22 '20 at 15:40
  • I've added a GIF to explain my exact process to run the code – ThomasG77 Feb 22 '20 at 15:47
  • What happens if you select multiple polygons? – Lorenzo Feb 22 '20 at 15:53
  • It works and only produce one convex hull polygon (normal with feat = layer.selectedFeatures()[0] part) – ThomasG77 Feb 22 '20 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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