Skip to content

Instantly share code, notes, and snippets.

@nygeog
Last active November 25, 2023 09:47
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save nygeog/2731427a74ed66ca0e420eaa7bcd0d2b to your computer and use it in GitHub Desktop.
Save nygeog/2731427a74ed66ca0e420eaa7bcd0d2b to your computer and use it in GitHub Desktop.
Read a CSV with Pandas and set as GeoDataFrame with geopandas and save as Shapefile with fiona
import pandas as pd
from geopandas import GeoDataFrame
from shapely.geometry import Point
import fiona
df = pd.read_csv('data.csv')
geometry = [Point(xy) for xy in zip(df.x, df.y)]
crs = {'init': 'epsg:2263'} #http://www.spatialreference.org/ref/epsg/2263/
geo_df = GeoDataFrame(df, crs=crs, geometry=geometry)
geo_df.to_file(driver='ESRI Shapefile', filename='data.shp')
# https://gis.stackexchange.com/questions/204201/geopandas-to-file-saves-geodataframe-without-coordinate-system
# http://geopandas.org/io.html#writing-spatial-data
# https://gis.stackexchange.com/questions/174159/convert-a-pandas-dataframe-to-a-geodataframe
@AliyuAziz
Copy link

Hi, many thanks for this. Please help with the reverse, i.e. convert shape file to csv. Thanks, Aziz

@zhaoyanthu
Copy link

zhaoyanthu commented Mar 24, 2018

To readers: {'init': 'epsg:2263'} is just one of the many options. For GPS points, you should replace line 9 by
crs = "+init=epsg:4326"
or
crs = {'init': 'epsg:4326'}

@TeresaSereno
Copy link

Hi,

How could this be achieved if the variable is a multipoint instead of a point?
Many thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment