Example #1
0
    def test_ranges(self):
        arr = np.array(
            [[[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]],
            dtype=int)
        tile = Tile(arr, 'INT', -500)

        rdd = BaseTestClass.pysc.parallelize([(self.projected_extent, tile)])
        raster_rdd = RasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd)

        value_map = {2: 20}

        result = raster_rdd.reclassify(value_map, int,
                                       ClassificationStrategy.GREATER_THAN
                                       ).to_numpy_rdd().first()[1].cells

        expected = np.array(
            [[[-500, -500, -500, -500], [-500, -500, -500, -500],
              [20, 20, 20, 20], [20, 20, 20, 20]]],
            dtype=int)

        self.assertTrue((result == expected).all())
Example #2
0
    def test_pyramid_class(self):
        arr = np.zeros((1, 16, 16))
        epsg_code = 3857
        extent = Extent(0.0, 0.0, 10.0, 10.0)

        tile = Tile(arr, 'FLOAT', False)
        projected_extent = ProjectedExtent(extent, epsg_code)

        rdd = BaseTestClass.pysc.parallelize([(projected_extent, tile)])
        raster_rdd = RasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd)
        tile_layout = TileLayout(1, 1, 16, 16)
        reprojected = raster_rdd.tile_to_layout(
            layout=GlobalLayout(tile_size=16), target_crs=3857)

        result = reprojected.pyramid()
        hist = result.get_histogram()

        self.assertEqual(result.max_zoom, reprojected.zoom_level)
        self.assertTrue(set(result.levels.keys()).issuperset(range(1, 13)))
        self.assertEqual(hist.mean(), 0.0)
        self.assertEqual(hist.min_max(), (0.0, 0.0))
Example #3
0
    def test_temporal_projected_extent(self):
        pes = [
            TemporalProjectedExtent(extent=self.extents[0],
                                    epsg=self.crs,
                                    instant=self.time),
            TemporalProjectedExtent(extent=self.extents[1],
                                    epsg=self.crs,
                                    instant=self.time),
        ]

        pe_layer = [(pes[0], self.tile_1), (pes[1], self.tile_1),
                    (pes[0], self.tile_2), (pes[1], self.tile_2)]

        rdd = self.pysc.parallelize(pe_layer)
        layer = RasterLayer.from_numpy_rdd(LayerType.SPACETIME, rdd)

        actual = layer.merge()

        self.assertEqual(actual.srdd.rdd().count(), 2)

        for k, v in actual.to_numpy_rdd().collect():
            self.assertTrue((v.cells == self.arr_2).all())
Example #4
0
class WithNoDataTest(BaseTestClass):
    epsg_code = 3857
    extent = Extent(0.0, 0.0, 10.0, 10.0)
    projected_extent = ProjectedExtent(extent, epsg_code)

    arr = np.zeros((1, 16, 16))
    tile = Tile(arr, 'FLOAT', -500.0)

    rdd = BaseTestClass.pysc.parallelize([(projected_extent, tile)])

    layer = RasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd)
    tiled_layer = layer.tile_to_layout()

    @pytest.fixture(autouse=True)
    def tearDown(self):
        yield
        BaseTestClass.pysc._gateway.close()

    def test_with_no_data_raster_layers(self):
        no_data_layer = self.layer.with_no_data(-10)
        tile = no_data_layer.to_numpy_rdd().first()[1]

        self.assertEqual(tile.no_data_value, -10)

        metadata = no_data_layer.collect_metadata()

        self.assertEqual(metadata.cell_type, "float32ud-10.0")
        self.assertEqual(metadata.no_data_value, -10)

    def test_with_no_data_tiled_raster_layers(self):
        no_data_layer = self.tiled_layer.with_no_data(18)
        tile = no_data_layer.to_numpy_rdd().first()[1]

        self.assertEqual(tile.no_data_value, 18)

        metadata = no_data_layer.layer_metadata

        self.assertEqual(metadata.cell_type, "float32ud18.0")
        self.assertEqual(metadata.no_data_value, 18)
