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.
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).
(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.
(4) Check your result
Step (3) will return a new layer (usually Matching features). Check the line visually, and its length.
If you can use PostGIS, try using the ST_LongestLine() function like this:
SELECT ST_LongestLine(geom, geom)
FROM mypolytable;
This will find the longest line that can be drawn within each polygon geometry. Note that I'm using this function that references the same geometry column geom twice.
"Sum line lengths" calculates length in the units of the input layer's CRS. So if the input layer is in WGS84, the results will be in degrees. If the input layer is in some UTM projection, it will be meters or feet.
NOTE: Reprojecting on-the file does NOT change the results of length calculations. You have to reproject the layers permanently using "Save as ..." and use the newly generated files.
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 theProcessing 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 theProcessing ToolBox - Vector overlay
.(3) Select the longest one
Use
Select by Expression
tool in theProcessing 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.
(4) Check your result
Step (3) will return a new layer (usually
Matching features
). Check the line visually, and its length.