Skip to content

Instantly share code, notes, and snippets.

@yassineAlouini
Created January 19, 2017 09:57
Show Gist options
  • Save yassineAlouini/e8e20402e6b2fd2889042245d0bb7178 to your computer and use it in GitHub Desktop.
Save yassineAlouini/e8e20402e6b2fd2889042245d0bb7178 to your computer and use it in GitHub Desktop.
Load a CSV file with latitude and longitude columns and transform it into a GeoJSON.
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
import json
def df_to_gdf(input_df):
"""
Convert a DataFrame with longitude and latitude columns
to a GeoDataFrame.
"""
df = input_df.copy()
geometry = [Point(xy) for xy in zip(df.longitude, df.latitude)]
return gpd.GeoDataFrame(df, crs=4326, geometry=geometry)
def csv_to_geojson(input_fp, output_fp):
"""
Read a CSV file, transform it into a GeoJSON and save it.
"""
csv_data = pd.read_csv(input_fp,
compression='bz2',
sep=';',
encoding='utf-8')
geojson_data = (csv_data.pipe(df_to_gdf)
.drop(['extra'], axis=1)
.to_json())
with open(output_fp, 'w') as geojson_file:
geojson_file.write(geojson_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment