Finding longest straight line within polygon in QGIS

When holes of the polygons have to be avoided

So, this is an extension to my previous answer Calculating the longest distance within polygon in QGIS but with some changes in the Step 3, particularly in the query.

SELECT p1.id, setsrid(make_line(p1.geometry, p2.geometry),  #put your srid here),
       max(st_length(make_line(p1.geometry, p2.geometry))) AS length
FROM "Points" AS p1, "polygons" AS p
JOIN "Points" AS p2 ON p1.id = p2.id
WHERE NOT st_equals(p1.geometry, p2.geometry)
      AND st_within(make_line(p1.geometry, p2.geometry), st_buffer(p.geometry, 0.00005))
GROUP BY p1.id

Note that in the query above additionally the geometry of the original polygons were used.

To be more example-realisting I considered different polygons to those that I had in my previous answer, see image below.

input

The corresponding result will be looking as

result

Note that the result is approximate because a bigger distance was used on the step 'Points along geometry'.

I made with 'Points along geometry', however it can also be done with the result of 'Extract vertices'.


This is also an approximation. It finds the longest line between existing polygon vertices within each polygon. So the more vertices you have the better the results should be (but the executing time will be longer). For example add more vertices using Densify by interval if the results are not good enough. But from what I can see you seem to have alot of vertices.

It can be slow, so try it on a subset of polygons first.

import itertools

layer = iface.activeLayer() #Click layer in tree

#Create empty line layer
vl = QgsVectorLayer("LineString?crs={}&index=yes".format(layer.crs().authid()), "Longest_line", "memory")
provider = vl.dataProvider()

#For each polygon find the longest line that is within the polygon
for feat in layer.getFeatures():
    verts = [v for v in feat.geometry().vertices()] #List all vertices
    all_lines = []
    for p1,p2 in itertools.combinations(verts, 2): #For every combination of two vertices
        all_lines.append(QgsGeometry.fromPolyline([p1,p2])) #Create a line
    all_lines = [line for line in all_lines if line.within(feat.geometry())] #Check if line is within polygon
    if len(all_lines)>0:
        longest_line = max(all_lines, key=lambda x: x.length()) #Find longest line
        #Create a line feature from the longest line within polygon
        f = QgsFeature()
        f.setGeometry(longest_line)
        provider.addFeature(f)

QgsProject.instance().addMapLayer(vl)

enter image description here


When holes of the polygons have to be considered

Let's assume there is a polygon layer "polygons" (pink) with its corresponding attribute table accordingly, see image below.

input

Step 1. Proceed with "Delete holes".

step_1

Step 2. Apply "Polygons to lines"

step_2

Step 3. Use "Points along geometry". The output of this algorithm save additionally as a permanent file. Both layers will be used at the Step 4.

step_3

Step 4. Make use of "Join by lines (hub lines)". Afterwards the application of "Fix geometries", "Remove null geometries" and "Delete duplicate geometries" is probable.

step_4

Step 5. Proceed with "Clip" between the result of the Step 4 and initial polygons.

step_5

Step 6. Apply a tiny 'Buffer' for initial polygons. And after make use of "Extract by location" (are within for geometrical predicate) for the Result of the Step 5.

Step 7. Use "Extract by expression" using the following expression $length = maximum($length, "id").

step_7