0

I'm creating a Esri Shapefile using Fiona. If I don't add any records, the dbf created by Fiona does not include the headers defined by the schema. My customers claim that results in an invalid shapefile. Is there a configuration option to include headers even with no records? I can't find anything in the documentation. Using Fiona 1.8.

This is where the file is opened:

self.__sink = fiona.open(
    self.__path,
    mode='w',
    driver='ESRI Shapefile',
    schema=self.SCHEMA,
    layer=self.__path.name,
    crs='EPSG:4326',
    encoding='utf-8'
)

This is within a method that is called for every instance of a feature. If there are no features, this is never called, and no headers are written.

...
record = {
    'geometry': geojson,
    'properties': {
        ...
    }
}

# Ensure exclusive access with lock
async with self.__write_lock:
    assert self.__sink is not None
    self.__sink.write(record)
    ...

Then the obvious close:

self.__sink.close()
self.__sink = None

Here's the line in Fiona that adds the fields. From how I read it, this should happen irregardless of adding records.

https://github.com/Toblerity/Fiona/blob/1.8.20/fiona/ogrext.pyx#L1221 exc_wrap_int(OGR_L_CreateField(self.cogr_layer, cogr_fielddefn, 1))

I enabled DEBUG logs for Fiona and it appears to be creating the fields

...
[2021-12-06 11:19:17] [DEBUG] [82320] Begin creating field: 'URL' value: 'str:254' [fiona.ogrext] (collection.py:165)
[2021-12-06 11:19:17] [DEBUG] [82320] Field format parsing, value: 'str', fmt: '254' [fiona.ogrext] (collection.py:165)
[2021-12-06 11:19:17] [DEBUG] [82320] End creating field 'URL' [fiona.ogrext] (collection.py:165)
3
  • 1
    Yes, an empty dBase file is not valid, and therefore the shapefile is not valid, so it's not just a claim. Please Edit the question to contain the code you've used to generate the empty shapefile.
    – Vince
    Dec 3, 2021 at 21:53
  • Code added. Pretty straight forward, surprised it would not always add the headers to the dbf, given it is merely a wrapper for GDAL which does. Dec 6, 2021 at 15:12
  • Added Fiona logs Dec 7, 2021 at 20:44

1 Answer 1

0

There was a bug that prevented the application from calling the close method on the Fiona object...I think to a point that everything stopped. With that that, Fiona did not have the opportunity to write the headers even without records. With the bug fixed, Fiona correctly writes the headers even without records.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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