asciigrid.py 9.31 KB
Newer Older
Hoek, Steven's avatar
Hoek, Steven committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import const;
import os.path;
import array;
from raster import Raster
from .gridenvelope2d import GridEnvelope2D;

class AsciiGrid(GridEnvelope2D, Raster):
    "A raster represented by an ASCII file, with extension 'asc'"
    
    # Constants
    const.FILEXT = "asc";     
    const.MAXDIGITSPERCELL = 11;
    
    # Data attributes - assign some dummy values for the mean time
    name = "dummy." + const.FILEXT;
    folder = os.getcwd();
    """
    ncols = 1;
    nrows = 1;
    xll = 0.0;
    yll = 0.0;
    """
    cellsize = 100.0;
    nodatavalue = -9999.0;
    datatype = const.FLOAT;
    dataformat='f'
    
    # Private attributes
    datafile = None;
    currow = 0;
    __mode = 'r';
    __digitspercell = 7;
    
    def __init__(self, filepath, *datatype):
        # Initialise
        DATAFILEXT = const.FILEXT
        HEADEREXT = ""
        # Retrieve the name from the filepath and assign - incl. extension
        self.name = os.path.basename(filepath);
        # Also derive the folder
        self.folder = os.path.dirname(filepath);
        # Finally set the datatype
        if len(datatype) > 0:
            if (datatype[0] == const.INTEGER): 
                self.datatype = const.INTEGER;
                self.dataformat = 'i'
            else: 
                self.datatype = const.FLOAT;
                
    def open(self, mode, ncols=1, nrows=1, xll=0.0, yll=0.0, cellsize=100.0, nodatavalue=-9999.0):
        # Initialise
        super(AsciiGrid, self).open(mode); 
               
        # If file does not exist and mode[0] = 'w', create it!
        if (mode[0] == 'w') and (not os.path.exists(self.folder + os.path.sep + self.name)):
            self.datafile = file(self.folder + os.path.sep + self.name, 'w');
            self.__mode = mode;
            GridEnvelope2D.__init__(self, ncols, nrows, xll, yll, cellsize, cellsize);
            self.cellsize = cellsize;
            self.nodatavalue = nodatavalue;
            self.writeheader();
            return True;
        else:    
            # Open the file
            if os.path.exists(self.folder + os.path.sep + self.name):            
                self.datafile = open(self.folder + os.path.sep + self.name, mode[0]); 
                if (mode[0] == 'w'):
                    # Assign the data attributes 
                    self.ncols = ncols;
                    self.nrows = nrows;                    
                    self.xll = xll;
                    self.yll = yll;
                    self.cellsize = cellsize;
                    self.nodatavalue = nodatavalue;
                    self.writeheader();
                else: 
                    # File is open - retrieve the data attributes from the header of the file
                    self.readheader();
                    
                    # Also find out how many digits per cell were used - assume it's constant
                    pos = self.datafile.tell();
                    line = self.datafile.readline();
                    self.__digitspercell = ((1 + len(line)) / self.ncols) - 1;
                    self.datafile.seek(pos);  # return to first line with data
                    GridEnvelope2D.__init__(self, self.ncols, self.nrows, self.xll, self.yll, self.cellsize, self.cellsize);
                return True;
            else: return False;
            
    def readheader(self):
        # Assume that the file is open; read header of the file and assign all attributes 
        if (self.datafile != None):
92
            # TODO: make this case-insensitive!
