Skip to content

Commit

Permalink
#91 NA-50 clean up Domain test
Browse files Browse the repository at this point in the history
  • Loading branch information
jeong1park committed Jan 15, 2018
1 parent 10d3433 commit 07a3a82
Showing 1 changed file with 8 additions and 28 deletions.
36 changes: 8 additions & 28 deletions nansat/tests/test_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,23 @@ def setUp(self):
raise ValueError('No test data available')

def test_init_from_strings(self):
d = Domain("+proj=latlong +datum=WGS84 +ellps=WGS84 +no_defs",
"-te 25 70 35 72 -ts 2000 2000")

d = Domain(srs="+proj=latlong +datum=WGS84 +ellps=WGS84 +no_defs",
ext="-te 25 70 35 72 -ts 2000 2000")
self.assertEqual(type(d), Domain)

def test_init_from_epsg_and_te_string(self):
d = Domain(4326, "-te 25 70 35 72 -ts 500 500")

d = Domain(srs=4326,
ext="-te 25 70 35 72 -ts 500 500")
self.assertEqual(type(d), Domain)

def test_init_from_epsg_and_lle_string(self):
d = Domain(4326, "-lle 25 70 35 72 -ts 500 500")

d = Domain(srs=4326,
ext="-lle 25 70 35 72 -ts 500 500")
self.assertEqual(type(d), Domain)

def test_init_from_lonlat(self):
lat, lon = np.mgrid[-90:90:0.5, -180:180:0.5]
d = Domain(lon=lon, lat=lat)

self.assertEqual(type(d), Domain)
self.assertEqual(d.shape(), lat.shape)

Expand Down Expand Up @@ -90,30 +88,26 @@ def test_dont_init_from_invalid_srs(self):
srs="unmatched srs")

def test_init_from_ds_srs(self):
ds = gdal.Open(self.test_file)
srs="+proj=latlong +datum=WGS84 +ellps=WGS84 +no_defs"
d = Domain(ds=ds, srs=srs)
d = Domain(ds=gdal.Open(self.test_file),
srs="+proj=latlong +datum=WGS84 +ellps=WGS84 +no_defs")
self.assertIsInstance(d, Domain)

def test_write_kml(self):
d = Domain(4326, "-te 25 70 35 72 -ts 500 500")
tmpfilename = os.path.join(ntd.tmp_data_path, 'domain_write_kml.kml')
d.write_kml(kmlFileName=tmpfilename)

self.assertTrue(os.path.exists(tmpfilename))

def test_get_geolocation_grids(self):
d = Domain(4326, "-te 25 70 35 72 -ts 500 500")
lon, lat = d.get_geolocation_grids()

self.assertEqual(type(lon), np.ndarray)
self.assertEqual(type(lat), np.ndarray)
self.assertEqual(lat.shape, (500, 500))

def test_get_border(self):
d = Domain(4326, "-te 25 70 35 72 -ts 500 500")
lon, lat = d.get_border()

self.assertEqual(type(lon), np.ndarray)
self.assertEqual(type(lat), np.ndarray)
self.assertEqual(len(lat), 44)
Expand All @@ -123,14 +117,12 @@ def test_get_border(self):
def test_get_border_wkt(self):
d = Domain(4326, "-te 25 70 35 72 -ts 500 500")
bwkt = d.get_border_wkt()

self.assertEqual(type(bwkt), str)
self.assertTrue('POLYGON' in bwkt)

def test_get_border_geometry(self):
d = Domain(4326, "-te 25 70 35 72 -ts 500 500")
geom = d.get_border_geometry()

self.assertEqual(type(geom), ogr.Geometry)
# the below test doesn't work in Travis
# probably some libs are missing in default anaconda install
Expand All @@ -139,58 +131,50 @@ def test_get_border_geometry(self):
def test_get_corners(self):
d = Domain(4326, "-te 25 70 35 72 -ts 500 500")
lon, lat = d.get_corners()

self.assertTrue(all(lon - [25., 25., 35., 35.] < 0.01))
self.assertTrue(all(lat - [72., 70., 72., 70.] < 0.01))

def test_get_pixelsize_meters(self):
d = Domain(4326, "-te 25 70 35 72 -ts 500 500")
x, y = d.get_pixelsize_meters()

self.assertTrue(x - 444 < 1)
self.assertTrue(y - 723 < 1)

def test_transform_points(self):
d = Domain(4326, "-te 25 70 35 72 -ts 500 500")
lon, lat = d.transform_points([1, 2, 3], [1, 2, 3])

self.assertEqual(type(lon), np.ndarray)
self.assertEqual(type(lat), np.ndarray)

def test_transform_points_inverse(self):
d = Domain(4326, "-te 25 70 35 72 -ts 500 500")
x, y = d.transform_points([25, 26, 27], [70, 71, 72], 1)

self.assertTrue(all(np.round(x) == [0, 50, 100]))
self.assertTrue(all(np.round(y) == [500, 250, 0]))

def test_transform_points_dstsrs(self):
d = Domain(4326, "-te 25 70 35 72 -ts 500 500")
lon, lat = d.transform_points([1, 2, 3], [1, 2, 3],
dstSRS=NSR('+proj=stere +datum=WGS84 +ellps=WGS84 +lat_0=75 +lon_0=10 +no_defs'))

self.assertEqual(type(lon), np.ndarray)
self.assertEqual(type(lat), np.ndarray)

def test_azimuth_y(self):
d = Domain(4326, "-te 25 70 35 72 -ts 500 500")
au = d.azimuth_y()

self.assertEqual(np.round(au[0, 0]), 0)
self.assertEqual(np.round(au[10, 10]), 0)

def test_shape(self):
d = Domain(4326, "-te 25 70 35 72 -ts 500 500")
shape = d.shape()

self.assertEqual(shape, (500, 500))

@unittest.skipUnless(BASEMAP_LIB_EXISTS, 'Basemap is required')
def test_write_map(self):
d = Domain(4326, "-te 25 70 35 72 -ts 500 500")
tmpfilename = os.path.join(ntd.tmp_data_path, 'domain_write_map.png')
d.write_map(tmpfilename)

self.assertTrue(os.path.exists(tmpfilename))
i = Image.open(tmpfilename)
i.verify()
Expand All @@ -202,7 +186,6 @@ def test_write_map_dpi100(self):
tmpfilename = os.path.join(ntd.tmp_data_path,
'domain_write_map_dpi100.png')
d.write_map(tmpfilename, dpi=100)

self.assertTrue(os.path.exists(tmpfilename))
i = Image.open(tmpfilename)
i.verify()
Expand All @@ -216,7 +199,6 @@ def test_write_map_labels(self):
d.write_map(tmpfilename,
merLabels=[False, False, False, True],
parLabels=[True, False, False, False])

self.assertTrue(os.path.exists(tmpfilename))
i = Image.open(tmpfilename)
i.verify()
Expand All @@ -226,15 +208,13 @@ def test_reproject_GCPs(self):
d = Domain(ds=ds)
d.reproject_GCPs('+proj=stere +datum=WGS84 +ellps=WGS84 +lat_0=75 +lon_0=10 +no_defs')
gcp = d.vrt.dataset.GetGCPs()[0]

self.assertTrue(gcp.GCPX > 636161)
self.assertTrue(gcp.GCPY < -288344)

def test_reproject_GCPs_auto(self):
ds = gdal.Open(self.test_file)
d = Domain(ds=ds)
d.reproject_GCPs()

gcpproj = NSR(d.vrt.dataset.GetGCPProjection())
self.assertEqual(gcpproj.GetAttrValue('PROJECTION'),
'Stereographic')
Expand Down

0 comments on commit 07a3a82

Please sign in to comment.