import sys

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

# Avoid the regressing from https://github.com/conda-forge/gdal-feedstock/pull/129
# See https://github.com/conda-forge/gdal-feedstock/issues/131
from osgeo.gdal_array import *

drivers = ['netCDF', 'HDF4', 'HDF5', 'GTiff', 'PNG', 'JPEG', 'GPKG', 'KEA', 'JP2OpenJPEG', 'WCS']
for driver in drivers:
    print(driver)
    assert gdal.GetDriverByName(driver)

drivers = ['GML', 'XLS', 'KML', 'SQLite', 'PostgreSQL']
for driver in drivers:
    print(driver)
    if sys.platform == 'darwin':
        print('Skipping driver test {} on OSX!'.format(driver))
    else:
        assert ogr.GetDriverByName(driver)

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'

# Test https://github.com/swig/swig/issues/567
def make_geom():
    geom = ogr.Geometry(ogr.wkbPoint)
    geom.AddPoint_2D(0, 0)
    return geom

def gen_list(N):
    for i in range(N):
        geom = make_geom()
        yield i

N = 10
assert list(gen_list(N)) == list(range(N))

# This module does some additional tests.
import extra_tests

@@ -1933,8 +1933,517 @@
 a_tests%0A
+%0A# Test international encoding.%0A# https://github.com/conda-forge/libgdal-feedstock/issues/32%0Afrom osgeo import ogr%0A%0Adriver = ogr.GetDriverByName(%22ESRI Shapefile%22)%0Ads = driver.CreateDataSource(%22test.shp%22)%0Alyr = ds.CreateLayer(%22test%22, options=%5B%22ENCODING=GB18030%22%5D)%0A%0Afield_defn = ogr.FieldDefn(%22myfield%22, ogr.OFTString)%0Alyr.CreateField(field_defn)%0A%0Alyr_defn = lyr.GetLayerDefn()%0Afeature = ogr.Feature(lyr_defn)%0A%0Afeature.SetField(%22myfield%22, %22%5Cu5f90%5Cu6c47%5Cu533a%22)%0A%0Alyr.CreateFeature(feature)%0A%0Alyr = None%0Ads = None%0A

Test issue https://github.com/conda-forge/libgdal-feedstock/issues/32