Last active
May 14, 2017 18:36
-
-
Save paulopperman/18e44aa73b8a3a0f50558a79bf3bee06 to your computer and use it in GitHub Desktop.
workaround for importing shapefile into geopandas
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 shapefile | |
import geopandas as gp | |
def convert_shapefile(filepath, crs=None): | |
# Function to read an ESRI shapefile and return a geopandas GeoDataFrame | |
reader = shapefile.Reader(filepath) | |
# Extract the column names for the data | |
fields = reader.fields[1:] | |
field_names = [field[0] for field in fields] | |
buffer = [] | |
for sr in reader.shapeRecords(): | |
atr = dict(zip(field_names, sr.record)) | |
geom = sr.shape.__geo_interface__ | |
buffer.append(dict(type="Feature", geometry=geom, properties=atr)) | |
frame = gp.GeoDataFrame.from_features(buffer, crs=crs) | |
return frame |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment