0

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 :

enter image description here

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)

enter image description here

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?

6
  • 1
    What code did you try?
    – nmtoken
    Aug 23 at 9:48
  • @user2856 Already tried that one, and it's not working for me. I will retry it but I am kinda of new in geopandas, dataframe and so I am still struggeling with finding solution to my problems :)
    – SKSLH
    Aug 23 at 12:16
  • 1
    Then to avoid your question getting closed, you should add your attempt and any errors/incorrect results to your question.
    – user2856
    Aug 23 at 12:38
  • 1
    Q/A on GIS SE site are not intended only to solve problem of particular user, but be helpful resource for anybody with similar questions/problems. That's the reason question has to be complete in itself and include all relevant code as text, since outside links tend to get lost in time and also that there is no need to visit other sites to get complete picture of what the problem/question is. Please edit your question and add relevant existing code.
    – TomazicM
    Aug 23 at 15:28

1 Answer 1

0
from shapely import Point
import json

def make_point(json_feature):
    dict_json = json.loads(json_feature)
    pnt = Point(dict_json['coordinates'][0][0],
                dict_json['coordinates'][0][1])

   return pnt

infra_gaz['new_pnt_geom'] = infra_gaz['geo_shape'].apply(
                   make_point, axis=1)

Can make shapely point with custom function and use apply to create new point column.

4
  • Thank you for answering but actually I am trying to create a LineString geometry with the "geo_shape" column. Is there a way to deal with that the way you propose to deal with the problem?
    – SKSLH
    Aug 28 at 6:51
  • Yes instead of point would use LineString. The array like data would work so would want LineString(dict_json['coordinates'])
    – MorrisAJ33
    Aug 28 at 13:04
  • Ok thank you very much, I'll try and keep you updated!
    – SKSLH
    Aug 28 at 13:31
  • Well it didn't quite work, in fact I have a nest dictionnary that is a type object... I will try to come up a solution, but so far I just dowloaded the same file geojson to deal with it.
    – SKSLH
    Sep 1 at 7:21

Your Answer

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

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