@@ -993,6 +993,48 @@ def test_write_csv_encoding(tmp_path, encoding):
993
993
assert csv_bytes == csv_pyogrio_bytes
994
994
995
995
996
+ @pytest .mark .parametrize (
997
+ "ext, fid_column, fid_param_value" ,
998
+ [
999
+ (".gpkg" , "fid" , None ),
1000
+ (".gpkg" , "FID" , None ),
1001
+ (".sqlite" , "ogc_fid" , None ),
1002
+ (".gpkg" , "fid_custom" , "fid_custom" ),
1003
+ (".gpkg" , "FID_custom" , "fid_custom" ),
1004
+ (".sqlite" , "ogc_fid_custom" , "ogc_fid_custom" ),
1005
+ ],
1006
+ )
1007
+ @pytest .mark .requires_arrow_write_api
1008
+ def test_write_custom_fids (tmp_path , ext , fid_column , fid_param_value , use_arrow ):
1009
+ """Test to specify FIDs to save when writing to a file.
1010
+
1011
+ Saving custom FIDs is only supported for formats that actually store the FID, like
1012
+ e.g. GPKG and SQLite. The fid_column name check is case-insensitive.
1013
+
1014
+ Typically, GDAL supports using a custom FID column for these file formats via a
1015
+ `FID` layer creation option, which is also tested here. If `fid_param_value` is
1016
+ specified (not None), an `fid` parameter is passed to `write_dataframe`, causing
1017
+ GDAL to use the column name specified for the FID.
1018
+ """
1019
+ input_gdf = gp .GeoDataFrame (
1020
+ {fid_column : [5 ]}, geometry = [shapely .Point (0 , 0 )], crs = "epsg:4326"
1021
+ )
1022
+ kwargs = {}
1023
+ if fid_param_value is not None :
1024
+ kwargs ["fid" ] = fid_param_value
1025
+ path = tmp_path / f"test{ ext } "
1026
+
1027
+ write_dataframe (input_gdf , path , use_arrow = use_arrow , ** kwargs )
1028
+
1029
+ assert path .exists ()
1030
+ output_gdf = read_dataframe (path , fid_as_index = True , use_arrow = use_arrow )
1031
+ output_gdf = output_gdf .reset_index ()
1032
+
1033
+ # pyogrio always sets "fid" as index name with `fid_as_index`
1034
+ expected_gdf = input_gdf .rename (columns = {fid_column : "fid" })
1035
+ assert_geodataframe_equal (output_gdf , expected_gdf )
1036
+
1037
+
996
1038
@pytest .mark .parametrize ("ext" , ALL_EXTS )
997
1039
@pytest .mark .requires_arrow_write_api
998
1040
def test_write_dataframe (tmp_path , naturalearth_lowres , ext , use_arrow ):
0 commit comments