コード例 #1
0
    def create_spatial_layer(self):
        cells = np.array([self.first, self.second], dtype='int')
        tile = Tile.from_numpy_array(cells, -1)

        layer = [(SpatialKey(0, 0), tile), (SpatialKey(1, 0), tile),
                 (SpatialKey(0, 1), tile), (SpatialKey(1, 1), tile)]

        rdd = BaseTestClass.pysc.parallelize(layer)

        metadata = {
            'cellType': 'int32ud-1',
            'extent': self.extent,
            'crs': '+proj=longlat +datum=WGS84 +no_defs ',
            'bounds': {
                'minKey': {
                    'col': 0,
                    'row': 0
                },
                'maxKey': {
                    'col': 1,
                    'row': 1
                }
            },
            'layoutDefinition': {
                'extent': self.extent,
                'tileLayout': self.layout
            }
        }

        return TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd,
                                               metadata)
コード例 #2
0
    def test_spatial_keys(self):
        keys = [
            SpatialKey(0, 0),
            SpatialKey(0, 1),
            SpatialKey(1, 0),
            SpatialKey(1, 1)
        ]

        key_layer = [(keys[0], self.tile), (keys[1], self.tile),
                     (keys[2], self.tile), (keys[3], self.tile)]

        bounds = Bounds(keys[0], keys[3])

        md = Metadata(bounds=bounds,
                      crs=self.md_proj,
                      cell_type=self.ct,
                      extent=self.extent,
                      layout_definition=self.ld)

        rdd = self.pysc.parallelize(key_layer)
        layer = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd, md)

        actual = layer.collect_keys()

        for x in actual:
            self.assertTrue(x in keys)
コード例 #3
0
class CellValueCountsTest(BaseTestClass):
    pysc = BaseTestClass.pysc

    cells = np.array([[
        [1.0, 1.0, 1.0, 1.0, 1.0],
        [2.0, 2.0, 2.0, 2.0, 2.0],
        [3.0, 3.0, 3.0, 3.0, 3.0],
        [4.0, 4.0, 4.0, 4.0, 4.0],
        [5.0, 5.0, 5.0, 5.0, 5.0]]])

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

    rdd = pysc.parallelize(layer)

    extent = {'xmin': 0.0, 'ymin': 0.0, 'xmax': 4.0, 'ymax': 4.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': layout}}

    raster_rdd = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd, metadata)

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

    def test_counts_with_no_area(self):
        actual = self.raster_rdd.get_cell_value_counts()
        expected = {
            1: 20,
            2: 20,
            3: 20,
            4: 20,
            5: 20
        }

        self.assertDictEqual(actual, expected)

    def test_counts_with_polygon(self):
        area_of_interest = box(0.0, 0.0, 2.0, 2.0)
        actual = self.raster_rdd.get_cell_value_counts(area_of_interest)
        expected = {
            1: 5,
            2: 5,
            3: 5,
            4: 5,
            5: 5
        }

        self.assertDictEqual(actual, expected)
コード例 #4
0
ファイル: stitch_test.py プロジェクト: lossyrob/geopyspark
class StitchTest(BaseTestClass):
    cells = 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, 0.0]]])

    layer = [(SpatialKey(0, 0), Tile(cells, 'FLOAT', -1.0)),
             (SpatialKey(1, 0), Tile(
                 cells,
                 'FLOAT',
                 -1.0,
             )), (SpatialKey(0, 1), Tile(
                 cells,
                 'FLOAT',
                 -1.0,
             )), (SpatialKey(1, 1), Tile(
                 cells,
                 '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
            }
        }
    }

    raster_rdd = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd,
                                                 metadata)

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

    def test_stitch(self):
        result = self.raster_rdd.stitch()
        self.assertTrue(result.cells.shape == (1, 10, 10))
コード例 #5
0
    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)
コード例 #6
0
ファイル: mask_test.py プロジェクト: zfcwbl/geopyspark
class MaskTest(BaseTestClass):
    pysc = BaseTestClass.pysc
    cells = np.zeros((1, 2, 2))
    cells.fill(1)

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

    rdd = pysc.parallelize(layer)

    extent = {'xmin': 0.0, 'ymin': 0.0, 'xmax': 4.0, 'ymax': 4.0}
    layout = {'layoutCols': 2, 'layoutRows': 2, 'tileCols': 2, 'tileRows': 2}
    metadata = {'cellType': 'float32ud-1.0',
                'extent': extent,
                'crs': 4326,
                'bounds': {
                    'minKey': {'col': 0, 'row': 0},
                    'maxKey': {'col': 1, 'row': 1}},
                'layoutDefinition': {
                    'extent': extent,
                    'tileLayout': layout}}

    geoms = [box(0.0, 0.0, 2.0, 2.0), box(3.0, 3.0, 4.0, 4.0)]
    raster_rdd = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd, Metadata.from_dict(metadata))

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

    def test_geotrellis_mask(self):
        result = self.raster_rdd.mask(geometries=self.geoms).to_numpy_rdd()
        n = result.map(lambda kv: np.sum(kv[1].cells)).reduce(lambda a, b: a + b)

        self.assertEqual(n, 2.0)

    def test_rdd_mask_no_partition_strategy(self):
        rdd = BaseTestClass.pysc.parallelize(self.geoms)

        result = self.raster_rdd.mask(rdd, options=RasterizerOptions(True, 'PixelIsArea')).to_numpy_rdd()
        n = result.map(lambda kv: np.sum(kv[1].cells)).reduce(lambda a, b: a + b)

        self.assertEqual(n, 2.0)

    def test_rdd_mask_with_partition_strategy(self):
        rdd = BaseTestClass.pysc.parallelize(self.geoms)

        result = self.raster_rdd.mask(rdd, partition_strategy=SpatialPartitionStrategy()).to_numpy_rdd()
        n = result.map(lambda kv: np.sum(kv[1].cells)).reduce(lambda a, b: a + b)

        self.assertEqual(n, 2.0)
