-
Notifications
You must be signed in to change notification settings - Fork 1
/
6_Raster_Merger.py
71 lines (53 loc) · 1.91 KB
/
6_Raster_Merger.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
# This script merges rasters. The gdal_merge.py script from GDAL installation needs to be in your directory root folder then imported as a module.
import sys
sys.path.append('F:\Files\GIS\python_projects\open_source_geospatial')
import gdal_merge as gm
from osgeo import gdal
import time
import os
import shutil
gdal.PushErrorHandler('CPLQuietErrorHandler')
start_time = time.time()
input_directory = raw_input("Drag in your folder: \n")
input_list = os.listdir(input_directory)
final_list = []
for x in input_list:
if x.endswith('.tif'):
final_list.append(os.path.join(input_directory, x))
def int_function():
global output_full
output_full = os.path.join(input_directory, 'Merged.tif')
if len(final_list) == 2:
filea = final_list[0]
fileb = final_list[1]
filec = ''
filed = ''
elif len(final_list) == 3:
filea = final_list[0]
fileb = final_list[1]
filec = final_list[2]
filed = ''
elif len(final_list) == 4:
filea = final_list[0]
fileb = final_list[1]
filec = final_list[2]
filed = final_list[3]
sys.argv = ['q', '-v', filea, fileb, filec, filed, "-o", output_full]
gm.main()
print 'Finished merging files...'
def move_files():
print 'Moving files to new folder...'
if not os.path.exists(os.path.join(input_directory, 'Merged')):
os.mkdir(os.path.join(input_directory, 'Merged'))
new_folder = os.path.join(input_directory, 'Merged')
shutil.move(os.path.join(input_directory, output_full), new_folder)
else:
os.mkdir(os.path.join(input_directory, 'Merged_2'))
new_folder = os.path.join(input_directory, 'Merged_2')
shutil.move(os.path.join(input_directory, output_full), new_folder)
int_function()
move_files()
print '\nFinished ...'
print 'Program took {} seconds'.format(time.time() - start_time)
print 'See you later...'
time.sleep(2)