[GIS] How to write a GeoPandas dataframe into a single file (preferably JSON or GeoPackage)

fionageopackagegeopandasjsonpython

I'm new to GIS and having a basic beginner problem. I've got data in the form of a geopackage .gpkg from GADM.org. I can read in and use the data without problem using Geopandas (via built-in Fiona).

I've added and deleted some columns to the data, and changed some column names to ones more intuitive to me (for ease of later use and consistency across files). Minor edits.

Now I want to export the data to a file format that is easy to later read into Python as a GeoPandas dataframe. However, whenever I try

CityShapeFile.to_file('CityShapeFile_edited.gpkg',encoding='utf-8')

No matter which format I choose (e.g. gpkg, json, shp, …) I don't get a single file, but rather a folder with 5 files: *.cpg, *.dbf, *.prj, *.shp, and *.shx. I'm sure those files together contain all the data I need, but in order to read the data back into Python I really need it in one file (as far as I know). Considering the original data I downloaded is in a single gpkg file, this seems possible and probably normal, but how to do it?

I read in the Fiona manual that it can write zipped shapefiles, but I couldn't find any simple example of doing that with a GeoPandas dataframe, nor am I sure whether that can be read in correctly. So,

How do I write a GeoPandas dataframe into a single file (preferably JSON or GeoPackage)?

Best Answer

To write to GeoJSON: dataframe.to_file("output.json", driver="GeoJSON")

To write to GeoPackage: dataframe.to_file("output.gpkg", driver="GPKG")

Documentation is here, though somewhat sparse.

Related Question