Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add index_as_fid option to write_dataframe? #287

Open
theroggy opened this issue Sep 6, 2023 · 5 comments
Open

ENH: Add index_as_fid option to write_dataframe? #287

theroggy opened this issue Sep 6, 2023 · 5 comments
Milestone

Comments

@theroggy
Copy link
Member

theroggy commented Sep 6, 2023

There is an fid_as_index option in read_dataframe to be able to... read the fid. However, there is no explicit option to e.g. index_as_fid in write_dataframe to write it again.

For e.g. shapefiles writing fid's is not relevant at all as they don't save fid's.

For Geopackages there is a workaround: if you add the index as a column named "fid" to the dataframe you will write the fid.

Not sure if an explicit option is needed... an alternative could be to just document the "workaround" or use this issue as documentation :-).

@jorisvandenbossche
Copy link
Member

Do you know if GDAL exposes some information or metadata about this? For example so that we can raise an error if the file format actually does not support it?

@theroggy
Copy link
Member Author

Do you know if GDAL exposes some information or metadata about this? For example so that we can raise an error if the file format actually does not support it?

Yes, if OGR_L_GetFIDColumn() returns "", the file type cannot save fid's. If it returns another string, that's the column name used to store the fid.

Examples:

  • for shapefile "" is returned.
  • for Geopackage "fid" is returned: that's the default name of the column the fid is stored in.
  • for sqlite files I never tested/used it, but according to the gdal doc the default column name for the fid is "OGC_FID", so I suppose that OGR_L_GetFIDColumn() will return that and I suppose that you'll have to call the column like that in the GeoDataFrame if you want the fid's to be retained.

@jorisvandenbossche
Copy link
Member

Nice, in that case this sounds as a good option to me.

@brendan-ward brendan-ward added this to the 0.8.0 milestone Jan 25, 2024
@theroggy
Copy link
Member Author

theroggy commented Mar 9, 2024

I just encountered another way to save the index of the GeoDataFrame as "fid" (primary key) in a GPKG: if the name of the index is "fid" it will also be saved as "fid" in the file.

PS: Fiona treats this the same way.

xref: geopandas/geopandas#3217

@jorisvandenbossche jorisvandenbossche modified the milestones: 0.8.0, 0.9.0 May 3, 2024
@jorisvandenbossche
Copy link
Member

(moved this to the next milestone)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants