diff --git a/fiona/ogrext.pyx b/fiona/ogrext.pyx index 8ae7f799..386a31b9 100644 --- a/fiona/ogrext.pyx +++ b/fiona/ogrext.pyx @@ -764,7 +764,7 @@ cdef class OGRFeatureBuilder: val_type = type(value) if val_type in self.property_setter_cache: - setter = self.property_setter_cache[val_type] + setter = self.property_setter_cache[(val_type, schema_type)] else: for cls in val_type.mro(): fieldkey = (*FIELD_TYPES_MAP2[NAMED_FIELD_TYPES[schema_type]], cls.__name__) @@ -773,7 +773,7 @@ cdef class OGRFeatureBuilder: except KeyError: continue else: - self.property_setter_cache[val_type] = setter + self.property_setter_cache[(val_type, schema_type)] = setter break else: log.warning("Skipping field because of invalid value: key=%r, value=%r", key, value) diff --git a/tests/test_bigint.py b/tests/test_bigint.py index 4f987731..c319a66e 100644 --- a/tests/test_bigint.py +++ b/tests/test_bigint.py @@ -74,3 +74,33 @@ def test_issue691(tmpdir, dtype): assert src.schema["properties"]["foo"] == "int:18" first = next(iter(src)) assert first["properties"]["foo"] == 3694063472 + +def test_mixed_int_widths(tmpdir): + """Demonstrate fix for #1501.""" + schema = {"geometry": "Any", "properties": {"A": 'int32', "B":"int64"}} + with fiona.open( + str(tmpdir.join("test.shp")), + "w", + driver="Shapefile", + schema=schema, + crs="epsg:4326", + ) as dst: + dst.write( + Feature.from_dict( + **{ + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": (-122.278015, 37.868995), + }, + "properties": {"A": 3, "B": 3694063472}, + } + ) + ) + + with fiona.open(str(tmpdir.join("test.shp"))) as src: + assert src.schema["properties"]["A"] == "int32:9" + assert src.schema["properties"]["B"] == "int:18" + first = next(iter(src)) + assert first["properties"]["A"] == 3 + assert first["properties"]["B"] == 3694063472 diff --git a/tests/test_datetime.py b/tests/test_datetime.py index 2008815e..fe246e29 100644 --- a/tests/test_datetime.py +++ b/tests/test_datetime.py @@ -854,3 +854,27 @@ class MyDate(datetime.date): assert colxn.schema["properties"]["when"] == "date" feat = next(colxn) assert feat.properties["when"] == "2024-04-15" + + +def test_write_date_and_string(tmp_path): + """Demonstrate fix for #1490.""" + + feat = Feature.from_dict( + { + "properties": {"date": "2022-09-01", "text":"n"}, + "geometry": {"type": "Point", "coordinates": [0, 0]}, + } + ) + with fiona.open( + tmp_path / "test.gpkg", + "w", + driver="GPKG", + crs="EPSG:4326", + schema={"geometry": "Point", "properties": {"date": "date", "text": "str"}}, + ) as colxn: + colxn.writerecords([feat]) + + with fiona.open(tmp_path / "test.gpkg") as colxn: + assert colxn.schema["properties"]["date"] == "date" + feat = next(colxn) + assert feat.properties["date"] == "2022-09-01"