def test_updateio(self): test_array = testArray((340, 270)) slices = slice(1, -1, 3) endings = ga._DRIVER_DICT.keys() for ending in endings: if ending != ".tif": continue with tempfile.NamedTemporaryFile(suffix=ending) as tf: test_array.tofile(tf.name) check_file = ga.fromfile(tf.name, "a") check_file[slices] = 42 check_file.close() check_file = ga.fromfile(tf.name, "r") self.assertTrue((check_file[slices] == 42).all())
def test_project(self): """ This test fails for gdal versions below 2.0. The warping is correct, but the void space around the original image is filled with fill_value in versions >= 2.0, else with 0. The tested function behaves like the more recent versions of GDAL """ codes = (2062, 3857) if gdal.VersionInfo().startswith("1"): warnings.warn( "Skipping incompatible warp test on GDAL versions < 2", RuntimeWarning) return for fname, base in zip(self.fnames, self.grids): # break if base.proj: for epsg in codes: # gdalwarp flips the warped image proj = ga.project(grid=base[::-1], proj={"init": "epsg:{:}".format(epsg)}, max_error=0) # proj = base[::-1].warp({"init":"epsg:{:}".format(epsg)}, 0) with tempfile.NamedTemporaryFile(suffix=".tif") as tf: subprocess.check_output( "gdalwarp -r 'near' -et 0 -t_srs 'EPSG:{:}' {:} {:}" .format(epsg, fname, tf.name), shell=True) compare = ga.fromfile(tf.name) self.assertTrue(np.all(proj.data == compare.data)) self.assertTrue(np.all(proj.mask == compare.mask)) self.assertDictEqual(proj.bbox, compare.bbox) else: self.assertRaises(AttributeError)
def createTestFiles(): createDirectory(TMPPATH) arrays = (testArray((340, 270)), testArray((4, 340, 270))) files, fnames = [], [] for ending in ga._DRIVER_DICT: for i, arr in enumerate(arrays): fname = os.path.join(TMPPATH, "test-{:}{:}".format(i, ending)) try: arr.tofile(fname) except RuntimeError: continue files.append(ga.fromfile(fname)) fnames.append(fname) return tuple(fnames), tuple(files)
def createTestFiles(): createDirectory(TMPPATH) arrays = ( testArray((340, 270)), testArray((4, 340, 270)) ) files, fnames = [], [] for ending in ga._DRIVER_DICT: for i, arr in enumerate(arrays): fname = os.path.join(TMPPATH, "test-{:}{:}".format(i, ending)) try: arr.tofile(fname) except RuntimeError: continue files.append(ga.fromfile(fname)) fnames.append(fname) return tuple(fnames), tuple(files)
def test_io(self): test_array = testArray((340, 270)) endings = ga._DRIVER_DICT.keys() for ending in endings: with tempfile.NamedTemporaryFile(suffix=ending) as tf: # write and read again test_array.tofile(tf.name) check_array = ga.fromfile(tf.name) # gdal truncates values smaller/larger than the datatype, numpy wraps around. # clip array to make things comparable. dinfo = dtypeInfo(check_array.dtype) grid = test_array.clip(dinfo["min"], dinfo["max"]) np.testing.assert_almost_equal(check_array, grid) self.assertDictEqual(check_array.bbox, test_array.bbox) self.assertEqual(check_array.cellsize, test_array.cellsize) self.assertEqual(check_array.proj, test_array.proj) self.assertEqual(check_array.fill_value, test_array.fill_value) self.assertEqual(check_array.mode, test_array.mode)
def test_project(self): """ This test fails for gdal versions below 2.0. The warping is correct, but the void space around the original image is filled with fill_value in versions >= 2.0, else with 0. The tested function behaves like the more recent versions of GDAL """ codes = (2062, 3857) if gdal.VersionInfo().startswith("1"): warnings.warn("Skipping incompatible warp test on GDAL versions < 2", RuntimeWarning) return for fname, base in zip(self.fnames, self.grids): # break if base.proj: for epsg in codes: # gdalwarp flips the warped image proj = ga.project( grid = base[::-1], proj = {"init":"epsg:{:}".format(epsg)}, max_error = 0 ) # proj = base[::-1].warp({"init":"epsg:{:}".format(epsg)}, 0) with tempfile.NamedTemporaryFile(suffix=".tif") as tf: subprocess.check_output( "gdalwarp -r 'near' -et 0 -t_srs 'EPSG:{:}' {:} {:}".format( epsg, fname, tf.name ), shell=True ) compare = ga.fromfile(tf.name) self.assertTrue(np.all(proj.data == compare.data)) self.assertTrue(np.all(proj.mask == compare.mask)) self.assertDictEqual(proj.bbox, compare.bbox) else: self.assertRaises(AttributeError)
def test_project(self): def assertGeoArrayEqual(ga1, ga2): self.assertTrue(np.all(ga1.data == ga1.data)) self.assertTrue(np.all(ga2.mask == ga2.mask)) self.assertDictEqual(ga1.bbox, ga2.bbox) self.assertEqual(ga1.proj, ga2.proj) epsgcodes = (32632, 32634) error = 0 cellsize = 1000 warpcmd = "gdalwarp -r 'near' -tr {cellsize} {cellsize} -et {error} -s_srs '{sproj}' -t_srs 'EPSG:{tproj}' {sfname} {tfname}" for fname, base in zip(self.fnames, self.grids): for epsgcode in epsgcodes: proj = ga.project(grid=base, proj={"init": "epsg:{:}".format(epsgcode)}, func="nearest", cellsize=cellsize, max_error=0) with tempfile.NamedTemporaryFile(suffix=".tif") as tf: subprocess.check_output(warpcmd.format(error=error, cellsize=cellsize, sproj=base.proj, tproj=epsgcode, sfname=fname, tfname=tf.name), shell=True) compare = ga.fromfile(tf.name).trim() try: assertGeoArrayEqual(proj, compare) except AssertionError: if fname.endswith("png"): # the fill_vaue is not set correctly with the png-driver compare.fill_value = 0 assertGeoArrayEqual(proj, compare.trim()) else: raise