0

How do I create oriented centerlines in QGIS as in the following image:

enter image description here

Input data is a shapefile of irregular polygons as in the image below and available on the following link:https://drive.google.com/drive/folders/1fAS3BDDis6z154L0nJVelWJoKzU04lFN?usp=sharing

enter image description here

Figure 1: Shapefile of irregular polygons

Secondly, Minimum bounding box tool was used to create a bounding box ('perfect rectangles') around each polygon as in the image below and available on the following link:https://drive.google.com/drive/folders/1Tr3umzfsE2dBVna9J1tYE89eoe3Vc0gR?usp=sharing.

QGIS > Processing Toolbox > Vector Geometry > Minimum bounding box

enter image description here

I need to create oriented centerlines to produce rows of lines as in the example image below:

enter image description here

6
  • 3
    Where the orientation should come from? – Babel Jan 10 at 12:41
  • It is not clear how you need to generate oriented centerlines. It should be simply diagonal instead of horizontal under certain conditions? – Nil Jan 10 at 13:12
  • The global orientation of centerlines of all vectors present in the tile must be calculated first. – HansrajR Jan 10 at 13:20
  • So the oriented centerlines all have the same azimuth - thus: they all lie on the same line? Calculation of global orientation is not part of this question, you have already solved this, haven't you? In any case, due to the focused question and answer form here on GIS SE, please post another question for this, if not yet solved. – Babel Jan 10 at 13:30
  • What can you say about the properties of your shapes: Will the line always pass through the centroid of the shapes? Are they perfect rectangles? – Babel Jan 10 at 13:34
5

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:

  1. Get centroid: centroid ($geometry)
  2. 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.
  3. Generate this second point starting from the centroid (step 1) and project (shift) this point using project(point,distance,azimuth). 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 radians(value) where value is the number in degrees).

See at the bottom how to get the angle for your polygons.

  1. 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 extend(geometry,start_distance,end_distance). For 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.
  2. 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 ($geometry).

Put all together, the expression looks like:

intersection (
    extend (
        make_line (
            centroid ($geometry), 
            project (
                centroid ($geometry), 
                25000, 
                4.443010855970499
            )
        ),
        25000,
        0
    ),
    $geometry
)

enter image description here

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.

enter image description here

And this is how it looks like if you zoom in:

enter image description here

12
  • The result is not as expected with multiple rows of these 'perfect rectangles' – HansrajR Jan 10 at 15:11
  • Can it be applied to achieve a row of lines @babel – HansrajR Jan 10 at 15:29
  • The question was edited to provide more information and sample data. I hope it provides a better insight. @babel – HansrajR Jan 10 at 16:12
  • The angle parameter is user defined.Is there a method to determine the average angle of all the lines in the layer. Breaking down the layer into further will increase the accuracy. – HansrajR Jan 10 at 17:45
  • I meant you used radians(29.974) by selecting one rectangle from the layer. Is there a method to calculate the average orientation of all rectangles in the layer to use as input angle parameter to create the lines. – HansrajR Jan 10 at 17:59

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.