Example #5
0
    def test_pyraminding_with_partitioner(self):
        arr = np.zeros((1, 16, 16))
        epsg_code = 3857
        extent = Extent(0.0, 0.0, 10.0, 10.0)

        tile = Tile(arr, 'FLOAT', False)
        projected_extent = ProjectedExtent(extent, epsg_code)

        rdd = BaseTestClass.pysc.parallelize([(projected_extent, tile)])
        raster_rdd = RasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd)
        tile_layout = TileLayout(32, 32, 16, 16)
        new_extent = Extent(-20037508.342789244, -20037508.342789244, 20037508.342789244,
                            20037508.342789244)

        layout_def = LayoutDefinition(new_extent, tile_layout)
        laid_out = raster_rdd.tile_to_layout(GlobalLayout(tile_size=16))

        strategy = SpatialPartitionStrategy(4)

        pyramided = laid_out.pyramid(partition_strategy=strategy)

        self.assertEqual(pyramided.levels[0].get_partition_strategy(), strategy)
Example #6
0
    def test_floating_voint_ranges(self):
        arr = np.array([[[0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0],
                         [1.5, 1.5, 1.5, 1.5], [2.0, 2.0, 2.0, 2.0]]],
                       dtype=float)

        tile = Tile(arr, 'FLOAT', float('nan'))
        rdd = BaseTestClass.pysc.parallelize([(self.projected_extent, tile)])
        raster_rdd = RasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd)

        value_map = {2.0: 5.0}

        result = raster_rdd.reclassify(
            value_map, float,
            ClassificationStrategy.LESS_THAN).to_numpy_rdd().first()[1].cells

        expected = np.array([[[5.0, 5.0, 5.0, 5.0], [5.0, 5.0, 5.0, 5.0],
                              [5.0, 5.0, 5.0, 5.0]]],
                            dtype=float)

        self.assertTrue((result[0, 2, ] == expected).all())
        for x in result[0, 3, ]:
            self.assertTrue(math.isnan(x))
