def test_pixel_coordinates(self): dataset = Dataset(inputfile=self.inputfile) spatial_ref = dataset.GetSpatialReference() # Upper-left corner coords = dataset.PixelCoordinates(0, 0) self.assertAlmostEqual(coords.x, -spatial_ref.GetMajorCircumference() / 2, places=2) self.assertAlmostEqual(coords.y, spatial_ref.GetMinorCircumference() / 2, places=2) # Bottom-right corner coords = dataset.PixelCoordinates(dataset.RasterXSize, dataset.RasterYSize) self.assertAlmostEqual(coords.x, spatial_ref.GetMajorCircumference() / 2, places=2) self.assertAlmostEqual(coords.y, -spatial_ref.GetMinorCircumference() / 2, places=2) # Out of bounds self.assertRaises(ValueError, dataset.PixelCoordinates, -1, 0) self.assertRaises(ValueError, dataset.PixelCoordinates, dataset.RasterXSize, dataset.RasterYSize + 1)
def test_minimum_value(self): self.assertEqual( Dataset(self.float32file).GetRasterBand(1).MinimumValue, float('-inf')) self.assertEqual( Dataset(self.int16file).GetRasterBand(1).MinimumValue, -(1 << 15)) self.assertEqual( Dataset(self.uint8file).GetRasterBand(1).MinimumValue, 0)
def test_delete_dataset(self): dataset = Dataset(self.float32file) band = dataset.GetRasterBand(1) # Delete the dataset del dataset # Band should still be valid self.assertEqual(band.GetNoDataValue(), None)
def test_get_world_tms_extents_wgs84(self): dataset = Dataset(self.wgs84file) # Resolution 0, WGS 84 projection, there are two tiles, one for # longitudinal hemisphere transform = dataset.GetCoordinateTransformation( dst_ref=SpatialReference(osr.SRS_WKT_WGS84)) self.assertExtentsEqual( dataset.GetWorldTmsExtents(transform=transform), dataset.GetTmsExtents(transform=transform))
def test_get_world_tms_extents_partial(self): world = Dataset(self.inputfile) aligned = Dataset(self.alignedfile) spanning = Dataset(self.spanningfile) # The whole world should always match the self.inputfile's extents self.assertExtentsEqual(aligned.GetWorldTmsExtents(), world.GetTmsExtents()) self.assertExtentsEqual(spanning.GetWorldTmsExtents(), world.GetTmsExtents())
def test_get_extents_wgs84(self): dataset = Dataset(inputfile=self.inputfile) transform = dataset.GetCoordinateTransformation( dst_ref=SpatialReference(osr.SRS_WKT_WGS84)) ll, ur = dataset.GetExtents(transform=transform) self.assertAlmostEqual(ll.x, -180.0, places=0) self.assertAlmostEqual(ll.y, -85.0, places=0) self.assertAlmostEqual(ur.x, 180.0, places=0) self.assertAlmostEqual(ur.y, 85.0, places=0)
def test_numpy_data_type(self): self.assertEqual( Dataset(self.float32file).GetRasterBand(1).NumPyDataType, numpy.float32) self.assertEqual( Dataset(self.int16file).GetRasterBand(1).NumPyDataType, numpy.int16) self.assertEqual( Dataset(self.uint8file).GetRasterBand(1).NumPyDataType, numpy.uint8)
def test_get_tms_extents(self): dataset = Dataset(self.inputfile) # The whole world goes from 0 to 3 in both dimensions self.assertExtentsEqual( dataset.GetTmsExtents(), Extents(lower_left=XY(0, 0), upper_right=XY(4, 4))) # At resolution 0, there's only one tile self.assertExtentsEqual( dataset.GetTmsExtents(resolution=0), Extents(lower_left=XY(0, 0), upper_right=XY(1, 1)))
def test_get_tms_extents_aligned(self): dataset = Dataset(self.alignedfile) # At native resolution, should only occupy its own tile self.assertExtentsEqual( dataset.GetTmsExtents(), Extents(lower_left=XY(1, 1), upper_right=XY(2, 2))) # At resolution 1, should only occupy lower-left quadrant self.assertExtentsEqual( dataset.GetTmsExtents(resolution=1), Extents(lower_left=XY(0, 0), upper_right=XY(1, 1)))
def test_get_extents(self): dataset = Dataset(inputfile=self.inputfile) mercator = dataset.GetSpatialReference() major_half_circumference = mercator.GetMajorCircumference() / 2 minor_half_circumference = mercator.GetMinorCircumference() / 2 ll, ur = dataset.GetExtents() self.assertAlmostEqual(ll.x, -major_half_circumference, places=0) self.assertAlmostEqual(ll.y, -minor_half_circumference, places=0) self.assertAlmostEqual(ur.x, major_half_circumference, places=0) self.assertAlmostEqual(ur.y, minor_half_circumference, places=0)
def test_get_extents_partial_aligned(self): dataset = Dataset(inputfile=self.alignedfile) mercator = dataset.GetSpatialReference() major_circumference = mercator.GetMajorCircumference() minor_circumference = mercator.GetMinorCircumference() ll, ur = dataset.GetExtents() self.assertAlmostEqual(ll.x, -major_circumference / 4, places=0) self.assertAlmostEqual(ll.y, -minor_circumference / 4, places=0) self.assertAlmostEqual(ur.x, 0.0, places=0) self.assertAlmostEqual(ur.y, 0.0, places=0)
def test_get_extents_partial_mercator(self): mercator = SpatialReference.FromEPSG(EPSG_WEB_MERCATOR) major_circumference = mercator.GetMajorCircumference() minor_circumference = mercator.GetMinorCircumference() dataset = Dataset(inputfile=self.alignedfile) ll, ur = dataset.GetExtents() self.assertAlmostEqual(ll.x, -major_circumference / 4, places=0) self.assertAlmostEqual(ll.y, -minor_circumference / 4, places=0) self.assertAlmostEqual(ur.x, 0, places=0) self.assertAlmostEqual(ur.y, 0, places=0)
def test_get_extents_mercator(self): mercator = SpatialReference.FromEPSG(EPSG_WEB_MERCATOR) major_half_circumference = mercator.GetMajorCircumference() / 2 minor_half_circumference = mercator.GetMinorCircumference() / 2 dataset = Dataset(inputfile=self.inputfile) transform = dataset.GetCoordinateTransformation(dst_ref=mercator) ll, ur = dataset.GetExtents(transform=transform) self.assertAlmostEqual(ll.x, -major_half_circumference, places=0) self.assertAlmostEqual(ll.y, -minor_half_circumference, places=0) self.assertAlmostEqual(ur.x, major_half_circumference, places=0) self.assertAlmostEqual(ur.y, minor_half_circumference, places=0)
def test_open(self): from osgeo.gdalconst import GA_Update # Valid self.assertTrue(Dataset(inputfile=self.inputfile)) self.assertTrue(Dataset(inputfile=self.inputfile, mode=GA_Update)) # Invalid self.assertRaises(GdalError, Dataset, inputfile='/dev/null') # Missing self.assertRaises(IOError, Dataset, inputfile='/dev/missing')
def test_get_extents_partial_wgs84(self): dataset = Dataset(inputfile=self.alignedfile) transform = dataset.GetCoordinateTransformation( dst_ref=SpatialReference(osr.SRS_WKT_WGS84)) ll, ur = dataset.GetExtents(transform=transform) # 66.5°S is due to the fact that the original file is in Mercator and # the southern latitudes take up more pixels in Mercator than in # WGS84. self.assertAlmostEqual(ll.x, -90.0, places=0) self.assertAlmostEqual(ll.y, -66.5, places=0) self.assertAlmostEqual(ur.x, 0.0, places=0) self.assertAlmostEqual(ur.y, 0.0, places=0)
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_simple(self): inputfile = os.path.join(__dir__, 'srtm.tif') with NamedTemporaryFile(suffix='.tif') as outputfile: preprocess(inputfile=inputfile, outputfile=outputfile.name) self.assertTrue(os.path.exists(outputfile.name)) self.assertTrue(os.stat(outputfile.name).st_size > 0) in_data = Dataset(inputfile) out_data = Dataset(outputfile.name) # Output size should match input size self.assertEqual(out_data.RasterXSize, in_data.RasterXSize) self.assertEqual(out_data.RasterYSize, in_data.RasterYSize) # No Data value never existed for band in range(1, out_data.RasterCount + 1): self.assertEqual( out_data.GetRasterBand(band).GetNoDataValue(), None)
def test_get_extents_partial_spanning(self): dataset = Dataset(inputfile=self.spanningfile) mercator = dataset.GetSpatialReference() major_half_circumference = mercator.GetMajorCircumference() / 2 minor_half_circumference = mercator.GetMinorCircumference() / 2 # Spanning file is 50 pixels in from alignment pixel_size = mercator.GetPixelDimensions( resolution=dataset.GetNativeResolution()) border = 50 * pixel_size.x ll, ur = dataset.GetExtents() self.assertAlmostEqual(ll.x, -major_half_circumference + border, places=0) self.assertAlmostEqual(ll.y, -minor_half_circumference + border, places=0) self.assertAlmostEqual(ur.x, 0.0 - border, places=0) self.assertAlmostEqual(ur.y, 0.0 - border, places=0)
def test_get_pixel_dimensions(self): # bluemarble.tif is a 1024 × 1024 whole-world map dataset = Dataset(inputfile=self.inputfile) self.assertEqual(dataset.GetPixelDimensions(), XY(x=39135.758476562499709, y=-39135.758476562499709)) # upsampling.tif is a 256 × 256 whole-world map dataset = Dataset(inputfile=self.upsamplingfile) self.assertEqual( dataset.GetPixelDimensions(), XY(x=156543.033906249998836, y=-156543.033906249998836)) # bluemarble-spanning-foreign.tif is a 154 × 154 partial map dataset = Dataset(inputfile=self.foreignfile) self.assertEqual( dataset.GetPixelDimensions(), XY(x=104362.022604166661040, y=-104362.022604166661040))
def test_get_nodata_value(self): band = Dataset(self.float32file).GetRasterBand(1) self.assertEqual(band.GetNoDataValue(), None) # No data band = Dataset(self.nodatafile).GetRasterBand(1) self.assertEqual(band.GetNoDataValue(), 10)
def test_get_pixel_dimensions(self): # bluemarble.tif is a 1024 × 1024 whole-world map dataset = Dataset(inputfile=self.inputfile) pixel_size = dataset.GetPixelDimensions() self.assertAlmostEqual(pixel_size.x, 39135.7584766) self.assertAlmostEqual(pixel_size.y, -39135.7584766) # upsampling.tif is a 256 × 256 whole-world map dataset = Dataset(inputfile=self.upsamplingfile) pixel_size = dataset.GetPixelDimensions() self.assertAlmostEqual(pixel_size.x, 156543.0339062) self.assertAlmostEqual(pixel_size.y, -156543.0339062) # bluemarble-spanning-foreign.tif is a 154 × 154 partial map dataset = Dataset(inputfile=self.foreignfile) pixel_size = dataset.GetPixelDimensions() self.assertAlmostEqual(pixel_size.x, 104362.0226042) self.assertAlmostEqual(pixel_size.y, -104362.0226042)
def test_get_tiled_extents_partial_spanning(self): dataset = Dataset(inputfile=self.spanningfile) mercator = SpatialReference.FromEPSG(EPSG_WEB_MERCATOR) major_circumference = mercator.GetMajorCircumference() minor_circumference = mercator.GetMinorCircumference() # Native resolution, source projection which is Mercator, spanning # tiles. This should be the south-western quadrant. ll, ur = dataset.GetTiledExtents() self.assertAlmostEqual(ll.x, -major_circumference / 2, places=0) self.assertAlmostEqual(ll.y, -minor_circumference / 2, places=0) self.assertAlmostEqual(ur.x, 0.0, places=0) self.assertAlmostEqual(ur.y, 0.0, places=0) # Resolution 1, source projection which is Mercator, spanning tiles. # This should be the south-western quadrant. ll, ur = dataset.GetTiledExtents(resolution=1) self.assertAlmostEqual(ll.x, -major_circumference / 2, places=0) self.assertAlmostEqual(ll.y, -minor_circumference / 2, places=0) self.assertAlmostEqual(ur.x, 0.0, places=0) self.assertAlmostEqual(ur.y, 0.0, places=0) # Resolution 5, source projection which is Mercator, spanning tiles. # This should be the south-western quadrant with a border of 32 pixels, # because the border of the dataset is 50 pixels. pixel_size = mercator.GetPixelDimensions( resolution=dataset.GetNativeResolution()) border = 32 * pixel_size.x ll, ur = dataset.GetTiledExtents(resolution=5) self.assertAlmostEqual(ll.x, -major_circumference / 2 + border, places=0) self.assertAlmostEqual(ll.y, -minor_circumference / 2 + border, places=0) self.assertAlmostEqual(ur.x, 0.0 - border, places=0) self.assertAlmostEqual(ur.y, 0.0 - border, places=0) # Resolution 0, WGS 84 projection, spanning tiles. This should be # the western hemisphere. ll, ur = dataset.GetTiledExtents( transform=dataset.GetCoordinateTransformation( dst_ref=SpatialReference(osr.SRS_WKT_WGS84)), resolution=0) self.assertAlmostEqual(ll.x, -180.0, places=0) self.assertAlmostEqual(ll.y, -90.0, places=0) self.assertAlmostEqual(ur.x, 0.0, places=0) self.assertAlmostEqual(ur.y, 90.0, places=0)
def test_get_world_tms_borders_aligned(self): # Aligned file should have borders marked as + below: # 3,3 # ++++ # ++++ # + ++ # ++++ # 0,0 dataset = Dataset(self.alignedfile) # At native resolution, every tile except for (1, 1) self.assertEqual( set(dataset.GetWorldTmsBorders()), set( XY(x, y) for x in range(0, 4) for y in range(0, 4) if (x, y) != (1, 1))) # At resolution 1, every tile except for (0, 0) self.assertEqual( set(dataset.GetWorldTmsBorders(resolution=1)), set( XY(x, y) for x in range(0, 2) for y in range(0, 2) if (x, y) != (0, 0)))
def test_spanning_partial(self): inputfile = os.path.join(__dir__, 'bluemarble-spanning-ll.tif') vrt = warp(inputfile) with NamedTemporaryFile(suffix='.tif') as tmpfile: outputfile = tmpfile.name vrt.render(outputfile=outputfile, compress='LZW') self.assertEqual( subprocess.call([GDALINFO, outputfile], stdout=open('/dev/null', 'w+')), 0) # Should be a 412×412 image out_data = Dataset(outputfile) self.assertEqual(out_data.RasterXSize, 412) self.assertEqual(out_data.RasterYSize, 412)
def test_get_world_tms_extents(self): # World extents are exactly the same as GetTmsExtents() for a # whole-world file. dataset = Dataset(self.inputfile) # The whole world goes from 0 to 3 in both dimensions self.assertExtentsEqual(dataset.GetWorldTmsExtents(), dataset.GetTmsExtents()) # At resolution 0, there's only one tile self.assertExtentsEqual(dataset.GetWorldTmsExtents(resolution=0), dataset.GetTmsExtents(resolution=0))
def test_nodata(self): inputfile = os.path.join(__dir__, 'srtm.nodata.tif') vrt = warp(inputfile=inputfile) with vrt.get_tempfile(suffix='.vrt') as outputfile: # No Data value must be the same as the input file's in_data = Dataset(inputfile) out_data = Dataset(outputfile.name) for band in range(1, out_data.RasterCount + 1): self.assertEqual( in_data.GetRasterBand(band).GetNoDataValue(), out_data.GetRasterBand(band).GetNoDataValue())
def test_get_tiled_extents_partial_aligned(self): dataset = Dataset(inputfile=self.alignedfile) mercator = SpatialReference.FromEPSG(EPSG_WEB_MERCATOR) major_circumference = mercator.GetMajorCircumference() minor_circumference = mercator.GetMinorCircumference() # Native resolution, source projection which is Mercator, already # aligned. ll, ur = dataset.GetTiledExtents() self.assertAlmostEqual(ll.x, -major_circumference / 4, places=0) self.assertAlmostEqual(ll.y, -minor_circumference / 4, places=0) self.assertAlmostEqual(ur.x, 0.0, places=0) self.assertAlmostEqual(ur.y, 0.0, places=0) # Resolution 1, source projection which is Mercator, already # aligned. This should be the south-western quadrant, because the tile # is the north-eastern section of that quadrant. ll, ur = dataset.GetTiledExtents(resolution=1) self.assertAlmostEqual(ll.x, -major_circumference / 2, places=0) self.assertAlmostEqual(ll.y, -minor_circumference / 2, places=0) self.assertAlmostEqual(ur.x, 0.0, places=0) self.assertAlmostEqual(ur.y, 0.0, places=0) # Native resolution, WGS 84 projection, already aligned ll, ur = dataset.GetTiledExtents( transform=dataset.GetCoordinateTransformation( dst_ref=SpatialReference(osr.SRS_WKT_WGS84))) self.assertAlmostEqual(ll.x, -90.0, places=0) self.assertAlmostEqual(ll.y, -90.0, places=0) self.assertAlmostEqual(ur.x, 0.0, places=0) self.assertAlmostEqual(ur.y, 0.0, places=0) # Resolution 0, WGS 84 projection, already aligned. This should be # the western hemisphere. ll, ur = dataset.GetTiledExtents( transform=dataset.GetCoordinateTransformation( dst_ref=SpatialReference(osr.SRS_WKT_WGS84)), resolution=0) self.assertAlmostEqual(ll.x, -180.0, places=0) self.assertAlmostEqual(ll.y, -90.0, places=0) self.assertAlmostEqual(ur.x, 0.0, places=0) self.assertAlmostEqual(ur.y, 90.0, places=0)
def test_get_tiled_extents(self): dataset = Dataset(inputfile=self.inputfile) mercator = SpatialReference.FromEPSG(EPSG_WEB_MERCATOR) major_half_circumference = mercator.GetMajorCircumference() / 2 minor_half_circumference = mercator.GetMinorCircumference() / 2 # Native resolution, source projection which is Mercator, already # aligned. ll, ur = dataset.GetTiledExtents() self.assertAlmostEqual(ll.x, -major_half_circumference, places=0) self.assertAlmostEqual(ll.y, -minor_half_circumference, places=0) self.assertAlmostEqual(ur.x, major_half_circumference, places=0) self.assertAlmostEqual(ur.y, minor_half_circumference, places=0) # Resolution 0, source projection which is Mercator, already # aligned. This is the same as above, because the dataset covers the # whole world. ll, ur = dataset.GetTiledExtents(resolution=0) self.assertAlmostEqual(ll.x, -major_half_circumference, places=0) self.assertAlmostEqual(ll.y, -minor_half_circumference, places=0) self.assertAlmostEqual(ur.x, major_half_circumference, places=0) self.assertAlmostEqual(ur.y, minor_half_circumference, places=0) # Native resolution, WGS 84 projection, already aligned ll, ur = dataset.GetTiledExtents( transform=dataset.GetCoordinateTransformation( dst_ref=SpatialReference(osr.SRS_WKT_WGS84))) self.assertAlmostEqual(ll.x, -180.0, places=0) self.assertAlmostEqual(ll.y, -90.0, places=0) self.assertAlmostEqual(ur.x, 180.0, places=0) self.assertAlmostEqual(ur.y, 90.0, places=0) # Resolution 0, WGS 84 projection, already aligned. This is the # same as above, because the dataset covers the whole world. ll, ur = dataset.GetTiledExtents( transform=dataset.GetCoordinateTransformation( dst_ref=SpatialReference(osr.SRS_WKT_WGS84)), resolution=0) self.assertAlmostEqual(ll.x, -180.0, places=0) self.assertAlmostEqual(ll.y, -90.0, places=0) self.assertAlmostEqual(ur.x, 180.0, places=0) self.assertAlmostEqual(ur.y, 90.0, places=0)
def test_nodata(self): inputfile = os.path.join(__dir__, 'srtm.nodata.tif') in_data = Dataset(inputfile) with NamedTemporaryFile(suffix='.tif') as outputfile: preprocess(inputfile=inputfile, outputfile=outputfile.name) self.assertTrue(os.path.exists(outputfile.name)) self.assertTrue(os.stat(outputfile.name).st_size > 0) # Output nodata value should be the same an input nodata value out_data = Dataset(outputfile.name) self.assertEqual(out_data.RasterCount, 1) # Only one output band self.assertEqual( out_data.GetRasterBand(1).GetNoDataValue(), in_data.GetRasterBand(1).GetNoDataValue())
def test_get_native_resolution(self): dataset = Dataset(inputfile=self.inputfile) # bluemarble.tif is a 1024×1024 image of the whole world self.assertEqual(dataset.GetNativeResolution(), 2) # Maximum self.assertEqual(dataset.GetNativeResolution(maximum=1), 1) self.assertEqual(dataset.GetNativeResolution(maximum=10), 2) # Transform into US survey feet sr = SpatialReference.FromEPSG(EPSG_WEB_MERCATOR) sr.ImportFromWkt(sr.ExportToWkt().replace( 'UNIT["metre",1,AUTHORITY["EPSG","9001"]]', 'UNIT["US survey foot",0.3048006096012192,AUTHORITY["EPSG","9003"]]' )) transform = dataset.GetCoordinateTransformation(dst_ref=sr) self.assertEqual(dataset.GetNativeResolution(transform=transform), 2 + int(round(log(3.28, 2)))) # 3.28 ft/m