I have a column ('geo_shape') that contains GeoJSON object type of linestring geometry and, so far, I didn't manage to read and transform it to a GeoPandas/shapefile type of geometry.
Here is a way to do it with QGIS Python module: Reading GeoJSON-String from CSV as Geometry in QGIS
I have tried this method also : CSV containing GeoJSON format geometries to DataFrame
All I get is this JSONDecodeError: Extra data: line 1 column 12 (char 11) Which I didn't manage to solve yet.
Here how the data look like (it's a type 'object' not a proper dictionary):
{"coordinates": [[5.807033273966051, 43.19284592374117], [5.807314867862657, 43.192757356667016]], "type": "LineString"}
When trying this method: CSV containing GeoJSON format geometries to DataFrame
import ast
def parse_geom(geom_str):
try:
return shape(json.loads(geom_str))
except (TypeError, AttributeError): # Handle NaN and empty strings
return None
df["geo_shape"] = df["geo_shape"].apply(parse_geom)
gdf = gpd.GeoDataFrame(df, geometry="geo_shape")
gdf.head()
All I get is a None value :
I also tried this method: https://stackoverflow.com/questions/68820085/how-to-convert-geojson-to-shapely-polygon
geo: dict = df["geo_shape"] linestring: LineString = shape(geo)
And this too: How can I use GeoPandas to read a string containing GeoJSON content into a GeoDataFrame?
gdf = gpd.read_file(df["geo_shape"], driver='GeoJSON') gdf.head()
The result is this: TypeError: expected string or bytes-like object
Does anyone has an idea how to deal with it with GeoPandas and GeoJSON?