class BandSelectionTest(BaseTestClass):
    band_1 = np.array([
        [1.0, 1.0, 1.0, 1.0, 1.0],
        [1.0, 1.0, 1.0, 1.0, 1.0],
        [1.0, 1.0, 1.0, 1.0, 1.0],
        [1.0, 1.0, 1.0, 1.0, 1.0],
        [1.0, 1.0, 1.0, 1.0, 1.0]])

    band_2 = np.array([
        [2.0, 2.0, 2.0, 2.0, 2.0],
        [2.0, 2.0, 2.0, 2.0, 2.0],
        [2.0, 2.0, 2.0, 2.0, 2.0],
        [2.0, 2.0, 2.0, 2.0, 2.0],
        [2.0, 2.0, 2.0, 2.0, 2.0]])

    band_3 = np.array([
        [3.0, 3.0, 3.0, 3.0, 3.0],
        [3.0, 3.0, 3.0, 3.0, 3.0],
        [3.0, 3.0, 3.0, 3.0, 3.0],
        [3.0, 3.0, 3.0, 3.0, 3.0],
        [3.0, 3.0, 3.0, 3.0, 3.0]])

    bands = np.array([band_1, band_2, band_3])

    layer = [(SpatialKey(0, 0), Tile(bands, 'FLOAT', -1.0)),
             (SpatialKey(1, 0), Tile(bands, 'FLOAT', -1.0,)),
             (SpatialKey(0, 1), Tile(bands, 'FLOAT', -1.0,)),
             (SpatialKey(1, 1), Tile(bands, 'FLOAT', -1.0,))]

    rdd = BaseTestClass.pysc.parallelize(layer)

    extent = {'xmin': 0.0, 'ymin': 0.0, 'xmax': 33.0, 'ymax': 33.0}
    layout = {'layoutCols': 2, 'layoutRows': 2, 'tileCols': 5, 'tileRows': 5}
    metadata = {'cellType': 'float32ud-1.0',
                'extent': extent,
                'crs': '+proj=longlat +datum=WGS84 +no_defs ',
                'bounds': {
                    'minKey': {'col': 0, 'row': 0},
                    'maxKey': {'col': 1, 'row': 1}},
                'layoutDefinition': {
                    'extent': extent,
                    'tileLayout': {'tileCols': 5, 'tileRows': 5, 'layoutCols': 2, 'layoutRows': 2}}}

    tiled_raster_rdd = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd, metadata, 5)

    layer2 = [(ProjectedExtent(Extent(0, 0, 1, 1), 3857), Tile(bands, 'FLOAT', -1.0)),
              (ProjectedExtent(Extent(1, 0, 2, 1), 3857), Tile(bands, 'FLOAT', -1.0)),
              (ProjectedExtent(Extent(0, 1, 1, 2), 3857), Tile(bands, 'FLOAT', -1.0)),
              (ProjectedExtent(Extent(1, 1, 2, 2), 3857), Tile(bands, 'FLOAT', -1.0))]
    rdd2 = BaseTestClass.pysc.parallelize(layer2)
    raster_rdd = RasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd2)

    @pytest.fixture(autouse=True)
    def tearDown(self):
        yield
        BaseTestClass.pysc._gateway.close()

    def test_bands_invalid(self):
        with pytest.raises(TypeError):
            self.tiled_raster_rdd.bands("hello").to_numpy_rdd().first()[1]

    def test_bands_int_tiled(self):
        actual = self.tiled_raster_rdd.bands(1).to_numpy_rdd().first()[1]
        expected = np.array(self.band_2)

        self.assertTrue((expected == actual.cells).all())

    def test_bands_int_raster(self):
        actual = self.raster_rdd.bands(1).to_numpy_rdd().first()[1]
        expected = np.array(self.band_2)

        self.assertTrue((expected == actual.cells).all())

    def test_bands_tuple_tiled(self):
        actual = self.tiled_raster_rdd.bands((1, 2)).to_numpy_rdd().first()[1]
        expected = np.array([self.band_2, self.band_3])

        self.assertTrue((expected == actual.cells).all())

    def test_bands_tuple_raster(self):
        actual = self.raster_rdd.bands((1, 2)).to_numpy_rdd().first()[1]
        expected = np.array([self.band_2, self.band_3])

        self.assertTrue((expected == actual.cells).all())

    def test_bands_list_tiled(self):
        actual = self.tiled_raster_rdd.bands([0, 2]).to_numpy_rdd().first()[1]
        expected = np.array([self.band_1, self.band_3])

        self.assertTrue((expected == actual.cells).all())

    def test_bands_list_raster(self):
        actual = self.raster_rdd.bands([0, 2]).to_numpy_rdd().first()[1]
        expected = np.array([self.band_1, self.band_3])

        self.assertTrue((expected == actual.cells).all())

    def test_band_range_tiled(self):
        actual = self.tiled_raster_rdd.bands(range(0, 3)).to_numpy_rdd().first()[1]
        expected = np.array([self.band_1, self.band_2, self.band_3])

        self.assertTrue((expected == actual.cells).all())

    def test_band_range_raster(self):
        actual = self.raster_rdd.bands(range(0, 3)).to_numpy_rdd().first()[1]
        expected = np.array([self.band_1, self.band_2, self.band_3])

        self.assertTrue((expected == actual.cells).all())

    def test_map_tiles_func_tiled(self):
        def test_func(tile):
            cells = tile.cells
            return Tile((cells[0] + cells[1]) / cells[2], tile.cell_type, tile.no_data_value)

        actual = self.tiled_raster_rdd.map_tiles(test_func).to_numpy_rdd().first()[1]
        expected = np.array([self.band_1])

        self.assertTrue((expected == actual.cells).all())

    def test_map_tiles_lambda_tiled(self):
        mapped_layer = self.tiled_raster_rdd.map_tiles(lambda tile: Tile(tile.cells[0], tile.cell_type, tile.no_data_value))
        actual = mapped_layer.to_numpy_rdd().first()[1]
        expected = np.array([self.band_1])

        self.assertEqual(mapped_layer.zoom_level, self.tiled_raster_rdd.zoom_level)
        self.assertTrue((expected == actual.cells).all())

    def test_map_cells_func_raster(self):
        def test_func(cells, nd):
            cells[cells >= 3.0] = nd
            return cells

        actual = self.raster_rdd.map_cells(test_func).to_numpy_rdd().first()[1]

        negative_band = np.array([
            [-1.0, -1.0, -1.0, -1.0, -1.0],
            [-1.0, -1.0, -1.0, -1.0, -1.0],
            [-1.0, -1.0, -1.0, -1.0, -1.0],
            [-1.0, -1.0, -1.0, -1.0, -1.0],
            [-1.0, -1.0, -1.0, -1.0, -1.0]])

        expected = np.array([self.band_1, self.band_2, negative_band])

        self.assertTrue((expected == actual.cells).all())

    def test_map_cells_lambda_raster(self):
        actual = self.raster_rdd.map_cells(lambda cells, nd: cells + nd).to_numpy_rdd().first()[1]

        self.assertTrue((0.0 == actual.cells[0, :]).all())
        self.assertTrue((self.band_1 == actual.cells[1, :]).all())
        self.assertTrue((self.band_2 == actual.cells[2, :]).all())

    def test_map_cells_func_tiled(self):
        def test_func(cells, nd):
            cells[cells >= 3.0] = nd
            return cells

        actual = self.tiled_raster_rdd.map_cells(test_func).to_numpy_rdd().first()[1]

        negative_band = np.array([
            [-1.0, -1.0, -1.0, -1.0, -1.0],
            [-1.0, -1.0, -1.0, -1.0, -1.0],
            [-1.0, -1.0, -1.0, -1.0, -1.0],
            [-1.0, -1.0, -1.0, -1.0, -1.0],
            [-1.0, -1.0, -1.0, -1.0, -1.0]])

        expected = np.array([self.band_1, self.band_2, negative_band])

        self.assertTrue((expected == actual.cells).all())

    def test_map_cells_lambda_tiled(self):
        mapped_layer = self.tiled_raster_rdd.map_cells(lambda cells, nd: cells + nd)
        actual = mapped_layer.to_numpy_rdd().first()[1]

        self.assertTrue((0.0 == actual.cells[0, :]).all())
        self.assertTrue((self.band_1 == actual.cells[1, :]).all())
        self.assertTrue((self.band_2 == actual.cells[2, :]).all())
        self.assertEqual(mapped_layer.zoom_level, self.tiled_raster_rdd.zoom_level)
