I have been looking around to find a way to calculate the longest line within a polygon.

Determining longest line segment within polygon that passes through its centroid? and Calculating maximum distance within polygon in x-direction (east-west direction) in PostGIS? is what I found on here however these are quite advanced solutions. Also they are pretty old (5 and 6 years).

Is there by now a pre made library or github repository that I can use to do this or is there no other way then to write it myself?

Secondly, if I have to do it myself, could anybody post some kind of pseudo code or plan that I can follow.

I am trying to understand the second link at the moment but i'm having a hard time.

  • In answer to your first question, I don't think the algorithm will have been improved on, so it being a few years old isn't relevant. So, is your question can someone convert this R code to some other (unspecified) language or point me to a github repository of the same or is there another third party tool, such as in whyzar's answer. Commented Sep 26, 2017 at 19:13
  • I understand that the method hasn't changed. The question is, is there an pre-build option right now, such as whyzar's answer. I believe GME is dependent on Arc? I would prefer an answer not being dependent on Arc, but will look into his answer.
    – ArnJac
    Commented Sep 26, 2017 at 19:15
  • You should state this preference for not using anyting ESRI-based as an edit to the question, as there are many potential answers to such a broad question. I have never heard of GME before, though apparently it might mean Geospatial modelling environment. Commented Sep 26, 2017 at 19:17
  • 1
    I think this is too broad for focused Q&A because the first question seeks a software recommendation whereas the second is more like seeking an algorithm. It is only tagged with the latter and the only answer seems to target the former.
    – PolyGeo
    Commented Sep 26, 2017 at 19:38
  • You are right. I will mark the answer as correct and try to make GME work. If necessary I will ask another question with exclusion of Arc.
    – ArnJac
    Commented Sep 26, 2017 at 19:47

1 Answer 1


This option, from GME, the will allow you to:

Determines the longest line that can be position within the bounds of a polygon, crossing no edges

geom.polygonfetch (Calculate Fetch In Polygons)

This tool estimates the longest straight line that can be positioned within a polygon, without crossing any edges. It corresponds to the 'fetch' of a lake: the longest stretch of water over which waves can build up as a function of wind action. The calculation is based on evaluating the lines created by connecting all pairs of non-neighbouring vertices and retaining the longest line that does not cross any interior or exterior boundaries of the polygons. This is a brute force algorithm that can take a long time for very complex polygons (e.g. polygons with more than 1000 vertices).

The 'where' clause can be used to define a subset of polygons to process. See the 'where' section for further details on how to formulate a where clause.

Here is the syntax:

geom.polygonfetch(in, uidfield, out, [where]);

in the input polygon data source
uidfield the unique ID field of the input feature data source
out the output line data source
[where] the selection statement that will be applied to the line feature data source to identify a subset of lines to process (see full Help documentation for further details)

Here is an example input:

geom.polygonfetch(in="C:\data\lakes.shp", out="C:\data\fetch.shp", uidfield="LAKEID");

geom.polygonfetch(in="C:\data\lakes.shp", out="C:\data\fetch.shp", uidfield="LAKEID", where="AREA > 10000");

  • Nice and complete! This seems to be what I need. However it is dependent on Arc right? I'm trying to run it from ubuntu. There might be a workaround but I would prefer a non Arc solution.
    – ArnJac
    Commented Sep 26, 2017 at 19:16
  • Perhaps edit your post, it doesn't say that you are after straight line. If so, mention of centroid is not relevant, because centroid might very well sit outside polygon.
    – FelixIP
    Commented Sep 26, 2017 at 19:56

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