def has_proj(): sr1 = osr.SpatialReference() sr1.ImportFromEPSG(4326) # lat, lon. sr2 = osr.SpatialReference() sr2.ImportFromEPSG(28355) # GDA94/MGA zone 55. osrex = osr.GetUseExceptions() osr.UseExceptions() hasproj = True # Use exceptions to determine if we have proj and epsg files # otherwise we can't reliably determine if it has failed. try: trans = osr.CoordinateTransformation(sr1, sr2) except RuntimeError: hasproj = False return hasproj
def test_gdal_calculations_py_2(): ''' Test environment getting/setting ''' try: from gdal_calculations import Env, Dataset from osgeo import gdal #Get/set cellsize assert Env.cellsize == "DEFAULT", "Env.cellsize != 'DEFAULT' ('%s')" % Env.cellsize Env.cellsize = "DEFAULT" Env.cellsize = "MINOF" Env.cellsize = "MAXOF" Env.cellsize = 1 Env.cellsize = [1, 1] try: Env.cellsize = "INCORRECT" except: pass else: return fail('Env.cellsize accepted an incorrect value') #Get/set extent assert Env.extent == "MINOF", "Env.extent != 'MINOF' ('%s')" % Env.extent Env.extent = "MINOF" Env.extent = "MAXOF" Env.extent = [1, 1, 2, 2] try: Env.extent = "INCORRECT" except: pass else: return fail('Env.extent accepted an incorrect value') #Get/set extent assert Env.nodata == False, "Env.nodata != False ('%s')" % Env.nodata Env.nodata = True Env.nodata = False #Get/set overwrite assert Env.overwrite == False, "Env.overwrite != False ('%s')" % Env.overwrite Env.overwrite = True Env.overwrite = False #Get/set reproject assert Env.reproject == False, "Env.reproject != False ('%s')" % Env.reproject Env.reproject = True Env.reproject = False #Get/set resampling assert Env.resampling == gdal.GRA_NearestNeighbour, 'Env.resampling != "NEAREST"' Env.resampling = "BILINEAR" Env.resampling = "CUBIC" Env.resampling = "CUBICSPLINE" Env.resampling = "LANCZOS" Env.resampling = "NEAREST" if int(gdal.VersionInfo("VERSION_NUM")) >= 1100000: Env.resampling = "AVERAGE" Env.resampling = "MODE" try: Env.resampling = "INCORRECT" except: pass else: return fail('Env.resampling accepted an incorrect value') #Get/set snap assert Env.snap is None, 'Env.snap should be None not %s' % repr( Env.snap) Env.snap = Dataset('data/tgc_geo.tif') assert isinstance(Env.snap, Dataset), 'Env.snap is not a Dataset' #Get/set srs epsg = 4283 wkt = 'GEOGCS["GDA94",DATUM["Geocentric_Datum_of_Australia_1994",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6283"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4283"]]' srs = osr.SpatialReference(wkt) assert Env.srs is None, 'Env.srs should be None' Env.srs = epsg assert isinstance( Env.srs, osr.SpatialReference), 'Env.srs is not a SpatialReference (EPSG)' Env.srs = wkt assert isinstance( Env.srs, osr.SpatialReference), 'Env.srs is not a SpatialReference (WKT)' Env.srs = srs assert isinstance( Env.srs, osr.SpatialReference ), 'Env.srs is not a SpatialReference (SpatialReference)' gue = osr.GetUseExceptions() osr.DontUseExceptions() try: Env.srs = 1 except: pass else: return fail('Env.srs accepted an invalid EPSG (1)') try: Env.srs = 'INCORRECT' except: pass else: return fail('Env.extent accepted an invalid WKT ("INCORRECT")') try: Env.srs = osr.SpatialReference('sdfsdfsdf') except: pass else: return fail('Env.extent accepted an invalid SpatialReference') if gue: osr.UseExceptions() else: osr.DontUseExceptions() #Get/set tempdir try: tempdir = tempfile.tempdir #use to reset tempfile.tempdir to default assert Env.tempdir == tempfile.tempdir, 'Env.tempdir != %s' % tempfile.tempdir Env.tempdir = 'tmp' #autotest/pyscripts/tmp assert tempfile.tempdir == 'tmp', 'tempfile.tempdir != tmp' try: Env.tempdir = "INCORRECT" except: pass else: gdaltest.post_reason('Env.tempdir accepted an incorrect value') return 'fail' finally: tempfile.tempdir = tempdir #Get/set tiled assert Env.tiled == True, "Env.tiled != True ('%s')" % Env.tiled Env.tiled = False Env.tiled = True return 'success' except ImportError: return 'skip' except AssertionError: return fail() finally: cleanup()