1

I don't know why this is so hard to find or to do, but how does one create an empty .shp with geopandas?

I essentially want to duplicate the processes of creating a type: polygon shapefile from QGIS, but in a Python script. There is no data to append at this stage. I understand the error, but I am not sure how to add data without adding data. Do I need to do something with schema?

def create_empty(save_name):
    df = gpd.GeoDataFrame(columns=['id', 'geometry'], geometry='geometry')
    df.set_crs(4326)
    print(df)
    df.to_file(filename=f'./QGIS_inputs/{save_name}.shp', driver='ESRI Shapefile')

The error that I get:

ValueError: Cannot write empty DataFrame to file.

2 Answers 2

4

Geopandas derives the schema from the geodataframe if you don't specify it. And since your geodataframe is empty, geopandas can't derive a schema.

If you want to save an empty shapefile, you need to pass a fiona schema dict that defines the geometry type and at least one column (that's a shapefile restriction, not applicable to all output formats):

import geopandas

shp_fname = "test.shp"
schema = {"geometry": "Polygon", "properties": {"id": "int"}}
crs = "EPSG:4326"

df = geopandas.GeoDataFrame(geometry=[])

df.to_file(shp_fname, driver='ESRI Shapefile', schema=schema, crs=crs)

More details on schemas:

2
  • Thank you! To follow up, are there other shapefile schemas? Why would geopandas not have a schema? To make it simplier?
    – Binx
    Jun 16 at 14:02
  • 1
    Geopandas derives the schema from the geodataframe if you don't specify it. Geopandas only uses fiona schemas, but you can set up any fiona schema that a shapefile supports.
    – user2856
    Jun 16 at 22:36
3

GeoPandas < 0.11

Try with Fiona instead:

import fiona
import geopandas

shp_fname = "test.shp"
schema = {"geometry": "Polygon", "properties": {"id": "int"}}
crs = "EPSG:4326"

# write an empty polygon shapefile
with fiona.open(shp_fname, "w", "ESRI Shapefile", schema=schema, crs=crs):
    pass

# now read in the empty shapefile
df = geopandas.read_file(shp_fname)

After GeoPandas >= 0.11

This release can write empty shapefiles:

import geopandas

df = geopandas.GeoDataFrame(
    columns=["id", "geometry"],
    geometry="geometry", crs="EPSG:4326")
df.to_file("test.shp")

now shows a warning:

UserWarning: You are attempting to write an empty DataFrame to file. For some drivers, this operation may fail.

Here is the metadata from a system shell with fio info:

$ fio info test.shp --indent 2
{
  "driver": "ESRI Shapefile",
  "schema": {
    "properties": {
      "index": "str:80",
      "id": "str:80"
    },
    "geometry": "LineString"
  },
  "crs": "EPSG:4326",
  "crs_wkt": "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.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AXIS[\"Latitude\",NORTH],AXIS[\"Longitude\",EAST],AUTHORITY[\"EPSG\",\"4326\"]]",
  "name": "test",
  "bounds": [
    0.0,
    0.0,
    0.0,
    0.0
  ],
  "count": 0
}

Note that this assumes LineString geometry.

3
  • Thanks for the solution, though I marked user2856 due to them using geopandas. Do you not need to save or close after fiona.open()?
    – Binx
    Jun 16 at 14:04
  • 1
    @Binx no, because of the with statement which is a python context manager.
    – user2856
    Jun 16 at 22:46
  • 1
    under the hood, geopandas uses fiona (until pyogrio is ready)
    – Mike T
    Jun 17 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.