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):
textnode = xmldoc.createTextNode(str(aRaster.nodatavalue))
nodataChild.appendChild(textnode)
xmltext = xmldoc.toprettyxml(indent =" ")
with open(pathtoxml) as x:
x.write(xmltext)
with open(pathtoxml, 'w') as outfile:
outfile.write(xmltext)
else:
# Check if such a file already contains a NoDataValue node
dirty = False
......
......@@ -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"""
__mode = 'r'
dataformat = 'i'
datatype = 'c'
nbands = 1 # default
roty = 0.0
rotx = 0.0
......@@ -34,7 +35,7 @@ class BitmapRaster(Raster, GridEnvelope2D):
__compression = -1
__bitcount = 8 # Possible: 1, 2, 4, 8, 16, 24 and 32 bits per pixel (bpp)
__rowsize = 8
__top_to_bottom = False
__top_to_bottom = True
__BMP_HDR = [
{'type':'char', 'name':'type', 'endpos':2},
......@@ -118,14 +119,14 @@ class BitmapRaster(Raster, GridEnvelope2D):
for header in [self.__BMP_HDR, self.__DIB_HDR]:
for item in header:
buflen = int(item['endpos']) - prevpos
dtype = item['type'][0]
if dtype == 'c':
self.datatype = item['type'][0]
if self.datatype == 'c':
mybuffer = bytearray(buflen)
value = bytes(item['value'], 'ascii')
struct.pack_into(buflen*'b', mybuffer, 0, *value)
else:
arrlen = buflen / struct.calcsize(dtype)
mybuffer = array(dtype, int(arrlen) * [0])
arrlen = buflen / struct.calcsize(self.datatype)
mybuffer = array(self.datatype, int(arrlen) * [0])
mybuffer[0] = item['value']
self.datafile.write(mybuffer)
prevpos = int(item['endpos'])
......@@ -189,7 +190,7 @@ class BitmapRaster(Raster, GridEnvelope2D):
dibhdr = self.datafile.read(40)
hdrsize = int.from_bytes(dibhdr[0:4], 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.__top_to_bottom = (tmp < 0)
imgplane = int.from_bytes(dibhdr[12:14], byteorder='little')
......
......@@ -53,6 +53,8 @@ class WorldFile(object):
aRaster.xul = float(line.strip()) - 0.5 * aRaster.dx
line = self.datafile.readline()
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:
print(e)
raise e
......
......@@ -8,6 +8,18 @@ from lmgeo.formats.csfraster import CsfRaster
__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):
test_class = None
int_extension = 'xxx'
......@@ -35,23 +47,34 @@ class TestBaseRaster(unittest.TestCase):
xll = float(hls[2].replace('xllcorner', ''));
yll = float(hls[3].replace('yllcorner', ''));
cellsize = float(hls[4].replace('cellsize', ''));
nodatavalue = int(hls[5].replace('NODATA_value', ''));
mdata = ma.array(data, mask = (data == nodatavalue))
nodatavalue = int(hls[5].replace('NODATA_value', ''))
# Get nodatavalue, minimum and maximum for the new raster
cellrepr = None
if (self.test_class.__name__ == 'CsfRaster'):
cellrepr = CsfRaster.dataformat2cellrepr('i')
_, _, 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
# Get minimum and maximum
mdata = ma.array(data, mask = (data == nodatavalue))
minimum = np.min(mdata)
maximum = np.max(mdata)
# 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'):
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')
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)
# 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