QGIS – How to Find the Longest Horizontal Line Inside a Polygon in QGIS

lengthlinemaximumqgisqgis-processing

I have a state polygon like the pic shown below, is there a way to find the line which is the maximum horizontal line?

Like this, but I want the maximum distance line.

enter image description here

Best Answer

Below is a workflow I would suggest - (1) create bunch of horizontal lines, (2) Clip (or intersect) horizontal lines by your polygon, and (3) Select the longest one among the clipped horizontal lines.

(1) Create horizontal lines.

Use Create grid tool in the Processing ToolBox - Vector creation. Set Horizontal spacing to be wide enough to cover the polygon entirely. Set Vertical spacing as small as reasonably practicable (fine spacing would give you accurate result, but it may be computationally intensive).

enter image description here

(2) Clip the lines by your polygon

Use Intersection tool in the Processing ToolBox - Vector overlay.

enter image description here

(3) Select the longest one

Use Select by Expression tool in the Processing ToolBox - Vector selection. Activate Expression window by clicking on a small epsilon button, then enter an expression (as below).

$length = maximum($length)

This expression will see if the length of the relevant line is equal to the maximum of all the clipped lines.

enter image description here

(4) Check your result

Step (3) will return a new layer (usually Matching features). Check the line visually, and its length.

enter image description here