raster.py 1.83 KB
Newer Older
Hoek, Steven's avatar
Hoek, Steven committed
1
# Copyright (c) 2004-2020 WUR, Wageningen
2
from .const import constants as const
Hoek, Steven's avatar
Hoek, Steven committed
3
4
import os;

5
6
__author__ = "Steven B. Hoek"

Hoek, Steven's avatar
Hoek, Steven committed
7
8
# Abstract base class
class Raster(object):
9
    # Attributes
Hoek, Steven's avatar
Hoek, Steven committed
10
11
12
13
14
15
    datafile = None
    name = "dummy";
    folder = os.getcwd();
    nodatavalue = -9999.0;
    cellsize = 1;
    currow = 0
16
    file_exists = False
Hoek, Steven's avatar
Hoek, Steven committed
17
18
    
    def __init__(self, filepath):
19
20
        self.file_exists = os.path.exists(filepath)

Hoek, Steven's avatar
Hoek, Steven committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    def open(self, mode, ncols=1, nrows=1, xll=0.0, yll=0.0, cellsize=1.0, nodatavalue=-9999.0):
        pass
    
    def readheader(self):
        pass

    def writeheader(self):
        pass
    
    def __iter__(self):
        return self; 
    
    def next(self, parseLine=True):
        pass

    def writenext(self, sequence_with_data):
        # input is sequence type - e.g. list, array.array or numpy.array
        pass
    
    @staticmethod
    def getDataFileExt(self):
42
43
44
45
        result = "xxx"
        if self != None and hasattr(self, "_const"):
            result = self._const.DATAFILEXT
        return result
Hoek, Steven's avatar
Hoek, Steven committed
46
47
48
    
    @staticmethod
    def getHeaderFileExt(self):
49
        return const.HEADEREXT 
Hoek, Steven's avatar
Hoek, Steven committed
50
51
    
    def close(self):
52
53
54
55
56
57
        try:
            if self.datafile:
                if hasattr(self.datafile, 'closed'):
                    if not self.datafile.closed:
                        self.datafile.close()
                else:
Hoek, Steven's avatar
Hoek, Steven committed
58
                    self.datafile.close()
59
60
        except Exception as e:
            print(e)
Hoek, Steven's avatar
Hoek, Steven committed
61
62
63

    def reset(self):
        self.currow = 0; 
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
        
    @property
    def dx(self):
        return self.cellsize

    @dx.setter
    def dx(self, dx):
        self.cellsize = dx
        
    @property
    def dy(self):
        # TODO: differentiate dx and dy!
        return self.cellsize

    @dy.setter
    def dy(self, dy):
        # TODO: differentiate dx and dy!
        self.cellsize = dy
Hoek, Steven's avatar
Hoek, Steven committed
82