12

In QGIS I have a layer that contains ellipse-like polygons as in the image. I need to convert them to ellipses. They come from a different data sources. These are all actually regular shapes that originate from ellipses. Therefore, I think, somehow, an ellipse containing all vertices can be formed. But an approximate ellipse also works for me.

enter image description here

I applied "Smooth" tool using different parameter values. But it doesn't give expected ellipses (brown polygons in the next image). And they are not bounding ellipses. If all ellipses had the same size, the "Smooth" + "Buffer" tools combination or "outer (positive) then inner(negative) buffer" could be work.

enter image description here

How can I convert them to proper (bounding) ellipses?

Babel
  • 71,072
  • 14
  • 78
  • 208
Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
  • 1
    Do you want an ellipse that goes through the vertex points of the polygons? In which case five vertex locations are sufficient to create a unique ellipse through them using planar geometry. See "Conic through 5 points" here: https://www.geogebra.org/classic – Spacedman May 02 '21 at 17:48
  • @Spacedman An approximate result works for me. All shapes are actually well shaped like a simplified ellipse which has 16-20-24... vertices. Therefore, somehow an ellipse containing every vertices can be formed. – Kadir Şahbaz May 02 '21 at 18:01
  • @CyrilMikhalchenko This is a good approach. Thank you. However, I think of using Python as a future work. – Kadir Şahbaz May 03 '21 at 11:39
  • 1
    +10, that's a good question and a new geo-tool could come out of it... – Cyril Mikhalchenko May 05 '21 at 18:35
  • @CyrilMikhalchenko Babel's answer almost worked for the polygons in my data. Because they all have regular shapes as in the image. But I wait for another answer fitting irregular polygons, too. Actually, I couldn't understand your solution. – Kadir Şahbaz May 05 '21 at 18:56
  • @CyrilMikhalchenko I've tried different values. It returns almost the same shape as source for small buffer values. When I use big values, negative buffer destroys the geometry. (I use occasionally a translator (google), too. So I may not understand you and I may not express what I mean. That's why I reply late, sorry). Projected (metre). – Kadir Şahbaz May 08 '21 at 20:31
  • Tell me what you don't like about this query: "WITH tbla AS (SELECT st_convexhull(st_boundary(geometry)) geometry FROM polygon_pseudo) SELECT ST_Buffer(ST_Buffer((geometry),-5),5) geometry FROM tbla" or are they eaten up by their width by the negative buffer? – Cyril Mikhalchenko May 08 '21 at 20:46

2 Answers2

18
  1. Apply the "Oriented minimum bounding box" (see documentation)

  2. From these, create ellipses with values from the attribute table calculated in the first step:

    make_ellipse(centroid($geometry), "height"/2, "width"/2 ,"angle")
    

Screenshot: here with segements=200 for a smooth ellipse (blue):

enter image description here

Taras
  • 32,823
  • 4
  • 66
  • 137
Babel
  • 71,072
  • 14
  • 78
  • 208
12

You could try something like:

make_ellipse(
    centroid($geometry),
    bounds_width($geometry)/2,
    bounds_height($geometry)/2,
    main_angle($geometry),
    45
    )

45 is the number of segments, change that to whatever smoothness you wish.

Or alternatively if the main_angle() does not match your needs, try something like:

make_ellipse(
    centroid($geometry),
    bounds_width($geometry)/2,
    bounds_height($geometry)/2,
    main_angle(
        make_line(
            centroid($geometry),
            closest_point(centroid($geometry),$geometry)
            )
        )-90,
    45
    )
Taras
  • 32,823
  • 4
  • 66
  • 137
MrXsquared
  • 34,292
  • 21
  • 67
  • 117