コード例 #7
0
ファイル: to_spatial_test.py プロジェクト: zfcwbl/geopyspark
    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)
コード例 #8
0
ファイル: catalog_test.py プロジェクト: dyeden/geopyspark
    def test_query3(self):
        intersection = box(8348915.46680623, 543988.943201519, 8348915.4669,
                           543988.943201520).wkb
        queried = query(self.uri, self.layer_name, 11, intersection)

        self.assertEqual(queried.to_numpy_rdd().first()[0],
                         SpatialKey(1450, 996))
コード例 #9
0
    def test_query2(self):
        intersection = Extent(8348915.46680623, 543988.943201519, 8348915.4669, 543988.943201520)
        queried = query(self.uri, self.layer_name,
                        11, intersection,
                        query_proj=3857)

        self.assertEqual(queried.to_numpy_rdd().first()[0], SpatialKey(1450, 996))
コード例 #10
0
ファイル: union_test.py プロジェクト: 82d28a/geopyspark
    def test_union_of_tiled_raster_layers(self):
        result = union(self.tiled_layer_1, self.tiled_layer_2)

        bounds_1 = self.tiled_layer_1.layer_metadata.bounds
        bounds_2 = self.tiled_layer_2.layer_metadata.bounds

        min_col = min(bounds_1.minKey.col, bounds_2.minKey.col)
        min_row = min(bounds_1.minKey.row, bounds_2.minKey.row)
        max_col = max(bounds_1.maxKey.col, bounds_2.maxKey.col)
        max_row = max(bounds_1.maxKey.row, bounds_2.maxKey.row)

        min_key = SpatialKey(min_col, min_row)
        max_key = SpatialKey(max_col, max_row)

        self.assertTrue(result.srdd.rdd().count(), 2)
        self.assertEqual(result.layer_metadata.bounds,
                         Bounds(min_key, max_key))
コード例 #11
0
ファイル: hillshade_test.py プロジェクト: zfcwbl/geopyspark
class HillshadeTest(BaseTestClass):
    cells = np.array([[[1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
                       [1.0, 3.0, 3.0, 2.0, 1.0, -1.0],
                       [1.0, 1.0, 3.0, 2.0, 2.0, 2.0],
                       [1.0, 2.0, 2.0, 2.0, 2.0, 2.0],
                       [1.0, 1.0, 1.0, 2.0, 2.0, 2.0],
                       [1.0, 1.0, 1.0, 1.0, 1.0, 2.0]]])

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

    rdd = BaseTestClass.pysc.parallelize(layer)

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

    raster_rdd = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd,
                                                 metadata)

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

    def test_hillshade(self):
        calc = zfactor_lat_lng_calculator(Unit.METERS)
        result = hillshade(self.raster_rdd,
                           calc,
                           band=0,
                           azimuth=99.0,
                           altitude=33.0)

        data = result.to_numpy_rdd().first()[1].cells[0][0][0]
        self.assertEqual(data, 63)
コード例 #12
0
ファイル: to_spatial_test.py プロジェクト: zfcwbl/geopyspark
    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())
コード例 #13
0
ファイル: catalog_test.py プロジェクト: yoyodev/geopyspark
    def test_query_crs(self):
        intersection = box(74.99958369653905, 4.8808219582513095,
                           74.99958369738141, 4.880821958251324)
        queried = query(self.uri,
                        self.layer_name,
                        11,
                        intersection,
                        query_proj=4326)

        self.assertEqual(queried.to_numpy_rdd().first()[0],
                         SpatialKey(1450, 996))
コード例 #14
0
def from_pb_spatial_key(pb_spatial_key):
    """Creates a ``SpatialKey`` from a ``ProtoSpatialKey``.

    Args:
        pb_spatial_key (ProtoSpatialKey): An instance of ``ProtoSpatialKey``.

    Returns:
        :obj:`~geopyspark.geotrellis.SpatialKey`
    """

    return SpatialKey(col=pb_spatial_key.col, row=pb_spatial_key.row)
コード例 #15
0
class SlopeTest(BaseTestClass):
    cells = np.array([[
        [0.0, 0.0, 0.0, 0.0, 0.0],
        [0.0, 0.0, 0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0, 1.0, 0.0],
        [0.0, 0.0, 1.0, 1.0, 0.0],
        [0.0, 0.0, 0.0, 0.0, 0.0]]])

    tile = Tile.from_numpy_array(cells, -1.0)

    layer = [(SpatialKey(0, 0), tile),
             (SpatialKey(1, 0), tile),
             (SpatialKey(0, 1), tile),
             (SpatialKey(1, 1), 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},
                    'maxKey': {'col': 1, 'row': 1}},
                'layoutDefinition': {
                    'extent': extent,
                    'tileLayout': {'tileCols': 5, 'tileRows': 5, 'layoutCols': 2, 'layoutRows': 2}}}

    raster_rdd = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd, metadata)

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

    def test_slope(self):

        calc = zfactor_lat_lng_calculator(Unit.METERS)
        result = self.raster_rdd.slope(calc).to_numpy_rdd().values().first().cells

        self.assertEqual(result[0, 0, 0], 0.0)
コード例 #16
0
    def test_query_crs(self):
        intersection = box(8348915.46680623, 543988.943201519, 8348915.4669,
                           543988.943201520)
        queried = query(LayerType.SPATIAL,
                        self.uri,
                        self.layer_name,
                        11,
                        intersection,
                        proj_query=3857)

        self.assertEqual(queried.to_numpy_rdd().first()[0],
                         SpatialKey(1450, 996))
コード例 #17
0
 def test_query_partitions(self):
     intersection = box(8348915.46680623, 543988.943201519, 8348915.4669,
                        543988.943201520)
     queried = query(BaseTestClass.geopysc,
                     SPATIAL,
                     self.uri,
                     self.layer_name,
                     11,
                     intersection,
                     numPartitions=2)
     self.assertEqual(queried.to_numpy_rdd().first()[0],
                      SpatialKey(1450, 996))
コード例 #18
0
    def test_segment_tiles(self):
        # GeoTrellis will read GeoTiff Segments given no window size
        # Retile them to match Rasterio read and check the cell values
        geotrellis_tiles = self.read_multiband_geotrellis()\
                               .tile_to_layout(LocalLayout(512))\
                               .to_numpy_rdd().collect()
        # TODO: assert there is only one geotrellis tile
        geotrellis_tile = dict(geotrellis_tiles)[SpatialKey(0, 0)]

        rasterio_tiles = self.read_geotiff_rasterio([self.file_path], False)
        self.assertEquals(len(rasterio_tiles), 1)
        rasterio_tile = rasterio_tiles[0]

        self.assertTilesEqual(geotrellis_tile.cells, rasterio_tile['cells'])
コード例 #19
0
ファイル: merge_test.py プロジェクト: zfcwbl/geopyspark
    def test_spatial_keys(self):
        keys = [SpatialKey(0, 0), SpatialKey(0, 1)]

        key_layer = [(keys[0], self.tile_1), (keys[1], self.tile_1),
                     (keys[0], self.tile_2), (keys[1], self.tile_2)]

        bounds = Bounds(keys[0], keys[1])

        md = Metadata(bounds=bounds,
                      crs=self.md_proj,
                      cell_type=self.ct,
                      extent=self.extent,
                      layout_definition=self.ld)

        rdd = self.pysc.parallelize(key_layer)
        layer = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd, md)

        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())
コード例 #20
0
    def test_from_histogram(self):
        extent = {'xmin': 0.0, 'ymin': 0.0, 'xmax': 33.0, 'ymax': 33.0}
        layout = {
            'layoutCols': 1,
            'layoutRows': 1,
            'tileCols': 2,
            'tileRows': 4
        }
        metadata = {
            'cellType': 'float32ud-1.0',
            'extent': extent,
            'crs': '+proj=longlat +datum=WGS84 +no_defs ',
            'bounds': {
                'minKey': {
                    'col': 0,
                    'row': 0
                },
                'maxKey': {
                    'col': 0,
                    'row': 0
                }
            },
            'layoutDefinition': {
                'extent': extent,
                'tileLayout': {
                    'tileCols': 2,
                    'tileRows': 4,
                    'layoutCols': 1,
                    'layoutRows': 1
                }
            }
        }
        spatial_key = SpatialKey(0, 0)
        arr = np.array([[[1.0, 5.0, 2.0, 3.0], [4.0, 6.0, 7.0, 0.0]]],
                       dtype=float)
        tile = Tile(arr, 'FLOAT', -500)
        rdd = BaseTestClass.pysc.parallelize([(spatial_key, tile)])
        tiled = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd,
                                                metadata)
        hist = tiled.get_histogram()

        color_list = self.color_list
        result = ColorMap.from_histogram(hist, color_list)
        self.assertTrue(isinstance(result, ColorMap))
コード例 #21
0
class NormalizeTest(BaseTestClass):
    cells = 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, 0.0]]])

    layer = [(SpatialKey(0, 0), Tile(cells + 0, 'FLOAT', -1.0)),
             (SpatialKey(1, 0), Tile(
                 cells + 1,
                 'FLOAT',
                 -1.0,
             )), (SpatialKey(0, 1), Tile(
                 cells + 2,
                 'FLOAT',
                 -1.0,
             )), (SpatialKey(1, 1), Tile(
                 cells + 3,
                 '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
            }
        }
    }

    raster_rdd = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd,
                                                 metadata)

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

    def test_normalize_all_parameters(self):
        normalized = self.raster_rdd.normalize(old_min=0.0,
                                               old_max=4.0,
                                               new_min=5.0,
                                               new_max=10.0)

        self.assertEqual(normalized.get_min_max(), (5.0, 10.0))

    def test_normalize_no_optinal_parameters(self):
        normalized = self.raster_rdd.normalize(new_min=5.0, new_max=10.0)

        self.assertEqual(normalized.get_min_max(), (5.0, 10.0))

    def test_normalize_old_min(self):
        normalized = self.raster_rdd.normalize(old_min=-1,
                                               new_min=5.0,
                                               new_max=10.0)

        self.assertEqual(normalized.get_min_max(), (6.0, 10.0))

    def test_normalize_old_max(self):
        normalized = self.raster_rdd.normalize(old_max=5.0,
                                               new_min=5.0,
                                               new_max=10.0)

        self.assertEqual(normalized.get_min_max(), (5.0, 9.0))
コード例 #22
0
ファイル: local_ops_test.py プロジェクト: lossyrob/geopyspark
class LocalOpertaionsTest(BaseTestClass):
    extent = {'xmin': 0.0, 'ymin': 0.0, 'xmax': 33.0, 'ymax': 33.0}
    layout = {'layoutCols': 1, 'layoutRows': 1, 'tileCols': 4, 'tileRows': 4}
    metadata = {'cellType': 'float32ud-1.0',
                'extent': extent,
                'crs': '+proj=longlat +datum=WGS84 +no_defs ',
                'bounds': {
                    'minKey': {'col': 0, 'row': 0},
                    'maxKey': {'col': 0, 'row': 0}},
                'layoutDefinition': {
                    'extent': extent,
                    'tileLayout': {'tileCols': 4, 'tileRows': 4, 'layoutCols': 1, 'layoutRows': 1}}}

    spatial_key = SpatialKey(0, 0)

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

    def test_add_int(self):
        arr = np.zeros((1, 4, 4))

        tile = Tile(arr, 'FLOAT', -500)
        rdd = BaseTestClass.pysc.parallelize([(self.spatial_key, tile)])
        tiled = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd, self.metadata)

        result = tiled + 1
        actual = result.to_numpy_rdd().first()[1].cells

        self.assertTrue((actual == 1).all())

    def test_subtract_double(self):
        arr = np.array([[[1.0, 1.0, 1.0, 1.0],
                         [2.0, 2.0, 2.0, 2.0],
                         [3.0, 3.0, 3.0, 3.0],
                         [4.0, 4.0, 4.0, 4.0]]], dtype=float)

        tile = Tile(arr, 'FLOAT', -500)
        rdd = BaseTestClass.pysc.parallelize([(self.spatial_key, tile)])
        tiled = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd, self.metadata)

        result = 5.0 - tiled
        actual = result.to_numpy_rdd().first()[1].cells

        expected = np.array([[[4.0, 4.0, 4.0, 4.0],
                              [3.0, 3.0, 3.0, 3.0],
                              [2.0, 2.0, 2.0, 2.0],
                              [1.0, 1.0, 1.0, 1.0]]], dtype=float)

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

    def test_multiply_double(self):
        arr = np.array([[[1.0, 1.0, 1.0, 1.0],
                         [2.0, 2.0, 2.0, 2.0],
                         [3.0, 3.0, 3.0, 3.0],
                         [4.0, 4.0, 4.0, 4.0]]], dtype=float)

        tile = Tile(arr, 'FLOAT', float('nan'))
        rdd = BaseTestClass.pysc.parallelize([(self.spatial_key, tile)])
        tiled = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd, self.metadata)

        result = 5.0 * tiled
        actual = result.to_numpy_rdd().first()[1].cells

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

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

    def test_divide_tiled_rdd(self):
        arr = 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],
                         [5.0, 5.0, 5.0, 5.0]]], dtype=float)

        divider = 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]]], dtype=float)

        tile = Tile(arr, 'FLOAT', float('nan'))
        tile2 = Tile(divider, 'FLOAT', float('nan'))

        rdd = BaseTestClass.pysc.parallelize([(self.spatial_key, tile)])
        rdd2 = BaseTestClass.pysc.parallelize([(self.spatial_key, tile2)])

        tiled = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd, self.metadata)
        tiled2 = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd2, self.metadata)

        result = tiled / tiled2
        actual = result.to_numpy_rdd().first()[1].cells

        self.assertTrue((actual == 5.0).all())

    def test_combined_operations(self):
        arr = np.array([[[10, 10, 10, 10],
                         [20, 20, 20, 20],
                         [10, 10, 10, 10],
                         [20, 20, 20, 20]]], dtype=int)

        tile = Tile(arr, 'INT', -500)
        rdd = BaseTestClass.pysc.parallelize([(self.spatial_key, tile)])

        tiled = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd, self.metadata)

        result = (tiled + tiled) / 2
        actual = result.to_numpy_rdd().first()[1].cells

        self.assertTrue((actual == arr).all())
コード例 #23
0
class PolygonalSummariesTest(BaseTestClass):
    cells_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, 0.0]]])

    cells_2 = 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, 0.0]]])

    cells = np.array([cells_1, cells_2])

    layer = [(SpatialKey(0, 0), Tile(cells, 'FLOAT', -1.0)),
             (SpatialKey(1, 0), Tile(
                 cells,
                 'FLOAT',
                 -1.0,
             )), (SpatialKey(0, 1), Tile(
                 cells,
                 'FLOAT',
                 -1.0,
             )), (SpatialKey(1, 1), Tile(
                 cells,
                 '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_rdd = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd,
                                                metadata)

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

    def test_polygonal_min(self):
        polygon = Polygon([(0.0, 0.0), (0.0, 33.0), (33.0, 33.0), (33.0, 0.0),
                           (0.0, 0.0)])
        result = self.tiled_rdd.polygonal_min(polygon, float)

        self.assertEqual(result, [0.0, 0.0])

    def test_polygonal_max(self):
        polygon = Polygon([(1.0, 1.0), (1.0, 10.0), (10.0, 10.0), (10.0, 1.0)])
        result = self.tiled_rdd.polygonal_max(polygon, float)

        self.assertEqual(result, [1.0, 1.0])

    def test_polygonal_sum(self):
        polygon = Polygon([(0.0, 0.0), (0.0, 33.0), (33.0, 33.0), (33.0, 0.0),
                           (0.0, 0.0)])
        result = self.tiled_rdd.polygonal_sum(polygon, float)

        self.assertEqual(result, [96.0, 96.0])

    def test_polygonal_mean(self):
        polygon = Polygon([(1.0, 1.0), (1.0, 10.0), (10.0, 10.0), (10.0, 1.0)])
        result = self.tiled_rdd.polygonal_mean(polygon)

        self.assertEqual(result, [1.0, 1.0])
コード例 #24
0
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)
コード例 #25
0
ファイル: focal_test.py プロジェクト: zfcwbl/geopyspark
class FocalTest(BaseTestClass):
    cells = 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, 0.0]]])

    tile = Tile.from_numpy_array(cells, -1.0)

    layer = [(SpatialKey(0, 0), tile), (SpatialKey(1, 0), tile),
             (SpatialKey(0, 1), tile), (SpatialKey(1, 1), 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
            },
            'maxKey': {
                'col': 1,
                'row': 1
            }
        },
        'layoutDefinition': {
            'extent': extent,
            'tileLayout': {
                'tileCols': 5,
                'tileRows': 5,
                'layoutCols': 2,
                'layoutRows': 2
            }
        }
    }

    raster_rdd = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd,
                                                 metadata)

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

    def test_focal_sum_square(self):
        result = self.raster_rdd.focal(
            operation=Operation.SUM,
            neighborhood=Neighborhood.SQUARE,
            param_1=1.0,
            partition_strategy=HashPartitionStrategy())

        self.assertTrue(result.to_numpy_rdd().first()[1].cells[0][1][0] >= 6)

    def test_focal_sum_wedge(self):
        neighborhood = Wedge(radius=1.0, start_angle=0.0, end_angle=180.0)
        self.assertEqual(str(neighborhood), repr(neighborhood))

        result = self.raster_rdd.focal(
            operation=Operation.SUM,
            neighborhood=neighborhood,
            partition_strategy=HashPartitionStrategy(2))

        self.assertTrue(result.to_numpy_rdd().first()[1].cells[0][1][0] >= 3)

    def test_focal_sum_circle(self):
        neighborhood = Circle(radius=1.0)
        self.assertEqual(str(neighborhood), repr(neighborhood))

        result = self.raster_rdd.focal(
            operation=Operation.SUM,
            neighborhood=neighborhood,
            partition_strategy=SpatialPartitionStrategy(2))

        self.assertTrue(result.to_numpy_rdd().first()[1].cells[0][1][0] >= 4)

    def test_focal_sum_nesw(self):
        neighborhood = Nesw(extent=1.0)
        self.assertEqual(str(neighborhood), repr(neighborhood))

        result = self.raster_rdd.focal(
            operation=Operation.SUM,
            neighborhood=neighborhood,
            partition_strategy=SpatialPartitionStrategy())

        self.assertTrue(result.to_numpy_rdd().first()[1].cells[0][1][0] >= 4)

    def test_focal_sum_annulus(self):
        neighborhood = Annulus(inner_radius=0.5, outer_radius=1.5)
        self.assertEqual(str(neighborhood), repr(neighborhood))

        result = self.raster_rdd.focal(operation=Operation.SUM,
                                       neighborhood=neighborhood)

        self.assertTrue(result.to_numpy_rdd().first()[1].cells[0][1][0] >= 5.0)

    def test_square(self):
        neighborhood = Square(extent=1.0)
        self.assertEqual(str(neighborhood), repr(neighborhood))

        result = self.raster_rdd.focal(operation=Operation.SUM,
                                       neighborhood=neighborhood)

        self.assertTrue(result.to_numpy_rdd().first()[1].cells[0][1][0] >= 6.0)

    def test_focal_sum_int(self):
        result = self.raster_rdd.focal(operation=Operation.SUM,
                                       neighborhood=Neighborhood.SQUARE,
                                       param_1=1)

        self.assertTrue(result.to_numpy_rdd().first()[1].cells[0][1][0] >= 6)

    def test_focal_sum_square_with_square(self):
        square = Square(extent=1.0)
        result = self.raster_rdd.focal(operation=Operation.SUM,
                                       neighborhood=square)

        self.assertTrue(result.to_numpy_rdd().first()[1].cells[0][1][0] >= 6)

    def test_focal_min(self):
        result = self.raster_rdd.focal(operation=Operation.MIN,
                                       neighborhood=Neighborhood.ANNULUS,
                                       param_1=2.0,
                                       param_2=1.0)

        self.assertEqual(result.to_numpy_rdd().first()[1].cells[0][0][0], -1)

    def test_focal_min_annulus(self):
        annulus = Annulus(inner_radius=2.0, outer_radius=1.0)
        result = self.raster_rdd.focal(operation=Operation.MIN,
                                       neighborhood=annulus)

        self.assertEqual(result.to_numpy_rdd().first()[1].cells[0][0][0], -1)

    def test_focal_min_int(self):
        result = self.raster_rdd.focal(operation=Operation.MIN,
                                       neighborhood=Neighborhood.ANNULUS,
                                       param_1=2,
                                       param_2=1)

        self.assertEqual(result.to_numpy_rdd().first()[1].cells[0][0][0], -1)

    def test_tobler(self):
        result = self.raster_rdd.tobler()
