1

I have generated GeoPandas dataframes with several LineStrings like the one below

   id                                           geometry
0    2  LINESTRING (499944.093 7655354.222, 499943.989...
1    3  LINESTRING (499931.260 7655335.450, 499929.092...
2    4  LINESTRING (499891.617 7655361.402, 499889.306...
3    5  LINESTRING (499853.654 7655395.586, 499851.955...
4    6  LINESTRING (499817.456 7655442.479, 499817.444...
5    7  LINESTRING (499782.535 7655482.898, 499781.033...
6    8  LINESTRING (499749.455 7655508.540, 499747.330...
7    9  LINESTRING (499717.076 7655527.056, 499713.627...

Then I wanted to clip these linestrings using a polygon and, for that, I applied the following code

import geopandas as gpd
from shapely.geometry import Point, LineString, shape, GeometryCollection, Polygon
gp_df_clip = gpd.clip(gp_df, polygon)

where gp_df is the dataframe

It works most of the time, but in some cases, the clipped geopandas dataframe replaces the LineString with a MultiLinestring or a collection (point and Linestring). like below:

    id                                           geometry
0    2  MULTILINESTRING ((499944.093 7655354.222, 4999...
1    3  LINESTRING (499931.260 7655335.450, 499929.092...
2    4  MULTILINESTRING ((499891.617 7655361.402, 4998...
3    5  MULTILINESTRING ((499853.654 7655395.586, 4998...
4    6  LINESTRING (499817.456 7655442.479, 499817.444...
5    7  LINESTRING (499782.535 7655482.898, 499781.033...
6    8  LINESTRING (499749.455 7655508.540, 499747.330...
7    9  LINESTRING (499717.076 7655527.056, 499713.627...

I have attempted to add keep_geom_type=True without success

gp_df_clip = gpd.clip(gp_df, polygon, keep_geom_type=True)

The original linestrings cross the polygon twice, and when only one point remains, I eliminate the point because I want segments. I have been editing the clipped dataframe with some success a posteriori. However, I would like these MultiLinestrings or Collections not to be generated in the first place. I wonder if somebody had a similar issue?

Below there is a screenshot of the end result that I obtained if I save the "problematic" dataframe as shapefile. The polygon used for clipping and the linestrings within it. The original dataframes only contains linestrings. Again, it is only after clipping that the phenomenom occurs. I clip to eliminate pieces of the linestring outside of the polygon. Apparently it does not affect the map I generate. The problem is that I manipulate these dataframes after and I need dataframes composed only with linestrings.

enter image description here

0
1

As per the geopandas documentation for keep_geom_type:

If True, return only geometries of original type in case of intersection resulting in multiple geometry types or GeometryCollections. If False, return all resulting geometries (potentially mixed-types).

Therefore, this will not prevent you from getting MultiLineStrings, because they are collections of the original geometry type.

I'm not sure whether preventing the clip method from returning MultiLineStrings is possible. Alternatively, you could explode the collections like this:

gp_df_clip.explode()

which results in the following:

        geometry
0   0   LINESTRING (933710.819 5909056.986, 933655.494...
1   LINESTRING (933325.905 5936518.992, 933317.750...
2   LINESTRING (933325.905 5936518.992, 933312.296...
3   LINESTRING (923959.501 5937164.004, 923952.108...
4   LINESTRING (902824.286 5901442.262, 902774.910...
...     ...     ...
440     1   LINESTRING (933325.905 5936518.992, 933317.750...
2   LINESTRING (933325.905 5936518.992, 933312.296...
3   LINESTRING (1064366.496 5937164.004, 1064398.6...
4   LINESTRING (1065773.655 5937164.004, 1065804.8...
5   LINESTRING (1077961.343 5937164.004, 1078017.8...
2
  • That didn't really work. In addition to the MultiLineStrings, I am still getting: 20 22 GEOMETRYCOLLECTION (LINESTRING (499601.015 765... Jul 7 at 21:04
  • Could you include a minimal, reproducible example with a coordinate sample in your question so I can reproduce your output?
    – Christian
    Jul 9 at 6:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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