4

enter image description here

I have a grid. I want to create a new line in the middle of two lines. For example, calculate the distance between line 1 and 2 and create a new line (the red line in the image) exactly in the middle and of the same length as the grid lines, also between line 2 and 3 until the end of the grid.

The attribute table contains the number of rows and their type.

enter image description here

1
  • I deleted the pyqgis-tag because no code was provided
    – Taras
    Commented Apr 29, 2021 at 6:16

3 Answers 3

2
  1. Densify by interval to create vertices every n meters
  2. Extract the vertices as points
  3. Voronoi polygons
  4. Polygons to line
  5. Explode lines to split each line into segments
  6. Extract the short segments

enter image description here

2

The principal idea
You can create lines with the following principle: I suppose that all the parallel lines are drawn in the same direction (top-down or down to top). Find the end-point of each line and connect it to the end-point of the following line. Take the middle of this line (centroid). This is the starting point of each middle line.

Now simply project this point to the bottom, using the length of your existing line and its azimuth (angle).

In this case, even with varying distances between the lines, you will get in each case a perfect middle line.

The implementation
You can use QGIS expressions for this. Use the "Geometry Generator" or the "Geometry by Expression", see here for details. In both cases, the expression remains the same. There are some additional elements to get the correct lines: lines should only be created for lines of the type row and not for the last row line:

if ( 
    "type" = 'row' and
    attribute (
        get_feature_by_id (
            @layer,  
            $id+1
        ),
        'type'
    ) = 'row'
    ,
    with_variable (
        'centroid',
        centroid (
            make_line(
                end_point (
                    $geometry
                ), 
                end_point (
                    geometry (
                        get_feature_by_id (
                            @layer,  
                            $id+1
                        )
                    )
                )
            )
        ),
        make_line (
            @centroid,
            project (
                @centroid ,
                -length (
                    $geometry
                ), 
                radians (
                    main_angle($geometry)
                )
            )
        )
    ),
    ''
)

Screenshot: the black dotted lines are created with the expression from above and Geometry Generator, based on the red lines: enter image description here

1

Not sure if it is included in QGIS 3.16 but if the distance between line 1 and 2 is constant you can use offset lines under 'vector geometries'. There you can specify your distance.

First make a selection using an expression akin "TYPE" = 'ROW'. Then compute offset lines for your selection by ticking the box in the processing tool.

1
  • I tried and it works(only if distance between line 1 and 2 is constant), but the distance between the lines can be different in my case. Therefore the distance between the rows must be calculated each time.Thanks for your answer. Commented Nov 23, 2020 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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