QGIS – How to Find Longest Straight Line Within Polygon in QGIS

distancemaximumpolygonpolyline-creationqgis

I'm looking for an algorithm to draw max distance straight lines contained in a polygon (without crossing polygon boundaries). How can I perform it?

As you can see some polygons are very oddly shaped.

enter image description here

The area I am working with is millions of acres and contains hundreds of thousands of individual polygons. From what I can tell the polygons are pretty clean (no holes, overlaps, etc.)

The practical use of this is to filter what bodies of water are capable of having a floatplane land and take off on them.

The only criteria I have for a solution is that I can perform it in QGIS and that the lines are straight.

Best Answer

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