Hoek, Steven's avatar
Hoek, Steven committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
            if (not self.datafile.closed):            
                hl = self.datafile.readline();
                self.ncols = int(hl.replace('ncols', ''));
                hl = self.datafile.readline();
                self.nrows = int(hl.replace('nrows', ''));
                hl = self.datafile.readline();
                self.xll = float(hl.replace('xllcorner', ''));        
                hl = self.datafile.readline();
                self.yll = float(hl.replace('yllcorner', ''));        
                hl = self.datafile.readline();
                self.cellsize = float(hl.replace('cellsize', ''));        
                hl = self.datafile.readline();
                if (self.datatype == const.INTEGER): 
                    self.nodatavalue = int(hl.replace('NODATA_value', ''));
                else: 
                    self.nodatavalue = float(hl.replace('NODATA_value', ''));
            else: 
                msg = "File " + self.name + " not found in folder " + self.folder;
                raise IOError(msg);                
    
    def next(self, parseLine=True):
        # Read the next row if possible, otherwise generate StopIteration
        # Assume that the header lines have been read and are correct wrt. ncols and nrows
        result = None;
        try:
            if (self.datafile != None):
                if (not self.datafile.closed):
                    self.currow += 1;
                    if (self.currow > self.nrows): 
                        raise StopIteration("Attempt to move beyond last row.");
                    
                    # Allocate a new array with ncols of the right type
                    if (self.datatype == const.INTEGER):
                        result = array.array('l', self.ncols * [self.nodatavalue]);
                    else:
                        result = array.array('f', self.ncols * [self.nodatavalue]);

                    # Now fill the array - first translate whitespace into space
                    rawline = self.datafile.readline();
                    if parseLine:
                        i = 0;                    
                        for x in rawline.split(): 
                            if (i < self.ncols):
                                if (self.datatype == const.INTEGER):
                                    result[i] = int(x);
                                else:
                                    result[i] = float(x);
                            i = i + 1;
                    return result;
                else: raise StopIteration("Attempt to read raster data from a closed file.");
            else: raise StopIteration("Attempt to read raster data from an unassigned file.")
        except Exception, e:
            print "Error: " + str(e);
            raise StopIteration;
    
    """       
    def hasSameExtent(self, obj):
        if not isinstance(obj, AsciiGrid):
            return False;
        if self.ncols != obj.ncols:
            return False;
        if self.nrows != obj.nrows:
            return False;
        if abs(self.cellsize - obj.cellsize) > const.epsilon:
            return False;
        if abs(self.xll - obj.xll) > const.epsilon:
            return False;
        if abs(self.yll - obj.yll) > const.epsilon:
            return False;
        else:
            return True;
    """
           
    @staticmethod
    def getFileExt():
        return const.FILEXT;
        
    def writeheader(self):
        # Assume that the file is open; write header of the file with all attributes 
        if (self.datafile != None):
            if (not self.datafile.closed):
                try:
                    self.datafile.write("ncols         " + str(self.ncols).rjust(const.MAXDIGITSPERCELL + 1) + "\n");
                    self.datafile.write("nrows         " + str(self.nrows).rjust(const.MAXDIGITSPERCELL + 1) + "\n");
                    self.datafile.write("xllcorner     " + str(self.xll).rjust(const.MAXDIGITSPERCELL + 1) + "\n");
                    self.datafile.write("yllcorner     " + str(self.yll).rjust(const.MAXDIGITSPERCELL + 1) + "\n");
                    self.datafile.write("cellsize      " + str(self.cellsize).rjust(const.MAXDIGITSPERCELL + 1) + "\n");
                    self.datafile.write("NODATA_value  " + str(self.nodatavalue).rjust(const.MAXDIGITSPERCELL + 1) + "\n");
                except Exception, e:
                    print e;
                    msg = "Header lines could not be written to file " + self.name + " in folder " + self.folder;
                    raise IOError(msg);
        
    def writenext(self, sequence_with_data):
        # Write the next line if possible, otherwise generate StopIteration
        # We assume that exactly 1 row is included.
        try:          
            if (self.datatype == const.INTEGER):
191
                # TODO deal with numpy arrays if necessary
Hoek, Steven's avatar
Hoek, Steven committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
                for k in range(0, self.ncols):
                    s = str(sequence_with_data[k]).rjust(const.MAXDIGITSPERCELL + 1);
                    self.datafile.write(s);  
            else:
                totalwidth = const.MAXDIGITSPERCELL - 1
                fmtstr = "{:" + str(totalwidth) + ".2f}"
                for k in range(0, self.ncols):
                    s = fmtstr.format(sequence_with_data[k]).rjust(const.MAXDIGITSPERCELL + 1);
                    self.datafile.write(s);                 
            return self.datafile.write("\n");
        except Exception, e:
            print e;            
            raise StopIteration
    
    def flush(self):
        self.datafile.flush();
                
    def reset(self):
        self.datafile.seek(0);
        if (self.__mode[0] == 'r'):
            self.readheader();
        super(AsciiGrid, self).reset() 
214
215
216
217
218
219
220
      
    def get_value(self, i, k):    
        # Return the wanted value
        for _ in range(0, i): self.next(False)
        line = self.next()
        self.reset()
        return line[int(k)]
Hoek, Steven's avatar
Hoek, Steven committed
221