コード例 #26
0
class MaskTest(BaseTestClass):
    pysc = BaseTestClass.pysc

    cells = 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]]])

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

    rdd = 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': layout
        }
    }

    geometries = Polygon([(17, 17), (42, 17), (42, 42), (17, 42)])
    raster_rdd = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd,
                                                 metadata)

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

    def test_geotrellis_mask(self):
        result = self.raster_rdd.mask(
            geometries=self.geometries).to_numpy_rdd()
        n = result.map(lambda kv: np.sum(kv[1].cells)).reduce(
            lambda a, b: a + b)
        self.assertEqual(n, 25.0)
コード例 #27
0
class CostDistanceTest(BaseTestClass):
    cells = 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, 0.0]]])

    tile = Tile.from_numpy_array(cells, -1.0)

    layer = [(SpatialKey(0, 0), tile),
             (SpatialKey(1, 0), tile),
             (SpatialKey(0, 1), tile),
             (SpatialKey(1, 1), 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},
                    'maxKey': {'col': 1, 'row': 1}},
                'layoutDefinition': {
                    'extent': extent,
                    'tileLayout': {'tileCols': 5, 'tileRows': 5, 'layoutCols': 2, 'layoutRows': 2}}}

    raster_rdd = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd, metadata)

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

    def test_costdistance_finite(self):
        def zero_one(kv):
            k = kv[0]
            return (k.col == 0 and k.row == 1)

        result = cost_distance(self.raster_rdd,
                               geometries=[Point(13, 13)], max_distance=144000.0)

        tile = result.to_numpy_rdd().filter(zero_one).first()[1]
        point_distance = tile.cells[0][1][3]
        self.assertEqual(point_distance, 0.0)

    def test_costdistance_finite_int(self):
        def zero_one(kv):
            k = kv[0]
            return (k.col == 0 and k.row == 1)

        result = cost_distance(self.raster_rdd,
                               geometries=[Point(13, 13)], max_distance=144000)

        tile = result.to_numpy_rdd().filter(zero_one).first()[1]
        point_distance = tile.cells[0][1][3]
        self.assertEqual(point_distance, 0.0)

    def test_costdistance_infinite(self):
        def zero_one(kv):
            k = kv[0]
            return (k.col == 0 and k.row == 1)

        result = cost_distance(self.raster_rdd,
                               geometries=[Point(13, 13)], max_distance=float('inf'))

        tile = result.to_numpy_rdd().filter(zero_one).first()[1]
        point_distance = tile.cells[0][0][0]
        self.assertTrue(point_distance > 1250000)
コード例 #28
0
ファイル: lookup_test.py プロジェクト: lossyrob/geopyspark
class LookupTest(BaseTestClass):
    cells = 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, 0.0]]])

    layer = [(SpatialKey(0, 0), Tile(cells + 0, 'FLOAT', -1.0)),
             (SpatialKey(1, 0), Tile(cells + 1, 'FLOAT', -1.0,)),
             (SpatialKey(0, 1), Tile(cells + 2, 'FLOAT', -1.0,)),
             (SpatialKey(1, 1), Tile(cells + 3, '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}}}

    raster_rdd = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd, metadata)

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

    def test_lookup_1(self):
        result = self.raster_rdd.lookup(0, 0)[0]
        n = np.sum(result.cells)
        self.assertEqual(n, 24 + 0*25)

    def test_lookup_2(self):
        result = self.raster_rdd.lookup(0, 1)[0]
        n = np.sum(result.cells)
        self.assertEqual(n, 24 + 2*25)

    def test_lookup_3(self):
        result = self.raster_rdd.lookup(1, 0)[0]
        n = np.sum(result.cells)
        self.assertEqual(n, 24 + 1*25)

    def test_lookup_4(self):
        result = self.raster_rdd.lookup(1, 1)[0]
        n = np.sum(result.cells)
        self.assertEqual(n, 24 + 3*25)

    def test_invalid_1(self):
        with pytest.raises(IndexError):
            result = self.raster_rdd.lookup(13, 33)

    def test_invalid_2(self):
        with pytest.raises(IndexError):
            result = self.raster_rdd.lookup(33, 13)
コード例 #29
0
class HistogramTest(BaseTestClass):
    extent = {'xmin': 0.0, 'ymin': 0.0, 'xmax': 33.0, 'ymax': 33.0}
    layout = {'layoutCols': 1, 'layoutRows': 1, 'tileCols': 4, 'tileRows': 4}
    metadata = {'cellType': 'float32ud-1.0',
                'extent': extent,
                'crs': '+proj=longlat +datum=WGS84 +no_defs ',
                'bounds': {
                    'minKey': {'col': 0, 'row': 0},
                    'maxKey': {'col': 0, 'row': 0}},
                'layoutDefinition': {
                    'extent': extent,
                    'tileLayout': {'tileCols': 4, 'tileRows': 4, 'layoutCols': 1, 'layoutRows': 1}}}

    spatial_key = SpatialKey(0, 0)

    arr = np.array([[[1.0, 1.0, 1.0, 1.0],
                     [2.0, 2.0, 2.0, 2.0],
                     [3.0, 3.0, 3.0, 3.0],
                     [4.0, 4.0, 4.0, 4.0]]], dtype=float)

    tile = Tile(arr, 'FLOAT', -500)
    rdd = BaseTestClass.pysc.parallelize([(spatial_key, tile)])
    tiled = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd, metadata)
    hist = tiled.get_histogram()

    def test_min(self):
        self.assertEqual(self.hist.min(), 1.0)

    def test_max(self):
        self.assertEqual(self.hist.max(), 4.0)

    def test_min_max(self):
        self.assertEqual(self.hist.min_max(), (1.0, 4.0))

    def test_mean(self):
        self.assertEqual(self.hist.mean(), 2.5)

    def test_mode(self):
        arr2 = np.array([[[1.0, 1.0, 1.0, 1.0],
                          [2.0, 2.0, 2.0, 2.0],
                          [1.0, 3.0, 3.0, 3.0],
                          [4.0, 4.0, 4.0, 4.0]]], dtype=float)

        tile2 = Tile(arr2, 'FLOAT', -500)
        rdd2 = BaseTestClass.pysc.parallelize([(self.spatial_key, tile2)])
        tiled2 = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd2, self.metadata)
        hist2 = tiled2.get_histogram()

        self.assertEqual(hist2.mode(), 1.0)

    def test_quantile_breaks(self):
        result = self.hist.quantile_breaks(4)
        self.assertEqual(result, [1, 2, 3, 4])

    def test_median(self):
        self.assertEqual(self.hist.median(), 2.5)

    def test_cdf(self):
        expected_cdf = [(1.0, 0.25), (2.0, 0.5), (3.0, 0.75), (4.0, 1.0)]

        self.assertEqual(self.hist.cdf(), expected_cdf)

    def test_bucket_count(self):
        self.assertEqual(self.hist.bucket_count(), 4)

    def test_values(self):
        self.assertEqual(self.hist.values(), [1.0, 2.0, 3.0, 4.0])

    def test_item_count(self):
        self.assertEqual(self.hist.item_count(3.0), 4)

    def test_bin_counts(self):
        metadata2 = {'cellType': 'int32ud-500',
                     'extent': self.extent,
                     'crs': '+proj=longlat +datum=WGS84 +no_defs ',
                     'bounds': {
                         'minKey': {'col': 0, 'row': 0},
                         'maxKey': {'col': 0, 'row': 0}},
                     'layoutDefinition': {
                         'extent': self.extent,
                         'tileLayout': {'tileCols': 4, 'tileRows': 4, 'layoutCols': 1, 'layoutRows': 1}}}

        arr2 = np.int8([[[1, 1, 1, 1],
                         [3, 1, 1, 1],
                         [4, 3, 1, 1],
                         [5, 4, 3, 1]]])

        tile2 = Tile(arr2, 'INT', -500)
        rdd2 = BaseTestClass.pysc.parallelize([(self.spatial_key, tile2)])
        tiled2 = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd2,
                                                 metadata2)

        hist2 = tiled2.get_class_histogram()
        bin_counts = hist2.bin_counts()

        self.assertEqual(bin_counts, [(1, 10), (3, 3), (4, 2), (5, 1)])

    def test_merge(self):
        arr2 = 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]]], dtype=float)

        tile2 = Tile(arr2, 'FLOAT', -500)
        rdd2 = BaseTestClass.pysc.parallelize([(self.spatial_key, tile2)])
        tiled2 = TiledRasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd2,
                                                 self.metadata)

        hist2 = tiled2.get_histogram()

        merged = self.hist.merge(hist2)


        self.assertEqual(merged.values(), [1.0, 2.0, 3.0, 4.0])
        self.assertEqual(merged.mean(), 1.75)

    def test_dict_methods(self):
        dict_hist = self.hist.to_dict()
        # value produced by histogram of doubles
        self.assertEqual(dict_hist['maxBucketCount'], 80)

        rebuilt_hist = Histogram.from_dict(dict_hist)
        self.assertEqual(self.hist.min_max(), rebuilt_hist.min_max())
