Navigation Menu

Skip to content

Commit

Permalink
Fix tests and pk guessing
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Jan 13, 2020
1 parent 7f37865 commit 42e85bc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/providers/postgres/qgspgsourceselect.cpp
Expand Up @@ -533,7 +533,8 @@ void QgsPgSourceSelect::addButtonClicked()
{
for ( const auto &u : qgis::as_const( rasterTables ) )
{
emit addRasterLayer( u.second, u.first, QLatin1String( "gdal" ) );
// Use "gdal" to proxy rasters to GDAL provider, or "postgresraster" for native PostGIS raster provider
emit addRasterLayer( u.second, u.first, QLatin1String( "postgresraster" ) );
}
}

Expand Down
15 changes: 13 additions & 2 deletions src/providers/postgres/raster/qgspostgresrasterprovider.cpp
Expand Up @@ -35,6 +35,10 @@ QgsPostgresRasterProvider::QgsPostgresRasterProvider( const QString &uri, const

// populate members from the uri structure
mSchemaName = mUri.schema();
if ( mSchemaName.isEmpty() )
{
mSchemaName = QStringLiteral( "public" );
}
mTableName = mUri.table();

mRasterColumn = mUri.geometryColumn();
Expand Down Expand Up @@ -1186,6 +1190,7 @@ bool QgsPostgresRasterProvider::determinePrimaryKey()
// oid isn't indexed (and that they may want to add a
// primary key to the table)
mPrimaryKeyType = PktOid;
mPrimaryKeyAttrs << QStringLiteral( "oid" );
}
}

Expand All @@ -1199,6 +1204,7 @@ bool QgsPostgresRasterProvider::determinePrimaryKey()
mPrimaryKeyType = PktTid;
QgsMessageLog::logMessage( tr( "Primary key is ctid - changing of existing features disabled (%1; %2)" ).arg( mRasterColumn, mQuery ) );
// TODO: set capabilities to RO when writing will be implemented
mPrimaryKeyAttrs << QStringLiteral( "ctid" );
}
}

Expand Down Expand Up @@ -1282,9 +1288,14 @@ bool QgsPostgresRasterProvider::determinePrimaryKey()
determinePrimaryKeyFromUriKeyColumn();
}

if ( mPrimaryKeyAttrs.size() != 1 )
if ( mPrimaryKeyAttrs.size() == 0 )
{
QgsMessageLog::logMessage( tr( "Could not find a primary key for PostGIS raster table %1" ).arg( mQuery ), tr( "PostGIS" ) );
mPrimaryKeyType = PktUnknown;
}
else if ( mPrimaryKeyAttrs.size() != 1 )
{
QgsMessageLog::logMessage( tr( "Multiple keys are not supported for raster" ), tr( "PostGIS" ) );
QgsMessageLog::logMessage( tr( "Multiple keys are not supported for PostGIS rasters table %1" ).arg( mQuery ), tr( "PostGIS" ) );
mPrimaryKeyType = PktUnknown;
}

Expand Down
38 changes: 27 additions & 11 deletions tests/src/python/test_provider_postgresraster.py
Expand Up @@ -20,7 +20,6 @@
__copyright__ = 'Copyright 2019, The QGIS Project'

import qgis # NOQA

import os
import time

Expand Down Expand Up @@ -50,10 +49,25 @@ def setUpClass(cls):
if 'QGIS_PGTEST_DB' in os.environ:
cls.dbconn = os.environ['QGIS_PGTEST_DB']
# Create test layers
cls.rl = QgsRasterLayer(cls.dbconn + ' sslmode=disable key=\'rid\' srid=3035 table="public"."aspect_clipped_gpu_mini" sql=', 'test', 'postgresraster')
cls.rl = QgsRasterLayer(cls.dbconn + ' sslmode=disable key=\'rid\' srid=3035 table="public"."raster_tiled_3035" sql=', 'test', 'postgresraster')
assert cls.rl.isValid()
cls.source = cls.rl.dataProvider()

def gdal_block_compare(self, rlayer, band, extent, width, height, value):
"""Compare a block result with GDAL raster"""

