Skip to content
Permalink
Browse files

PG raster: tests and data for issue #36689

  • Loading branch information
elpaso committed May 28, 2020
1 parent 0496ce5 commit efbd3ad7decf1ae2579e0b0556cb43f3969641ba
@@ -60,7 +60,8 @@ def _load_test_table(cls, schemaname, tablename, basename=None):
with open(os.path.join(TEST_DATA_DIR, 'provider', 'postgresraster', basename + '.sql'), 'r') as f:
sql = f.read()
conn.executeSql(sql)
assert (tablename in [n.tableName() for n in conn.tables(schemaname)]), tablename + ' not found!'
assert (tablename in [n.tableName() for n in conn.tables(
schemaname)]), tablename + ' not found!'

@classmethod
def setUpClass(cls):
@@ -101,15 +102,17 @@ def gdal_block_compare(self, rlayer, band, extent, width, height, value):
})
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())
self.assertEqual(value, gdal_rl.dataProvider().block(
band, self.rl.extent(), 6, 5).data().toHex())

@classmethod
def tearDownClass(cls):
"""Run after all tests"""

def testExtent(self):
extent = self.rl.extent()
self.assertEqual(extent, QgsRectangle(4080050, 2430625, 4080200, 2430750))
self.assertEqual(extent, QgsRectangle(
4080050, 2430625, 4080200, 2430750))

def testSize(self):
self.assertEqual(self.source.xSize(), 6)
@@ -119,7 +122,8 @@ def testCrs(self):
self.assertEqual(self.source.crs().authid(), 'EPSG:3035')

def testGetData(self):
identify = self.source.identify(QgsPointXY(4080137.9, 2430687.9), QgsRaster.IdentifyFormatValue)
identify = self.source.identify(QgsPointXY(
4080137.9, 2430687.9), QgsRaster.IdentifyFormatValue)
expected = 192.51044
self.assertAlmostEqual(identify.results()[1], expected, 4)

@@ -214,13 +218,16 @@ def testSpeed(self):

def _speed_check(schema, table, width, height):
print('-' * 80)
print("Testing: {schema}.{table}".format(table=table, schema=schema))
print("Testing: {schema}.{table}".format(
table=table, schema=schema))
print('-' * 80)

# GDAL
start = time.time()
rl = QgsRasterLayer(
"PG: " + conn + "table={table} mode=2 schema={schema}".format(table=table, schema=schema), 'gdal_layer',
"PG: " + conn +
"table={table} mode=2 schema={schema}".format(
table=table, schema=schema), 'gdal_layer',
'gdal')
self.assertTrue(rl.isValid())
# Make is smaller than full extent
@@ -229,10 +236,12 @@ def _speed_check(schema, table, width, height):
print("Tiled GDAL start time: {:.6f}".format(checkpoint_1 - start))
rl.dataProvider().block(1, extent, width, height)
checkpoint_2 = time.time()
print("Tiled GDAL first block time: {:.6f}".format(checkpoint_2 - checkpoint_1))
print("Tiled GDAL first block time: {:.6f}".format(
checkpoint_2 - checkpoint_1))
# rl.dataProvider().block(1, extent, width, height)
checkpoint_3 = time.time()
print("Tiled GDAL second block time: {:.6f}".format(checkpoint_3 - checkpoint_2))
print("Tiled GDAL second block time: {:.6f}".format(
checkpoint_3 - checkpoint_2))
print("Total GDAL time: {:.6f}".format(checkpoint_3 - start))
print('-' * 80)

@@ -246,10 +255,12 @@ def _speed_check(schema, table, width, height):
print("Tiled PG start time: {:.6f}".format(checkpoint_1 - start))
rl.dataProvider().block(1, extent, width, height)
checkpoint_2 = time.time()
print("Tiled PG first block time: {:.6f}".format(checkpoint_2 - checkpoint_1))
print("Tiled PG first block time: {:.6f}".format(
checkpoint_2 - checkpoint_1))
rl.dataProvider().block(1, extent, width, height)
checkpoint_3 = time.time()
print("Tiled PG second block time: {:.6f}".format(checkpoint_3 - checkpoint_2))
print("Tiled PG second block time: {:.6f}".format(
checkpoint_3 - checkpoint_2))
print("Total PG time: {:.6f}".format(checkpoint_3 - start))
print('-' * 80)