コード例 #30
0
class FocalTest(BaseTestClass):
    data = 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, 0.0]]])
    '''
    layer = [({'row': 0, 'col': 0}, {'no_data_value': -1.0, 'data': data}),
             ({'row': 1, 'col': 0}, {'no_data_value': -1.0, 'data': data}),
             ({'row': 0, 'col': 1}, {'no_data_value': -1.0, 'data': data}),
             ({'row': 1, 'col': 1}, {'no_data_value': -1.0, 'data': data})]
    '''

    layer = [(SpatialKey(0, 0), {
        'no_data_value': -1.0,
        'data': data
    }), (SpatialKey(0, 1), {
        'no_data_value': -1.0,
        'data': data
    }), (SpatialKey(1, 0), {
        'no_data_value': -1.0,
        'data': data
    }), (SpatialKey(1, 1), {
        'no_data_value': -1.0,
        'data': data
    })]
    rdd = BaseTestClass.geopysc.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
            }
        }
    }

    raster_rdd = TiledRasterRDD.from_numpy_rdd(BaseTestClass.geopysc, SPATIAL,
                                               rdd, metadata)

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

    def test_focal_sum(self):
        result = self.raster_rdd.focal(operation=SUM,
                                       neighborhood=SQUARE,
                                       param_1=1.0)

        self.assertTrue(result.to_numpy_rdd().first()[1]['data'][0][1][0] >= 6)

    def test_focal_sum_int(self):
        result = self.raster_rdd.focal(operation=SUM,
                                       neighborhood=SQUARE,
                                       param_1=1)

        self.assertTrue(result.to_numpy_rdd().first()[1]['data'][0][1][0] >= 6)

    def test_focal_sum_square(self):
        square = Square(extent=1.0)
        result = self.raster_rdd.focal(operation=SUM, neighborhood=square)

        self.assertTrue(result.to_numpy_rdd().first()[1]['data'][0][1][0] >= 6)

    def test_focal_min(self):
        result = self.raster_rdd.focal(operation=MIN,
                                       neighborhood=ANNULUS,
                                       param_1=2.0,
                                       param_2=1.0)

        self.assertEqual(result.to_numpy_rdd().first()[1]['data'][0][0][0], -1)

    def test_focal_min_annulus(self):
        annulus = Annulus(inner_radius=2.0, outer_radius=1.0)
        result = self.raster_rdd.focal(operation=MIN, neighborhood=annulus)

        self.assertEqual(result.to_numpy_rdd().first()[1]['data'][0][0][0], -1)

    def test_focal_min_int(self):
        result = self.raster_rdd.focal(operation=MIN,
                                       neighborhood=ANNULUS,
                                       param_1=2,
                                       param_2=1)

        self.assertEqual(result.to_numpy_rdd().first()[1]['data'][0][0][0], -1)