You can dynamically create a centerline (defined here: a line passing through the centroid of the polygon shape) using QGIS expressions. You might use this expression for visualization purpose only, than create a new symbol layer where you paste it: this is what is shown on the screenshot below. The other option is to create actual geometries, using
Menu Processing / Toolbox / Geometry by expression, pasting again the same expression.
You find the whole expression at the bottom, here is a step by step instruction how to build it:
- Get centroid:
- Create a line that starts here with
make_line(). The expression expects at least two arguments, both being points. The first one is the centroid from step 1. The second point will be created in step 3.
- Generate this second point starting from the centroid (step 1) and project (shift) this point using
point is again the centroid from step 1,
distance is an arbitrary value: be sure to select it high enough that every line overlaps the boundary of the polygon. In my example, it's
25000, change it accordingly.
azimuth is the azimuth (angle) of the line. I suppose you already have this value. In my case, it's
4.443010855970499 (value must be in radians, if your value is in degrees, convert it with
value is the number in degrees).
See at the bottom how to get the angle for your polygons.
- Now you have a line starting at the centroid and going in one direction just over the boundary of the polygon. You want the line to go in the oteher direction as well, thus use
geometry, use the line we created before. For start_distance, use the same distance as above (step 3) and set
end_distance to 0: we only want to extend the line in one direction.
- You now have the line, but want to clip it to the polygon boundary. For this, use
intersection. You want to intersect the line created in steps 1 to 4 with each polygon (
Put all together, the expression looks like:
Update: how to create the mean angle
You should get the right angle for your use case. You can use the expression
main_angle($geometry) on your polygons to get the orientation of every polygon.
To calculate the average orientation of all rectangles in the layer, create a new field with the field calculator and introduce this expression:
main_angle($geometry). Than use
Menu Vector / Analysis Tools / Basic statistics for fields with your polygon layer as input and set
Field to calculate statistics on to the main_angle field created before (see screenshot). You get a mean angle of (rounded) 29.64 degrees = 0.5173 radians.
And this is how it looks like if you zoom in: