Skip to content

Commit

Permalink
GeoPackage: Fix handling of invalid SRS ID when writing
Browse files Browse the repository at this point in the history
Improves OSGeo#3286
Closes OSGeo#3302
  • Loading branch information
mloskot committed Dec 22, 2020
1 parent 65144a3 commit 69aebed
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
73 changes: 73 additions & 0 deletions autotest/ogr/ogr_gpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,79 @@ def test_ogr_gpkg_srs_non_consistent_with_official_definition():
gdal.Unlink('/vsimem/ogr_gpkg_20.gpkg')


def test_ogr_gpkg_write_srs_undefined_geographic():

gdal.Unlink('tmp/ogr_gpkg_srs_undefined_geographic.gpkg')

gpkg_ds = gdaltest.gpkg_dr.CreateDataSource('tmp/ogr_gpkg_srs_undefined_geographic.gpkg')
assert gpkg_ds is not None

# Check initial default SRS entries in gpkg_spatial_ref_sys
sql_lyr = gpkg_ds.ExecuteSQL("SELECT COUNT(*) FROM gpkg_spatial_ref_sys")
gpkg_spatial_ref_sys_total = sql_lyr.GetNextFeature().GetField(0)
assert gpkg_spatial_ref_sys_total == 3 # entries with SRS IDs: -1, 0, 4326
gpkg_ds.ReleaseResultSet(sql_lyr)

srs= osr.SpatialReference()
srs.SetFromUserInput('GEOGCS["Undefined geographic SRS",DATUM["unknown",SPHEROID["unknown",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST]]')
lyr = gpkg_ds.CreateLayer('srs_test_geographic_layer', geom_type=ogr.wkbPoint, srs=srs)
srs_wkt = lyr.GetSpatialRef().ExportToWkt()
assert srs_wkt.find('Undefined geographic SRS') >= 0, srs_wkt
assert lyr.GetSpatialRef().IsGeographic()

gpkg_ds = None
gpkg_ds = ogr.Open('tmp/ogr_gpkg_srs_undefined_geographic.gpkg')

# Check no new SRS entries have been inserted into gpkg_spatial_ref_sys
sql_lyr = gpkg_ds.ExecuteSQL("SELECT COUNT(*) FROM gpkg_spatial_ref_sys")
assert gpkg_spatial_ref_sys_total == sql_lyr.GetNextFeature().GetField(0)
gpkg_ds.ReleaseResultSet(sql_lyr)

lyr = gpkg_ds.GetLayer(0)
srs_wkt = lyr.GetSpatialRef().ExportToWkt()
assert srs_wkt.find('Undefined geographic SRS') >= 0, srs_wkt
assert lyr.GetSpatialRef().IsGeographic()

gpkg_ds = None
gdal.Unlink('tmp/ogr_gpkg_srs_undefined_geographic.gpkg')


def test_ogr_gpkg_write_srs_undefined_cartesian():

gdal.Unlink('tmp/ogr_gpkg_srs_cartesian.gpkg')

gpkg_ds = gdaltest.gpkg_dr.CreateDataSource('tmp/ogr_gpkg_srs_cartesian.gpkg')
assert gpkg_ds is not None

# Check initial default SRS entries in gpkg_spatial_ref_sys
sql_lyr = gpkg_ds.ExecuteSQL("SELECT COUNT(*) FROM gpkg_spatial_ref_sys")
gpkg_spatial_ref_sys_total = sql_lyr.GetNextFeature().GetField(0)
assert gpkg_spatial_ref_sys_total == 3 # SRS with IDs: -1, 0, 4326
gpkg_ds.ReleaseResultSet(sql_lyr)

srs= osr.SpatialReference()
srs.SetFromUserInput('LOCAL_CS["Undefined cartesian SRS"]')
lyr = gpkg_ds.CreateLayer('srs_test_cartesian_layer', geom_type=ogr.wkbPoint, srs=srs)
srs_wkt = lyr.GetSpatialRef().ExportToWkt()
assert srs_wkt.find('Undefined cartesian SRS') >= 0
assert lyr.GetSpatialRef().IsLocal()

gpkg_ds = None
gpkg_ds = ogr.Open('tmp/ogr_gpkg_srs_cartesian.gpkg')

# Check no new SRS entries have been inserted into gpkg_spatial_ref_sys
sql_lyr = gpkg_ds.ExecuteSQL("SELECT COUNT(*) FROM gpkg_spatial_ref_sys")
assert gpkg_spatial_ref_sys_total == sql_lyr.GetNextFeature().GetField(0)
gpkg_ds.ReleaseResultSet(sql_lyr)

lyr = gpkg_ds.GetLayer(0)
srs_wkt = lyr.GetSpatialRef().ExportToWkt()
assert srs_wkt.find('Undefined cartesian SRS') >= 0, srs_wkt
assert lyr.GetSpatialRef().IsLocal()

gpkg_ds = None
gdal.Unlink('tmp/ogr_gpkg_srs_cartesian.gpkg')

###############################################################################
# Test maximum width of text fields

Expand Down
15 changes: 15 additions & 0 deletions gdal/ogr/ogrsf_frmts/gpkg/ogrgeopackagedatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ OGRSpatialReference* GDALGeoPackageDataset::GetSpatialRef(int iSrsId,
OGRSpatialReference *poSpatialRef = new OGRSpatialReference();
poSpatialRef->SetAxisMappingStrategy( OAMS_TRADITIONAL_GIS_ORDER );

// See corresponding tests in GDALGeoPackageDataset::GetSrsId
if( iSrsId == 0)
{
poSpatialRef->SetGeogCS("Undefined geographic SRS",
Expand Down Expand Up @@ -575,6 +576,20 @@ int GDALGeoPackageDataset::GetSrsId(const OGRSpatialReference& oSRS)
{
std::unique_ptr<OGRSpatialReference> poSRS(oSRS.Clone());

if (poSRS->IsGeographic() || poSRS->IsLocal())
{
// See corresponding tests in GDALGeoPackageDataset::GetSpatialRef
const char* pszName = poSRS->GetName();
if ( pszName != nullptr && strlen(pszName) > 0 )
{
if (EQUAL(pszName, "Undefined geographic SRS"))
return 0;

if (EQUAL(pszName, "Undefined cartesian SRS"))
return -1;
}
}

const char* pszAuthorityName = poSRS->GetAuthorityName( nullptr );

if ( pszAuthorityName == nullptr || strlen(pszAuthorityName) == 0 )
Expand Down

0 comments on commit 69aebed

Please sign in to comment.