Fiona no longer casts `int` to `float` since 1.10
Expected behavior and actual behavior.
Hi all, in the current version of fiona rather than upcasting integers to floats. Worse it appears to drop the field in output types (like GeoJSON). In previous versions of fiona it would upcast integers automatically to floating point numbers.
I think it's related to https://github.com/Toblerity/Fiona/issues/1376
Steps to reproduce the problem.
Simple example with the WARNING logs on
>>> import fiona
>>> fiona.__version__
'1.10.1'
>>> import logging
>>> logging.basicConfig(level=logging.WARNING)
>>> schema = {'geometry': 'Point', 'properties': {'a': 'float', 'b': 'int'}}
>>> with fiona.open("test_fiona110.geojson", mode="w", schema=schema) as col:
... col.writerecords([{'id': '0', 'type': 'Feature', 'properties': {'a': 1, 'b': 1}, 'geometry': {'type': 'Point', 'coordinates': (0.0, 0.0)}}])
...
WARNING:fiona.ogrext:Skipping field because of invalid value: key='a', value=1
>>> with open("test_fiona110.geojson") as f:
... print(f.read())
...
{
"type": "FeatureCollection",
"name": "test_fiona110",
"features": [
{ "type": "Feature", "properties": { "b": 1 }, "geometry": { "type": "Point", "coordinates": [ 0.0, 0.0 ] } }
]
}
Operating system
For example: Debian linux
> lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux trixie/sid
Release: n/a
Codename: trixie
Fiona and GDAL version and provenance
- fiona version: 1.10.1 (installed via apt-get)
- Python 3.12.7 (installed via apt-get)
- gdal version: GDAL 3.9.3, released 2024/10/07 (installed via apt-get)
I'm also seeing a variation of this, which leads to an overflow error instead of omitting the field. If other fields are set as int32, then trying to write an int>32 bits with a schema declaring it a float leads to an overflow:
import logging
logging.basicConfig(
level=logging.DEBUG, # capture debug and above
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
)
logger = logging.getLogger(__name__)
import fiona
schema={'properties': {'Anode': 'int32', 'Bnode': 'int32', 'LinkID': 'float', }, 'geometry': 'LineString'}
crs = "EPSG:4326"
coordinates = [(115.638812, -32.62857), (115.637933, -32.631091)]
records = [fiona.Feature(geometry=fiona.Geometry(coordinates=coordinates, type='LineString'), id='23998',
properties=fiona.Properties(Anode=25194, Bnode=25196, LinkID=2519425196))]
with fiona.open(
"test_links.gpkg",
"w",
driver='GPKG',
crs=crs,
schema=schema,
) as f:
print(records)
# works on 1.9.6 (writes LinkID as a float)
# at ea24f55bb0b7f5f160f9a1742b2b88c6258f9787 write NULL for LinkID WARNING - fiona.ogrext - Skipping field LinkID: invalid type (2, 0, 'int')
# at b6e62ccecf31d533d79edc3ad29754197fb9f4b8 fails, OverflowError: value too large to convert to int
# fails on 1.10.0
f.writerecords(records)
Traceback (most recent call last):
File "/home/m-richards/git/Fiona/issue_untracked.py", line 34, in <module>
f.writerecords(records)
File "/home/m-richards/git/Fiona/fiona/collection.py", line 541, in writerecords
self.session.writerecs(records, self)
File "fiona/ogrext.pyx", line 1665, in fiona.ogrext.WritingSession.writerecs
cogr_feature = feat_builder.build(record, collection)
File "fiona/ogrext.pyx", line 787, in fiona.ogrext.OGRFeatureBuilder.build
setter.set(cogr_feature, i, value, {"encoding": encoding})
File "fiona/ogrext.pyx", line 242, in fiona.ogrext.IntegerField.set
OGR_F_SetFieldInteger(feature, i, int(value))
OverflowError: value too large to convert to int
This seems to be based on the property setter cache in #1377 (probably earlier as well, that's just as far as I've traced it) where the field type setter cached based on the type of the input field - in this case inputs are all ints, leading to the overflow when 2519425196 is received. I don't know if this field coercion was ever intended to be possible, or if the legacy code I've been looking at just happens to be using it. Either way I'd be happy to try and help pull together a PR to fix this, or at least throw a more specific error message.
SYSTEM INFO
python : 3.12.11 (main, Aug 18 2025, 19:19:11) [Clang 20.1.4 ] executable : /home/m-richards/git/Fiona/.venv/bin/python3 machine : Linux-6.6.87.2-microsoft-standard-WSL2-x86_64-with-glibc2.35
GEOS, GDAL, PROJ INFO
GEOS : 3.13.1 GEOS lib : None GDAL : 3.10.3 GDAL data dir: /home/m-richards/git/Fiona/.venv/lib/python3.12/site-packages/pyogrio/gdal_data/ PROJ : 9.5.1 PROJ data dir: /home/m-richards/git/Fiona/.venv/lib/python3.12/site-packages/pyproj/proj_dir/share/proj