-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiff_generator_win.py
127 lines (82 loc) · 3.38 KB
/
tiff_generator_win.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
import sys
import subprocess
sys.path.append('Scripts/')
import gdal_merge
import zipfile
import os
import time
import glob
from pyreadline import Readline
from pathlib import Path
readline = Readline()
def complete(text, state):
return (glob.glob(text+'*')+[None])[state]
def get_immediate_subdirectories(a_dir):
return [name for name in os.listdir(a_dir)
if os.path.isdir(os.path.join(a_dir, name))]
def generate_geotiffs(inputProductPath, outputPath):
basename = os.path.basename(inputProductPath)
if os.path.isdir(outputPath + basename[:-3] + "SAFE") :
print('Already extracted')
else:
zip = zipfile.ZipFile(inputProductPath)
zip.extractall(outputPath)
print("Extracting Done")
directoryName = outputPath + basename[:-3] + "SAFE/GRANULE"
productName = os.path.basename(inputProductPath)[:-4]
outputPathSubdirectory = outputPath + productName + "_PROCESSED"
if not os.path.exists(outputPathSubdirectory):
os.makedirs(outputPathSubdirectory)
subDirectorys = get_immediate_subdirectories(directoryName)
results = []
for granule in subDirectorys:
unprocessedBandPath = outputPath + productName + ".SAFE/GRANULE/" + granule + "/" + "IMG_DATA/"
results.append(generate_all_bands(unprocessedBandPath, granule, outputPathSubdirectory))
#gdal_merge.py -n 0 -a_nodata 0 -of GTiff -o /home/daire/Desktop/merged.tif /home/daire/Desktop/aa.tif /home/daire/Desktop/rgbTiff-16Bit-AllBands.tif
merged = outputPathSubdirectory + "/merged.tif"
params = ['',"-of", "GTiff", "-o", merged]
for granule in results:
params.append(granule)
gdal_merge.main(params)
def generate_all_bands(unprocessedBandPath, granule, outputPathSubdirectory):
granuleBandTemplate = granule[:-6]
outputPathSubdirectory = outputPathSubdirectory
if not os.path.exists(outputPathSubdirectory+ "/IMAGE_DATA"):
os.makedirs(outputPathSubdirectory+ "/IMAGE_DATA")
outPutTiff = '/'+granule[:-6]+'16Bit-AllBands.tif'
outPutVRT = '/'+granule[:-6]+'16Bit-AllBands.vrt'
outPutFullPath = outputPathSubdirectory + "/IMAGE_DATA/" + outPutTiff
outPutFullVrt = outputPathSubdirectory + "/IMAGE_DATA/" + outPutVRT
inputPath = unprocessedBandPath + granuleBandTemplate[4:11] + granuleBandTemplate[19:] + "103029_"
print(inputPath)
bands = {
"band_02" : inputPath + "B02.jp2",
"band_03" : inputPath + "B03.jp2",
"band_04" : inputPath + "B04.jp2",
"band_08" : inputPath + "B08.jp2",
"band_11" : inputPath + "B11.jp2",
}
cmd = ['gdalbuildvrt', '-resolution', 'user', '-tr' ,'20', '20', '-separate' ,outPutFullVrt]
for band in sorted(bands.values()):
cmd.append(band)
my_file = Path(outPutFullVrt)
if not my_file.is_file():
# file exists
subprocess.call(cmd)
#, '-a_srs', 'EPSG:3857'
cmd = ['gdal_translate', '-of' ,'GTiff', outPutFullVrt, outPutFullPath]
my_file = Path(outPutTiff)
if not my_file.is_file():
# file exists
subprocess.call(cmd)
#params = ['', '-o', outPutFullPath, '-separate', band_01, band_02, band_03, band_04, band_05, band_06, band_07, band_08, band_8A, band_09, band_10, band_11, band_12]
#gdal_merge.main(params)
return(outPutFullPath)
outputPath = '../Output/'
readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
readline.set_completer(complete)
inputPath = input("Input Path? ")
start_time = time.time()
generate_geotiffs(inputPath, outputPath)
print("--- %s seconds ---" % (time.time() - start_time))