forked from LEOXINGU/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bands2RGB.py
62 lines (57 loc) · 2.61 KB
/
bands2RGB.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
"""
/***************************************************************************
3 CGEO
3th Brazilian Geoinformation Center
-------------------
begin : 2017-03-31
copyright : (C) 2017 by Leandro Franca - Cartographic Engineer @ Brazilian Army
email : franca.leandro@eb.mil.br
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
##Bandas para RGB=name
##LF5) Raster=group
##Band_R=raster
##Band_G=raster
##Band_B=raster
##RGB_Output_image_file=output raster
import numpy, gdal
from osgeo import osr
# Open band R
image = gdal.Open(Band_R)
bandR = image.GetRasterBand(1).ReadAsArray()
prj=image.GetProjection()
geotransform = image.GetGeoTransform()
# Open band G
image = gdal.Open(Band_G)
bandG = image.GetRasterBand(1).ReadAsArray()
# Open band B
image = gdal.Open(Band_B)
bandB = image.GetRasterBand(1).ReadAsArray()
# Create CRS object
CRS=osr.SpatialReference(wkt=prj)
# Number of rows and columns
cols = image.RasterXSize # Number of columns
rows = image.RasterYSize # Number of rows
image=None # Close image
# Create RGB image
RGB = gdal.GetDriverByName('GTiff').Create(RGB_Output_image_file, cols, rows, 3, gdal.GDT_UInt16)
RGB.SetGeoTransform(geotransform) # specify coords
RGB.SetProjection(CRS.ExportToWkt()) # export coords to file
RGB.GetRasterBand(1).WriteArray(bandR) # write R band to the raster
RGB.GetRasterBand(2).WriteArray(bandG) # write G band to the raster
RGB.GetRasterBand(3).WriteArray(bandB) # write B band to the raster
RGB.FlushCache() # write to disk
RGB = None # save, close
CRS = None
progress.setInfo('<b><font color="#ff0000">Suas alterações serão salvas e seu Projeto QGIS será fechado em 9 segundos.</b><br/><br/>')
progress.setInfo('<b>Operação concluída!</b><br/><br/>')
progress.setInfo('<b>Leandro França - Eng Cart</b><br/>')
iface.messageBar().pushMessage(u'Situacao', "Operacao Concluida com Sucesso!", level=QgsMessageBar.INFO, duration=5)
time.sleep(4)