I want to calculate the "diameter" of polygons in QGIS.

The "diameter" defined as the distance between the two most distant points of the polygon.

I couldn't find a fitting solution in the field calculator. Maybe you have some ideas?

  • Conceptually thinking: get the smallest possible bounding box (tool), then you should be able to calculate the length of the diagonal using area & perimeter. – Erik May 13 at 13:36

It is possible to do with simple expressions in the field calculator (at least in QGIS 3.12.x). Take for example these two polygons. The symbology shows four things (using the geometry generator, for explanation purposes):

  • Red outline of the true polygon
  • Semi-transparent orange circle resulting from the minimal_circle() function
  • Blue point resulting from the centroid() function of the minimal circle
  • White point resulting from the point_n() function of the minimal circle's first vertex

enter image description here

So to get the diameter of the minimal circle containing the polygon, go to the field calculator and use this expression in a new decimal field:

distance(centroid(minimal_circle($geometry)),point_n(minimal_circle($geometry),1)) * 2

This will calculate the distance between the centroid and the first vertex along the circle (the radius), then multiply it by two.

| improve this answer | |
  • i get this error: Parser Error: syntax error, unexpected '(', expecting COMMA or ')' – Jan-Pieter Van Parys May 13 at 15:31
  • @Jan-PieterVanParys It is definitely working for me, I double checked the expression above. What version of QGIS are you running? Maybe some of the functions were added in a more recent version? – Gabriel C. May 13 at 15:35
  • 1
    @Jan-PieterVanParys There we go. Expressions have received a LOT of love since then. – Gabriel C. May 13 at 15:37
  • 1
    @Jan-PieterVanParys You can go with the current LTR, 3.10.5. It should be quite stable although you will probably need a little bit of adjusting to the new UI and ways of doing things. – Gabriel C. May 13 at 15:41
  • 1
    @Legisey Yes, triangles don't work well. That's something to keep in mind, but I've observed that they aren't very common in the datasets I've had to use. It could be possible to aggregate the distances from the centroid to each vertex then build a minimal circle from the two largest value points. I'll try that out. edit Also I think it was maybe a problem with the question as asked as the diameter (minimal circle) can often be different from the distance between the two most distant vertices. – Gabriel C. May 25 at 12:58

You can use PyQGIS to measure the distances between all vertices of each polygon and find max:

import itertools

layer = iface.activeLayer() #Click layer in tree

for feat in layer.getFeatures():
    verts = [v for v in feat.geometry().vertices()] #List all vertices
    maxdistance = max([p1.distance(p2) for p1,p2 in itertools.combinations(verts, 2)]) #Find max distance for all combinations of vertices (https://stackoverflow.com/questions/464864/how-to-get-all-possible-combinations-of-a-list-s-elements)
    print('Polygon: {0}, max distance: {1}'.format(feat.id(), round(maxdistance,0))) #Print results


To save max distances in a field:

import itertools

layer = iface.activeLayer() #Click layer in tree
field_to_save_maxdistance_in = 'maxdist' #Change maxdist to the name of your field

fields = layer.fields()
fx = fields.indexFromName(field_to_save_maxdistance_in)

with edit(layer):
    for feat in layer.getFeatures():
        verts = [v for v in feat.geometry().convexHull().vertices()] #List all vertices
        maxdistance = max([p1.distance(p2) for p1,p2 in itertools.combinations(verts, 2)]) #Find max distance for all combinations of vertices
        layer.changeAttributeValue(feat.id(), fx, maxdistance)


You can also create a line layer:

import itertools

layer = iface.activeLayer() #Click layer in tree

#Create line layer
vl = QgsVectorLayer("LineString?crs={}&index=yes".format(layer.crs().authid()), "myLayer", "memory")
provider = vl.dataProvider()

#For each polygon find the two points most far apart
for feat in layer.getFeatures():
    all_points = []
    verts = [v for v in feat.geometry().vertices()] #List all vertices
    for p1,p2 in itertools.combinations(verts, 2):

    #Create a line feature
    pointpair_most_far_apart = max(all_points, key=lambda x: x[0].distance(x[1]))
    gLine = QgsGeometry.fromPolyline(pointpair_most_far_apart)
    f = QgsFeature()


enter image description here

| improve this answer | |
  • This seems like exactly what I want to do! Since I am not familiar with Python and pyqgis: is there a way to save the maximum distance that the console is showing me as a column in my layer? So I can continue using it for my following calculations. – gis_student May 13 at 13:55
  • Somehow the newest code doesn't work. I get a "SyntaxError: invalid syntax" error or QGIS just shuts down while trying to run it. The first one you posted without saving it to my layer worked perfectly. – gis_student May 13 at 14:23
  • 1
    Added it as a real type. But now it somehow worked. QGIS shut down again and when I restarted it, the column was there and the results seem correct. Weird indeed! But anyways, thank you! This has helped me a lot already! – gis_student May 13 at 14:34

Let's assume there is a polygon layer "Layer_A" (blue) with its corresponding attribute table accordingly, see image below.


Step 1. Proceed with 'Polygons to lines'


Step 2. Proceed with 'Points along geometry'. Mind that the distance affects the quality of the final result and the efficiency of the Virtual Layer in Step 3.


Step 3. By means of a "Virtual Layer" through Layer > Add Layer > Add/Edit Virtual Layer... apply this query

SELECT p1.id,
       setsrid(make_line(p1.geometry, p2.geometry), 'put your srid here'),
       max(st_length(make_line(p1.geometry, p2.geometry))) AS length
FROM "Points" AS p1
JOIN "Points" AS p2 ON p1.id = p2.id
WHERE NOT st_equals(p1.geometry, p2.geometry)
GROUP BY p1.id


P.S. if you are interested in the longest distances between vertices, than extract vertices from polygon via 'Extract vertices' and go directly to Step 3.


| improve this answer | |
  • I just tried this and unfortunately QGIS can't finish the third step. It runs for a long time already, but never ends. – gis_student May 13 at 15:10
  • How many points did you generate ? How many polygons do you have ? – Taras May 13 at 18:01
  • I have more than 400 polygons and I've got 371939 points. Maybe those are too many? – gis_student May 23 at 11:51
  • yes, you are right – Taras Jun 26 at 9:35

If you have your data in PostGIS you can use these functions:


Returns the smallest circle polygon that can fully contain a geometry.


Returns a record containing the center point and radius of the smallest circle that can fully contain a geometry.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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