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