I'm trying to create an algorithm similar to minimum bounding box (though it may end up looking nothing like it). In this case the angle will be passed as a parameter and given the angle I need the smallest rectangle covering all my points/polygons. So far my line of thought is to find the center of my points (centroid algorithm), and from there create two parallel lines with same angle as paramter angle, and two more lines perpendicular to them. Then using iteration move these lines outwards (in opposite directions) until they contain all points. Also doesn't have to be exact minimum bounding box, approximate works (I guess would depend on size of each iteration step).
Here is my code so far. I have already dissolved all my polygons into one. I then take a convex hull to reduce vertices. I then put all vertices in a list – not sure if this helps yet…
a = layer.getFeatures()
for feat in a:
geom = feat.geometry()
a = geom.convexHull()
vertexId = QgsVertexId()
vertices = []
b = a.constGet().nextVertex(vertexId)
while b[0]:
vertices.append(b[1])
b = a.constGet().nextVertex(vertexId)
Notes: At some point I need to pass the angle of box. I am using QGIS 3, and need to create this in Python. Layer 'layer' has one geometry, the dissolved polygon of all other polygons – maybe iteration isn't needed to access it..
Please let me know if I should be passing on more details/info.
Best Answer
Here is the complete code. It contains too many lines (much more than needed for sure) but it works. Now you can clean it if you like.
In resume the algorithm calculates the maximum distance betweeen parallel lines that have the slope defined by the rotation parameter and pass though the points. For each point there will be created a 'horizontal' and 'vertical' line. This names are just orientative as they are defined at position 0 (rotation = 0). So, for each external point there will be created this 2 posible lines and then, iteratively, the poligon will be created based on the 4 external, or said in other way, where the distance of parallel lines are maximum.
One last thing: it is made to be used in QGIS 3.8 with grass.