# Finding the longest horizontal line inside polygon in QGIS

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. • There is very simple solution using rastets and hydrology tools. I know solution in Arcmap, but it will most certainly work in Qgis, which I don't have. Aug 8 at 3:08
• Sure. Find the Ys at local maximum and minimum X values, and use these as seed points for line construction, then shift slightly north and south (repeating as necessary) to see if the distance increases or decreases. Expensive as heck, but it will find the mark about 30% of the way into the figure. You're on your own for coding this, though. Aug 8 at 3:16
• Even I was thinking the same, finding minimum and max X vals and shifting north and south and finding values, Would be really expensive upto 2-3 loops i believe Aug 8 at 3:50
• This is very different to what I have in mind anyway. Aug 8 at 5:31
• Algorithmically you could construct horizontal lines on every vertex, clip them to the perimeter and discard any that touch the perimeter more than two times, then pick the longest one. Aug 8 at 8:31

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). (2) Clip the lines by your polygon

Use `Intersection` tool in the `Processing ToolBox - Vector overlay`. (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. Step (3) will return a new layer (usually `Matching features`). Check the line visually, and its length. • In the create grid algorithm, what does the grid extent mean? Can you pls explain how did you enter the values, is it the coordinate values of max (x, y) and min (x,y) Aug 8 at 8:22
• @Anand The `grid extent` is the area you want to cover by the grid. I selected 'Draw on Canvas' (by clicking on small three-dots icon) and then manually drew an rectangle to cover the polygon. Aug 8 at 8:27
• This will only find the longest segment at the specified interval. It requires more compute time (albeit less coding effort) then using the vertices of the figure to seed a search array. Aug 8 at 15:43
• @Anand EPSG 32654 is only valid for Eastern Japan. You will need to find the proper coordinate system for your working area. Can I ask whereabout? Aug 10 at 4:49
• I got it, I chose the wrong CRS and my project was in different CRS, Thanks a lot for the quick reply, really appreciate it Aug 10 at 4:50

Picture below shows horizontal distance inside polygon (without holes) to Eastern border. I also shows contour lines of above surface. Points are the cells where focal maximum (3*3) equal to original raster value. The one shown in blue are the most remote per individual polygon segment. They coincide (in raster terms of course) with polygon vertices: It proves @bugmenot123 statement, that vertices are the only candidates to be checked. This makes task solvable in virtually no time. Note: for polygon with holes the longest line might sit on vertex of internal ring.

So, this is more accurate vector solution: 