|
| 1 | +import os |
| 2 | +import unittest |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from geopyspark.geotrellis import SpatialKey, Tile |
| 8 | +from geopyspark.tests.base_test_class import BaseTestClass |
| 9 | +from geopyspark.geotrellis.layer import TiledRasterLayer |
| 10 | +from geopyspark.geotrellis.constants import LayerType |
| 11 | + |
| 12 | + |
| 13 | +class NormalizeTest(BaseTestClass): |
| 14 | + cells = np.array([[ |
| 15 | + [1.0, 1.0, 1.0, 1.0, 1.0], |
| 16 | + [1.0, 1.0, 1.0, 1.0, 1.0], |
| 17 | + [1.0, 1.0, 1.0, 1.0, 1.0], |
| 18 | + [1.0, 1.0, 1.0, 1.0, 1.0], |
| 19 | + [1.0, 1.0, 1.0, 1.0, 0.0]]]) |
| 20 | + |
| 21 | + layer = [(SpatialKey(0, 0), Tile(cells + 0, 'FLOAT', -1.0)), |
| 22 | + (SpatialKey(1, 0), Tile(cells + 1, 'FLOAT', -1.0,)), |
| 23 | + (SpatialKey(0, 1), Tile(cells + 2, 'FLOAT', -1.0,)), |
| 24 | + (SpatialKey(1, 1), Tile(cells + 3, 'FLOAT', -1.0,))] |
| 25 | + |
| 26 | + rdd = BaseTestClass.pysc.parallelize(layer) |
| 27 | + |
| 28 | + extent = {'xmin': 0.0, 'ymin': 0.0, 'xmax': 33.0, 'ymax': 33.0} |
| 29 | + layout = {'layoutCols': 2, 'layoutRows': 2, 'tileCols': 5, 'tileRows': 5} |
| 30 | + metadata = {'cellType': 'float32ud-1.0', |
| 31 | + 'extent': extent, |
| 32 | + 'crs': '+proj=longlat +datum=WGS84 +no_defs ', |
| 33 | + 'bounds': { |
| 34 | + 'minKey': {'col': 0, 'row': 0}, |
| 35 | + 'maxKey': {'col': 1, 'row': 1}}, |
| 36 | + 'layoutDefinition': { |
| 37 | + 'extent': extent, |
| 38 | + 'tileLayout': {'tileCols': 5, 'tileRows': 5, 'layoutCols': 2, 'layoutRows': 2}}} |
| 39 | + |
| 40 | + raster_rdd = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd, metadata) |
| 41 | + |
| 42 | + @pytest.fixture(autouse=True) |
| 43 | + def tearDown(self): |
| 44 | + yield |
| 45 | + BaseTestClass.pysc._gateway.close() |
| 46 | + |
| 47 | + def test_normalize_all_parameters(self): |
| 48 | + normalized = self.raster_rdd.normalize(old_min=0.0, old_max=4.0, new_min=5.0, new_max=10.0) |
| 49 | + |
| 50 | + self.assertEqual(normalized.get_min_max(), (5.0, 10.0)) |
| 51 | + |
| 52 | + def test_normalize_no_optinal_parameters(self): |
| 53 | + normalized = self.raster_rdd.normalize(new_min=5.0, new_max=10.0) |
| 54 | + |
| 55 | + self.assertEqual(normalized.get_min_max(), (5.0, 10.0)) |
| 56 | + |
| 57 | + def test_normalize_old_min(self): |
| 58 | + normalized = self.raster_rdd.normalize(old_min=-1, new_min=5.0, new_max=10.0) |
| 59 | + |
| 60 | + self.assertEqual(normalized.get_min_max(), (6.0, 10.0)) |
| 61 | + |
| 62 | + def test_normalize_old_max(self): |
| 63 | + normalized = self.raster_rdd.normalize(old_max=5.0, new_min=5.0, new_max=10.0) |
| 64 | + |
| 65 | + self.assertEqual(normalized.get_min_max(), (5.0, 9.0)) |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + unittest.main() |
0 commit comments