# Creating vector line in the middle of two lines (grid) in QGIS 3.16

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.

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

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

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
),
main_angle(\$geometry)
)
)
)
),
''
)
``````

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

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.

• 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