tiletiffraster.py 11.5 KB
Newer Older
Hoek, Steven's avatar
Hoek, Steven committed
1
# Copyright (c) 2004-2020 WUR, Wageningen
Hoek, Steven's avatar
Hoek, Steven committed
2
from __future__ import division
3
4
5
from .const import constants as const
from .gridenvelope2d import GridEnvelope2D;
from .basetiffraster import BaseTiffRaster
Hoek, Steven's avatar
Hoek, Steven committed
6
7
8
from libtiff import TIFF, libtiff
import numpy as np
import os
9
from math import floor, ceil, sqrt
Hoek, Steven's avatar
Hoek, Steven committed
10

11
12
13
__author__ = "Steven B. Hoek"

class TileTiffRaster(BaseTiffRaster, GridEnvelope2D):
Hoek, Steven's avatar
Hoek, Steven committed
14
15
16
17
18
19
20
21
22
    "A raster represented by 2 files, with extensions 'tif' and 'tfw'"
    "This class can deal with tiff files of which the data are stored in tiles"
    "Different layers - e.g. RGB - are as planes: contiguously = chunky = interleaved"
    "or separately = per channel. More info: http://www.fileformat.info/format/tiff/egff.htm"
    "It means that the number of planes and the planar configuration determines the shape"
    "of the array written as bitmapped data, with dimensions image_depth, image_height, "
    "image_width and samples. E.g. in the case of rgb and contiguous configuration, the last"
    "dimension of the array is expected to be 3 and the field samples per pixel will also be 3"
    # Private attributes
23
    _const = None
Hoek, Steven's avatar
Hoek, Steven committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    __mode = 'r'
    __datatype = const.FLOAT;
    currow = -1;
    __envelope = None;
    __image = None; # contains current strip
    __bits_per_sample = 8
    __sample_format = 1
    __samples_per_pixel = 1
    __numpy_type = np.uint8
    __itemsize = 1
    __layer_size = 1
    __tile_width = 1
    __tile_length = 1
    __ntiles = 1
    __nstrips = 1 
    
    def __init__(self, filepath, *datatype):
41
42
43
44
45
        # Initialise
        super(TileTiffRaster, self).__init__(filepath)
        if self._const == None:
            raise AttributeError("TIFF raster not properly initialised!")
        
Hoek, Steven's avatar
Hoek, Steven committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
        # Finally set the datatype
        if len(datatype) > 0:
            if (datatype[0] == const.INTEGER): 
                self.__datatype = const.INTEGER;
            else: 
                self.__datatype = const.FLOAT;
    
    def __get_sample_format(self, arr):
        result = None
        
        # Not considered: SAMPLEFORMAT_VOID=4 and SAMPLEFORMAT_COMPLEXINT=5
        if arr.dtype in np.sctypes['float']:
            result = 3 #SAMPLEFORMAT_IEEEFP
        elif arr.dtype in np.sctypes['uint']+[np.bool]:
            result = 1 #SAMPLEFORMAT_UINT
        elif arr.dtype in np.sctypes['int']:
            result = 2 #SAMPLEFORMAT_INT
        elif arr.dtype in np.sctypes['complex']:
            result = 6 #SAMPLEFORMAT_COMPLEXIEEEFP
        else:
66
            raise NotImplementedError(arr.dtype)
Hoek, Steven's avatar
Hoek, Steven committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
        return result
    
    def set_numpy_type(self, atype):
        self.__numpy_type = atype
    
    def get_numpy_type(self):
        return self.datafile.get_numpy_type(self.__bits_per_sample, self.__sample_format)
    
    def open(self, mode, ncols=1, nrows=1, xll=0, yll=0, cellsize=100, nodatavalue=-9999.0, byteorder='II', compression=1):
        # Initialise
        super(TileTiffRaster, self).open(mode);
        
        # If file does not exist and mode[0] = 'w', create it!
        if (mode[0] == 'w'):
            self.__mode = mode
82
            self.datafile = TIFF.open(os.path.join(self.folder, self.name), mode='w');
Hoek, Steven's avatar
Hoek, Steven committed
83
            self.__envelope = GridEnvelope2D.__init__(self, ncols, nrows, xll, yll, cellsize, cellsize);
84
85
86
87
            self.datafile.SetField(self._const.IMAGE_WIDTH, ncols)
            self.datafile.SetField(self._const.IMAGE_LENGTH, nrows)
            self.datafile.SetField(self._const.BITS_PER_SAMPLE, self.__bits_per_sample)
            self.datafile.SetField(self._const.SAMPLE_PER_PIXEL, self.__samples_per_pixel)
Hoek, Steven's avatar
Hoek, Steven committed
88
89
90
91
92
93
94
95
96
            super(TileTiffRaster, self).set_extra_tags()
             
            # Data are organised into square tiles. Let each tile be about 8K bytes
            bits_per_pixel = self.__samples_per_pixel * self.__bits_per_sample
            pixels_per_tile = max(int(floor(8 * 8000 / bits_per_pixel)), 1)
            self.__tile_width = floor(sqrt(pixels_per_tile))
            self.__tile_length = self.__tile_width
            self.__ntiles = int(ceil(ncols / self.__tile_width) * ceil(nrows / self.__tile_length))
            self.__nstrips = int(self.ntiles / ceil(self.ncols / self.__tile_width))
97
98
99
100
101
102
103
            self.datafile.SetField(b"TileWidth", self.__tile_width)
            self.datafile.SetField(b"TileLength", self.__tile_length)
            self.datafile.SetField(self._const.PLANAR_CONFIG, 1) # contiguous
            self.datafile.SetField(self._const.ORIENTATION, 1)  # top left
            self.datafile.SetField(self._const.PAGE_NUMBER, 1, 1)
            self.datafile.SetField(self._const.FILL_ORDER, 1) # MSB2LSB
            self.datafile.SetField(self._const.COMPRESSION, compression)
Hoek, Steven's avatar
Hoek, Steven committed
104
105
106
107
108
109
            self.writeheader()
            shape = (self.__tile_length * self.ncols, self.__samples_per_pixel)
            self.__image = np.zeros(shape, self.__numpy_type)
            return True;
        else: 
            # Open the file as well as the header file
110
111
            if self.file_exists:            
                self.datafile = TIFF.open(os.path.join(self.folder, self.name), mode='r');
Hoek, Steven's avatar
Hoek, Steven committed
112
113
114
                self.readheader();
                
                # Check whether found values warrant further execution
115
116
                self.ncols = int(self.datafile.GetField(self._const.IMAGE_WIDTH))
                self.nrows = int(self.datafile.GetField(self._const.IMAGE_LENGTH))
Hoek, Steven's avatar
Hoek, Steven committed
117
118
119
120
121
122
123
124
125
126
                self.__tile_width = int(self.datafile.GetField("TileWidth"))
                self.__tile_length = int(self.datafile.GetField("TileLength"))
                self.__ntiles = libtiff.TIFFNumberOfTiles(self.datafile).value
                
                # Tiles can be joined to form strips: ceil(ncols / tile_width) in number, with height = tile_length
                # Those strips can be joined to form the image: ceil(nrows / tile_length) in number 
                msg = "Number of tiles not in accordance with tile and image dimensions!"
                self.__nstrips = int(ceil(self.nrows / self.__tile_length))
                num_tiles_per_strip = int(ceil(self.ncols / self.__tile_width))
                assert self.__ntiles == self.__nstrips * num_tiles_per_strip, msg
127
                planar_config = self.datafile.GetField(self._const.PLANAR_CONFIG)
Hoek, Steven's avatar
Hoek, Steven committed
128
129
                if (planar_config > 1):
                    raise NotImplementedError("Not yet able to deal with data organised in separate planes")
130
131
132
133
134
135
                
                if self.datafile.GetField(self._const.GDAL_NODATA) != None:
                    if self.__datatype == const.INTEGER:
                        self.nodatavalue = int(self.datafile.GetField(self._const.GDAL_NODATA))
                    else:
                        self.nodatavalue = float(self.datafile.GetField(self._const.GDAL_NODATA))
Hoek, Steven's avatar
Hoek, Steven committed
136
137
138
139
                super(TileTiffRaster, self).get_extra_tags()
                
                # Process further information from the header file
                self.xll = self.xul;
140
                if self.ycoords_sort == const.DESC:
Hoek, Steven's avatar
Hoek, Steven committed
141
142
143
144
145
                    self.yll = self.yul - self.nrows * self.dy;
                else:
                    self.yll = self.yul + self.nrows * self.dy; 
                    
                # Prepare to read the file (strip by strip and under the hood tile by tile)
