0

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

enter image description here

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.

Taras
  • 32,823
  • 4
  • 66
  • 137

1 Answers1

3

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.

input

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 run script and get the output that will look like

resul1

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 run script and get the output that will look like

result2

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 run script and get the output that will look like

result3

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:

Taras
  • 32,823
  • 4
  • 66
  • 137
  • Thanks for the response @Taras, the closest approach using minimumboundinggeometry is type 3, but it's not close enough. I'll keep searching. – Francisco Camello May 05 '21 at 18:08
  • You are welcome. Maybe extend your answer with this comment. – Taras May 05 '21 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! – Francisco Camello May 06 '21 at 12:15
  • I hope I could help you a bi =) – Taras May 06 '21 at 13:09