How to Export GeoJSON in Python with GeoPandas
How to export a GeoDataFrame to GeoJSON in Python using GeoPandas, with driver options and output examples.
Problem statement
A common GIS task is exporting spatial data from Python into GeoJSON so it can be used in web maps, APIs, QGIS, or other downstream workflows.
In many cases, the data is already in a GeoPandas GeoDataFrame. You may have:
- read a shapefile into Python
- loaded a GeoPackage layer
- filtered an existing GeoDataFrame
- created new geometries in code
The main question is usually: what is the correct way to write that GeoDataFrame to a .geojson file?
The practical issues are:
- using the right GeoPandas export method
- choosing the correct driver
- making sure the CRS is appropriate
- avoiding problems with invalid geometries or messy attribute fields
If your goal is to export GeoJSON in Python with GeoPandas, the standard workflow is straightforward once those checks are in place.
Quick answer
Use GeoDataFrame.to_file() with the GeoJSON driver:
import geopandas as gpd
gdf = gpd.read_file("data/roads.shp")
gdf = gdf.to_crs("EPSG:4326") # recommended for GeoJSON compatibility
gdf.to_file("output/roads.geojson", driver="GeoJSON")
Before exporting, check:
- that
gdfis a valid GeoDataFrame - that it has a geometry column
- that the CRS is correct
- that the output path is valid
For GeoJSON interoperability, exporting in WGS84 longitude/latitude (EPSG:4326) is the safest choice.
Step-by-step solution
Load or prepare the GeoDataFrame
Start with an existing GeoDataFrame or read spatial data from disk.
import geopandas as gpd
gdf = gpd.read_file("data/parcels.shp")
print(type(gdf))
print(gdf.geometry.name)
You should confirm that:
- the object is a
GeoDataFrame - the geometry column exists
- the data loaded correctly
If you already have a GeoDataFrame from previous processing, you can export it directly.
Inspect the data before export
Before writing the file, inspect the structure of the dataset.
print(gdf.head())
print(gdf.columns)
print(len(gdf))
print(gdf.geom_type.unique())
print(gdf.crs)
This helps catch common problems early:
- unexpected columns
- mixed geometry types
- missing CRS
- empty datasets
If the output will be used in web tools or APIs, also confirm whether the coordinates are already longitude/latitude.
Reproject to the correct CRS if needed
For best compatibility, GeoJSON should generally be exported in WGS84 longitude/latitude coordinates (EPSG:4326). Many web tools expect this, and using another CRS can cause display or interoperability problems.
if gdf.crs != "EPSG:4326":
gdf = gdf.to_crs("EPSG:4326")
Use this when:
- the output will be used in Leaflet, Mapbox, OpenLayers, or web APIs
- the source data is projected, such as UTM or a local state plane CRS
If you keep the source CRS, confirm that the target tool supports it correctly before exporting GeoJSON.
Export the GeoDataFrame to GeoJSON
Use to_file() and set the driver to GeoJSON.
output_file = "output/parcels.geojson"
gdf.to_file(output_file, driver="GeoJSON")
That is the standard GeoPandas method for writing GeoJSON from a GeoDataFrame.
Choose a clear filename and make sure the output folder exists. If you want predictable overwrite behavior, delete or version the output file explicitly before writing.
Verify the exported GeoJSON file
After export, read the file back in or open it in QGIS.
check_gdf = gpd.read_file("output/parcels.geojson")
print(len(check_gdf))
print(check_gdf.columns)
print(check_gdf.crs)
You should confirm:
- the feature count matches the original
- expected attribute fields are present
- geometries display correctly
- the CRS is what you intended
Code examples
Example 1: Export a GeoDataFrame to GeoJSON
This is the minimal workflow to export a GeoDataFrame to GeoJSON.
import geopandas as gpd
gdf = gpd.read_file("data/buildings.gpkg")
gdf.to_file("output/buildings.geojson", driver="GeoJSON")
Example 2: Read a shapefile and convert it to GeoJSON
This is a practical shapefile-to-GeoJSON conversion.
import geopandas as gpd
gdf = gpd.read_file("data/roads.shp")
gdf.to_file("output/roads.geojson", driver="GeoJSON")
This is one of the most common cases when users want to convert a shapefile to GeoJSON in Python.
Example 3: Reproject to EPSG:4326 before export
If the input data is in a projected CRS, convert it before writing GeoJSON.
import geopandas as gpd
gdf = gpd.read_file("data/zoning.shp")
print("Original CRS:", gdf.crs)
gdf_wgs84 = gdf.to_crs("EPSG:4326")
gdf_wgs84.to_file("output/zoning_wgs84.geojson", driver="GeoJSON")
This is the recommended pattern for web mapping workflows.
Example 4: Export only selected columns and features
You can filter rows and keep only the fields you need before saving.
import geopandas as gpd
gdf = gpd.read_file("data/parcels.shp")
filtered = gdf[gdf["land_use"] == "residential"][["parcel_id", "owner", "geometry"]]
filtered = filtered.to_crs("EPSG:4326")
filtered.to_file("output/residential_parcels.geojson", driver="GeoJSON")
This is useful when you want a smaller, cleaner output for sharing or web use.
Explanation
When you call GeoDataFrame.to_file(), GeoPandas writes vector data using the file driver you specify. For GeoJSON output, that driver is GeoJSON.
In practical GeoPandas workflows, to_file() is the standard method for writing formats such as:
- Shapefile
- GeoPackage
- GeoJSON
The driver tells GeoPandas which file format to create.
CRS matters because GeoJSON is commonly consumed by tools that expect longitude and latitude coordinates in WGS84. If you export projected coordinates without checking CRS, the file may still write successfully, but it may display in the wrong place or fail in some web tools.
Attribute fields matter too. Simple text, numbers, and dates usually work well. More complex values, such as nested Python objects or inconsistent types in a column, can cause problems during export or when the file is opened elsewhere.
Geometry quality also matters. Invalid polygons, empty geometries, or mixed geometry types may still write, but can create issues in QGIS, web maps, or spatial APIs.
Edge cases or notes
File already exists
If the output file already exists, do not assume overwrite behavior will be handled the way you want. For predictable results, remove the old file first if that fits your workflow.
from pathlib import Path
import geopandas as gpd
output_file = Path("output/roads.geojson")
if output_file.exists():
output_file.unlink()
gdf = gpd.read_file("data/roads.shp")
gdf.to_file(output_file, driver="GeoJSON")
CRS is missing
If gdf.crs is None, the data has no defined CRS.
print(gdf.crs)
That does not always stop export, but it creates confusion later. Set the CRS only if you know what the source coordinates represent.
gdf = gdf.set_crs("EPSG:32633")
Invalid geometries
Broken geometries can cause export or display problems.
invalid_count = (~gdf.geometry.is_valid).sum()
print("Invalid geometries:", invalid_count)
If needed, you can try repairing geometries before export:
gdf["geometry"] = gdf.buffer(0)
This workaround sometimes helps, especially with polygon issues, but it can change geometry and does not fix every problem.
Large files
GeoJSON is text-based and can become large and slow with many features. It is good for sharing and web delivery, but not always the best format for heavy processing. For large datasets, consider GeoPackage or Parquet for internal workflows.
Attribute field compatibility
Avoid exporting columns that contain:
- lists or dictionaries
- mixed Python object types
- values you do not need
A smaller attribute table usually produces a more compatible GeoJSON file.
Internal links
If you need background first, see What Is a GeoDataFrame in GeoPandas?.
Related tasks:
If export is failing, check Why GeoPandas to_file Is Not Writing the Output File.
FAQ
How do I export a GeoDataFrame to GeoJSON in GeoPandas?
Use to_file() with the GeoJSON driver:
gdf.to_file("output/data.geojson", driver="GeoJSON")
That is the standard GeoPandas method to write GeoJSON from a GeoDataFrame.
Do I need to convert my CRS to EPSG:4326 before saving GeoJSON?
Not always, but it is strongly recommended for web maps and many APIs. EPSG:4326 is the most interoperable choice for GeoJSON.
gdf = gdf.to_crs("EPSG:4326")
Can I convert a shapefile to GeoJSON with GeoPandas?
Yes. Read the shapefile, then write it as GeoJSON.
gdf = gpd.read_file("data/input.shp")
gdf.to_file("output/output.geojson", driver="GeoJSON")
Why is my exported GeoJSON not opening correctly in QGIS or a web map?
Common causes include:
- wrong or missing CRS
- invalid geometries
- unsupported attribute values
- corrupted or incomplete output path
Check the CRS, validate geometries, and try reading the exported file back into GeoPandas.