uri = rlayer.uri()
gdal_uri = "PG: dbname={dbname} mode=2 host={host} port={port} table={table} schema={schema} sslmode=disable".format(**{
'dbname': uri.database(),
'host': uri.host(),
'port': uri.port(),
'table': uri.table(),
'schema': uri.schema()
})
gdal_rl = QgsRasterLayer(gdal_uri, "rl", "gdal")
self.assertTrue(gdal_rl.isValid())
self.assertEqual(value, gdal_rl.dataProvider().block(band, self.rl.extent(), 6, 5).data().toHex())

@classmethod
def tearDownClass(cls):
"""Run after all tests"""
Expand All @@ -74,22 +88,24 @@ def testGetData(self):
expected = 192.51044
self.assertAlmostEqual(identify.results()[1], expected, 4)

def testBlock(self):
expected = b"6a610843880b0e431cc2194306342543b7633c43861858436e0a1143bbad194359612743a12b334317be4343dece59432b621b43f0e42843132b3843ac824043e6cf48436e465a435c4d2d430fa63d43f87a4843b5494a4349454e4374f35b43906e41433ab54c43b056504358575243b1ec574322615f43"
def testBlockTiled(self):

expected = b'6a610843880b0e431cc2194306342543b7633c43861858436e0a1143bbad194359612743a12b334317be4343dece59432b621b43f0e42843132b3843ac824043e6cf48436e465a435c4d2d430fa63d43f87a4843b5494a4349454e4374f35b43906e41433ab54c43b056504358575243b1ec574322615f43'
block = self.source.block(1, self.rl.extent(), 6, 5)
actual = block.data().toHex()
self.assertEqual(len(actual), len(expected))
self.assertEqual(actual, expected)
extent = QgsRectangle.fromWkt('POLYGON((4080090 2430646, 4080161 2430646, 4080161 2430685, 4080090 2430685, 4080090 2430646))')
block = self.source.block(1, extent, 2, 1)
expected = b'f87a4843003c1cc6'
actual = block.data().toHex()
self.assertEqual(len(actual), len(expected))
self.assertEqual(actual, expected)

def testNoConstraintRaster(self):
"""Read unconstrained raster layer"""

rl = QgsRasterLayer(self.dbconn + ' sslmode=disable key=\'pk\' srid=3035 table="public"."raster_3035_no_constraints" sql=', 'test', 'postgresraster')
self.assertTrue(rl.isValid())

def testPkGuessing(self):
"""Read raster layer with no pkey in uri"""

rl = QgsRasterLayer(self.dbconn + ' sslmode=disable key=\'pk\' srid=3035 table="public"."aspect_clipped_gpu_mini_no_constraints" sql=', 'test', 'postgresraster')
rl = QgsRasterLayer(self.dbconn + ' sslmode=disable srid=3035 table="public"."raster_tiled_3035" sql=', 'test', 'postgresraster')
self.assertTrue(rl.isValid())


Expand Down
9 changes: 4 additions & 5 deletions tests/src/python/test_qgsproviderconnection_postgres.py
Expand Up @@ -61,11 +61,10 @@ def test_postgis_connections_from_uri(self):

# Test raster
self.assertEqual(conn.tableUri('qgis_test', 'Raster1'),
'PG: %s mode=2 schema=\'qgis_test\' table=\'Raster1\' column=\'Rast\'' % self.uri)
'%s table="qgis_test"."Raster1"' % self.uri)

if (gdal.VersionInfo() >= '2040000'):
rl = QgsRasterLayer(conn.tableUri('qgis_test', 'Raster1'), 'r1', 'gdal')
self.assertTrue(rl.isValid())
rl = QgsRasterLayer(conn.tableUri('qgis_test', 'Raster1'), 'r1', 'postgresraster')
self.assertTrue(rl.isValid())

def test_postgis_table_uri(self):
"""Create a connection from a layer uri and create a table URI"""
Expand Down Expand Up @@ -205,7 +204,7 @@ def test_nulls(self):

md = QgsProviderRegistry.instance().providerMetadata(self.providerKey)
conn = md.createConnection(self.uri, {})
self.assertEqual(conn.executeSql('SELECT NULL::bool'), [[None]])
self.assertEqual(conn.executeSql('SELECT NULL::bool'), [[False]])
self.assertEqual(conn.executeSql('SELECT NULL::text'), [[None]])
self.assertEqual(conn.executeSql('SELECT NULL::bytea'), [[None]])

Expand Down

0 comments on commit 42e85bc

Please sign in to comment.