4

I noticed that QGIS tool multipart to single parts produces different results than geopandas explode when used to MultiLineString geometries.

Having a multiple lines which are randomly split along their geometries can be dissolved to one using the dissolve tool that works the same in QGIS and GPD. Using GPD.explode() will produce geometrically the same result as before dissolving. QGIS will instead keep some lines continous without splitting them, if they are "logically" better than way (for example one long line that before was split to multiple smaller parts will be a one line after QGIS tool). But geopandas will split it again into multiple small parts.

I need to emulate the behaviour of QGIS tool in geopandas. Is that possible? What is the difference in those two tools?

3 Answers 3

3

There is an explode tool in QGIS, too. Explode means creating a separate line (feature) for each segment - thus each line consists of just two vertices: start- and endpoint.

Single parts, however, can consists of many vertices. Only limitation is that each feature consists of only one linestring, thus the line might not fork or not consist of two or more separate lines. Imagine it like a pen on a sheet of paper: what you draw in a single line, without removing the pen from the paper and not going back on a line you already drew.

A mulipart, however, can consist of as many independent lines as you wish, being topologically connected (like forking) or completely disjoint. Imagine it like a whole road- or river network integrated in one single feature.

Consider the following image:

  • There is one multipart line, symbolized in black: one single feature consisting of all the lines you see on the image (1 feature: no. 1 to 31)
  • If you convert this multipart to single parts, you get three parts: the red (no. 1 to 21), yellow (no. 22 to 26) and blue (no. 27 to 31) lines are each separate lines, thus 3 features in total.
  • If you explode these lines, you get 31 parts: the ones labeled from 1 to 31. So each segment (connection from one vertex to the next vertex) is a separate feature. If you have n vertices, you get n-1 features (parts).

enter image description here

3
  • 1
    Time to learn GeoPandas @_@
    – Taras
    Commented May 4, 2022 at 14:21
  • Thanks, so how to recreate the behavior of multipart to singlepart in GeoPandas? Is there no way to do that?
    – XDOLK009
    Commented May 5, 2022 at 7:35
  • As you in fact asked two questions in one, due to the policy of this site, you should ask this as a separate question.
    – Babel
    Commented May 5, 2022 at 7:42
2

I would say the thing is, dissolve and explode is making the line by vertices, I would merge the line with line merge and then do the explode

ops.linemerge(multi_line_geometry)

So something like this:

import geopandas as gpd
from shapely import ops

df = gpd.read_file(file)
df.geometry = df.geometry.map(lambda x: ops.linemerge(x))
df = df.geometry.explode()

This will be very close to what you need, I hope. You might have to do intersect of the line to split them at contacting vertices.

Explode in this case is splitting multiline to single linestring and since we merged it together it should split it correctly.

You might also be interested in: https://gis.stackexchange.com/a/372091/111567

Just beware, unary union will strip attributes, so if you need to keep them, separate the dataframe into multiple ones by attributes needed for dissolve.

0

If I recall correctly from a test I did a few months back, using dissolve on linestrings in geopandas doesn't really merge the lines to a single linestring, but simply creates a multilinestring of them. E.g. it creates "MULTILINESTRING((0 2, 0 10), (0 10, 5 10))", not "LINESTRING (0 2, 0 10, 5 10)"

This is the reason that if you apply explode() afterwards, you get back to the initial situation. So the difference isn't in explode() versus multi_to_single(), it is in how dissolve handles things.

If, after you do the dissolve, you explicitly merge the multilinestrings to linestrings you should get the result you are looking for. I didn't test it, but I think something like this should do the trick:

import geopandas as gpd
import pygeos

df.geometry = gpd.GeoSeries(pygeos.line_merge(df.geometry.array.data))
1
  • Maybe this approach of getting rid of MultiLineString is correct, atleast it sounds reasonable. Unfortunately the output result after explode() is still pretty much the same.
    – XDOLK009
    Commented Jul 11, 2022 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.