Skip to content

Commit

Permalink
Merge pull request #38763 from elpaso/bugfix-gh38567-pg-unrestricted-…
Browse files Browse the repository at this point in the history
…geometry-srid

PG unrestricted geometry: trust SRID from geometry_columns
  • Loading branch information
elpaso committed Sep 16, 2020
2 parents f6c98ec + 235864f commit 49037e0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/providers/postgres/qgspostgresconn.cpp
Expand Up @@ -1848,7 +1848,7 @@ void QgsPostgresConn::retrieveLayerTypes( QVector<QgsPostgresLayerProperty *> &l
}
else // vectors
{
// our estimatation ignores that a where clause might restrict the feature type or srid
// our estimation ignores that a where clause might restrict the feature type or srid
if ( useEstimatedMetadata )
{
table = QStringLiteral( "(SELECT %1 FROM %2 WHERE %3%1 IS NOT NULL LIMIT %4) AS t" )
Expand Down
13 changes: 9 additions & 4 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -3857,7 +3857,7 @@ bool QgsPostgresProvider::getGeometryDetails()
}

QString detectedType;
QString detectedSrid = mRequestedSrid;
QString detectedSrid;
if ( !schemaName.isEmpty() )
{
// check geometry columns
Expand All @@ -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
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()
4 changes: 2 additions & 2 deletions tests/testdata/provider/testdata_pg.sql
Expand Up @@ -714,10 +714,10 @@ CREATE TABLE test_table_default_values (

CREATE TABLE IF NOT EXISTS "test_check_constraint" (
"id" SERIAL PRIMARY KEY,
"geom" geometry(POINT),
"geom" geometry(POINT, 4326),
"i_will_fail_on_no_name" TEXT CHECK ("i_will_fail_on_no_name" != 'no name')
);

INSERT INTO test_check_constraint (geom, i_will_fail_on_no_name)
VALUES ('POINT(9 45)'::geometry, 'I have a name'), ('POINT(10 46)'::geometry, 'I have a name too');
VALUES ('SRID=4326;POINT(9 45)'::geometry, 'I have a name'), ('SRID=4326;POINT(10 46)'::geometry, 'I have a name too');

0 comments on commit 49037e0

Please sign in to comment.