[GIS] MultiLineString to separate individual lines using Python with GDAL/OGR, Fiona, Shapely

fionageoprocessingogrpythonshapely

There are lots of MultiLineString features in a polyline shapefile. The problem is to split these lines into separate individual lines. There is a similar question with PostGIS, but I need to code in Python.

I have been planning to extract the geometry from each MultiLineString feature and create separate features and append a shpfile with those new features. Later, in the second pass, I would delete the MultiLineString object which is no more required as it is now split into its components. It will require reading data from original shpfile and appending the newly created features to a copy of the shpfile. Then the appended (copy) shpfile needs to be rid of the MultiLineString features. It seems cumbersome as I find no way to read and write the shapefile simultaneously.

There should be better ideas to do that. How to split these lines into separate individual lines using Python with OGR, Shapely, Fiona or Pyshp? A snippet of code to give an idea is below.

import fiona
from shapely.geometry import LineString

original_shpfile = fiona.open(path_to_road_network_shapefile)
with fiona.open(path_to_road_network_shapefile, 'a') as copy_shpfile:
    rec = original_shpfile.next()
    while rec:
        try:
            vertices_on_link = rec['geometry']['coordinates']
            LineString(vertices_on_link) #reconstruct the line from vertices
            #if multipart, 
            #has multiple lists within the list, 
            #one for each of the Linestring component. also throws AssertionError

        except AssertionError:
            number_of_parts = len(vertices_on_link) 
            for x in range(0,number_of_parts):
                part = rec
                part['geometry']['coordinates'] = vertices_on_link[x]
                copy_shpfile.write(part)
        rec = original_shpfile.next()

#in the next phase, delete each of the the MultiLineString features from the copy of the shapefile

Best Answer

A MultiLineString is a list of lines:

from shapely.geometry import  MultiLineString, mapping, shape
coords = [((0, 0), (1, 1)), ((-1, 0), (1, 0))]
lines = MultiLineString(coords)
print lines
MULTILINESTRING ((0 0, 1 1), (-1 0, 1 0))
for line in lines:
     print line
LINESTRING (0 0, 1 1)
LINESTRING (-1 0, 1 0)

# convert to GeoJSON format:
print mapping(lines)
{'type': 'MultiLineString', 'coordinates': (((0.0, 0.0), (1.0, 1.0)), ((-1.0, 0.0), (1.0, 0.0)))}
# convert from GeoJSON to shapely
print shape(mapping(lines))
MULTILINESTRING ((0 0, 1 1), (-1 0, 1 0))

It is the same when you read a shapefile with Fiona:

import fiona
from shapely.geometry import shape
with fiona.open(path_to_road_network_shapefile) as copy_shpfile:
    for feature in copy_shpfile:
        geom = feature['geometry'])
        # geom in GeoJSON format -> {'type': 'MultiLineString', 'coordinates': (((0.0, 0.0), (1.0, 1.0)), ((-1.0, 0.0), (1.0, 0.0)))}
           if geom['type'] == 'MultiLineString':
               # convert to shapely geometry
               shapely_geom = shape(geom) # = MULTILINESTRING ((0 0, 1 1), (-1 0, 1 0))
               for lines in shapely_geom:
                   print lines 

New

A shapefile has no MultiLineString schema geometries: a shapefile that indicates LineString in its schema may yield either LineString or MultiLineString features (simple list of LineStrings)

However, it is the same algorithm:

# open the original shapefile
with fiona.open('multiline.shp') as source:
     # create the new shapefile
     with fiona.open('lines.shp','w', driver='ESRI Shapefile',crs=source.crs,schema=source.schema) as ouput:
         # iterate the features of the original shapefile
         for elem in source:
               # iterate the list of geometries in one element (split the MultiLineString)
               for line in shape(elem['geometry']): 
                    # write the line to the new shapefile
                    ouput.write({'geometry':mapping(line),'properties':elem['properties']})