[GIS] Creating shapefile file with Fiona and GeoPandas

fionageopandaspandaspython

I am trying to create a shapefile and a geojson using geopandas and fiona.

import fiona
import fiona.crs

geometry = [Point(xy) for xy in zip(df['longitude'],df['latitude'])]
crs = {'init': 'epsg:4326'}
gdf = GeoDataFrame(df, crs=crs, geometry=geometry)

some more stuffs and finally:

gdf.to_file('localization/shapefiles/localizaciones.shp', driver='ESRI Shapefile')

but the .prj file is in blank. Then the projection is wrong.

I am writing the content of the the .prj file after the file is created in blank, but the crs problems still in progress either in geojson file or gpx files. No crs is created

Best Answer

Found two solutions to have coordinate reference system defined in the output shapefile: Proj4 style mappings and WKT strings (source: Fiona). The OGC WKT strings can be found on spatialreference.

import geopandas as gpd
from shapely.geometry import Point

lat_point_list = [50.854457, 52.518172, 50.072651, 48.853033, 50.854457]
lon_point_list = [4.377184, 13.407759, 14.435935, 2.349553, 4.377184]

geometry = [Point(xy) for xy in zip(lon_point_list, lat_point_list)]
#crs1 = {'init': 'epsg:4326'}         # Does NOT work
crs2 = {'proj': 'longlat', 'ellps': 'WGS84', 'datum': 'WGS84', 'no_defs': True}     # Works
#crs3 = 'EPSG:4326'       # Does NOT work
crs4 = 'GEOGCS["WGS 84",DATUM["WGS_1984",' \
       'SPHEROID["WGS 84",6378137,298.257223563,' \
       'AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],' \
       'PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],' \
       'UNIT["degree",0.01745329251994328,' \
       'AUTHORITY["EPSG","9122"]],' \
       'AUTHORITY["EPSG","4326"]]'     # Works

points = gpd.GeoDataFrame(index=[0, 1, 2, 3, 4], crs=crs4, geometry=geometry)
points.to_file(filename='points.shp', driver="ESRI Shapefile")
Related Question