Skip to content

Commit

Permalink
adding reclass_masked_values method to Raster class
Browse files Browse the repository at this point in the history
  • Loading branch information
wbierbower committed Apr 29, 2015
1 parent b0490c0 commit 0c9b8aa
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions fauxgeo/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,13 @@ def resize_pixels(self, pixel_size, resample_method):

return Raster.from_tempfile(output_uri)

def reclass_masked_values(self, mask_raster, new_value):
def reclass_masked_closure(nodata):
def reclass(x, y):
return np.where((np.not_equal(y, 0)), x, new_value)
return reclass
return self.local_op(mask_raster, reclass_masked_closure)

def sample_from_raster(self, raster):
'''way too slow!'''
shape = self.get_shape()
Expand Down
21 changes: 21 additions & 0 deletions tests/test_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
----------------------------------
Tests for Raster class.
python -m unittest test_raster
"""

import unittest
Expand Down Expand Up @@ -261,5 +263,24 @@ def test_resize_pixels(self):
b = a.resize_pixels(0.5, 'nearest')
assert(b.get_shape() == (8, 8))

class TestRasterReclassMaskedValues(unittest.TestCase):
def setUp(self):
self.shape = (4, 4)
self.array = np.ones(self.shape)
self.affine = Affine(1, 0, 0, 0, -1, 4)
self.proj = 4326
self.datatype = gdal.GDT_Float64
self.nodata_val = -9999
self.factory = RasterFactory(
self.proj, self.datatype, self.nodata_val, *self.shape, affine=self.affine)

def test_reclass_masked_values(self):
new_value = 20
masked_raster = self.factory.alternating(0, 1.0)
raster = self.factory.uniform(np.nan)
new_raster = raster.reclass_masked_values(masked_raster, new_value)
assert(new_raster.get_band(1)[0, 0] == new_value)


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

0 comments on commit 0c9b8aa

Please sign in to comment.