- GeoDataFrame.to_file(filename, driver=None, schema=None, index=None, **kwargs)¶
GeoDataFrameto 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
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 https://github.com/Toblerity/Fiona/blob/master/fiona/drvsupport.py
- 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.
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.
>>> 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")