Creating polygon around set of points using PyQGIS

I need to create a polygon (as close to a regular polygon) around a set of points like this image below.

So, I thought in offset the points and create a polygon from it. But, there may be in the middle of the set a point that does not belong to the border, like red ones in the image.

I need suggestions (not code, but is welcome too) to solve this problem in QGIS 3. Maybe there is already a Plugin or script exists.

• Is the polygon orientation must be "North-South" or it must follow the points locations ? May 5, 2021 at 13:14
• A good starting point to get rid of internal points might be to use convex hull on the point groups and try to reshape the resulting polygon so it's regular. May 5, 2021 at 13:15
• If always north-south-oriented: Create a grid, extract by location, dissolve
– BERA
May 5, 2021 at 13:37
• @J.Monticolo, the polygon orientation doesn't matter. May 5, 2021 at 13:48
• Does this answer your question? Drawing outline of point cloud using QGIS Dec 10, 2021 at 9:26

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

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

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

'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:

• Thanks for the response @Taras, the closest approach using minimumboundinggeometry is type 3, but it's not close enough. I'll keep searching. May 5, 2021 at 18:08
• You are welcome. Maybe extend your answer with this comment. May 5, 2021 at 18:14
• I can't thank you enough @Taras. I've had a lot of headache searching how to do it programmatically (at least I've had learned new things lol) when the "simple" convex hull doesn't fit my needs, and you come with a ready-to-go solution. I owe you one! May 6, 2021 at 12:15
• I hope I could help you a bi =) May 6, 2021 at 13:09