Created
April 26, 2023 16:29
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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