Skip to content

Commit

Permalink
More geo_image testing.
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Raspaud <martin.raspaud@smhi.se>
  • Loading branch information
mraspaud committed Mar 11, 2014
1 parent e473d7a commit 8689da2
Showing 1 changed file with 302 additions and 9 deletions.
311 changes: 302 additions & 9 deletions mpop/tests/test_geo_image.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2014

# SMHI,
# Folkborgsvägen 1,
# Norrköping,
# Sweden

# Author(s):

# Martin Raspaud <martin.raspaud@smhi.se>

# This file is part of mpop.

# mpop is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# mpop is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with mpop. If not, see <http://www.gnu.org/licenses/>.

"""Module for testing the pp.geo_image module.
"""
import datetime
Expand All @@ -10,6 +38,7 @@
# Mock some modules, so we don't need them for tests.
sys.modules['osgeo'] = MagicMock()
sys.modules['pyresample'] = MagicMock()
#sys.modules['trollimage.image'] = MagicMock()
import mpop.imageo.geo_image as geo_image


Expand All @@ -21,11 +50,12 @@ class TestGeoImage(unittest.TestCase):
def setUp(self):
"""Setup the test.
"""
time_slot = datetime.datetime(2009, 10, 8, 14, 30)
self.time_slot = datetime.datetime(2009, 10, 8, 14, 30)
self.data = np.zeros((512, 512), dtype=np.uint8)
self.img = geo_image.GeoImage(self.data,
area="euro",
time_slot=time_slot)
time_slot=self.time_slot)



@patch.object(geo_image.GeoImage, 'geotiff_save')
Expand All @@ -43,6 +73,20 @@ def test_save(self, mock_save):
mock_save.assert_called_once_with("test.tif", 9, None, None, 256,
floating_point=True)

mock_save.reset_mock()
self.img.save("test.tif", compression=9, tags={"NBITS": 20})
mock_save.assert_called_once_with("test.tif", 9, {"NBITS": 20},
None, 256)

with patch.object(geo_image.Image, 'save') as mock_isave:
self.img.save("test.png")
mock_isave.assert_called_once_with(self.img, 'test.png', 6,
fformat='png')
mock_isave.side_effect = geo_image.UnknownImageFormat("Boom!")
self.assertRaises(geo_image.UnknownImageFormat,
self.img.save, "test.dummy")


