Last active
May 25, 2020 17:15
-
-
Save tcramm0nd/abd00a3d2c303b9249e1139041a749aa to your computer and use it in GitHub Desktop.
How to import an SHP file into a Geopandas DataFrame
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
# A breif overview of how to how to import an SHP file to a Geopandas DataFrame. For a more detailed breakdown of the | |
#process you can find the original post here: http://timcrammond.com/blog/creating-a-geopandas-dataframe-from-a-shp-file | |
import shapefile | |
import geopandas as gpd | |
from shapely.geometry import shape | |
import osr | |
tracts = shapefile.Reader('data/cb_2018_42_tract_500k.shp') | |
fields = [field[0] for field in tracts.fields[1:]] | |
attributes = [] | |
geometry = [] | |
for row in tracts.shapeRecords(): | |
geometry.append(shape(row.shape.__geo_interface__)) | |
attributes.append(dict(zip(fields, row.record))) | |
with open('data/cb_2018_42_tract_500k.prj') as p: | |
proj4 = osr.SpatialReference(p.read()).ExportToProj4() | |
print(proj4) | |
gdf = gpd.GeoDataFrame(data=attributes, geometry=geometry, crs=proj4) | |
gdf.head() | |
gdf.plot(figsize=(16,8), cmap='Blues', edgecolor='black', linewidth=0.4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment