Skip to content

Commit

Permalink
Merge pull request #1 from snowman2/tests
Browse files Browse the repository at this point in the history
Tests
  • Loading branch information
Alan D. Snow authored Jun 10, 2017
2 parents 2bac02b + 711e815 commit b2a319a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
5 changes: 5 additions & 0 deletions gazar/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ def y_size(self):
"""int: size of y dimensions"""
return self.dataset.RasterYSize

@property
def num_bands(self):
"""int: number of bands in raster"""
return self.dataset.RasterCount

@property
def wkt(self):
""":obj:`str`:WKT projection string"""
Expand Down
77 changes: 75 additions & 2 deletions tests/test_grid_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# License: BSD 3-Clause

from numpy.testing import assert_almost_equal
import numpy as np
from os import path
from osgeo import osr
from pyproj import Proj
Expand All @@ -15,7 +16,7 @@

from .conftest import compare_files

from gazar.grid import ArrayGrid, GDALGrid
from gazar.grid import ArrayGrid, GDALGrid, utm_proj_from_latlon
import gazar
gazar.log_to_console(level='DEBUG')

Expand All @@ -40,6 +41,33 @@ def prep(request, tgrid):
return input_raster, compare_path


def test_gdal_grid_projection(prep, tgrid):
"""
Tests test_gdal_grid_projection
"""
input_raster, compare_path = prep
compare_projection_file = path.join(compare_path, 'test_projection.prj')
ggrid = GDALGrid(input_raster, compare_projection_file)

# check properties
assert_almost_equal(ggrid.geotransform,
(120.99986111111112,
0.008333333333333333,
0.0,
16.008194444444445,
0.0,
-0.008333333333333333))
assert ggrid.x_size == 120
assert ggrid.y_size == 120
assert ggrid.num_bands == 1
assert ggrid.wkt == ('GEOGCS["WGS 84",DATUM["WGS_1984",'
'SPHEROID["WGS 84",6378137,298.257223563,'
'AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG",'
'"6326"]],PRIMEM["Greenwich",0],UNIT["degree",'
'0.0174532925199433],AUTHORITY["EPSG","4326"]]')
assert ggrid.proj4 == '+proj=longlat +datum=WGS84 +no_defs '


def test_gdal_grid(prep, tgrid):
"""
Tests test_gdal_grid
Expand All @@ -57,6 +85,7 @@ def test_gdal_grid(prep, tgrid):
-0.008333333333333333))
assert ggrid.x_size == 120
assert ggrid.y_size == 120
assert ggrid.num_bands == 1
assert ggrid.wkt == ('GEOGCS["WGS 84",DATUM["WGS_1984",'
'SPHEROID["WGS 84",6378137,298.257223563,'
'AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG",'
Expand All @@ -68,6 +97,15 @@ def test_gdal_grid(prep, tgrid):
sp_ref = osr.SpatialReference()
sp_ref.ImportFromEPSG(32651)

latitude, longitude = ggrid.latlon
assert latitude.shape == (120, 120)
assert longitude.shape == (120, 120)
assert_almost_equal(latitude[20:22, 20:22],
[[15.83736111, 15.83736111],
[15.82902778, 15.82902778]])
assert_almost_equal(longitude[20:22, 20:22],
[[121.17069444, 121.17902778],
[121.17069444, 121.17902778]])
# check functions
assert_almost_equal(ggrid.bounds(),
(120.99986111111112,
Expand Down Expand Up @@ -105,7 +143,13 @@ def test_gdal_grid(prep, tgrid):
with pytest.raises(IndexError):
x_loc, y_loc = ggrid.pixel2coord(5, 10000000)

# check write functions
with pytest.raises(IndexError):
x_coord, y_coord = ggrid.coord2pixel(1870872, 1669170)

with pytest.raises(IndexError):
x_coord, y_coord = ggrid.coord2pixel(284940, 10000000)

# check write functions
projection_name = 'test_projection.prj'
out_projection_file = path.join(tgrid.write, projection_name)
ggrid.write_prj(out_projection_file)
Expand Down Expand Up @@ -155,3 +199,32 @@ def test_array_grid(prep):
assert ggrid.y_size == arrg.y_size
assert ggrid.proj4 == arrg.proj4
assert (ggrid.np_array() == arrg.np_array()).all()


def test_array_grid3d(prep):
"""
Test array grid 3d version
"""
input_raster, compare_path = prep
ggrid = GDALGrid(input_raster)
orig_array = ggrid.np_array(masked=True)
grid_array = np.array([orig_array, 5 * orig_array, 4 * orig_array])
arrg = ArrayGrid(in_array=grid_array,
wkt_projection=ggrid.wkt,
geotransform=ggrid.geotransform)

assert_almost_equal(ggrid.geotransform,
arrg.geotransform)
assert ggrid.x_size == arrg.x_size
assert ggrid.y_size == arrg.y_size
assert ggrid.proj4 == arrg.proj4
assert arrg.num_bands == 3
assert (arrg.np_array(band='all') == grid_array).all()


def test_utm_from_latlon():
"""
Test retrieving a UTM projection from a latitude and longitude
"""
assert utm_proj_from_latlon(-25.2744, 133.7751) == \
'+proj=utm +zone=53 +south +datum=WGS84 +units=m +no_defs '

0 comments on commit b2a319a

Please sign in to comment.