forked from Key-Log-Economics/EcoValuator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
appinter.py
159 lines (131 loc) · 4.84 KB
/
appinter.py
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
92
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
"""
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
__author__ = 'Roman Geisthövel'
__date__ = '2018-03-05'
__copyright__ = '(C) 2018 by Roman Geisthövel'
import os
__all__ = """
Common
App
Raster
""".split()
def running_qgis():
try:
from qgis.core import Qgis
return Qgis.QGIS_VERSION_INT
except ImportError:
return 0
class Common:
"Functionality common to all platforms"
@staticmethod
def folder():
return os.path.dirname(os.path.realpath(__file__))
@staticmethod
def mkpath(*args):
return os.path.join(*args)
@staticmethod
def file_size(path, unit="byte"):
if os.path.isfile(path):
x = os.path.getsize(path)
unit = unit.lower()
if unit in ("mb", "mega", "megabyte"):
return x >> 20
elif unit in ("gb", "giga", "gigabyte"):
return x >> 30
else:
return x
else:
return 0
if running_qgis():
from qgis.core import QgsMessageLog, QgsRasterDataProvider, QgsRasterLayer
from qgis.core import Qgis
from osgeo import gdal
#-------------------------------------------------------------------------
class App:
@staticmethod
def info(*msg, **opt):
App.log(*msg, level=Qgis.Info, **opt)
@staticmethod
def warn(*msg, **opt):
App.log(*msg, level=Qgis.Warning, **opt)
@staticmethod
def critical(*msg, **opt):
App.log(*msg, level=Qgis.Critical, **opt)
@staticmethod
def log(*msg, **opt):
src = opt.get("src", "")
lvl = opt.get("level", Qgis.Info)
sep = opt.get("sep", " ")
QgsMessageLog.logMessage(sep.join(str(_) for _ in msg), tag=src, level=lvl)
#-------------------------------------------------------------------------
class Raster:
gdal2numpy_type = dict(Byte="u1", Int16="i2", UInt16="u2", Int32="i4",
UInt32="u4", Float32="f4", Float64="f8")
@staticmethod
def geo_transform(x):
dy,dx = Raster.cellsize(x)
p = x.extent()
return (p.xMinimum(),dx,0,p.yMinimum(),0,-dy)
@staticmethod
def crs(x):
return x.crs().toWkt()
# Returns tuple (dy, dx)
@staticmethod
def cellsize(x):
return (x.rasterUnitsPerPixelY(), x.rasterUnitsPerPixelX())
@staticmethod
def num_bands(x):
return x.bandCount()
@staticmethod
def from_numpy(x, **opt):
ras = QgsRasterLayer()
dp = QgsRasterDataProvider()
ras.setDataProvider(dp)
@staticmethod
def numpy_to_file(x, file_name, **opt):
h,w = x.shape
src = opt.get("src", None)
if src is not None:
srcds = gdal.Open(src, gdal.GA_ReadOnly)
crs = srcds.GetProjectionRef()
gt = srcds.GetGeoTransform()
else:
crs = opt.get("crs", None)
gt = opt.get("geo_transform", (0,1,0,0,0,1))
drv = gdal.GetDriverByName("GTiff")
ds = drv.Create(file_name, w, h, 1, gdal.GDT_Float32,
"""COMPRESS=DEFLATE
ZLEVEL=4
BIGTIFF=IF_SAFER
PREDICTOR=3
NUM_THREADS=ALL_CPUS""".split())
if crs:
ds.SetProjection(crs)
ds.SetGeoTransform(gt)
ds.GetRasterBand(1).WriteArray(x)
ds.FlushCache()
@staticmethod
def to_numpy(x, **opt):
"""
Options Description Default
band Band number 1
dtype Data type of result None, i.e. dtype of raster layer
"""
ds = gdal.Open(str(x.source()), gdal.GA_ReadOnly)
band = ds.GetRasterBand(opt.get("band", 1))
out = band.ReadAsArray()
dtype = opt.get("dtype", out.dtype)
if dtype != out.dtype:
out = out.astype(dtype, copy=False)
return out
@staticmethod
def shape(x):
return (x.height(), x.width())