Commit 082b2088 authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

Fixed gdal v2.4.0 incompatibility and added tests for io.raster.gdal.

parent 4a399041
Pipeline #3612 passed with stages
in 2 minutes and 22 seconds
...@@ -23,7 +23,7 @@ gdal_env.try2set_GDAL_DATA() ...@@ -23,7 +23,7 @@ gdal_env.try2set_GDAL_DATA()
def get_proj4info(ds=None, proj=None): def get_proj4info(ds=None, proj=None):
# type: (gdal.Dataset, Union[str, int]) -> str # type: (gdal.Dataset, Union[str, int]) -> str
"""Returns PROJ4 formatted projection info for the given gdal.Dataset or projection respectivly, """Returns PROJ4 formatted projection info for the given gdal.Dataset or projection respectively,
e.g. '+proj=utm +zone=43 +datum=WGS84 +units=m +no_defs ' e.g. '+proj=utm +zone=43 +datum=WGS84 +units=m +no_defs '
:param ds: <gdal.Dataset> the gdal dataset to get PROJ4 info for :param ds: <gdal.Dataset> the gdal dataset to get PROJ4 info for
......
...@@ -36,7 +36,7 @@ def get_GDAL_ds_inmem(array, gt=None, prj=None, nodata=None): ...@@ -36,7 +36,7 @@ def get_GDAL_ds_inmem(array, gt=None, prj=None, nodata=None):
if gdal_array.NumericTypeCodeToGDALTypeCode(array.dtype) is None: if gdal_array.NumericTypeCodeToGDALTypeCode(array.dtype) is None:
array = array.astype(dTypeDic_NumPy2GDALcompatible[str(np.dtype(array.dtype))]) array = array.astype(dTypeDic_NumPy2GDALcompatible[str(np.dtype(array.dtype))])
OpenNumPyArray = get_gdalnumeric_func('OpenNumPyArray') OpenNumPyArray = get_gdalnumeric_func('OpenArray')
ds = OpenNumPyArray(array) ds = OpenNumPyArray(array)
if ds is None: if ds is None:
...@@ -52,7 +52,7 @@ def get_GDAL_ds_inmem(array, gt=None, prj=None, nodata=None): ...@@ -52,7 +52,7 @@ def get_GDAL_ds_inmem(array, gt=None, prj=None, nodata=None):
try: try:
band.SetNoDataValue(nodata) band.SetNoDataValue(nodata)
except TypeError: except TypeError:
raise Exception('TypeError while trying to set NoDataValue to %s. ' % nodata) raise TypeError(type(nodata), 'TypeError while trying to set NoDataValue to %s. ' % nodata)
del band del band
ds.FlushCache() # Write to disk. ds.FlushCache() # Write to disk.
...@@ -82,7 +82,8 @@ def wait_if_used(path_file, lockfile, timeout=100, try_kill=0): ...@@ -82,7 +82,8 @@ def wait_if_used(path_file, lockfile, timeout=100, try_kill=0):
isinstance(globs[k], gdal.Dataset) and globs[k].GetDescription() == path_file] isinstance(globs[k], gdal.Dataset) and globs[k].GetDescription() == path_file]
t0 = time.time() t0 = time.time()
def update_same_gdalRefs(sRs): return [sR for sR in sRs if sR in globals() and globals()[sR] is not None] def update_same_gdalRefs(sRs):
return [sR for sR in sRs if sR in globals() and globals()[sR] is not None]
while same_gdalRefs != [] or os.path.exists(lockfile): while same_gdalRefs != [] or os.path.exists(lockfile):
if os.path.exists(lockfile): if os.path.exists(lockfile):
......
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
test_gdal
---------
Tests for `py_tools_ds.io.raster.gdal` module.
"""
from unittest import TestCase
import numpy as np
from gdal import Dataset
from pandas import DataFrame
from py_tools_ds.io.raster.gdal import get_GDAL_ds_inmem, get_GDAL_driverList
class Test_get_GDAL_ds_inmem(TestCase):
def setUp(self):
self.arr2d = np.random.randint(0, 10, (5, 5))
self.arr3d = np.random.randint(0, 10, (5, 5, 2))
self.gt = [465567.6, 0.6, 0.0, 3840310.8000000003, 0.0, -0.6]
self.prj = \
"""
PROJCS["WGS 84 / UTM zone 36N",
GEOGCS["WGS 84",
DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.257223563,
AUTHORITY["EPSG","7030"]],
AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0,
AUTHORITY["EPSG","8901"]],
UNIT["degree",0.0174532925199433,
AUTHORITY["EPSG","9122"]],
AUTHORITY["EPSG","4326"]],
PROJECTION["Transverse_Mercator"],
PARAMETER["latitude_of_origin",0],
PARAMETER["central_meridian",33],
PARAMETER["scale_factor",0.9996],
PARAMETER["false_easting",500000],
PARAMETER["false_northing",0],
UNIT["metre",1,
AUTHORITY["EPSG","9001"]],
AXIS["Easting",EAST],
AXIS["Northing",NORTH],
AUTHORITY["EPSG","32636"]]
"""
def test_2d_array(self):
ds = get_GDAL_ds_inmem(array=self.arr2d)
self.assertIsInstance(ds, Dataset)
def test_3d_array(self):
ds = get_GDAL_ds_inmem(array=self.arr3d)
self.assertIsInstance(ds, Dataset)
def test_2d_array_gt_prj(self):
ds = get_GDAL_ds_inmem(array=self.arr3d, gt=self.gt, prj=self.prj)
self.assertIsInstance(ds, Dataset)
def test_2d_array_gt_prj_nodata(self):
ds = get_GDAL_ds_inmem(array=self.arr3d, gt=self.gt, prj=self.prj, nodata=0)
self.assertIsInstance(ds, Dataset)
with self.assertRaises(TypeError):
ds = get_GDAL_ds_inmem(array=self.arr3d, gt=self.gt, prj=self.prj, nodata=np.array([0], dtype=np.bool))
self.assertIsInstance(ds, Dataset)
class Test_get_GDAL_driverList(TestCase):
def test(self):
drvList = get_GDAL_driverList()
self.assertIsInstance(drvList, DataFrame)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment