Skip to main content
added 46 characters in body
Source Link
gene
  • 55.4k
  • 3
  • 113
  • 191

To automate a process outside of QGIS why don't use pure Python geospatial modules instead of working with PyQGIS ? It is easier and faster if you want to work in the Python shell.

This task is very easy to do with Fiona and Shapely, for example.

Original polylines

enter image description here

from shapely.geometry import Point, LineString, mapping
import fiona
def pair(list):
    '''Iterate over pairs in a list '''
    for i in range(1, len(list)):
         yield list[i-1], list[i]
        
with fiona.open('original_lines.shp') as input:
    # get the crs, driver, and schema 
    meta = input.meta
    # explode lines
    with fiona.open('ligne_splited'lines_splitted.shp', 'w',**meta) as output:
        for line in input:
        # use  pair function to extract the line segments
        for seg_start, seg_end in pair(line['geometry']['coordinates']):
            segment = LineString([Point(seg_start),Point(seg_end)])
            output.write({'geometry':mapping(segment),'properties':line['properties']})

Result

enter image description here

To automate a process outside of QGIS why don't use pure Python geospatial modules instead of working with PyQGIS ? It is easier and faster

This task is very easy to do with Fiona and Shapely, for example.

Original polylines

enter image description here

from shapely.geometry import Point, LineString, mapping
import fiona
def pair(list):
    '''Iterate over pairs in a list '''
    for i in range(1, len(list)):
         yield list[i-1], list[i]
        
with fiona.open('original_lines.shp') as input:
    # get the crs, driver, and schema 
    meta = input.meta
    with fiona.open('ligne_splited.shp', 'w',**meta) as output:
        for line in input:
        # use  pair function to extract the line segments
        for seg_start, seg_end in pair(line['geometry']['coordinates']):
            segment = LineString([Point(seg_start),Point(seg_end)])
            output.write({'geometry':mapping(segment),'properties':line['properties']})

Result

enter image description here

To automate a process outside of QGIS why don't use pure Python geospatial modules instead of working with PyQGIS ? It is easier and faster if you want to work in the Python shell.

This task is very easy to do with Fiona and Shapely, for example.

Original polylines

enter image description here

from shapely.geometry import Point, LineString, mapping
import fiona
def pair(list):
    '''Iterate over pairs in a list '''
    for i in range(1, len(list)):
         yield list[i-1], list[i]
        
with fiona.open('original_lines.shp') as input:
    # get the crs, driver, and schema 
    meta = input.meta
    # explode lines
    with fiona.open('lines_splitted.shp', 'w',**meta) as output:
        for line in input:
        # use  pair function to extract the line segments
        for seg_start, seg_end in pair(line['geometry']['coordinates']):
            segment = LineString([Point(seg_start),Point(seg_end)])
            output.write({'geometry':mapping(segment),'properties':line['properties']})

Result

enter image description here

Source Link
gene
  • 55.4k
  • 3
  • 113
  • 191

To automate a process outside of QGIS why don't use pure Python geospatial modules instead of working with PyQGIS ? It is easier and faster

This task is very easy to do with Fiona and Shapely, for example.

Original polylines

enter image description here

from shapely.geometry import Point, LineString, mapping
import fiona
def pair(list):
    '''Iterate over pairs in a list '''
    for i in range(1, len(list)):
         yield list[i-1], list[i]
        
with fiona.open('original_lines.shp') as input:
    # get the crs, driver, and schema 
    meta = input.meta
    with fiona.open('ligne_splited.shp', 'w',**meta) as output:
        for line in input:
        # use  pair function to extract the line segments
        for seg_start, seg_end in pair(line['geometry']['coordinates']):
            segment = LineString([Point(seg_start),Point(seg_end)])
            output.write({'geometry':mapping(segment),'properties':line['properties']})

Result

enter image description here