-
Notifications
You must be signed in to change notification settings - Fork 18
/
LSDGDALBatchProcessing.py
132 lines (110 loc) · 4.53 KB
/
LSDGDALBatchProcessing.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
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 09 16:52:27 2015
@author: smudd
"""
import numpy as np
from glob import glob
import LSDOSystemTools as LSDost
import os
import shutil
import subprocess
# This function looks for all the files of a certain format in a directory and
# then translates them into a new format into a subdirectory named
# after the target format.
def GDALBatchConvert(DataDirectory,raster_format,target_format):
NewDataDirectory = LSDost.ReformatSeperators(DataDirectory)
DataDirectory = LSDost.AppendSepToDirectoryPath(NewDataDirectory)
# Check the target format
if target_format == "ENVI":
target_extension = ".bil"
elif target_format == "EHdr":
target_extension = ".bil"
elif target_format == "GTiff":
target_extension = ".tiff"
else:
print "You have not selcted a valid raster format!"
print "Options are ENVI, EHdr and GTiff"
target_extension = "NULL"
# now make a directory
if target_extension != "NULL":
target_directory = DataDirectory+target_format
if not os.access(target_directory,os.F_OK):
print "Making path: "
os.mkdir(target_directory)
print "I made a directory: " + target_directory
else:
print "Path: " +target_directory+" already exists."
# Now check the source format
if raster_format == "ENVI":
raster_extension = ".bil"
elif raster_format == "EHdr":
raster_extension = ".bil"
elif raster_format == "GTiff":
raster_extension = ".tif"
else:
print "You have not selcted a valid raster format!"
print "Options are ENVI, EHdr and GTiff"
raster_extension = "NULL"
# find all the dataset of the source format
print "The data directory is: " + DataDirectory
print "The raster extension is: " + raster_extension
if raster_extension != "NULL":
for FileName in glob(DataDirectory+"*"+raster_extension):
print "found file: " + FileName
subprocess.call(['gdalinfo',FileName])
def GDALBatchMerge(DataDirectory,merge_subfolder_name,merge_filename,raster_format,target_format):
NewDataDirectory = LSDost.ReformatSeperators(DataDirectory)
DataDirectory = LSDost.AppendSepToDirectoryPath(NewDataDirectory)
# get the name of the data directory into which the file should be merged
merge_DataDirectory = DataDirectory+merge_subfolder_name
mDataDriectory = LSDost.AppendSepToDirectoryPath(merge_DataDirectory)
# make the directory
if not os.access(mDataDriectory,os.F_OK):
print "Making path: "
os.mkdir(mDataDriectory)
print "I made a directory: " + mDataDriectory
else:
print "Path: " +mDataDriectory+" already exists."
# Check the source format
if raster_format == "ENVI":
raster_extension = ".bil"
elif raster_format == "EHdr":
raster_extension = ".bil"
elif raster_format == "GTiff":
raster_extension = ".tif"
else:
print "You have not selcted a valid raster format!"
print "Options are ENVI, EHdr and GTiff"
raster_extension = "NULL"
# Check the target format. Default is geotiff
if target_format == "ENVI":
target_extension = ".bil"
elif target_format == "EHdr":
target_extension = ".bil"
elif target_format == "GTiff":
target_extension = ".tif"
else:
print "You have not selcted a valid raster format!"
print "Defaulting to GTiff"
target_format == "GTiff"
target_extension = ".tif"
# set the name of the target file
target_FileName = mDataDriectory+merge_filename+target_extension
# find all the dataset of the source format
print "The data directory is: " + DataDirectory
print "The raster extension is: " + raster_extension
if raster_extension != "NULL":
# Set up the list for holding command prompt commands
command_prompt = []
command_prompt.append("gdal_merge.py")
command_prompt.append("-of")
command_prompt.append(target_format)
command_prompt.append("-o")
command_prompt.append(target_FileName)
for FileName in glob(DataDirectory+"*"+raster_extension):
print "found file: " + FileName
command_prompt.append(FileName)
print "The subprocess call is: "
print command_prompt
subprocess.call(command_prompt)