@@ -260,7 +271,8 @@ def testOtherSchema(self):
See: GH #34823"""

rl = QgsRasterLayer(
self.dbconn + " sslmode=disable table={table} schema={schema}".format(table='cosmo_i5_snow', schema='idro'),
self.dbconn + " sslmode=disable table={table} schema={schema}".format(
table='cosmo_i5_snow', schema='idro'),
'pg_layer', 'postgresraster')
self.assertTrue(rl.isValid())
self.assertTrue(compareWkt(rl.extent().asWktPolygon(),
@@ -303,7 +315,8 @@ def testSetSubsetString(self):
data.append(int(block.value(i, j)))
self.assertEqual(data, [136, 142, 161, 169])

stats = rl.dataProvider().bandStatistics(1, QgsRasterBandStats.Min | QgsRasterBandStats.Max, rl.extent())
stats = rl.dataProvider().bandStatistics(
1, QgsRasterBandStats.Min | QgsRasterBandStats.Max, rl.extent())
self.assertEqual(int(stats.minimumValue), 136)
self.assertEqual(int(stats.maximumValue), 169)

@@ -321,7 +334,8 @@ def testSetSubsetString(self):
self.assertEqual(data, [136, 142, 145, 153])

# Check that we have new statistics
stats = rl.dataProvider().bandStatistics(1, QgsRasterBandStats.Min | QgsRasterBandStats.Max, rl.extent())
stats = rl.dataProvider().bandStatistics(
1, QgsRasterBandStats.Min | QgsRasterBandStats.Max, rl.extent())
self.assertEqual(int(stats.minimumValue), 136)
self.assertEqual(int(stats.maximumValue), 153)

@@ -449,6 +463,23 @@ def _round_trip(uri):
'username': 'my username',
})

def testInt16(self):
"""Test regression https://github.com/qgis/QGIS/issues/36689"""

rl = QgsRasterLayer(
self.dbconn + " sslmode=disable table={table} schema={schema}".format(
table='int16_regression_36689', schema='public'), 'pg_layer', 'postgresraster')

self.assertTrue(rl.isValid())
block = rl.dataProvider().block(1, rl.extent(), 6, 6)
data = []
for i in range(6):
for j in range(6):
data.append(int(block.value(i, j)))

self.assertEqual(data, [55, 52, 46, 39, 33, 30, 58, 54, 49, 45, 41, 37, 58, 54, 50,
47, 45, 43, 54, 51, 49, 47, 46, 44, 47, 47, 47, 47, 46, 45, 41, 43, 45, 48, 49, 46])


if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,14 @@
---------------------------
--
-- Test github issue 36689
--

BEGIN;
DROP TABLE IF EXISTS "int16_regression_36689";
CREATE TABLE "int16_regression_36689" ("rid" serial PRIMARY KEY,"rast" raster,"filename" text);
INSERT INTO "int16_regression_36689" ("rast","filename") VALUES ('0100000100555555555555C53F555555555555C5BF0000000000002240000000000000474000000000000000000000000000000000E610000006000600450080370034002E00270021001E003A00360031002D00290025003A00360032002F002D002B003600330031002F002E002C002F002F002F002F002E002D0029002B002D00300031002E00'::raster,'int16.tiff');
CREATE INDEX ON "int16_regression_36689" USING gist (st_convexhull("rast"));
ANALYZE "int16_regression_36689";
SELECT AddRasterConstraints('','int16_regression_36689','rast',TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,FALSE,TRUE,TRUE,TRUE,TRUE,TRUE);
END;
VACUUM ANALYZE "int16_regression_36689";

0 comments on commit efbd3ad

Please sign in to comment.
You can’t perform that action at this time.