Created
October 3, 2022 22:14
-
-
Save dblodgett-usgs/61425b5cb58bd570a47e7ed054a8d7b5 to your computer and use it in GitHub Desktop.
remove gpkg layer with R using RSQLite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
remove_gpkg_table <- function(db, table) { | |
con <- RSQLite::dbConnect(RSQLite::SQLite(), db) | |
on.exit(RSQLite::dbDisconnect(con)) | |
o <- RSQLite::dbRemoveTable(con, table) | |
o <- RSQLite::dbSendQuery(con, sprintf("DELETE FROM gpkg_contents where table_name='%s';", table)) | |
RSQLite::dbClearResult(o) | |
} |
Thanks @rouault -- I opened an issue in sf
to ask about it. The above is a hack I used in some tests -- not planning on putting this into anything that matters!
For completeness: sf::st_delete()
does this.
lol -- I've searched for this a number of times and totally missed that st_delete exists. Thanks @edzer
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is not enough in the general case to remove all references to the deleted table.
Calling GDALDataset::DeleteLayer(layer_index) or GDALDataset::ExecuteSQL("DROP TABLE table_name") would make sure every reference to the deleted layer in auxiliary gpkg_ tables are removed. Note that the "DROP TABLE table_name" is intercepted in the GPKG driver to do more than just forwarding it to SQLite: https://github.com/OSGeo/gdal/blob/master/ogr/ogrsf_frmts/gpkg/ogrgeopackagedatasource.cpp#L6362