Skip to content

Commit

Permalink
adding affine class and test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
wbierbower committed Apr 27, 2015
1 parent 16ab58f commit 3452263
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 22 deletions.
1 change: 1 addition & 0 deletions fauxgeo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
from raster import *
from raster_factory import *
from vector import *
from affine import *

__version__ = '0.2.0'
39 changes: 39 additions & 0 deletions fauxgeo/affine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@


class Affine(object):
# any global variables here
def __init__(self, a, b, c, d, e, f):
self.a = a
self.b = b
self.c = c
self.d = d
self.e = e
self.f = f

def __repr__(self):
rep = "\n| %f %f %f |" % (self.a, self.b, self.c)
rep += "\n| %f %f %f |\n" % (self.d, self.e, self.f)
return rep

def __eq__(self, other):
a = (self.a == other.a)
b = (self.b == other.b)
c = (self.c == other.c)
d = (self.d == other.d)
e = (self.e == other.e)
f = (self.f == other.f)
if all([a, b, c, d, e, f]):
return True
else:
return False

@classmethod
def identity():
return Affine(1.0, 0.0, 0.0, 0.0, 1.0, 0.0)

@ classmethod
def from_gdal(self, c, a, b, f, d, e):
return Affine(a, b, c, d, e, f)

def to_gdal(self):
return (self.c, self.a, self.b, self.f, self.d, self.e)
118 changes: 96 additions & 22 deletions tests/test_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
from affine import Affine
import gdal

from fauxgeo.raster import Raster
from fauxgeo import Raster
from fauxgeo import RasterFactory


class Test_Raster(unittest.TestCase):
class TestRasterGetAndAlign(unittest.TestCase):

def setUp(self):
self.shape = (3, 4)
Expand Down Expand Up @@ -50,9 +51,6 @@ def test_get_functions(self):
assert(self.raster.get_affine() == self.affine)
assert(self.raster.get_projection() == 4326)

def test_set_functions(self):
pass

def test_is_aligned(self):
assert(self.raster.is_aligned(self.aligned_raster) == True)
assert(self.raster.is_aligned(self.misaligned_raster) == False)
Expand All @@ -67,32 +65,108 @@ def test_align_to(self):
new_raster = self.misaligned_raster.align_to(self.raster, "nearest")
assert(self.raster.is_aligned(new_raster) == True)

def test_clip(self):
# self.raster.clip()
pass

# def test_reproject(self):
# reprojected_raster = self.raster.reproject(26917, "nearest")
# print reprojected_raster.get_band(1)
class TestRasterCopy(unittest.TestCase):
def setUp(self):
self.shape = (3, 4)
self.array = np.ones(self.shape)
self.affine = Affine(1, 0, 0, 0, -1, 3)
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_bounding_box(self):
print self.raster.get_bbox()
def test_copy(self):
a = self.factory.uniform(1)
b = a.copy()
assert(a.uri != b.uri)
assert(a.is_aligned(b))

def test_reclass(self):
pass

def test_overlay(self):
pass
class TestRasterMath(unittest.TestCase):
def setUp(self):
self.shape = (3, 4)
self.array = np.ones(self.shape)
self.affine = Affine(1, 0, 0, 0, -1, 3)
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_add(self):
a = self.factory.alternating(1.0, 2.0)
b = a + a
assert(b.get_band(1)[0, 0] == 2)

def test_sub(self):
a = self.factory.alternating(1.0, 2.0)
b = a - a
assert(b.get_band(1)[0, 0] == 0)

def test_mul(self):
a = self.factory.alternating(2.0, 3.0)
b = a * a
assert(b.get_band(1)[0, 0] == 4)

def test_div(self):
a = self.factory.alternating(2.0, 3.0)
b = a / a
assert(b.get_band(1)[0, 0] == 1)

def test_pow(self):
a = self.factory.alternating(2.0, 3.0)
b = a ** a
assert(b.get_band(1)[0, 0] == 4)

def test_radd(self):
a = self.factory.alternating(2.0, 3.0)
b = 4 + a
assert(b.get_band(1)[0, 0] == 6)

def test_rsub(self):
a = self.factory.alternating(2.0, 3.0)
b = 4 - a
assert(b.get_band(1)[0, 0] == 2)

def test_rmul(self):
a = self.factory.alternating(2.0, 3.0)
b = 4 * a
assert(b.get_band(1)[0, 0] == 8)

def test_rdiv(self):
a = self.factory.alternating(2.0, 3.0)
b = 4 / a
assert(b.get_band(1)[0, 0] == 2)

def test_rpow(self):
a = self.factory.alternating(2.0, 3.0)
b = 4 ** a
assert(b.get_band(1)[0, 0] == 8)


class TestRasterReclass(unittest.TestCase):
def setUp(self):
self.shape = (3, 4)
self.array = np.ones(self.shape)
self.affine = Affine(1, 0, 0, 0, -1, 3)
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_copy(self):
def test_reclass(self):
pass

def test_to_vector(self):
pass

def tearDown(self):
pass
class TestRasterClip(unittest.TestCase):
pass


class TestRasterBoundingBox(unittest.TestCase):
pass

if __name__ == '__main__':
unittest.main()
Empty file removed tests/test_vector_factory.py
Empty file.

0 comments on commit 3452263

Please sign in to comment.