-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
138 lines (113 loc) · 4.78 KB
/
main.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
# Main python file to run
import argparse
import yaml
import os
import sys
# from algorithm_runner import run_qgis_algorithm
from ndvi_provider import NDVIProvider
# default directory in docker
INPUT_DIRECTORY = '/data/input'
TEMP_DIRECTORY = '/data/tmp'
OUTPUT_DIRECTORY = '/data/output'
def main_function(tif_file_name, output_file_name, input_directory=INPUT_DIRECTORY, output_directory=OUTPUT_DIRECTORY):
"""The main function to calculate NDVI from tif file in `tif_path` to `output_path`
"""
full_tif_path = os.path.join(input_directory, tif_file_name)
full_output_path = os.path.join(output_directory, output_file_name)
if not os.path.exists(full_tif_path):
print('TIF file %s is not exist' % full_tif_path)
else:
print('TIF file %s is exist' % full_tif_path)
#### Prepare QGIS ####
import sys
import qgis.utils
from qgis.core import (
QgsApplication,
QgsProcessingFeedback,
QgsVectorLayer,
QgsProcessingProvider,
QgsProcessingRegistry,
QgsRasterLayer,
QgsProject
)
from qgis.analysis import QgsNativeAlgorithms
# See https://gis.stackexchange.com/a/155852/4972 for details about the prefix
QgsApplication.setPrefixPath('/usr', True)
qgs = QgsApplication([], False)
qgs.initQgis()
# # Append the path where processing plugin can be found
sys.path.append('/usr/share/qgis/python/plugins/')
import processing
from processing.core.Processing import Processing
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
#### Add NDVI Provider ####
provider = NDVIProvider()
provider.loadAlgorithms()
QgsApplication.processingRegistry().addProvider(provider)
#### Run split bands algorithm ####
# algorithm id:
split_band_algorithm_id = 'uas:Split_bands'
# parameters
split_band_algorithm_parameters = {
'inputimage': full_tif_path,
'Red': os.path.join(TEMP_DIRECTORY, 'red.sdat'),
'Green': os.path.join(TEMP_DIRECTORY, 'green.sdat'),
'Blue': os.path.join(TEMP_DIRECTORY, 'nir.sdat'),
'R_conv': os.path.join(OUTPUT_DIRECTORY, 'red_conv.tif'),
'G_conv': os.path.join(OUTPUT_DIRECTORY, 'green_conv.tif'),
'B_conv': os.path.join(OUTPUT_DIRECTORY, 'nir_conv.tif'),
}
# Run algorithm
split_band_result = processing.run(split_band_algorithm_id, split_band_algorithm_parameters)
# Check result
print('Path of G_conv: %s is exist = %s' % (split_band_result.get('G_conv'), os.path.exists(split_band_result.get('G_conv'))))
print('Path of R_conv: %s is exist = %s' % (split_band_result.get('R_conv'), os.path.exists(split_band_result.get('R_conv'))))
#### Run NDVI raster calculation algorithm ####
# algorithm id:
ndvi_algorithm_id = 'uas:Calculate_NDVI'
# parameters
ndvi_algorithm_parameters = {
'inputnirband': split_band_result['B_conv'],
'inputredband': split_band_result['R_conv'],
'Output': full_output_path
}
if False:
# Run algorithm
ndvi_result = processing.run(ndvi_algorithm_id, ndvi_algorithm_parameters)
# Check result
print('Path of NDVI: %s is exist = %s' % (ndvi_result.get('Output'), os.path.exists(ndvi_result.get('Output'))))
else:
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
entries = []
nir_layer = QgsRasterLayer(ndvi_algorithm_parameters['inputnirband'], "nir")
QgsProject.instance().addMapLayer(nir_layer)
nir = QgsRasterCalculatorEntry()
nir.ref = 'nir@1'
nir.raster = nir_layer
nir.bandNumber = 1
entries.append(nir)
red_layer = QgsRasterLayer(ndvi_algorithm_parameters['inputredband'], "red")
QgsProject.instance().addMapLayer(red_layer)
red = QgsRasterCalculatorEntry()
red.ref = 'red@1'
red.raster = red_layer
red.bandNumber = 1
entries.append(red)
ndvi_expression = 'Float( nir@1 - red@1 ) / Float( nir@1 + red@1 )'
calc = QgsRasterCalculator(
ndvi_expression, ndvi_algorithm_parameters['Output'], 'GTiff', nir_layer.extent(), nir_layer.width(), nir_layer.height(), entries)
a = calc.processCalculation()
print('Result: ', a)
# Exit QGIS
qgs.exitQgis()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("tif_file_name", help="TIF input file name")
parser.add_argument("output_file_name", help="Output file name")
args = parser.parse_args()
tif_path = args.tif_file_name
print('Input TIF file name: %s' % args.tif_file_name)
print('Output path: %s' % args.output_file_name)
main_function(args.tif_file_name, args.output_file_name)
print('fin')