Skip to content

Instantly share code, notes, and snippets.

@Zeitsperre
Forked from aerispaha/read_shapefile.py
Created August 9, 2021 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zeitsperre/484dbe065c3445d48574400f062dea41 to your computer and use it in GitHub Desktop.
Save Zeitsperre/484dbe065c3445d48574400f062dea41 to your computer and use it in GitHub Desktop.
Read a shapefile into a Pandas dataframe
def read_shapefile(shp_path):
"""
Read a shapefile into a Pandas dataframe with a 'coords' column holding
the geometry information. This uses the pyshp package
"""
import shapefile
#read file, parse out the records and shapes
sf = shapefile.Reader(shp_path)
fields = [x[0] for x in sf.fields][1:]
records = sf.records()
shps = [s.points for s in sf.shapes()]
#write into a dataframe
df = pd.DataFrame(columns=fields, data=records)
df = df.assign(coords=shps)
return df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment