Skip to content

Commit

Permalink
Merge pull request #602 from jbouffard/feature/local-operations/max
Browse files Browse the repository at this point in the history
local_max Opertion for TiledRasterLayers
  • Loading branch information
echeipesh committed Jan 4, 2018
2 parents a3ed95d + 4ef1ed8 commit 5b87a40
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,21 @@ abstract class TiledRasterLayer[K: SpatialComponent: JsonFormat: ClassTag: Bound
band: Int
): TiledRasterLayer[K]

def localMax(i: Int): TiledRasterLayer[K] =
withRDD(rdd.mapValues { x => MultibandTile(x.bands.map(_.localMax(i))) })

def localMax(d: Double): TiledRasterLayer[K] =
withRDD(rdd.mapValues { x => MultibandTile(x.bands.map(_.localMax(d))) })

def localMax(other: TiledRasterLayer[K]): TiledRasterLayer[K] =
withRDD(rdd.combineValues(other.rdd) {
case (x: MultibandTile, y: MultibandTile) => {
val tiles: Vector[Tile] =
x.bands.zip(y.bands).map { case (b1, b2) => Max(b1, b2) }
MultibandTile(tiles)
}
})

def localAdd(i: Int): TiledRasterLayer[K] =
withRDD(rdd.mapValues { x => MultibandTile(x.bands.map { y => y + i }) })

Expand Down
22 changes: 22 additions & 0 deletions geopyspark/geotrellis/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1842,6 +1842,28 @@ def _process_operation(self, value, operation):

return TiledRasterLayer(self.layer_type, srdd)

def local_max(self, value):
"""Determines the maximum value for each cell of each ``Tile`` in the layer.
This method takes a ``max_constant`` that is compared to each cell in the
layer. If ``max_constant`` is larger, then the resulting cell value will
be that value. Otherwise, that cell will retain its original value.
Note:
``NoData`` values are handled such that taking the max between
a normal value and ``NoData`` value will always result in ``NoData``.
Args:
value (int or float or :class:`~geopyspark.geotrellis.layer.TiledRasterLayer`): The
constant value that will be compared to each cell. If this is a ``TiledRasterLayer``,
then ``Tile``\s who share a key will have each of their cell values compared.
Returns:
:class:`~geopyspark.geotrellis.layer.TiledRasterLayer`
"""

return self._process_operation(value, self.srdd.localMax)

def __add__(self, value):
return self._process_operation(value, self.srdd.localAdd)

Expand Down
25 changes: 25 additions & 0 deletions geopyspark/tests/geotrellis/local_ops_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,31 @@ def test_pow_layer(self):

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

def test_max_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.local_max(4)
actual = result.to_numpy_rdd().first()[1].cells

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

def test_max_layer(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.local_max(tiled + 1)
actual = result.to_numpy_rdd().first()[1].cells

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



if __name__ == "__main__":
unittest.main()

0 comments on commit 5b87a40

Please sign in to comment.