Commit c2622290 authored by Hoek, Steven's avatar Hoek, Steven
Browse files

Test added for bitmapraster - some bug fixes were necessary for the test to work properly

parent 1a5d75ed
...@@ -52,8 +52,8 @@ class AuxiliaryFile(object): ...@@ -52,8 +52,8 @@ class AuxiliaryFile(object):
textnode = xmldoc.createTextNode(str(aRaster.nodatavalue)) textnode = xmldoc.createTextNode(str(aRaster.nodatavalue))
nodataChild.appendChild(textnode) nodataChild.appendChild(textnode)
xmltext = xmldoc.toprettyxml(indent =" ") xmltext = xmldoc.toprettyxml(indent =" ")
with open(pathtoxml) as x: with open(pathtoxml, 'w') as outfile:
x.write(xmltext) outfile.write(xmltext)
else: else:
# Check if such a file already contains a NoDataValue node # Check if such a file already contains a NoDataValue node
dirty = False dirty = False
......
...@@ -25,6 +25,7 @@ class BitmapRaster(Raster, GridEnvelope2D): ...@@ -25,6 +25,7 @@ class BitmapRaster(Raster, GridEnvelope2D):
"""A raster represented by a binary file - with extension 'bmp' - so-called device-independent bitmaps or DIBs""" """A raster represented by a binary file - with extension 'bmp' - so-called device-independent bitmaps or DIBs"""
__mode = 'r' __mode = 'r'
dataformat = 'i' dataformat = 'i'
datatype = 'c'
nbands = 1 # default nbands = 1 # default
roty = 0.0 roty = 0.0
rotx = 0.0 rotx = 0.0
...@@ -34,7 +35,7 @@ class BitmapRaster(Raster, GridEnvelope2D): ...@@ -34,7 +35,7 @@ class BitmapRaster(Raster, GridEnvelope2D):
__compression = -1 __compression = -1
__bitcount = 8 # Possible: 1, 2, 4, 8, 16, 24 and 32 bits per pixel (bpp) __bitcount = 8 # Possible: 1, 2, 4, 8, 16, 24 and 32 bits per pixel (bpp)
__rowsize = 8 __rowsize = 8
__top_to_bottom = False __top_to_bottom = True
__BMP_HDR = [ __BMP_HDR = [
{'type':'char', 'name':'type', 'endpos':2}, {'type':'char', 'name':'type', 'endpos':2},
...@@ -118,14 +119,14 @@ class BitmapRaster(Raster, GridEnvelope2D): ...@@ -118,14 +119,14 @@ class BitmapRaster(Raster, GridEnvelope2D):
for header in [self.__BMP_HDR, self.__DIB_HDR]: for header in [self.__BMP_HDR, self.__DIB_HDR]:
for item in header: for item in header:
buflen = int(item['endpos']) - prevpos buflen = int(item['endpos']) - prevpos
dtype = item['type'][0] self.datatype = item['type'][0]
if dtype == 'c': if self.datatype == 'c':
mybuffer = bytearray(buflen) mybuffer = bytearray(buflen)
value = bytes(item['value'], 'ascii') value = bytes(item['value'], 'ascii')
struct.pack_into(buflen*'b', mybuffer, 0, *value) struct.pack_into(buflen*'b', mybuffer, 0, *value)
else: else:
arrlen = buflen / struct.calcsize(dtype) arrlen = buflen / struct.calcsize(self.datatype)
mybuffer = array(dtype, int(arrlen) * [0]) mybuffer = array(self.datatype, int(arrlen) * [0])
mybuffer[0] = item['value'] mybuffer[0] = item['value']
self.datafile.write(mybuffer) self.datafile.write(mybuffer)
prevpos = int(item['endpos']) prevpos = int(item['endpos'])
...@@ -165,7 +166,7 @@ class BitmapRaster(Raster, GridEnvelope2D): ...@@ -165,7 +166,7 @@ class BitmapRaster(Raster, GridEnvelope2D):
GridEnvelope2D.__init__(self, ncols, nrows, xll, yll, cellsize, cellsize) GridEnvelope2D.__init__(self, ncols, nrows, xll, yll, cellsize, cellsize)
self.cellsize = cellsize self.cellsize = cellsize
self.nodatavalue = nodatavalue self.nodatavalue = nodatavalue
self.__colortable = 256 * [4 * [0]] self.__colortable = 256 * [4 * [0]]
self.writeheader() self.writeheader()
self.flush() self.flush()
else: else:
...@@ -189,7 +190,7 @@ class BitmapRaster(Raster, GridEnvelope2D): ...@@ -189,7 +190,7 @@ class BitmapRaster(Raster, GridEnvelope2D):
dibhdr = self.datafile.read(40) dibhdr = self.datafile.read(40)
hdrsize = int.from_bytes(dibhdr[0:4], byteorder='little') hdrsize = int.from_bytes(dibhdr[0:4], byteorder='little')
self.ncols = int.from_bytes(dibhdr[4:8], byteorder='little') self.ncols = int.from_bytes(dibhdr[4:8], byteorder='little')
tmp = int.from_bytes(dibhdr[8:12], byteorder='little') tmp = int.from_bytes(dibhdr[8:12], byteorder='little', signed=True)
self.nrows = abs(tmp) self.nrows = abs(tmp)
self.__top_to_bottom = (tmp < 0) self.__top_to_bottom = (tmp < 0)
imgplane = int.from_bytes(dibhdr[12:14], byteorder='little') imgplane = int.from_bytes(dibhdr[12:14], byteorder='little')
......
...@@ -53,6 +53,8 @@ class WorldFile(object): ...@@ -53,6 +53,8 @@ class WorldFile(object):
aRaster.xul = float(line.strip()) - 0.5 * aRaster.dx aRaster.xul = float(line.strip()) - 0.5 * aRaster.dx
line = self.datafile.readline() line = self.datafile.readline()
aRaster.yul = float(line.strip()) + 0.5 * aRaster.dy aRaster.yul = float(line.strip()) + 0.5 * aRaster.dy
aRaster.xll = aRaster.xul
aRaster.yll = aRaster.yul - (aRaster.nrows * aRaster.dy)
except Exception as e: except Exception as e:
print(e) print(e)
raise e raise e
......
...@@ -8,6 +8,18 @@ from lmgeo.formats.csfraster import CsfRaster ...@@ -8,6 +8,18 @@ from lmgeo.formats.csfraster import CsfRaster
__author__ = "Steven B. Hoek" __author__ = "Steven B. Hoek"
def get_dtype_from_fmtspec(fmt):
# TODO: check this and work out further
if fmt == 'c':
result = np.ubyte
elif fmt == 'h':
result = np.int16
elif fmt == 'H':
result = np.uint16
else:
result = np.int32
return result
class TestBaseRaster(unittest.TestCase): class TestBaseRaster(unittest.TestCase):
test_class = None test_class = None
int_extension = 'xxx' int_extension = 'xxx'
...@@ -35,23 +47,34 @@ class TestBaseRaster(unittest.TestCase): ...@@ -35,23 +47,34 @@ class TestBaseRaster(unittest.TestCase):
xll = float(hls[2].replace('xllcorner', '')); xll = float(hls[2].replace('xllcorner', ''));
yll = float(hls[3].replace('yllcorner', '')); yll = float(hls[3].replace('yllcorner', ''));
cellsize = float(hls[4].replace('cellsize', '')); cellsize = float(hls[4].replace('cellsize', ''));
nodatavalue = int(hls[5].replace('NODATA_value', '')); nodatavalue = int(hls[5].replace('NODATA_value', ''))
mdata = ma.array(data, mask = (data == nodatavalue))
# Get nodatavalue, minimum and maximum for the new raster # Get nodatavalue, minimum and maximum for the new raster
cellrepr = None cellrepr = None
if (self.test_class.__name__ == 'CsfRaster'): if (self.test_class.__name__ == 'CsfRaster'):
cellrepr = CsfRaster.dataformat2cellrepr('i') cellrepr = CsfRaster.dataformat2cellrepr('i')
_, _, specialnodatavalue = CsfRaster.get_min_max_nodata(None, cellrepr) _, _, specialnodatavalue = CsfRaster.get_min_max_nodata(None, cellrepr)
elif (self.test_class.__name__ == 'BitmapRaster'):
# Assume that either minimum or maximum value has not been used yet
dtype = get_dtype_from_fmtspec(self.test_class.datatype)
if not np.iinfo(dtype).max in data: specialnodatavalue = np.iinfo(dtype).max
else: specialnodatavalue = np.iinfo(dtype).min
data[data == nodatavalue] = specialnodatavalue
nodatavalue = specialnodatavalue
else: specialnodatavalue = nodatavalue else: specialnodatavalue = nodatavalue
# Get minimum and maximum
mdata = ma.array(data, mask = (data == nodatavalue))
minimum = np.min(mdata) minimum = np.min(mdata)
maximum = np.max(mdata) maximum = np.max(mdata)
# Write the file # Write the file
br = self.test_class(os.path.join(curdir, 'output', 'int.' + self.int_extension), 'i', minimum, maximum, cellrepr)
if (self.test_class.__name__ == 'CsfRaster'): if (self.test_class.__name__ == 'CsfRaster'):
br = self.test_class(os.path.join(curdir, 'output', 'int.' + self.int_extension), 'i', minimum, maximum, cellrepr)
br.open('w', ncols=ncols, nrows=nrows, xll=xll, yll=yll, cellsize=cellsize, nodatavalue=specialnodatavalue, valueScale='VS_NOMINAL') br.open('w', ncols=ncols, nrows=nrows, xll=xll, yll=yll, cellsize=cellsize, nodatavalue=specialnodatavalue, valueScale='VS_NOMINAL')
else: else:
br = self.test_class(os.path.join(curdir, 'output', 'int.' + self.int_extension), 'i')
br.open('w', ncols=ncols, nrows=nrows, xll=xll, yll=yll, cellsize=cellsize, nodatavalue=nodatavalue) br.open('w', ncols=ncols, nrows=nrows, xll=xll, yll=yll, cellsize=cellsize, nodatavalue=nodatavalue)
# Now loop over the data # Now loop over the data
......
from lmgeo.formats.bitmapraster import BitmapRaster
from lmgeo.tests.test_baseraster import TestBaseRaster
import unittest
__author__ = "Steven B. Hoek"
class TestBilRaster(TestBaseRaster):
# Load data from a pickle file and metadata from a header file
# Write the data to an AsciiGrid. Close and open teh file again for reading and check
test_class = BitmapRaster
int_extension = 'bmp'
def suite():
""" This defines all the tests of a module"""
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(BitmapRaster))
return suite
'''
def main():
# Test reading
path = "./output/int4.bmp" # ./output/DecSelectorBG.bmp" #
br = BitmapRaster(path, "i")
br.open('r')
for i in range(br.nrows):
line = br.next()
print(line)
br.close()
# Test writing
ag = AsciiGrid('./output/int2.asc', 'i')
ag.open('r')
path = "./output/int4.bmp"
br = BitmapRaster(path, "i", True)
br.open('w', ag.ncols, ag.nrows, ag.xll, ag.yll, ag.cellsize, ag.nodatavalue)
for i in range(ag.nrows):
line = ag.next()
br.writenext(line)
ag.close()
br.close()
'''
if __name__ == "__main__":
main()
\ No newline at end of file
from lmgeo.formats.numpyraster import NumpyRaster
from test_baseraster import TestBaseRaster
import unittest
__author__ = "Steven B. Hoek"
class TestNumpyRaster(TestBaseRaster):
# Load data from a pickle file and metadata from a header file
# Write the data to an AsciiGrid. Close and open teh file again for reading and check
test_class = NumpyRaster
int_extension = 'npy'
flt_extension = 'npy'
def suite():
""" This defines all the tests of a module"""
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestNumpyRaster))
return suite
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment