Skip to main content
edited title
Link
ThomasG77
  • 31.2k
  • 1
  • 55
  • 95

Convexhull Convex hull for selected features - PyQGIS

added 413 characters in body
Source Link
Lorenzo
  • 1.1k
  • 1
  • 7
  • 19

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

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()

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

Source Link
Lorenzo
  • 1.1k
  • 1
  • 7
  • 19

Convexhull for selected features - PyQGIS

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()