import os

from osgeo import gdal
from osgeo import ogr
from osgeo import osr

# Set GDAL_DATA. This is done normally done by the activate script,
# but this doesn't happen in the testing environment
if 'LIBRARY_PREFIX' in os.environ:
    # Windows.
    gdalData = os.path.join(os.environ['LIBRARY_PREFIX'], 'share', 'gdal')
else:
    # Linux/OS X.
    gdalData = os.path.join(os.environ['PREFIX'], 'share', 'gdal')

os.environ['GDAL_DATA'] = gdalData

driver = gdal.GetDriverByName("netCDF")
assert driver is not None

driver = gdal.GetDriverByName("HDF4")
assert driver is not None

driver = gdal.GetDriverByName("HDF5")
assert driver is not None

driver = gdal.GetDriverByName("GTiff")
assert driver is not None

driver = gdal.GetDriverByName("PNG")
assert driver is not None

driver = gdal.GetDriverByName("JPEG")
assert driver is not None

driver = gdal.GetDriverByName("GPKG")
assert driver is not None

# only available when libkea successfully linked in.
driver = gdal.GetDriverByName("KEA")
assert driver is not None

# only available when xerces-c++ successfully linked in.
driver = ogr.GetDriverByName("GML")
assert driver is not None

# only available when openjpeg successfully linked in.
driver = gdal.GetDriverByName("JP2OpenJPEG")
assert driver is not None

# only available when curl successfully linked in.
driver = gdal.GetDriverByName("WCS")
assert driver is not None

# only available when freexl successfully linked in.
driver = ogr.GetDriverByName("XLS")
assert driver is not None

# only available when expat successfully linked in.
driver = ogr.GetDriverByName("KML")
assert driver is not None

# only available when SQLite successfully linked in.
driver = ogr.GetDriverByName("SQLite")
assert driver is not None

# only available when PostgreSQL successfully linked in.
driver = ogr.GetDriverByName("PostgreSQL")
assert driver is not None

def has_geos():
    pnt1 = ogr.CreateGeometryFromWkt( 'POINT(10 20)' )
    pnt2 = ogr.CreateGeometryFromWkt( 'POINT(30 20)' )
    ogrex = ogr.GetUseExceptions()
    ogr.DontUseExceptions()
    hasgeos = pnt1.Union( pnt2 ) is not None
    if ogrex:
        ogr.UseExceptions()
    return hasgeos

assert has_geos(), 'GEOS not available within GDAL'

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

assert has_proj(), 'PROJ not available within GDAL'

# This module does some additional tests.
import extra_tests

@@ -2773,16 +2773,297 @@
  GDAL'%0A%0A
+# Test https://github.com/swig/swig/issues/567%0Adef make_geom():%0A    geom = ogr.Geometry(ogr.wkbPoint)%0A    geom.AddPoint_2D(0, 0)%0A    return geom%0A%0Adef gen_list(N):%0A    for i in range(N):%0A        geom = make_geom()%0A        yield i%0A%0AN = 10%0Aassert list(gen_list(N)) == list(range(N))%0A%0A
 # This m

add test