2

In QGIS I want to create a series of lines that have an alternating crossline spacing. So for example :

  • Line 2 is 50m away from Line 1
  • Line 3 is 100m away from Line 2
  • Line 4 is 50m away from Line 3
  • Line 5 is 100m away from Line 4
  • etc...

I can't seem to find a QGIS function for this is the basic set of offset tools, can anyone suggest a way ahead?

J. Monticolo
  • 15,695
  • 1
  • 29
  • 64
user225959
  • 23
  • 3

2 Answers2

2

You can simply use the Offset Line tool in QGIS :

offset lines processing tool

and to automate that for many lines you could adapt this working python script (tested and working) to match your specific needs:

EDIT :

offset_distances = [50, 100, 50, 100]  # Specify the offset distances for each line

Initial line

alg_params = { 'DISTANCE': offset_distances[0], 'INPUT': iface.activeLayer(), 'JOIN_STYLE': 0, 'MITER_LIMIT': 2, 'SEGMENTS': 8, 'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT } outputs = processing.runAndLoadResults('native:offsetline', alg_params)

previous_line = outputs['OUTPUT'] # Initialize the previous line with Line1

Generate subsequent lines with alternating offset distances

for i in range(1, len(offset_distances)): alg_params['DISTANCE'] = offset_distances[i] alg_params['INPUT'] = previous_line outputs[f'Line{i+1}'] = processing.runAndLoadResults('native:offsetline', alg_params)['OUTPUT'] previous_line = outputs[f'Line{i+1}'] # Update the previous line with the current line

wanderzen
  • 2,130
  • 6
  • 26
  • Thanks, worked great – user225959 Jun 14 '23 at 10:06
  • Hi, i have now updated to the latest QGIS version 3.32.3 Lima and above code does not now seem to work? Is it an issue with the script or is there something in the latest version of QGIS that makes it not work? – user225959 Sep 28 '23 at 13:31
1

Use the following expression with Geometry generator or Geometry by expression (see here for differences and details about both options). In line 3, define how many lines you want by changing the value of 10 to the desired number of lines.

collect_geometries(
    array_foreach (
        generate_series (0,10),  -- change 10 to the number of lines you want to create
        offset_curve (
            $geometry,
            @element*50 + floor(@element/2)*50
        )
    )
)

Red: initial lines; black: lines created with geometry generator: enter image description here

Babel
  • 71,072
  • 14
  • 78
  • 208