-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqgis_funcs.py
42 lines (35 loc) · 1.15 KB
/
qgis_funcs.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
from osgeo import gdal
from tempfile import gettempdir
from qgis.core import QgsRasterLayer, QgsProject
import os
# Render raster from array
def render_raster(array, bounding_box, layer_name, epsg):
driver = gdal.GetDriverByName("GTiff")
dataset = driver.Create(
os.path.join(gettempdir(), layer_name + ".tif"),
array.shape[2],
array.shape[1],
array.shape[0],
gdal.GDT_Byte,
)
out_srs = gdal.osr.SpatialReference()
out_srs.ImportFromEPSG(epsg)
dataset.SetProjection(out_srs.ExportToWkt())
dataset.SetGeoTransform(
(
bounding_box.xMinimum(), # 0
bounding_box.width() / array.shape[2], # 1
0, # 2
bounding_box.yMaximum(), # 3
0, # 4
-bounding_box.height() / array.shape[1],
)
)
for c in range(array.shape[0]):
dataset.GetRasterBand(c + 1).WriteArray(array[c, :, :])
dataset = None
raster_layer = QgsRasterLayer(
os.path.join(gettempdir(), layer_name + ".tif"), layer_name
)
raster_layer.renderer().setOpacity(1.0)
QgsProject.instance().addMapLayer(raster_layer, True)