Skip to content
Permalink
Browse files

Merge pull request #38763 from elpaso/bugfix-gh38567-pg-unrestricted-…

…geometry-srid

PG unrestricted geometry: trust SRID from geometry_columns
  • Loading branch information
elpaso committed Sep 16, 2020
2 parents f6c98ec + 235864f commit 49037e050a4eef07f0e527e52f0d20ea6f4d9ca9
@@ -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" )
@@ -3857,7 +3857,7 @@ bool QgsPostgresProvider::getGeometryDetails()
}

QString detectedType;
QString detectedSrid = mRequestedSrid;
QString detectedSrid;
if ( !schemaName.isEmpty() )
{
// check geometry columns
@@ -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" );
@@ -4047,7 +4053,6 @@ bool QgsPostgresProvider::getGeometryDetails()

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

QgsPostgresLayerProperty layerProperty;
if ( !mIsQuery )
@@ -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 ) );
}
@@ -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()
@@ -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.
You can’t perform that action at this time.