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

  • 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.
    – FelixIP
    Aug 8, 2021 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.
    – Vince
    Aug 8, 2021 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
    – Anand
    Aug 8, 2021 at 3:50
  • This is very different to what I have in mind anyway.
    – FelixIP
    Aug 8, 2021 at 5:31
  • 4
    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, 2021 at 8:31

2 Answers 2


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

  • 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)
    – Anand
    Aug 8, 2021 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.
    – Kazuhito
    Aug 8, 2021 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.
    – Vince
    Aug 8, 2021 at 15:43
  • 1
    @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?
    – Kazuhito
    Aug 10, 2021 at 4:49
  • 1
    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
    – Anand
    Aug 10, 2021 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:

enter image description here

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:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.