Example #8
0
class ToSpatialLayerTest(BaseTestClass):
    band_1 = np.array([
        [1.0, 1.0, 1.0, 1.0, 1.0],
        [1.0, 1.0, 1.0, 1.0, 1.0],
        [1.0, 1.0, 1.0, 1.0, 1.0],
        [1.0, 1.0, 1.0, 1.0, 1.0],
        [1.0, 1.0, 1.0, 1.0, 1.0]])

    band_2 = np.array([
        [2.0, 2.0, 2.0, 2.0, 2.0],
        [2.0, 2.0, 2.0, 2.0, 2.0],
        [2.0, 2.0, 2.0, 2.0, 2.0],
        [2.0, 2.0, 2.0, 2.0, 2.0],
        [2.0, 2.0, 2.0, 2.0, 2.0]])

    bands = np.array([band_1, band_2])
    time = datetime.datetime.strptime("2016-08-24T09:00:00Z", '%Y-%m-%dT%H:%M:%SZ')

    layer = [(SpaceTimeKey(0, 0, time), Tile(bands, 'FLOAT', -1.0)),
             (SpaceTimeKey(1, 0, time), Tile(bands, 'FLOAT', -1.0,)),
             (SpaceTimeKey(0, 1, time), Tile(bands, 'FLOAT', -1.0,)),
             (SpaceTimeKey(1, 1, time), Tile(bands, 'FLOAT', -1.0,))]

    rdd = BaseTestClass.pysc.parallelize(layer)

    extent = {'xmin': 0.0, 'ymin': 0.0, 'xmax': 33.0, 'ymax': 33.0}
    layout = {'layoutCols': 2, 'layoutRows': 2, 'tileCols': 5, 'tileRows': 5}
    metadata = {'cellType': 'float32ud-1.0',
                'extent': extent,
                'crs': '+proj=longlat +datum=WGS84 +no_defs ',
                'bounds': {
                    'minKey': {'col': 0, 'row': 0, 'instant': 1},
                    'maxKey': {'col': 1, 'row': 1, 'instant': 1}},
                'layoutDefinition': {
                    'extent': extent,
                    'tileLayout': {'tileCols': 5, 'tileRows': 5, 'layoutCols': 2, 'layoutRows': 2}}}

    tiled_raster_rdd = TiledRasterLayer.from_numpy_rdd(LayerType.SPACETIME, rdd, metadata)

    layer2 = [(TemporalProjectedExtent(Extent(0, 0, 1, 1), epsg=3857, instant=time), Tile(bands, 'FLOAT', -1.0)),
              (TemporalProjectedExtent(Extent(1, 0, 2, 1), epsg=3857, instant=time), Tile(bands, 'FLOAT', -1.0)),
              (TemporalProjectedExtent(Extent(0, 1, 1, 2), epsg=3857, instant=time), Tile(bands, 'FLOAT', -1.0)),
              (TemporalProjectedExtent(Extent(1, 1, 2, 2), epsg=3857, instant=time), Tile(bands, 'FLOAT', -1.0))]
    rdd2 = BaseTestClass.pysc.parallelize(layer2)
    raster_rdd = RasterLayer.from_numpy_rdd(LayerType.SPACETIME, rdd2)

    @pytest.fixture(autouse=True)
    def tearDown(self):
        yield
        BaseTestClass.pysc._gateway.close()

    # This test should be moved to a more appropriate file once more spatial-temporal
    # tests are made.
    def test_spatial_metadata(self):
        metadata = self.raster_rdd.collect_metadata()
        min_key = metadata.bounds.minKey
        max_key = metadata.bounds.maxKey

        self.assertEqual(min_key.instant, self.time)
        self.assertEqual(max_key.instant, self.time)

    def test_to_spatial_raster_layer(self):
        actual = [k for k, v in self.raster_rdd.to_spatial_layer().to_numpy_rdd().collect()]

        expected = [
            ProjectedExtent(Extent(0, 0, 1, 1), 3857),
            ProjectedExtent(Extent(1, 0, 2, 1), 3857),
            ProjectedExtent(Extent(0, 1, 1, 2), 3857),
            ProjectedExtent(Extent(1, 1, 2, 2), 3857)
        ]

        for a, e in zip(actual, expected):
            self.assertEqual(a, e)

    def test_to_spatial_tiled_layer(self):
        actual = [k for k, v in self.tiled_raster_rdd.to_spatial_layer().to_numpy_rdd().collect()]

        expected = [
            SpatialKey(0, 0),
            SpatialKey(1, 0),
            SpatialKey(0, 1),
            SpatialKey(1, 1)
        ]

        for a, e in zip(actual, expected):
            self.assertEqual(a, e)
