Skip to content

Instantly share code, notes, and snippets.

@jamesdamillington
Created April 26, 2023 16:29
Show Gist options
  • Save jamesdamillington/576c86b37291c8827de6244cb074ed45 to your computer and use it in GitHub Desktop.
Save jamesdamillington/576c86b37291c8827de6244cb074ed45 to your computer and use it in GitHub Desktop.
Convert a list of coordinates (e.g. lat, long) with an id in a csv to a shapefile and GeoPandas gdf
#import rquired packages
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
#read data and check column names (for coords)
df = pd.read_csv("data/StationLocationIGS.csv")
df.columns
#create geometry column from lat and long using shapely
df['geometry'] = df.apply(lambda row: Point(row.lng, row.lat), axis=1)
#convert pandas df to geopandas gdf (and check)
gdf = gpd.GeoDataFrame(df)
gdf.head()
#write to file
gdf.to_file("data/stations.shp")
#read shp back in to a gdf to check
check = gpd.read_file("data/stations.shp")
check.plot()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment