8

enter image description here Is it possible to get the direction of the longest side of a rectangle, in Qgis?

Is it possible to get the direction of the longest side of a rectangle in QGIS?

I know how to get the direction of a line layer. But I need to know the direction of a polygon. One could start at the center of a polygon and find the shortest way to the border by using a line and take the direction of this line, but I don't know how to draw this line automatically for 30000 polygons.

4
  • 1
    Do you have any experience programming in Python with the QGIS API? That might be the way to do it. Loop over the geometry of each feature, find the longest line, get its direction - all doable from Python.
    – Spacedman
    Mar 28, 2017 at 15:35
  • @spacedman ... No I don't have any experience with programming Python. Do you have?
    – Ma Fo
    Mar 28, 2017 at 16:00
  • You want the longest side of rectangle? A center point may be equidistant to 2+ edges (think of a square). I can make suggestions but it depends on the goal. Can you clarify the end goal? Mar 28, 2017 at 16:40
  • How does this differ from gis.stackexchange.com/questions/233771/…
    – underdark
    Mar 29, 2017 at 16:55

3 Answers 3

5

If you run the "Oriented Minimum Bounding Box" algorithm over your layer, you'll get a new angle field in your layer which indicates the angle of this minimum bounding box. I suspect that's what you require.

5

In QGIS 3.16+ there is a new function: main_angle() that makes things easier.

3

I have a way which might work just using QGIS vector operations and expressions. It finds the angle of the longest segment of polygon features.

In outline:

  • Convert the N polygon features to N linear features
  • Explode the N linear features to M simple line segments
  • The M simple line segments have an ID variable from the polygons. Select line segments with length not equal to the maximum length grouped by that ID and delete. That leaves just the longest simple line segment from each polygon.
  • Add an angle attribute to the remaining simple line segments using the atan2 function.
  • Join the polygon layer to the line segment layer on the ID attribute.

Now your polygon has a joined "angle" attribute which is the angle of the longest side (in radians).

That's the algorithm. The implementation follows:

You'll need "Polygons to lines" from the QGIS toolbox and then "Explode lines" from the toolbox for the first two steps.

To get the longest simple line segments grouped by "id" variable, do "Select by expression" and use: $length != maximum($length, "id") - your id variable might have another name, it needs to be unique per polygon. Apply that and you should have most of the simple line segments selected. Turn on editing for that layer and cut those features.

Create the angle column by adding a new attribute called "angle", and updating it with the following formula:

atan2(y_max($geometry) - y_min($geometry),x_max($geometry)-x_min($geometry))

Then switch editing off for that exploded layer and save.

Finally, use the polygon layer properties to create a Join from the polygons to the line segments, matching on the id attribute.

Look now at the attribute table of your polygons, and it should have the angle of the longest line segment from the joined line segment layer.

Note this is not dynamic so if you change the polygons it will not update properly, you'll have to start again. I suspect this workflow could be wrapped into a flow diagram....

0

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.