Skip to content

Commit

Permalink
adding to_binary_raster method in raster class
Browse files Browse the repository at this point in the history
  • Loading branch information
wbierbower committed Jul 28, 2015
1 parent 15b1389 commit bff7f3d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
9 changes: 9 additions & 0 deletions fauxgeo/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,15 @@ def to_vector(self):
proj = self.get_projection()
return Vector.from_shapely(aoi_shapely, proj)

def to_binary_raster(self, val):
values_list = list(set(self.get_band(1).data.flatten()))
reclass_list = [(v, 0) for v in values_list]
reclass_dict = dict(reclass_list)
if val in values_list:
print val
reclass_dict[val] = 1
return self.reclass(reclass_dict)

def local_op(self, raster, pixel_op_closure, broadcast=False):
bounding_box_mode = "dataset"
resample_method = "nearest"
Expand Down
17 changes: 17 additions & 0 deletions tests/test_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,23 @@ def test_unique(self):
assert(raster.unique() == [1, 2, 3, 4])


class TestRasterToBinaryRaster(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_Float32
self.nodata_val = -9999
self.factory = RasterFactory(
self.proj, self.datatype, self.nodata_val, *self.shape, affine=self.affine)

def test_unique(self):
raster = self.factory.horizontal_ramp(1, 4)
binary_raster = raster.to_binary_raster(4)
assert(binary_raster.unique() == [0, 1])


class TestRasterSlice(unittest.TestCase):
def setUp(self):
self.shape = (10, 10)
Expand Down

0 comments on commit bff7f3d

Please sign in to comment.