Example #9
0
class ToSpatialLayerTest(BaseTestClass):
    band_1 = np.array([[1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0],
                       [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0],
                       [1.0, 1.0, 1.0, 1.0, 1.0]])

    band_2 = np.array([[2.0, 2.0, 2.0, 2.0, 2.0], [2.0, 2.0, 2.0, 2.0, 2.0],
                       [2.0, 2.0, 2.0, 2.0, 2.0], [2.0, 2.0, 2.0, 2.0, 2.0],
                       [2.0, 2.0, 2.0, 2.0, 2.0]])

    tile_1 = Tile.from_numpy_array(np.array([band_1]))
    tile_2 = Tile.from_numpy_array(np.array([band_2]))
    time_1 = datetime.datetime.strptime("2016-08-24T09:00:00Z",
                                        '%Y-%m-%dT%H:%M:%SZ')
    time_2 = datetime.datetime.strptime("2017-08-24T09:00:00Z",
                                        '%Y-%m-%dT%H:%M:%SZ')

    layer = [(SpaceTimeKey(0, 0, time_1), tile_1),
             (SpaceTimeKey(1, 0, time_1), tile_1),
             (SpaceTimeKey(0, 1, time_1), tile_1),
             (SpaceTimeKey(1, 1, time_1), tile_1),
             (SpaceTimeKey(0, 0, time_2), tile_2),
             (SpaceTimeKey(1, 0, time_2), tile_2),
             (SpaceTimeKey(0, 1, time_2), tile_2),
             (SpaceTimeKey(1, 1, time_2), tile_2)]

    rdd = BaseTestClass.pysc.parallelize(layer)

    extent = {'xmin': 0.0, 'ymin': 0.0, 'xmax': 33.0, 'ymax': 33.0}
    layout = {'layoutCols': 2, 'layoutRows': 2, 'tileCols': 5, 'tileRows': 5}
    metadata = {
        'cellType': 'float32ud-1.0',
        'extent': extent,
        'crs': '+proj=longlat +datum=WGS84 +no_defs ',
        'bounds': {
            'minKey': {
                'col': 0,
                'row': 0,
                'instant': _convert_to_unix_time(time_1)
            },
            'maxKey': {
                'col': 1,
                'row': 1,
                'instant': _convert_to_unix_time(time_2)
            }
        },
        'layoutDefinition': {
            'extent': extent,
            'tileLayout': {
                'tileCols': 5,
                'tileRows': 5,
                'layoutCols': 2,
                'layoutRows': 2
            }
        }
    }

    tiled_raster_rdd = TiledRasterLayer.from_numpy_rdd(LayerType.SPACETIME,
                                                       rdd, metadata)

    layer2 = [(TemporalProjectedExtent(Extent(0, 0, 1, 1),
                                       epsg=3857,
                                       instant=time_1), tile_1),
              (TemporalProjectedExtent(Extent(1, 0, 2, 1),
                                       epsg=3857,
                                       instant=time_1), tile_1),
              (TemporalProjectedExtent(Extent(0, 1, 1, 2),
                                       epsg=3857,
                                       instant=time_1), tile_1),
              (TemporalProjectedExtent(Extent(1, 1, 2, 2),
                                       epsg=3857,
                                       instant=time_1), tile_1),
              (TemporalProjectedExtent(Extent(1, 0, 2, 1),
                                       epsg=3857,
                                       instant=time_2), tile_2),
              (TemporalProjectedExtent(Extent(1, 0, 2, 1),
                                       epsg=3857,
                                       instant=time_2), tile_2),
              (TemporalProjectedExtent(Extent(0, 1, 1, 2),
                                       epsg=3857,
                                       instant=time_2), tile_2),
              (TemporalProjectedExtent(Extent(1, 1, 2, 2),
                                       epsg=3857,
                                       instant=time_2), tile_2)]

    rdd2 = BaseTestClass.pysc.parallelize(layer2)
    raster_rdd = RasterLayer.from_numpy_rdd(LayerType.SPACETIME, rdd2)

    @pytest.fixture(autouse=True)
    def tearDown(self):
        yield
        BaseTestClass.pysc._gateway.close()

    # This test should be moved to a more appropriate file once more spatial-temporal
    # tests are made.
    def test_spatial_metadata(self):
        metadata = self.raster_rdd.collect_metadata()
        min_key = metadata.bounds.minKey
        max_key = metadata.bounds.maxKey

        self.assertEqual(min_key.instant, self.time_1)
        self.assertEqual(max_key.instant, self.time_2)

    def test_to_spatial_raster_layer(self):
        actual = self.raster_rdd.to_spatial_layer().to_numpy_rdd().keys(
        ).collect()

        expected = [
            ProjectedExtent(Extent(0, 0, 1, 1), 3857),
            ProjectedExtent(Extent(1, 0, 2, 1), 3857),
            ProjectedExtent(Extent(0, 1, 1, 2), 3857),
            ProjectedExtent(Extent(1, 1, 2, 2), 3857)
        ]

        for x in actual:
            self.assertTrue(x in expected)

    def test_to_spatial_target_time_raster_layer(self):
        converted = self.raster_rdd.to_spatial_layer(target_time=self.time_1)
        keys = converted.to_numpy_rdd().keys().collect()
        values = converted.to_numpy_rdd().values().collect()

        expected = [
            ProjectedExtent(Extent(0, 0, 1, 1), 3857),
            ProjectedExtent(Extent(1, 0, 2, 1), 3857),
            ProjectedExtent(Extent(0, 1, 1, 2), 3857),
            ProjectedExtent(Extent(1, 1, 2, 2), 3857)
        ]

        for x in keys:
            self.assertTrue(x in expected)

        for x in values:
            self.assertEqual(x.cells.shape, self.tile_1.cells.shape)
            self.assertTrue((x.cells == 1.0).all())

    def test_to_spatial_tiled_layer(self):
        actual = self.tiled_raster_rdd.to_spatial_layer().to_numpy_rdd().keys(
        ).collect()

        expected = [
            SpatialKey(0, 0),
            SpatialKey(1, 0),
            SpatialKey(0, 1),
            SpatialKey(1, 1)
        ]

        for x in actual:
            self.assertTrue(x in expected)

    def test_to_spatial_target_time_tiled_layer(self):
        converted = self.tiled_raster_rdd.to_spatial_layer(
            target_time=self.time_2)
        keys = converted.to_numpy_rdd().keys().collect()
        values = converted.to_numpy_rdd().values().collect()

        expected = [
            SpatialKey(0, 0),
            SpatialKey(1, 0),
            SpatialKey(0, 1),
            SpatialKey(1, 1)
        ]

        for x in keys:
            self.assertTrue(x in expected)

        for x in values:
            self.assertEqual(x.cells.shape, self.tile_2.cells.shape)
            self.assertTrue((x.cells == 2.0).all())
