Skip to content

Commit

Permalink
Merge pull request #167 from jbouffard/min-max
Browse files Browse the repository at this point in the history
Min Max Method
  • Loading branch information
echeipesh committed May 4, 2017
2 parents d6289b6 + 91372b5 commit d73ddbf
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ abstract class TileRDD[K: ClassTag] {
reclassifyDouble(reclassifiedRDD)
}

def getMinMax: (Double, Double) = {
val minMaxs: Array[(Double, Double)] = rdd.histogram.map{ x => x.minMaxValues.get }

minMaxs.foldLeft(minMaxs(0)) {
(acc, elem) =>
(math.min(acc._1, elem._1), math.max(acc._2, elem._2))
}
}

protected def reclassify(reclassifiedRDD: RDD[(K, MultibandTile)]): TileRDD[_]
protected def reclassifyDouble(reclassifiedRDD: RDD[(K, MultibandTile)]): TileRDD[_]
}
Expand Down
18 changes: 18 additions & 0 deletions geopyspark/geotrellis/rdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,15 @@ def reclassify(self, value_map, data_type, boundary_strategy=LESSTHANOREQUALTO,

return RasterRDD(self.geopysc, self.rdd_type, srdd)

def get_min_max(self):
"""Returns the maximum and minimum values of all of the rasters in the RDD.
Returns:
(float, float)
"""
min_max = self.srdd.getMinMax()
return (min_max._1(), min_max._2())


class TiledRasterRDD(object):
"""Wraps a RDD of tiled, GeoTrellis rasters.
Expand Down Expand Up @@ -638,6 +647,15 @@ def reclassify(self, value_map, data_type, boundary_strategy=LESSTHANOREQUALTO,

return TiledRasterRDD(self.geopysc, self.rdd_type, srdd)

def get_min_max(self):
"""Returns the maximum and minimum values of all of the rasters in the RDD.
Returns:
(float, float)
"""
min_max = self.srdd.getMinMax()
return (min_max._1(), min_max._2())

def _process_operation(self, value, operation):
if isinstance(value, int) or isinstance(value, float):
srdd = operation(value)
Expand Down
60 changes: 60 additions & 0 deletions geopyspark/tests/min_max_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import os
import sys
import numpy as np
import pytest
import unittest

from geopyspark.geotrellis.rdd import RasterRDD
from geopyspark.geotrellis.constants import SPATIAL
from geopyspark.tests.base_test_class import BaseTestClass


class MinMaxTest(BaseTestClass):
epsg_code = 3857
extent = {'xmin': 0.0, 'ymin': 0.0, 'xmax': 10.0, 'ymax': 10.0}

projected_extent = {'extent': extent, 'epsg': epsg_code}

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

def test_all_zeros(self):
arr = np.zeros((1, 16, 16))
tile = {'data': arr, 'no_data_value': -500}

rdd = BaseTestClass.geopysc.pysc.parallelize([(self.projected_extent, tile)])
raster_rdd = RasterRDD.from_numpy_rdd(BaseTestClass.geopysc, SPATIAL, rdd)
min_max = raster_rdd.get_min_max()

self.assertEqual((0.0, 0.0), min_max)

def test_multibands(self):
arr = np.array([[[1, 1, 1, 1]],
[[2, 2, 2, 2]],
[[3, 3, 3, 3]],
[[4, 4, 4, 4]]], dtype=int)
tile = {'data': arr, 'no_data_value': -500}

rdd = BaseTestClass.geopysc.pysc.parallelize([(self.projected_extent, tile)])
raster_rdd = RasterRDD.from_numpy_rdd(BaseTestClass.geopysc, SPATIAL, rdd)
min_max = raster_rdd.get_min_max()

self.assertEqual((1.0, 4.0), min_max)

def test_floating(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 = {'data': arr, 'no_data_value': float('nan')}
rdd = BaseTestClass.geopysc.pysc.parallelize([(self.projected_extent, tile)])
raster_rdd = RasterRDD.from_numpy_rdd(BaseTestClass.geopysc, SPATIAL, rdd)
min_max = raster_rdd.get_min_max()

self.assertEqual((0.0, 2.0), min_max)

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

0 comments on commit d73ddbf

Please sign in to comment.