@patch('osgeo.osr.SpatialReference')
@patch('mpop.projector.get_area_def')
@patch('osgeo.gdal.GDT_Float64')
Expand All @@ -66,15 +110,16 @@ def test_save_geotiff(self, mock_write_channels, gtbn, gui32, gui16, gby, gf, ga

raster = gtbn.return_value

self.img.geotiff_save("test.tif", 0, None, None, 256)
self.img.geotiff_save("test.tif", 0, None, {"BLA": "09"}, 256)
gtbn.assert_called_once_with("GTiff")

raster.Create.assert_called_once_with("test.tif",
self.data.shape[0],
self.data.shape[1],
2,
gby,
['TILED=YES',
["BLA=09",
'TILED=YES',
'BLOCKXSIZE=256',
'BLOCKYSIZE=256',
'ALPHA=YES'])
Expand All @@ -99,12 +144,27 @@ def test_save_geotiff(self, mock_write_channels, gtbn, gui32, gui16, gby, gf, ga
self.img.time_slot.strftime("%Y:%m:%d %H:%M:%S")}
dst_ds.SetMetadata.assert_called_once_with(time_tag, '')

# with compression set to 9
@patch('osgeo.osr.SpatialReference')
@patch('mpop.projector.get_area_def')
@patch('osgeo.gdal.GDT_Float64')
@patch('osgeo.gdal.GDT_Byte')
@patch('osgeo.gdal.GDT_UInt16')
@patch('osgeo.gdal.GDT_UInt32')
@patch('osgeo.gdal.GetDriverByName')
@patch.object(geo_image.GeoImage, '_gdal_write_channels')
def test_save_geotiff_compress(self, mock_write_channels, gtbn, gui32, gui16, gby, gf, gad, spaceref):
"""Save to geotiff format with compression.
"""
gadr = gad.return_value
gadr.area_extent = [1, 2, 3, 4]
gadr.pixel_size_x = 10
gadr.pixel_size_y = 11
gadr.proj4_string = "+proj=geos +ellps=WGS84"
gadr.proj_dict = {"proj": "geos", "ellps": "WGS84"}
gadr.proj_id = "geos0"

raster = gtbn.return_value

gtbn.reset_mock()
mock_write_channels.reset_mock()
raster.Create.reset_mock()

self.img.geotiff_save("test.tif", 9, None, None, 256)
gtbn.assert_called_once_with("GTiff")

Expand Down Expand Up @@ -138,7 +198,240 @@ def test_save_geotiff(self, mock_write_channels, gtbn, gui32, gui16, gby, gf, ga
self.img.time_slot.strftime("%Y:%m:%d %H:%M:%S")}
dst_ds.SetMetadata.assert_called_once_with(time_tag, '')

@patch('osgeo.osr.SpatialReference')
@patch('mpop.projector.get_area_def')
@patch('osgeo.gdal.GDT_Float64')
@patch('osgeo.gdal.GDT_Byte')
@patch('osgeo.gdal.GDT_UInt16')
@patch('osgeo.gdal.GDT_UInt32')
@patch('osgeo.gdal.GetDriverByName')
@patch.object(geo_image.GeoImage, '_gdal_write_channels')
def test_save_geotiff_floats(self, mock_write_channels, gtbn, gui32, gui16, gby, gf, gad, spaceref):
"""Save to geotiff format with floats.
"""
gadr = gad.return_value
gadr.area_extent = [1, 2, 3, 4]
gadr.pixel_size_x = 10
gadr.pixel_size_y = 11
gadr.proj4_string = "+proj=geos +ellps=WGS84"
gadr.proj_dict = {"proj": "geos", "ellps": "WGS84"}
gadr.proj_id = "geos0"
# test with floats

raster = gtbn.return_value

self.img.geotiff_save("test.tif", 0, None, None, 256,
floating_point=True)
gtbn.assert_called_once_with("GTiff")

raster.Create.assert_called_once_with("test.tif",
self.data.shape[0],
self.data.shape[1],
1,
gf,
['TILED=YES',
'BLOCKXSIZE=256',
'BLOCKYSIZE=256'])

dst_ds = raster.Create.return_value

#mock_write_channels.assert_called_once_with(dst_ds, self.data,
# 255, None)

self.assertEquals(mock_write_channels.call_count, 1)
self.assertEquals(mock_write_channels.call_args[0][0], dst_ds)
self.assertEquals(mock_write_channels.call_args[0][2], 0)
self.assertEquals(mock_write_channels.call_args[0][3], [0])
self.assertTrue(np.all(mock_write_channels.call_args[0][1]
== self.data))


dst_ds.SetGeoTransform.assert_called_once_with([1, 10, 0, 4, 0, -11])
srs = spaceref.return_value.ExportToWkt.return_value
dst_ds.SetProjection.assert_called_once_with(srs)

time_tag = {"TIFFTAG_DATETIME":
self.img.time_slot.strftime("%Y:%m:%d %H:%M:%S")}
dst_ds.SetMetadata.assert_called_once_with(time_tag, '')

self.fill_value = None
self.img.mode = "RGB"
self.assertRaises(ValueError, self.img.geotiff_save, "test.tif", 0,
None, None, 256,
floating_point=True)

@patch('osgeo.osr.SpatialReference')
@patch('mpop.projector.get_area_def')
@patch('osgeo.gdal.GDT_Float64')
@patch('osgeo.gdal.GDT_Byte')
@patch('osgeo.gdal.GDT_UInt16')
@patch('osgeo.gdal.GDT_UInt32')
@patch('osgeo.gdal.GetDriverByName')
@patch.object(geo_image.GeoImage, '_gdal_write_channels')
def test_save_geotiff_32(self, mock_write_channels, gtbn, gui32, gui16, gby, gf, gad, spaceref):
"""Save to geotiff 32-bits format.
"""
gadr = gad.return_value
gadr.area_extent = [1, 2, 3, 4]
gadr.pixel_size_x = 10
gadr.pixel_size_y = 11
gadr.proj4_string = "+proj=geos +ellps=WGS84"
gadr.proj_dict = {"proj": "geos", "ellps": "WGS84"}
gadr.proj_id = "geos0"

raster = gtbn.return_value


self.img.geotiff_save("test.tif", 9, {"NBITS": 20}, None, 256)
gtbn.assert_called_once_with("GTiff")

raster.Create.assert_called_once_with("test.tif",
self.data.shape[0],
self.data.shape[1],
2,
gui32,
['COMPRESS=DEFLATE',
'ZLEVEL=9',
'TILED=YES',
'BLOCKXSIZE=256',
'BLOCKYSIZE=256',
'ALPHA=YES'])
dst_ds = raster.Create.return_value

#mock_write_channels.assert_called_once_with(dst_ds, self.data,
# 255, None)

self.assertEquals(mock_write_channels.call_count, 1)
self.assertEquals(mock_write_channels.call_args[0][0], dst_ds)
self.assertEquals(mock_write_channels.call_args[0][2], 2**32 - 1)
self.assertTrue(mock_write_channels.call_args[0][3] is None)
self.assertTrue(np.all(mock_write_channels.call_args[0][1] == self.data))

dst_ds.SetGeoTransform.assert_called_once_with([1, 10, 0, 4, 0, -11])
srs = spaceref.return_value.ExportToWkt.return_value
dst_ds.SetProjection.assert_called_once_with(srs)

time_tag = {"TIFFTAG_DATETIME":
self.img.time_slot.strftime("%Y:%m:%d %H:%M:%S"),
"NBITS": 20}
dst_ds.SetMetadata.assert_called_once_with(time_tag, '')


@patch('osgeo.osr.SpatialReference')
@patch('mpop.projector.get_area_def')
@patch('osgeo.gdal.GDT_Float64')
@patch('osgeo.gdal.GDT_Byte')
@patch('osgeo.gdal.GDT_UInt16')
@patch('osgeo.gdal.GDT_UInt32')
@patch('osgeo.gdal.GetDriverByName')
@patch.object(geo_image.GeoImage, '_gdal_write_channels')
def test_save_geotiff_16(self, mock_write_channels, gtbn, gui32, gui16, gby, gf, gad, spaceref):
"""Save to geotiff 16-bits format.
"""
gadr = gad.return_value
gadr.area_extent = [1, 2, 3, 4]
gadr.pixel_size_x = 10
gadr.pixel_size_y = 11
gadr.proj4_string = "+proj=geos +ellps=WGS84"
gadr.proj_dict = {"proj": "geos", "ellps": "WGS84"}
gadr.proj_id = "geos0"

raster = gtbn.return_value


self.img.geotiff_save("test.tif", 9, {"NBITS": 15}, None, 256)
gtbn.assert_called_once_with("GTiff")

raster.Create.assert_called_once_with("test.tif",
self.data.shape[0],
self.data.shape[1],
2,
gui16,
['COMPRESS=DEFLATE',
'ZLEVEL=9',
'TILED=YES',
'BLOCKXSIZE=256',
'BLOCKYSIZE=256',
'ALPHA=YES'])
dst_ds = raster.Create.return_value

#mock_write_channels.assert_called_once_with(dst_ds, self.data,
# 255, None)

self.assertEquals(mock_write_channels.call_count, 1)
self.assertEquals(mock_write_channels.call_args[0][0], dst_ds)
self.assertEquals(mock_write_channels.call_args[0][2], 2**16 - 1)
self.assertTrue(mock_write_channels.call_args[0][3] is None)
self.assertTrue(np.all(mock_write_channels.call_args[0][1] == self.data))

dst_ds.SetGeoTransform.assert_called_once_with([1, 10, 0, 4, 0, -11])
srs = spaceref.return_value.ExportToWkt.return_value
dst_ds.SetProjection.assert_called_once_with(srs)

time_tag = {"TIFFTAG_DATETIME":
self.img.time_slot.strftime("%Y:%m:%d %H:%M:%S"),
"NBITS": 15}
dst_ds.SetMetadata.assert_called_once_with(time_tag, '')


@patch('osgeo.osr.SpatialReference')
@patch('mpop.projector.get_area_def')
@patch('osgeo.gdal.GDT_Float64')
@patch('osgeo.gdal.GDT_Byte')
@patch('osgeo.gdal.GDT_UInt16')
@patch('osgeo.gdal.GDT_UInt32')
@patch('osgeo.gdal.GetDriverByName')
@patch.object(geo_image.GeoImage, '_gdal_write_channels')
def test_save_geotiff_geotransform(self, mock_write_channels, gtbn, gui32, gui16, gby, gf, gad, spaceref):
"""Save to geotiff format with custom geotransform
"""
gadr = gad.return_value
gadr.area_extent = [1, 2, 3, 4]
gadr.pixel_size_x = 10
gadr.pixel_size_y = 11
gadr.proj4_string = "+proj=geos +ellps=WGS84"
gadr.proj_dict = {"proj": "geos", "ellps": "WGS84"}
gadr.proj_id = "geos0"

# test with 0 compression

raster = gtbn.return_value

self.img.geotiff_save("test.tif", 0, None, None, 256,
geotransform="best geotransform of the world",
spatialref=spaceref())
gtbn.assert_called_once_with("GTiff")

raster.Create.assert_called_once_with("test.tif",
self.data.shape[0],
self.data.shape[1],
2,
gby,
['TILED=YES',
'BLOCKXSIZE=256',
'BLOCKYSIZE=256',
'ALPHA=YES'])
dst_ds = raster.Create.return_value

#mock_write_channels.assert_called_once_with(dst_ds, self.data,
# 255, None)

self.assertEquals(mock_write_channels.call_count, 1)
self.assertEquals(mock_write_channels.call_args[0][0], dst_ds)
self.assertEquals(mock_write_channels.call_args[0][2], 255)
self.assertTrue(mock_write_channels.call_args[0][3] is None)
self.assertTrue(np.all(mock_write_channels.call_args[0][1]
== self.data))


dst_ds.SetGeoTransform.assert_called_once_with("best geotransform of"
" the world")
srs = spaceref.return_value.ExportToWkt.return_value
dst_ds.SetProjection.assert_called_once_with(srs)

time_tag = {"TIFFTAG_DATETIME":
self.img.time_slot.strftime("%Y:%m:%d %H:%M:%S")}
dst_ds.SetMetadata.assert_called_once_with(time_tag, '')


def suite():
Expand Down

0 comments on commit 8689da2

Please sign in to comment.