Skip to content

Commit

Permalink
PG unrestricted geometry: trust SRID from geometry_columns
Browse files Browse the repository at this point in the history
Fixes qgis#38567 where the SRID was not detected if the table was empty.

Also: do not override the SRID passed explicitly in the data source
URI.

Note: the logic in the provider is becoming a bit convoluted, there
are too many corner cases, at least the test coverage is pretty
good.
  • Loading branch information
elpaso committed Sep 15, 2020
1 parent 39b78b4 commit 1560d9a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3874,6 +3874,12 @@ bool QgsPostgresProvider::getGeometryDetails()
{
detectedType = result.PQgetvalue( 0, 0 );

// Do not override the SRID if set in the data source URI
if ( detectedSrid.isEmpty() )
{
detectedSrid = result.PQgetvalue( 0, 1 );
}

QString dim = result.PQgetvalue( 0, 2 );
if ( dim == QLatin1String( "3" ) && !detectedType.endsWith( 'M' ) )
detectedType += QLatin1String( "Z" );
Expand Down Expand Up @@ -4047,7 +4053,6 @@ bool QgsPostgresProvider::getGeometryDetails()

if ( mDetectedGeomType == QgsWkbTypes::Unknown )
{
mDetectedSrid.clear();

QgsPostgresLayerProperty layerProperty;
if ( !mIsQuery )
Expand Down Expand Up @@ -4077,8 +4082,8 @@ bool QgsPostgresProvider::getGeometryDetails()

if ( layerProperty.size() == 0 )
{
// no data - so take what's requested
if ( mRequestedGeomType == QgsWkbTypes::Unknown || mRequestedSrid.isEmpty() )
// no data - so take what's requested/detected
if ( mRequestedGeomType == QgsWkbTypes::Unknown || mDetectedSrid.isEmpty() )
{
QgsMessageLog::logMessage( tr( "Geometry type and srid for empty column %1 of %2 undefined." ).arg( mGeometryColumn, mQuery ) );
}
Expand Down
12 changes: 12 additions & 0 deletions tests/src/python/test_provider_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -2972,6 +2972,18 @@ def testUnrestrictedGeometryType(self):
with self.assertRaises(StopIteration):
next(polygons.getFeatures())

# Test regression GH #38567 (no SRID requested in the data source URI)
# Cleanup if needed
conn.executeSql('DELETE FROM "qgis_test"."test_unrestricted_geometry" WHERE \'t\'')

points = QgsVectorLayer(self.dbconn + ' sslmode=disable key=\'gid\' type=POINT table="qgis_test"."test_unrestricted_geometry" (geom) sql=', 'test_points', 'postgres')
lines = QgsVectorLayer(self.dbconn + ' sslmode=disable key=\'gid\' type=LINESTRING table="qgis_test"."test_unrestricted_geometry" (geom) sql=', 'test_lines', 'postgres')
polygons = QgsVectorLayer(self.dbconn + ' sslmode=disable key=\'gid\' type=POLYGON table="qgis_test"."test_unrestricted_geometry" (geom) sql=', 'test_polygons', 'postgres')

self.assertTrue(points.isValid())
self.assertTrue(lines.isValid())
self.assertTrue(polygons.isValid())


if __name__ == '__main__':
unittest.main()

0 comments on commit 1560d9a

Please sign in to comment.