I have a GeoPackage table with no crs definition. I would like to fix this now. There is no command like <ogrlayer> .SetSpatialRef (...)

Download d.gpkg

# -*- coding: utf-8 -*-
import osgeo.ogr as ogr

driver = ogr.GetDriverByName("GPKG")
data_source = driver.Open("c:/..../d.gpkg", update=True)


sr_25833=ly_25833.GetSpatialRef()     # EPSG:25833
sr_without=ly_without.GetSpatialRef() # None 

print (sr_25833)
print (sr_without)

# !!! what i wont !!!!
# sr_without=ly_without.SetSpatialRef(sr_25833)


Download d.gpkg


You have an interesting problem. Your table "tab_without" is not actually without SRS but it has srs_id=0, which means according to the standard an undefined, geographic system:

The record with an srs_id of -1 SHALL be used for undefined Cartesian coordinate reference systems. The record with an srs_id of 0 SHALL be used for undefined geographic coordinate reference systems.

Changing the srs_id of a table from zero to to some other srs_id is technically not a simple procedure. It is not enough to update the srs_id value in tables "gpkg_contents" and "gpkg_geometry_columns" because the SRID of the geometry is also stored into the GeoPackageBinaryHeader of the geometry BLOB and therefore also the geometries must be rewritten. For making the updates possible some triggers must be removed first and re-created again after the updates. All that must be done correctly for not to destroy the integrity of the database.

All this does not mean that it could not be done if someone writes SetSpatialRef() but I doubt that it will happen soon. Meanwhile I would suggest to create a copy of the table and assign a correct srs_id at creation time like CreateLayer( 'tbl_linestring', geom_type = ogr.wkbLineString, srs = srs) where srs if for example srs.ImportFromEPSG( 25833 ). Once done you can drop the original table. Because of triggers I do not recommend to try to rename the new table to use name of the old table. If names are important for you then create a new, empty GeoPackage and copy all the tables you want to have into it, with the names and SRS that you like.

  • Thanks. Is there a easy way to copy the data from "tab_without" to the new "tbl_linestring"? – Mike Apr 5 '18 at 7:53

You can edit the table gpkg_spatial_ref_sys and add a new SRS (you cna get all the info you need from speg.io). Then you must to edit the table gpkg_geometry_columns and assign the new SRS. Take into account the SRS must match the geometry you have in your tables, this proces do not reproject your geometry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.