5

I downloaded a CSV that contains a column which has a GeoJSON format and imported it as Pandas DataFrame. How can I convert this to a GeoJSON .geojson? I have about 10,000 rows, each with information as shown below:

This is an example of a cell in the column:

{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-0.0903517,9.488375],[-0.0905786,9.488523],[-0.0909767,9.48913],[-0.09122,9.4895258],[-0.0909733,9.4901503],[-0.0908833,9.4906802],[-0.0906984,9.4905612],[-0.0907146,9.4898184],[-0.090649,9.4895175],[-0.0907516,9.489142],[-0.0906146,9.4889654],[-0.0903517,9.488375]]]},"properties":{"pointCount":"11","length":"502.9413","area":"8043.091133117676"}}]}
Overview of my Pandas DataFrame print now: site_registration_gps_area ... geometry
11 {"type":"FeatureCollection","features":[{"type... ... POINT (-76.75880 2.38031)
14 {"type":"FeatureCollection","features":[{"type... ... POINT (-76.73718 2.33163)
40 {"type":"FeatureCollection","features":[{"type... ... POINT (-0.15727 9.69560)
42 {"type":"FeatureCollection","features":[{"type... ... POINT (-0.11686 9.65522)
44 {"type":"FeatureCollection","features":[{"type... ... POINT (-0.10379 9.65226)
Taras
35.8k5 gold badges77 silver badges152 bronze badges
asked Jan 31, 2023 at 14:30
1

1 Answer 1

7

You can use the following code. Just specify json_string_column and output file path.

import json
import pandas as pd
import geopandas as gpd
# df = YOUR DATAFRAME
json_string_column = "site_registration_gps_area" # in df
geojson_out_file = "c:/PATH/TO/file.geojson"
df = df[json_string_column].apply(lambda x: json.loads(x)['features'][0])
gdf = gpd.GeoDataFrame.from_features(df)
with open(geojson_out_file, 'w') as f:
 f.write(gdf.to_json())

The script exports JSON string data contained in json_string_column. If your Pandas DataFrame has other columns, it ignores them.

Taras
35.8k5 gold badges77 silver badges152 bronze badges
answered Jan 31, 2023 at 16:51
5
  • Great! This is really helpful; I'm almost there. The line "df = df[json_string_column].apply(lambda x: json.loads(x)['features'][0])" works, but the next line "gdf = gpd.GeoDataFrame.from_features(df)" works for the first 10 rows (df.head(10), but if I do the whole 'df' it gives this error: File "/Users/opt/anaconda3/envs/ak_ma/lib/python3.10/site-packages/geopandas/geodataframe.py", line 638, in from_features properties = feature["properties"] KeyError: 'properties' What to do here? Commented Jan 31, 2023 at 19:42
  • This is how one row in df looks like: {'type': 'Feature', 'geometry': {'type': 'Polygon', 'coordinates': [[[-51.495698, -25.021174], [-51.495605, -25.021202], [-51.495645, -25.021217], [-51.49566, -25.021277], [-51.495802, -25.021295], [-51.495915, -25.021306], [-51.495983, -25.021323], [-51.496124, -25.021339], [-51.496124, -25.021246], [-51.496052, -25.021259], [-51.496006, -25.021258], [-51.495965, -25.021234], [-51.495833, -25.021221], [-51.495772, -25.021166], [-51.495738, -25.021161], [-51.495716, -25.021171], [-51.495698, -25.021174]]]}} Commented Jan 31, 2023 at 19:45
  • 2
    Hmm. Some features don't have properties. This is the reason of the error. Commented Jan 31, 2023 at 20:24
  • I'm also getting this error: JSONDecodeError: Expecting ',' delimiter. I want to add the r' command to fix this, but how do you do this with a variable? json.loads(r'x) doesn't work.. What does? Commented Jan 31, 2023 at 20:42
  • I couldn't add additional solution as sample json doesn't reflect all data. You may encounter another similar error in every answer I add. Commented Feb 5, 2023 at 11:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.