Example #10
0
class FilterByTimesTest(BaseTestClass):
    band = np.array([[1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0],
                     [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0],
                     [1.0, 1.0, 1.0, 1.0, 1.0]])

    tile = Tile.from_numpy_array(band)
    time_1 = datetime.datetime.strptime("2016-08-24T09:00:00Z",
                                        '%Y-%m-%dT%H:%M:%SZ')
    time_2 = datetime.datetime.strptime("2017-08-24T09:00:00Z",
                                        '%Y-%m-%dT%H:%M:%SZ')
    time_3 = datetime.datetime.strptime("2017-10-17T09:00:00Z",
                                        '%Y-%m-%dT%H:%M:%SZ')

    layer = [(SpaceTimeKey(0, 0, time_1), tile),
             (SpaceTimeKey(1, 0, time_1), tile),
             (SpaceTimeKey(0, 1, time_1), tile),
             (SpaceTimeKey(1, 1, time_1), tile),
             (SpaceTimeKey(0, 0, time_2), tile),
             (SpaceTimeKey(1, 0, time_2), tile),
             (SpaceTimeKey(0, 1, time_2), tile),
             (SpaceTimeKey(1, 1, time_2), tile),
             (SpaceTimeKey(0, 0, time_3), tile),
             (SpaceTimeKey(1, 0, time_3), tile),
             (SpaceTimeKey(0, 1, time_3), tile),
             (SpaceTimeKey(1, 1, time_3), tile)]

    rdd = BaseTestClass.pysc.parallelize(layer)

    extent = {'xmin': 0.0, 'ymin': 0.0, 'xmax': 33.0, 'ymax': 33.0}
    layout = {'layoutCols': 2, 'layoutRows': 2, 'tileCols': 5, 'tileRows': 5}
    metadata = {
        'cellType': 'float32ud-1.0',
        'extent': extent,
        'crs': '+proj=longlat +datum=WGS84 +no_defs ',
        'bounds': {
            'minKey': {
                'col': 0,
                'row': 0,
                'instant': _convert_to_unix_time(time_1)
            },
            'maxKey': {
                'col': 1,
                'row': 1,
                'instant': _convert_to_unix_time(time_3)
            }
        },
        'layoutDefinition': {
            'extent': extent,
            'tileLayout': {
                'tileCols': 5,
                'tileRows': 5,
                'layoutCols': 2,
                'layoutRows': 2
            }
        }
    }

    tiled_raster_rdd = TiledRasterLayer.from_numpy_rdd(LayerType.SPACETIME,
                                                       rdd, metadata)

    layer2 = [(TemporalProjectedExtent(Extent(0, 0, 1, 1),
                                       epsg=3857,
                                       instant=time_1), tile),
              (TemporalProjectedExtent(Extent(1, 0, 2, 1),
                                       epsg=3857,
                                       instant=time_1), tile),
              (TemporalProjectedExtent(Extent(0, 1, 1, 2),
                                       epsg=3857,
                                       instant=time_1), tile),
              (TemporalProjectedExtent(Extent(1, 1, 2, 2),
                                       epsg=3857,
                                       instant=time_1), tile),
              (TemporalProjectedExtent(Extent(1, 0, 2, 1),
                                       epsg=3857,
                                       instant=time_2), tile),
              (TemporalProjectedExtent(Extent(1, 0, 2, 1),
                                       epsg=3857,
                                       instant=time_2), tile),
              (TemporalProjectedExtent(Extent(0, 1, 1, 2),
                                       epsg=3857,
                                       instant=time_2), tile),
              (TemporalProjectedExtent(Extent(1, 1, 2, 2),
                                       epsg=3857,
                                       instant=time_2), tile),
              (TemporalProjectedExtent(Extent(1, 0, 2, 1),
                                       epsg=3857,
                                       instant=time_3), tile),
              (TemporalProjectedExtent(Extent(1, 0, 2, 1),
                                       epsg=3857,
                                       instant=time_3), tile),
              (TemporalProjectedExtent(Extent(0, 1, 1, 2),
                                       epsg=3857,
                                       instant=time_3), tile),
              (TemporalProjectedExtent(Extent(1, 1, 2, 2),
                                       epsg=3857,
                                       instant=time_3), tile)]

    rdd2 = BaseTestClass.pysc.parallelize(layer2)
    raster_rdd = RasterLayer.from_numpy_rdd(LayerType.SPACETIME, rdd2)

    @pytest.fixture(autouse=True)
    def tearDown(self):
        yield
        BaseTestClass.pysc._gateway.close()

    def test_filter_temporal_projected_extent_single_time(self):
        result = self.raster_rdd.filter_by_times([self.time_1])
        expected = self.layer2[:4]
        actual = result.to_numpy_rdd().collect()

        self.assertEqual(len(expected), len(actual))

        for x, y in zip(expected, actual):
            self.assertEqual(x[0], y[0])
            self.assertTrue((x[1].cells == y[1].cells).all())

    def test_filter_temporal_projected_extent_multi_intervals(self):
        result = self.raster_rdd.filter_by_times([self.time_2, self.time_3])
        expected = self.layer2[4:]
        actual = result.to_numpy_rdd().collect()

        self.assertEqual(len(expected), len(actual))

        for x, y in zip(expected, actual):
            self.assertEqual(x[0], y[0])
            self.assertTrue((x[1].cells == y[1].cells).all())

    def test_filter_spacetime_key_single_time(self):
        result = self.tiled_raster_rdd.filter_by_times([self.time_3])
        expected = self.layer[8:]
        actual = result.to_numpy_rdd().collect()

        self.assertEqual(len(expected), len(actual))

        for x, y in zip(expected, actual):
            self.assertEqual(x[0], y[0])
            self.assertTrue((x[1].cells == y[1].cells).all())

    def test_filter_spacetime_key_multi_intervals(self):
        result = self.tiled_raster_rdd.filter_by_times(
            [self.time_1, self.time_2])
        expected = self.layer[:8]
        actual = result.to_numpy_rdd().collect()

        self.assertEqual(len(expected), len(actual))

        for x, y in zip(expected, actual):
            self.assertEqual(x[0], y[0])
            self.assertTrue((x[1].cells == y[1].cells).all())