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)
-
Please, do not forget about "What should I do when someone answers my question?"Taras– Taras ♦2023年02月04日 09:21:16 +00:00Commented Feb 4, 2023 at 9:21
1 Answer 1
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.
-
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?CrossLord– CrossLord2023年01月31日 19:42:05 +00:00Commented 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]]]}}CrossLord– CrossLord2023年01月31日 19:45:11 +00:00Commented Jan 31, 2023 at 19:45
-
2Hmm. Some features don't have
properties
. This is the reason of the error.Kadir Şahbaz– Kadir Şahbaz2023年01月31日 20:24:32 +00:00Commented 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?CrossLord– CrossLord2023年01月31日 20:42:09 +00:00Commented 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.Kadir Şahbaz– Kadir Şahbaz2023年02月05日 11:06:24 +00:00Commented Feb 5, 2023 at 11:06
Explore related questions
See similar questions with these tags.