146
147
148
                self.__bits_per_sample = self.datafile.GetField(self._const.BITS_PER_SAMPLE)
                self.__sample_format = self.datafile.GetField(self._const.SAMPLE_FORMAT)
                self.__samples_per_pixel = self.datafile.GetField(self._const.SAMPLE_PER_PIXEL)
Hoek, Steven's avatar
Hoek, Steven committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
                self.__numpy_type = self.datafile.get_numpy_type(self.__bits_per_sample, self.__sample_format)
                self.__itemsize = self.__bits_per_sample / 8
                shape = (self.__tile_length * self.ncols, self.__samples_per_pixel)
                self.__image = np.zeros(shape, self.__numpy_type)
                return True;
            else: return False;
    
    def get_tag(self, name):
        return self.datafile.get_tag_name(name)
    
    def next(self, parseLine=True):
        # Is it possible to proceed? Otherwise generate StopIteration
        result = None;
        self.currow += 1;
        try:
            if (self.currow >= self.nrows): raise StopIteration;
        
            # Read a new strip when necessary
            row_in_strip = self.currow % self.__tile_length # also zero-based!
            curstrip = int(floor(self.currow / self.__tile_length)) 
            if curstrip >= self.__nstrips: raise StopIteration;
            if row_in_strip == 0:
                # Are we dealing with one plane or with more? What configuratin?
                # self.datafile.GetField("PlanarConfig", 1)) 
                if curstrip == self.__nstrips-1:
                    # Last strip
                    length = self.nrows - self.__tile_length * curstrip
                    self.__layer_size = (self.ncols) * length * (self.__samples_per_pixel) * (self.__itemsize) 
                    self.__image = np.zeros((length, self.ncols, self.__samples_per_pixel), dtype=self.__numpy_type) 
                    #.resize((last_length, self.ncols, self.__samples_per_pixel)) 
                else:
                    length = self.__tile_length
                    self.__layer_size = (self.ncols) * length * (self.__samples_per_pixel) * (self.__itemsize) 
                    

                # Before trying to read, reset the buffer           
                self.__image.fill(0.0)
                self.__ReadStrip(curstrip, self.__image, int(self.__layer_size))
                self.__image = self.__image.reshape(length, self.ncols, self.__samples_per_pixel)
          
            # Read the next row
            result = self.__image[row_in_strip, :]
            return result     
        except StopIteration:
            raise StopIteration;   
        except Exception as e:
195
            print(str(e))
Hoek, Steven's avatar
Hoek, Steven committed
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
    
    def writenext(self, sequence_with_data):
        raise NotImplementedError("Not implemented yet")
    
    def reset(self):
        self.currow = -1;  
    
    def __ReadStrip(self, strip, buf, size):
        result = False
        try:
            num_tiles_per_strip = int(ceil(self.ncols / self.__tile_width))
            numpy_type = self.datafile.get_numpy_type(self.__bits_per_sample, self.__sample_format)
            if (strip == self.__nstrips-1): 
                length = self.nrows - (strip*self.__tile_length)
            else:
                length = self.__tile_length
            buf = buf.reshape(length, self.ncols, self.__samples_per_pixel)
            for k in range(num_tiles_per_strip): 
                if (k == num_tiles_per_strip-1):
                    # We only need part of the tile because we are on the edge
                    width = self.ncols - (num_tiles_per_strip-1)*self.__tile_width
                else:
                    width = self.__tile_width
                tmp_buf = np.ascontiguousarray(np.zeros((self.__tile_length, self.__tile_width), numpy_type))
                seq = libtiff.TIFFReadTile(self.datafile, tmp_buf.ctypes.data, k*self.__tile_width, strip*self.__tile_length, 0, 0)
                if seq != None:
                    start = k*self.__tile_width
                    buf[0:length, start:start+width, 0] = tmp_buf[0:length, 0:width]
            result = True
        except Exception as e:    
226
            print(str(e))
Hoek, Steven's avatar
Hoek, Steven committed
227
228
229
230
231
232
233
234
235
236
237
238
        finally:
            return result
    
    def __WriteStrip(self,strip, buf, size):
        raise NotImplementedError("Not implemented yet")
    
    def close(self):
        if self.__mode[0] == 'w':
            self.datafile.WriteDirectory()
        super(TileTiffRaster, self).close()