This may not be the best and smartest solution, but a working one.
Let's assume there is a point layer 'points', see image below.
Idea 1. Using the "Minimum bounding geometry" geoalgorithm.
Proceed with Plugins > Python Console > Show Editor
and paste the script below
import processing
# providing the point layer's name
layer_name = "points"
# accessing the point layer by name
try:
layer = QgsProject.instance().mapLayersByName(layer_name)[0]
except IndexError:
raise ValueError("The layer {} does not exist.".format(layer_name))
processing.runAndLoadResults("qgis:minimumboundinggeometry", {
'INPUT': layer,
'FIELD': '',
'TYPE': 3,
'OUTPUT': 'memory:'})
Press Run script
and get the output that will look like
There are also other 'TYPE'
-parameters, e.g. 0
— Envelope (Bounding Box), 1
— Minimum Oriented Rectangle, 2
— Minimum Enclosing Circle, and 3
— Convex Hull. To get more familiar with algorithm parameters, please use processing.algorithmHelp("qgis:minimumboundinggeometry")
.
Idea 2. Using the "Concave hull (alpha shapes)" geoalgorithm.
Proceed with Plugins > Python Console > Show Editor
and paste the script below
import processing
# providing the point layer's name
layer_name = "points"
# accessing the point layer by name
try:
layer = QgsProject.instance().mapLayersByName(layer_name)[0]
except IndexError:
raise ValueError("The layer {} does not exist.".format(layer_name))
processing.runAndLoadResults("qgis:concavehull", {
'INPUT': layer,
'ALPHA': 0.5,
'HOLES': False,
'NO_MULTIGEOMETRY': False,
'OUTPUT':'memory:'})
Press Run script
and get the output that will look like
Here some assumption for a 'ALPHA'
-parameter (a number from 0
(maximum concave hull) to 1
(convex hull)) should be done. To get more familiar with algorithm parameters, please use processing.algorithmHelp("qgis:concavehull")
.
Idea 3. Using the "Concave hull (k-nearest neighbor)" geoalgorithm.
Proceed with Plugins > Python Console > Show Editor
and paste the script below
import processing
# providing the point layer's name
layer_name = "points"
# accessing the point layer by name
try:
layer = QgsProject.instance().mapLayersByName(layer_name)[0]
except IndexError:
raise ValueError("The layer {} does not exist.".format(layer_name))
processing.runAndLoadResults("qgis:knearestconcavehull", {
'INPUT': layer,
'KNEIGHBORS': 10,
'FIELD': '',
'OUTPUT': 'memory:'})
Press Run script
and get the output that will look like
Here some assumption for a 'KNEIGHBORS'
-parameter (determines the concaveness of the output polygon. A small number will result in a concave hull that follows the points very closely, while a high number will make the polygon look more like the convex hull (if the number is equal to or larger than the number of features, the result will be the convex hull). Minimum value: 3
.) should be done. To get more familiar with algorithm parameters, please use processing.algorithmHelp("qgis:knearestconcavehull")
.
References: