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.