def test_gdal_calculations_py_17(): try: from gdal_calculations import NewDataset, ArrayDataset, Dataset, Env Env.tempdir='/vsimem' #Regular raster f='data/tgc_geo.tif' dsf=Dataset(f) dat=dsf.ReadAsArray() dsn=NewDataset('/vsimem/test.tif',prototype_ds=dsf) dsn.write_data(dat) ok=(dsn.ReadAsArray())==(dsf.ReadAsArray()) assert ok.all(), "dsn.ReadAsArray()!=dsf.ReadAsArray()" assert dsn.RasterXSize==dsf.RasterXSize, "dsn.RasterXSize!=dsf.RasterXSize" assert dsn.RasterYSize==dsf.RasterYSize, "dsn.RasterYSize!=dsf.RasterYSize" assert dsn.GetGeoTransform()==dsf.GetGeoTransform(), "dsn.GetGeoTransform()!=dsf.GetGeoTransform()" assert dsn.GetProjection()==dsf.GetProjection(), "dsn.GetProjection()!=dsf.GetProjection()" dsa=ArrayDataset(dat,prototype_ds=dsf) ok=(dsa.ReadAsArray())==(dsf.ReadAsArray()) assert ok.all(), "dsa.ReadAsArray()!=dsf.ReadAsArray()" assert dsa.RasterXSize==dsf.RasterXSize, "dsa.RasterXSize!=dsf.RasterXSize" assert dsa.RasterYSize==dsf.RasterYSize, "dsa.RasterYSize!=dsf.RasterYSize" assert dsa.GetGeoTransform()==dsf.GetGeoTransform(), "dsa.GetGeoTransform()!=dsf.GetGeoTransform()" assert dsa.GetProjection()==dsf.GetProjection(), "dsa.GetProjection()!=dsf.GetProjection()" return 'success' except AssertionError: return fail() finally: cleanup()
def test_gdal_calculations_py_4(): ''' Test dataset reads and getattr calls ''' try: from gdal_calculations import Env,Dataset Env.tiled=True f='data/tgc_geo.tif' dsf=Dataset(f) #ReadAsArray() (also a __getattr__ gdal.Dataset method) data=dsf.ReadAsArray() assert data.shape==(100,100), "data.shape==%s"%repr(data.shape) data=None #ReadBlocksAsArray() (gdal_calculations.Dataset method) for block in dsf.ReadBlocksAsArray(): assert block.data.shape==(40,100), "data.shape==%s"%repr(block.data.shape) break del block #__getattr__ np attribute assert dsf.shape==(40,100), "dsf.shape==%s"%repr(dsf.shape) #ReadBlocksAsArray() (gdal_calculations.Dataset method) Env.ntiles=2 for block in dsf.ReadBlocksAsArray(): assert block.data.shape==(80,100), "data.shape==%s"%repr(block.data.shape) break del block Env.ntiles=1 #__getattr__ np method that doesn't return a temp dataset Env.tiled=False s=dsf.sum() assert s==50005000, "dsf.sum()==%s"%repr(s) s=dsf.max() assert s==10000, "dsf.max()==%s"%repr(s) #__getattr__ np method that returns a temp dataset Env.tiled=True s=dsf.astype(np.int32) assert isinstance(s,Dataset), "isinstance(%s,Dataset)!=True"%repr(s) del s #__getattr__ gdal.Dataset method gt=dsf.GetGeoTransform() assert gt==(147.5, 0.01, 0.0, -34.5, 0.0, -0.01), "dsf.GetGeoTransform()==%s"%repr(gt) del gt Env.tiled=True dsf=None return 'success' except ImportError: return 'skip' except AssertionError: return fail() finally:cleanup()
def test_gdal_calculations_py_18(): try: from gdal_calculations import NewDataset, ArrayDataset, Dataset, Env Env.tempdir = '/vsimem' #Regular raster f = 'data/tgc_geo.tif' dsf = Dataset(f) dat = dsf.ReadAsArray() dsn = NewDataset('/vsimem/test.tif', prototype_ds=dsf) dsn.set_nodata_value(123) dsn.write_data(dat) del dsn dsn = Dataset('/vsimem/test.tif') assert dsn.nodata == [123], "dsn.nodata != [123]" assert dsn[0].GetNoDataValue() == 123, "dsn[0].GetNoDataValue() != 123" return 'success' except AssertionError: return fail() finally: cleanup()
def test_gdal_calculations_py_6(): ''' Test some arithmetic ''' try: from gdal_calculations import Dataset, Int16 olderr = np.seterr(**{'all': 'ignore'}) #Regular raster f = 'data/tgc_geo.tif' dsf = Dataset(f) #original values assert dsf.ReadAsArray(0, 0, 1, 1) == 1, "dsf.ReadAsArray(0, 0, 1, 1)!=1" assert dsf.ReadAsArray(1, 0, 1, 1) == 2, "dsf.ReadAsArray(1, 0, 1, 1)!=2" assert dsf.ReadAsArray(55, 2, 1, 1) == 256, "dsf.ReadAsArray(55,2,1,1)!=256" #=========================================================================== #Arithmetic operations #=========================================================================== #__add__ val = (dsf + 1).ReadAsArray(0, 0, 1, 1) assert val == 2, "(dsf+1).ReadAsArray(0, 0, 1, 1)==%s" % repr(val) #__sub__ and __rsub__ val = (dsf - 1).ReadAsArray(0, 0, 1, 1) assert val == 0, "(dsf-1).ReadAsArray(0, 0, 1, 1)==%s" % repr(val) val = (10 - dsf).ReadAsArray(0, 0, 1, 1) assert val == 9, "(10-dsf).ReadAsArray(0, 0, 1, 1)==%s" % repr(val) #__mul__ val = (dsf * 2).ReadAsArray(0, 0, 1, 1) assert val == 2, "(dsf*2).ReadAsArray(0, 0, 1, 1)==%s" % repr(val) #__div__ and __rdiv__ val = (dsf / 2).ReadAsArray(0, 0, 1, 1) assert val == 0, "(dsf/2).ReadAsArray(0, 0, 1, 1)==%s" % repr(val) val = (2 / dsf).ReadAsArray(0, 0, 1, 1) assert val == 2, "(2/dsf).ReadAsArray(0, 0, 1, 1)==%s" % repr(val) #__truediv__ ##Can't do `from __future__ import division` except at the top of the script import __future__ expr = compile('(dsf/2).ReadAsArray(0, 0, 1, 1)', '', 'eval', __future__.CO_FUTURE_DIVISION) val = eval(expr) assert val == 0.5, "(dsf/2).ReadAsArray(0, 0, 1, 1)==%s" % repr(val) expr = compile('(2/dsf).ReadAsArray(0, 0, 1, 1)', '', 'eval', __future__.CO_FUTURE_DIVISION) val = eval(expr) assert val == 2, "(2/dsf).ReadAsArray(0, 0, 1, 1)==%s" % repr(val) #__floordiv__ and __rfloordiv__ val = (dsf // 2).ReadAsArray(0, 0, 1, 1) assert val == 0, "(dsf//2).ReadAsArray(0, 0, 1, 1)==%s" % repr(val) val = (2 // dsf).ReadAsArray(0, 0, 1, 1) assert val == 2, "(2//dsf).ReadAsArray(0, 0, 1, 1)==%s" % repr(val) #__mod__ and __rmod__ val = (dsf % 1.5).ReadAsArray(1, 0, 1, 1) assert val == 0.5, "(dsf % 1.5).ReadAsArray(1, 0, 1, 1)==%s" % repr( val) val = (1.5 % dsf).ReadAsArray(1, 0, 1, 1) assert val == 1.5, "(dsf % 1.5).ReadAsArray(1, 0, 1, 1)==%s" % repr( val) #__pow__ and __rpow__ val = (dsf**2).ReadAsArray(2, 0, 1, 1) assert val == 9, "(dsf ** 2).ReadAsArray(2, 0, 1, 1)==%s" % repr(val) val = (2**dsf).ReadAsArray(2, 0, 1, 1) assert val == 8, "(2 ** dsf).ReadAsArray(2, 0, 1, 1)==%s" % repr(val) #__neg__ val = -dsf.ReadAsArray(0, 0, 1, 1) assert val == 65535, "-dsf.ReadAsArray(0, 0, 1, 1)==%s" % repr(val) val = -Int16(dsf).ReadAsArray(0, 0, 1, 1) assert val == -1, "-Int16(dsf).ReadAsArray(0, 0, 1, 1)==%s" % repr(val) #=========================================================================== #Bitwise operations #=========================================================================== #__and__ and __rand__ val = (dsf & 3).ReadAsArray(4, 0, 1, 1) assert val == 1, "(dsf&3).ReadAsArray(4,0,1,1)==%s" % repr(val) val = (3 & dsf).ReadAsArray(4, 0, 1, 1) assert val == 1, "(3&dsf).ReadAsArray(4,0,1,1)==%s" % repr(val) #__or__ and __ror__ val = (dsf & 3).ReadAsArray(4, 0, 1, 1) assert val == 1, "(dsf&3).ReadAsArray(4,0,1,1)==%s" % repr(val) val = (3 & dsf).ReadAsArray(4, 0, 1, 1) assert val == 1, "(3&dsf).ReadAsArray(4,0,1,1)==%s" % repr(val) #__lshift__ and __rlshift__ val = (dsf << 2).ReadAsArray(2, 0, 1, 1) assert val == 12, "(dsf<<2).ReadAsArray(2, 0, 1, 1)==%s" % repr(val) val = (2 << dsf).ReadAsArray(2, 0, 1, 1) assert val == 16, "(dsf<<2).ReadAsArray(2, 0, 1, 1)==%s" % repr(val) #__rshift__ and __rrshift__ val = (dsf >> 2).ReadAsArray(55, 2, 1, 1) assert val == 64, "(dsf>>2).ReadAsArray(55,2,1,1)==%s" % repr(val) val = (256 >> dsf).ReadAsArray(1, 0, 1, 1) assert val == 64, "(dsf>>2).ReadAsArray(1,0,1,1)==%s" % repr(val) #__xor__ and __rxor__ val = (dsf ^ 3).ReadAsArray(4, 0, 1, 1) assert val == 6, "(dsf^3).ReadAsArray(4,0,1,1)==%s" % repr(val) val = (3 ^ dsf).ReadAsArray(4, 0, 1, 1) assert val == 6, "(3^dsf).ReadAsArray(4,0,1,1)==%s" % repr(val) #=========================================================================== #Boolean operations #=========================================================================== #__lt__ val = (dsf < 2).ReadAsArray(0, 0, 2, 1) assert (val[0][0], val[0][1]) == ( 1, 0), "(dsf<2).ReadAsArray(0,0,2,1)==%s" % repr(val) val = (1 < dsf).ReadAsArray(0, 0, 2, 1) assert (val[0][0], val[0][1]) == ( 0, 1), "(1<dsf).ReadAsArray(0,0,2,1)==%s" % repr(val) #__le__ val = (dsf <= 1).ReadAsArray(0, 0, 2, 1) assert (val[0][0], val[0][1]) == ( 1, 0), "(dsf<=1).ReadAsArray(0,0,2,1)==%s" % repr(val) val = (2 <= dsf).ReadAsArray(0, 0, 2, 1) assert (val[0][0], val[0][1]) == ( 0, 1), "(2<=dsf).ReadAsArray(0,0,2,1)==%s" % repr(val) #__eq__ val = (dsf == 1).ReadAsArray(0, 0, 2, 1) assert (val[0][0], val[0][1]) == ( 1, 0), "(dsf==1).ReadAsArray(0,0,2,1)==%s" % repr(val) val = (1 == dsf).ReadAsArray(0, 0, 2, 1) assert (val[0][0], val[0][1]) == ( 1, 0), "(1==dsf).ReadAsArray(0,0,2,1)==%s" % repr(val) #__ne__ val = (dsf != 1).ReadAsArray(0, 0, 2, 1) assert (val[0][0], val[0][1]) == ( 0, 1), "(dsf==1).ReadAsArray(0,0,2,1)==%s" % repr(val) val = (1 != dsf).ReadAsArray(0, 0, 2, 1) assert (val[0][0], val[0][1]) == ( 0, 1), "(1==dsf).ReadAsArray(0,0,2,1)==%s" % repr(val) #__gt__ val = (dsf > 1).ReadAsArray(0, 0, 2, 1) assert (val[0][0], val[0][1]) == ( 0, 1), "(dsf>1).ReadAsArray(0,0,2,1)==%s" % repr(val) val = (2 > dsf).ReadAsArray(0, 0, 2, 1) assert (val[0][0], val[0][1]) == ( 1, 0), "(2>dsf).ReadAsArray(0,0,2,1)==%s" % repr(val) #__ge__ val = (dsf >= 2).ReadAsArray(0, 0, 2, 1) assert (val[0][0], val[0][1]) == ( 0, 1), "(dsf>=2).ReadAsArray(0,0,2,1)==%s" % repr(val) val = (1 >= dsf).ReadAsArray(0, 0, 2, 1) assert (val[0][0], val[0][1]) == ( 1, 0), "(1>=dsf).ReadAsArray(0,0,2,1)==%s" % repr(val) dsf = None return 'success' except ImportError: return 'skip' except AssertionError: return fail() finally: try: np.seterr(**olderr) except: pass cleanup() cleanup('__future__')