def test_upsample(self): # bluemarble-foreign.tif is a 500 × 250 whole-world map. dataset = VipsDataset(inputfile=self.foreignfile) dataset.resample(resolution=None) self.assertEqual(dataset.RasterXSize, dataset.image.width) self.assertEqual(dataset.RasterYSize, dataset.image.height) self.assertEqual(dataset.RasterXSize, 512) self.assertEqual(dataset.RasterYSize, 512)
def test_downsample(self): """ Test that a 258x258 file will get downsampled to 256x256 instead of upsampled to the next resolution. Because the pixel size is within error tolerance of the lower resolution's pixel size """ dataset = VipsDataset(inputfile=self.slightlytoobigfile) dataset.resample(resolution=None) self.assertEqual(dataset.RasterXSize, dataset.image.width) self.assertEqual(dataset.RasterYSize, dataset.image.height) self.assertEqual(dataset.RasterXSize, 256) self.assertEqual(dataset.RasterYSize, 256)
def test_readasarray(self): with LibVips.disable_warnings(): vips_ds = VipsDataset(inputfile=self.upsamplingfile) gdal_ds = Dataset(inputfile=self.upsamplingfile) # Reading the whole file self.assertEqual( vips_ds.ReadAsArray(xoff=0, yoff=0).all(), gdal_ds.ReadAsArray(xoff=0, yoff=0).all()) # Reading from an offset vips_data = vips_ds.ReadAsArray(xoff=128, yoff=128) gdal_data = gdal_ds.ReadAsArray(xoff=128, yoff=128, xsize=128, ysize=128) self.assertEqual(vips_data.all(), gdal_data.all()) vips_blue = vips_ds.GetRasterBand(3) gdal_blue = gdal_ds.GetRasterBand(3) # Reading the whole band self.assertEqual( vips_blue.ReadAsArray(xoff=0, yoff=0).all(), gdal_blue.ReadAsArray(xoff=0, yoff=0).all()) # Reading from an offset vips_band_data = vips_blue.ReadAsArray(xoff=128, yoff=128) gdal_band_data = gdal_blue.ReadAsArray(xoff=128, yoff=128, win_xsize=128, win_ysize=128) self.assertEqual(vips_band_data.all(), gdal_band_data.all()) # Test for errors self.assertRaises(ValueError, vips_ds.ReadAsArray, xoff=0, yoff=0, buf_obj=[]) self.assertRaises(ValueError, vips_blue.ReadAsArray, xoff=0, yoff=0, buf_xsize=1, buf_ysize=1)
def test_align_to_grid(self): with LibVips.disable_warnings(): # bluemarble.tif is a 1024 × 1024 whole-world map. dataset = VipsDataset(inputfile=self.inputfile) dataset.align_to_grid() self.assertEqual(dataset.image.width, 1024) self.assertEqual(dataset.image.height, 1024) self.assertEqual(dataset.RasterXSize, 1024) self.assertEqual(dataset.RasterYSize, 1024) self.assertExtentsEqual(dataset.GetExtents(), dataset.GetTiledExtents()) # bluemarble-foreign.tif is a 500 × 250 whole-world map. dataset = VipsDataset(inputfile=self.foreignfile) dataset.align_to_grid() self.assertEqual(dataset.image.width, 512) self.assertEqual(dataset.image.height, 512) self.assertEqual(dataset.RasterXSize, 512) self.assertEqual(dataset.RasterYSize, 512) self.assertEqual(dataset.GetExtents(), dataset.GetTiledExtents()) # bluemarble-spanning-foreign.tif is a 154 × 154 whole-world map. dataset = VipsDataset(inputfile=self.spanningforeignfile) dataset.align_to_grid() self.assertEqual(dataset.image.width, 256) self.assertEqual(dataset.image.height, 256) self.assertEqual(dataset.RasterXSize, 256) self.assertEqual(dataset.RasterYSize, 256) self.assertExtentsEqual(dataset.GetExtents(), dataset.GetTiledExtents()) # The upper-left corner should be transparent data = numpy.frombuffer(dataset.image.write_to_memory(), dtype=numpy.uint8) self.assertEqual(tuple(data[0:4]), rgba(0, 0, 0, 0))