geopandas.GeoDataFrame.to_file#

GeoDataFrame.to_file(filename, driver=None, schema=None, index=None, **kwargs)[source]#

Write the GeoDataFrame to a file.

By default, an ESRI shapefile is written, but any OGR data source supported by Fiona can be written. A dictionary of supported OGR providers is available via:

>>> import fiona
>>> fiona.supported_drivers  
Parameters:
filenamestring

File path or file handle to write to. The path may specify a GDAL VSI scheme.

driverstring, default None

The OGR format driver used to write the vector file. If not specified, it attempts to infer it from the file extension. If no extension is specified, it saves ESRI Shapefile to a folder.

schemadict, default None

If specified, the schema dictionary is passed to Fiona to better control how the file is written. If None, GeoPandas will determine the schema based on each column’s dtype. Not supported for the “pyogrio” engine.

indexbool, default None

If True, write index into one or more columns (for MultiIndex). Default None writes the index into one or more columns only if the index is named, is a MultiIndex, or has a non-integer data type. If False, no index is written.

New in version 0.7: Previously the index was not written.

modestring, default ‘w’

The write mode, ‘w’ to overwrite the existing file and ‘a’ to append. Not all drivers support appending. The drivers that support appending are listed in fiona.supported_drivers or Toblerity/Fiona

crspyproj.CRS, default None

If specified, the CRS is passed to Fiona to better control how the file is written. If None, GeoPandas will determine the crs based on crs df attribute. The value can be anything accepted by pyproj.CRS.from_user_input(), such as an authority string (eg “EPSG:4326”) or a WKT string.

enginestr, “fiona” or “pyogrio”

The underlying library that is used to write the file. Currently, the supported options are “fiona” and “pyogrio”. Defaults to “fiona” if installed, otherwise tries “pyogrio”.

**kwargs

Keyword args to be passed to the engine, and can be used to write to multi-layer data, store data within archives (zip files), etc. In case of the “fiona” engine, the keyword arguments are passed to fiona.open`. For more information on possible keywords, type: import fiona; help(fiona.open). In case of the “pyogrio” engine, the keyword arguments are passed to pyogrio.write_dataframe.

See also

GeoSeries.to_file
GeoDataFrame.to_postgis

write GeoDataFrame to PostGIS database

GeoDataFrame.to_parquet

write GeoDataFrame to parquet

GeoDataFrame.to_feather

write GeoDataFrame to feather

Notes

The format drivers will attempt to detect the encoding of your data, but may fail. In this case, the proper encoding can be specified explicitly by using the encoding keyword parameter, e.g. encoding='utf-8'.

Examples

>>> gdf.to_file('dataframe.shp')  
>>> gdf.to_file('dataframe.gpkg', driver='GPKG', layer='name')  
>>> gdf.to_file('dataframe.geojson', driver='GeoJSON')  

With selected drivers you can also append to a file with mode=”a”:

>>> gdf.to_file('dataframe.shp', mode="a")  

Using the engine-specific keyword arguments it is possible to e.g. create a spatialite file with a custom layer name:

>>> gdf.to_file(
...     'dataframe.sqlite', driver='SQLite', spatialite=True, layer='test'
... )